Registry / auth-security / nuxt-security

nuxt-security

JSON →
library2.5.1jsnpmunverified

The Nuxt Security module is a robust solution for enhancing the security posture of Nuxt 3 applications by automatically configuring HTTP headers and server middleware according to OWASP principles. It provides features such as Content Security Policy (CSP), HTTP Strict Transport Security (HSTS), X-XSS-Protection, Referrer-Policy, and Permissions-Policy, alongside runtime protections like request size and rate limiters, Cross-Site Scripting (XSS) validation, and Cross-Origin Resource Sharing (CORS) support. The module also offers optional features like basic authentication, allowed HTTP methods control, and CSRF protection. Currently stable at version 2.5.1, `nuxt-security` maintains a rapid release cycle, with frequent hotfixes and minor versions addressing issues, introducing new features, and keeping pace with Nuxt 3 updates. Its primary differentiator is the comprehensive, opinionated, and automatic application of common security best practices without extensive manual configuration. It focuses on server-side protections and integration with Nuxt's SSR/SSG capabilities.

npm install nuxt-security
INSTALL
IMPORT
SIG · NUXT-SECURITY
N
nuxt-security
auth-securityjavascriptv2.5.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Module registration (string)
export default defineNuxtConfig({ modules: [ 'nuxt-security' ] })
import { nuxtSecurity } from 'nuxt-security'
Nuxt modules are added as strings to the `modules` array in `nuxt.config.ts`, not imported as direct JavaScript symbols.
ModuleOptions
import type { ModuleOptions } from 'nuxt-security'
import { ModuleOptions } from 'nuxt-security'
This type provides full IntelliSense and type checking for the `security` configuration object in `nuxt.config.ts`.
BasicAuth
import type { BasicAuth } from 'nuxt-security'
import BasicAuth from 'nuxt-security/types/basic-auth'
Specific types for individual security features like `BasicAuth`, `CSPConfig`, or `RateLimiter` can be imported for more granular type-safety.

This configuration enables various security features for a Nuxt application, including a strict Content Security Policy, XSS protection, rate limiting for all API routes, restricted HTTP methods, XSS validation for form submissions, and basic authentication for an '/admin' section. Remember to set `BASIC_AUTH_USERNAME` and `BASIC_AUTH_PASSWORD` environment variables for basic authentication.

import { defineNuxtConfig } from 'nuxt'; import type { ModuleOptions } from 'nuxt-security'; const securityConfig: ModuleOptions = { headers: { contentSecurityPolicy: { value: { 'default-src': ["'self'", "https://cdn.example.com"], 'script-src': ["'self'", "'unsafe-inline'", "'unsafe-eval'", "https://cdn.example.com"], 'style-src': ["'self'", "'unsafe-inline'", "https://cdn.example.com"] }, route: '/**' }, xXSSProtection: { value: '1; mode=block', route: '/**' }, noSniff: { value: true, route: '/**' } }, rateLimiter: { value: { tokens: 10, interval: 30000, headers: true, driver: { name: 'lru-cache', options: { max: 1000, ttl: 60000 } }, statusCode: 429, statusMessage: 'Too Many Requests' }, route: '/api/**' }, allowedHTTPMethods: { value: ['GET', 'POST', 'PUT', 'DELETE'], route: '/api/**' }, xssValidator: { value: true, route: '/forms/**' }, basicAuth: { value: { name: process.env.BASIC_AUTH_USERNAME ?? 'admin', pass: process.env.BASIC_AUTH_PASSWORD ?? 'password', enabled: true, message: 'Authentication Required' }, route: '/admin/**' } }; export default defineNuxtConfig({ modules: [ 'nuxt-security' ], security: securityConfig });
Debug
Known issues
breakingThe module upgraded its minimum Node.js version requirement to 20.0.0 in v2.3.0. Applications deployed with older Node.js versions will encounter runtime errors or fail to build.
fix
Upgrade your Node.js environment to version 20 or higher using a Node Version Manager (e.g., nvm, fnm) or update your deployment platform's Node.js version.
affects: >=2.3.0
gotchaPrior to v2.1.2, `console.log` statements were removed in development mode by default if the `removeLoggers` option was implicitly or explicitly enabled, leading to unexpected debugging challenges. This behavior was changed in a hotfix.
fix
Upgrade to v2.1.2 or newer. If staying on an older version, explicitly set `removeLoggers: { value: false, route: '/**' }` in your development `nuxt.config.ts`.
affects: <2.1.2
gotchaIncorrectly configured Content Security Policy (CSP) can severely restrict a web application, blocking legitimate scripts, styles, images, and other resources. This often results in a broken user interface or functionality, especially in SSR/SSG contexts where nonces or hashes might be required.
fix
Develop your CSP iteratively and test thoroughly across all environments (development, production, SSR, SSG). Utilize CSP reporting mechanisms to identify violations, and leverage `nonce` or `hash` attributes for dynamically generated content where 'unsafe-inline' is not desirable.
affects: >=1.0.0
gotchaThe XSS validator can be overly aggressive and block legitimate user input, particularly in applications that handle rich text, HTML snippets, or other forms of complex data. This can lead to a poor user experience or data loss.
fix
Test the XSS validator rigorously with various expected user inputs. Consider applying XSS validation only to specific routes (e.g., `/forms/**`) where user-generated content is expected, or disable it entirely for routes where rich content is intentionally permitted and sanitized via other means.
affects: >=1.0.0
gotchaAggressive rate limiting configurations can unexpectedly block legitimate API clients, bots, or integrations, leading to service disruption or inaccessible features. This is particularly problematic for applications with diverse client bases or sudden spikes in legitimate traffic.
fix
Configure rate limiter `tokens` and `interval` values based on a realistic assessment of expected traffic patterns and client behavior. Utilize the `whiteList` option (introduced in v2.2.0) to exempt known, high-volume clients or internal services from rate limiting.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'nuxt-security' or its corresponding type declarations.
The module is not installed, or it's not correctly added to the `modules` array in `nuxt.config.ts`, or the type declarations are not properly resolved.
fix
Run `npx nuxi@latest module add security` or `npm install nuxt-security` to ensure the package is installed. Verify that `'nuxt-security'` is present in the `modules` array within your `nuxt.config.ts`.
Refused to load the script '<URL>' because it violates the following Content Security Policy directive: "script-src".
Your Content Security Policy (CSP) headers, configured by `nuxt-security`, are preventing a script from loading because its origin or attributes do not match the allowed directives.
fix
Review the `security.headers.contentSecurityPolicy` configuration in `nuxt.config.ts`. Add the problematic URL's domain to the `script-src` directive, or consider using `'unsafe-inline'`/`'unsafe-eval'` (with caution) or `nonce`/`hash` attributes if dynamic inline scripts are necessary.
405 Method Not Allowed
The HTTP request used a method (e.g., PUT) that is not permitted for the specific route by the `security.allowedHTTPMethods` configuration.
fix
Check the `security.allowedHTTPMethods` configuration in `nuxt.config.ts` for the route in question. Ensure that the HTTP method used by your client is included in the `value` array. Adjust the configuration or client request method as appropriate.
429 Too Many Requests
The rate limiter middleware, configured via `security.rateLimiter`, has detected that too many requests originated from the same client within the defined interval.
fix
Increase the `tokens` (maximum requests) or `interval` (time window) values in your `security.rateLimiter` configuration. Alternatively, if the client is legitimate and requires higher limits, consider adding its IP address to the `whiteList` option (if applicable).
ERR_PNPM_PEER_DEP_ISSUES: Peer dependencies error (or similar npm/yarn error related to Node.js version)
Your Node.js environment does not meet the minimum version requirement (Node.js >=20.0.0) specified by `nuxt-security` since version 2.3.0.
fix
Upgrade your local and deployment Node.js version to 20 or higher. Use a Node Version Manager like `nvm` or `fnm` to manage different Node.js versions efficiently.
Upgrade
Version history
2.5.1latest on npm
Audit
Dependencies
nuxtrequiredPeer dependency; this is a Nuxt module and requires Nuxt 3.x or newer (4.x) to function.
noderequiredRuntime environment; requires Node.js >=20.0.0 since v2.3.0.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources