A fetch() wrapper that implements Double Submit Cookies CSRF protection. Version 0.2.1 is the current release, last updated in 2015. It automatically adds a CSRF token header (default x-csrf-token) from a cookie (default csrf-token) for same-origin requests. The library sets credentials: 'same-origin' by default and can be configured via global properties. It requires a server that sets the CSRF cookie and expects the header. This package is minimal and unmaintained, with no TypeScript definitions and limited browser support (requires native fetch or polyfill). It is designed for simple CSRF prevention in fetch-based web applications.
npm install safe-fetchNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic usage with POST request including credentials and CSRF protection.
Set credentials to 'same-origin' (default) or 'include' to ensure CSRF header is sent.
Either import safe-fetch as a replacement for fetch or use native fetch via window.fetch before importing safe-fetch.
Consider using a maintained alternative or implement CSRF protection manually.
Set cookieName and headerName before making any fetch calls or reset them after use.
Use a polyfill like 'whatwg-fetch' before importing safe-fetch, or import with 'import fetch from 'safe-fetch'' (not named import).
Ensure the server sets a cookie with the expected name and that credentials are set to 'same-origin' or 'include' in the request.
Install a fetch polyfill (e.g., 'whatwg-fetch') and import it before safe-fetch.