The `@eggjs/security` (formerly `egg-security`) package is a robust security plugin specifically designed for the Egg.js framework. It provides comprehensive protection against common web vulnerabilities, including Cross-Site Request Forgery (CSRF), Cross-Site Scripting (XSS), Server-Side Request Forgery (SSRF), SQL injection, and more. The current stable version is 4.0.1 (under the `@eggjs/security` namespace), with the 3.x branch (`egg-security`) also receiving maintenance updates, with `3.8.0` being the latest for that line. The project maintains an active release cadence, frequently publishing minor and patch versions to introduce new features, improve existing protections, and address bug fixes. A significant update to version 4.0.0 migrated the codebase to TypeScript and dropped support for Node.js versions older than 18.19.0. Its key differentiator lies in its deep integration with the Egg.js ecosystem, offering out-of-the-box security measures that are easily configurable within the framework's convention-over-configuration paradigm, simplifying the implementation of robust security practices for developers building Egg.js applications.
npm install egg-securityVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a basic Egg.js application, enable the `@eggjs/security` plugin, configure its CSRF protection, and show how to embed and validate a CSRF token in a simple HTML form using TypeScript.
In `package.json`, change `"egg-security": "^3.x.x"` to `"@eggjs/security": "^4.x.x"`. In `config/plugin.ts` (or `.js`), update `package: 'egg-security'` to `package: '@eggjs/security'`.
Upgrade your Node.js environment to version 18.19.0 or newer to ensure compatibility.
For TypeScript projects, ensure `tsconfig.json` is correctly configured. For JavaScript projects, be aware of potential changes and review the documentation for any new configuration patterns or type-related impacts.
For form submissions, include `<input type="hidden" name="_csrf" value="${ctx.csrf}" />`. For AJAX requests, send the token in a header (e.g., `X-CSRF-Token`) or as a body parameter (e.g., `_csrf`). The token can be accessed via `ctx.csrf`.Always use `ctx.safeCurl` (or `app.safeCurl`/`agent.safeCurl`) for making HTTP requests to external or user-provided URLs. Configure `security.ssrf.hostnameExceptionList` for explicitly whitelisting internal hosts if necessary.
Update your `package.json` to depend on `@eggjs/security` and adjust `config/plugin.ts` (or `.js`) to use `package: '@eggjs/security'`. Then run `npm install`.
Ensure that the CSRF token (obtained from `ctx.csrf`) is included in your form submissions (as a hidden field `_csrf`) or AJAX requests (e.g., in the `X-CSRF-Token` header).
Verify that `config/plugin.ts` (or `.js`) explicitly enables the security plugin: `security: { enable: true, package: '@eggjs/security' }`. Also ensure Egg.js is initialized correctly.Upgrade your Node.js runtime to version 18.19.0 or higher. You can use a Node Version Manager (NVM) to manage multiple Node.js versions.
If the URL is legitimate, configure `config.security.ssrf.hostnameExceptionList` or `checkAddress` to explicitly allow access to that hostname or IP. Avoid using `ctx.curl` for external or user-provided URLs.