The `fetch-metadata` package provides Node.js middleware designed for Express and Connect applications to enforce browser Fetch metadata request headers, such as `Sec-Fetch-Site`, `Sec-Fetch-Mode`, and `Sec-Fetch-Dest`. This middleware plays a crucial role in enhancing application security by helping to prevent common web vulnerabilities like Cross-Site Request Forgery (CSRF), Cross-Site Script Inclusion (XSSI), and information leakage attacks, as part of a defense-in-depth strategy. Currently at stable version 1.0.0, it offers a highly configurable API allowing developers to define granular policies for request origins, navigation types, and specific allowed paths. While a specific release cadence isn't published, its initial stable release suggests a focus on reliability for security-critical applications. Its key differentiator lies in its specific focus on these modern browser security headers, providing a ready-to-use solution for integrating these protections into existing Node.js web servers.
npm install fetch-metadataVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to install `fetch-metadata` and integrate it into an Express application with a basic configuration. This example shows how to set allowed fetch sites, disallow specific navigation requests, define allowed paths to bypass checks, and implement a custom error handler.
Thoroughly test configurations in various browser contexts and user scenarios. Start with a more permissive configuration and tighten it gradually, monitoring logs for blocked requests (e.g., via the `onError` callback).
Always ensure your `onError` implementation includes `response.status(statusCode).send(message)` or `next()`.
Refer to the `url-pattern` documentation for advanced path matching syntax. Test all `allowedPaths` entries thoroughly, especially those with dynamic segments or regular expressions, to ensure they match as expected.
Use `import fetchMetadata from 'fetch-metadata'` for ESM modules, or `const fetchMetadata = require('fetch-metadata').default` for CommonJS environments (though the former is recommended).Check the server console for warnings from the `onError` callback. Adjust `allowedFetchSites`, `disallowedNavigationRequests`, or add the problematic path to `allowedPaths` configuration. Ensure your client-side requests are sending appropriate Fetch Metadata headers.
Modify your `onError` callback to either call `response.status(statusCode).send(message)` to terminate the request with an error, or `next()` if you wish to bypass the block and allow the request to proceed (e.g., for logging and allowing in specific cases).
No dependency data recorded yet.