The `permissions-policy` package provides Express and Connect middleware for managing the `Permissions-Policy` HTTP header. This header controls which browser features and APIs (like camera, microphone, geolocation) are available to a document and its iframes, enhancing web security. It is the modern successor to the deprecated `Feature-Policy` header and is built upon the foundational work of Evan Hahn, a well-known contributor to web security middleware (e.g., Helmet). Currently stable at version 0.6.0, the package receives periodic updates, with recent releases focusing on improved ESM exports and increased flexibility in defining policy directives. Its primary differentiator is its dedicated, streamlined focus on this single, crucial security header, making it an ideal component for integration into broader security middleware suites.
npm install permissions-policyVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an Express app and applies the `permissions-policy` middleware to set the `Permissions-Policy` HTTP header with common feature configurations, demonstrating how to enable or disable various browser APIs for a web application.
Ensure you are using the `Permissions-Policy` header and its syntax, as `Feature-Policy` is no longer supported by modern browsers.
Carefully review all feature names and their values in your configuration to ensure they are valid Permissions Policy directives, especially when adding new or experimental ones. The middleware will no longer prevent potentially invalid directives from being sent.
For CommonJS environments, ensure your Node.js version supports dual-packaging correctly, or if using a bundler, confirm it correctly interprets the `main` or `exports` field in the package's `package.json`. For modern applications, prefer ESM imports: `import permissionsPolicy from 'permissions-policy';`.
For ESM, use `import permissionsPolicy from 'permissions-policy';`. For CommonJS, use `const permissionsPolicy = require('permissions-policy');`. Verify your `tsconfig.json` (if TypeScript) and `package.json` `type` field are correctly configured for your project's module system.Change your import statement to use ESM syntax: `import permissionsPolicy from 'permissions-policy';`. If you need to use CommonJS modules, ensure your file is treated as CommonJS (e.g., `.cjs` extension or `type: "commonjs"` in `package.json`).
Ensure `app.use(permissionsPolicy(...))` is called early in your application's setup, typically before any route handlers or other middleware that might send a response. Also, verify that the `features` configuration object is correctly structured and not empty.