eslint-plugin-nestjs-security

中文文档
3.2.2发布于 2026/11/03MITTypeScriptCommonJS构建来源已验证

用于NestJS安全的ESLint插件——检测缺失的认证保护、缺失的验证管道、未节流的路由和暴露的私有字段。

中文使用文档
文档目录13 节
  1. 描述
  2. 开始
  3. ⚙️配置预设
  4. 📚支持的库
  5. ⚠️全局配置处理
  6. 逃生出口: assumeGlobal* 和 detectGlobal* 选项
  7. 可选:使用跳过装饰器
  8. 费率限制报告在哪里
  9. 📦兼容性
  10. 规则
  11. 🔗相关ESLint插件
  12. ⭐支持与关注
  13. 📄许可证

Interlace    NestJS    oxlint    ESLint

为NestJS应用量身定制的安全规则(控制器、提供者、装饰器)。

NPM Version NPM Downloads Install Size Package License Codecov Since Dec 2025 OpenSSF Scorecard

⭐如果这个插件为你捕获了一个真正的错误, 用星号标记回购 -这是维持这些规则的信号。

描述

这个插件提供了针对NestJS应用(控制器、提供者、装饰器)量身定制的安全规则。

  • 为什么 ——没人读的书什么也保护不了。我们宁愿错过一个发现 而不是把你的注意力花在一个不真实的人身上。
  • 如何 -证据,不是名字。一个规则触发什么代码 做,解决 通过AST和ESLint自己的作用域分析。
  • 什么 ——每一个发现都有它的意义,对一个人来说,都是结构化的 代理的JSON。安全规则添加一个CWE映射,并在分配时添加一个CVSS分数。

这种贸易成本让我们回想起来,我们对它进行了衡量: 方法 · 结果 · 误报是错误的。

开始

oxlint / Vite+ -同样的规则下同样的 nestjs-security/<rule> id,没有ESLint:

npm install oxlint-plugin-nestjs-security --save-dev
// .oxlintrc.json
{ "jsPlugins": ["oxlint-plugin-nestjs-security"] }
// vite.config.ts
import { defineConfig } from 'vite-plus';

export default defineConfig({
  lint: { jsPlugins: ['oxlint-plugin-nestjs-security'] },
});

ESLint:

npm install eslint-plugin-nestjs-security --save-dev

⚙️配置预设

预设描述
recommended启用具有合理严重级别的所有安全规则
strict所有安全规则设置为“error”以获得最大保护

📚支持的库

图书馆npm下载检测
@nestjs/commonnpmdownloadsdecorator,警卫
@nestjs/corenpmdownloads应用程序配置
class-validatornpmdownloadsDTO验证
@nestjs/throttlernpmdownloads速度限制

⚠️全局配置处理

NestJS应用了守卫、管道和速率限制 应用程序,在一个文件中 控制器从不导入。从v1.3.0开始,插件会发现这些注册 本身:在第一次查找时,它定位项目根(最近的) package.json) 扫描引导和 *.module.ts 文件一次,缓存结果。

方法例子检测到?
Per-Controller@UseGuards(AuthGuard) 在类✅
每个方法@UseGuards(AuthGuard) 在方法✅
复合@AuthJwtAccessProtected() 包装 UseGuards✅¹
全局(main.ts)app.useGlobalGuards() / app.useGlobalPipes()✅
全局(模块){ provide: APP_GUARD, useClass: AuthGuard }✅
全局(模块){ provide: APP_PIPE, useClass: ValidationPipe }✅
全局(模块)ThrottlerModule.forRoot([{ ttl: 60000, limit: 10 }])✅

¹复合装饰器不能被语法过滤器解析,所以任何路由 携带一个插件无法识别的装饰器被认为是受保护的。一个 错过查找比在别人的代码库中出现误报要便宜得多。集 allowCustomDecorators: false 在 require-guards 如果你想要严格的 行为。

ThrottlerGuard 注册为 APP_GUARD 作为速率限制, 不 作为 身份验证——一个只会节流的项目 require-guards 发现。

逃生出口: assumeGlobal* 和 detectGlobal* 选项

assumeGlobal*: true 直接禁用规则而不扫描项目; detectGlobal*: false 禁用项目扫描并恢复严格的每个文件 检查:

// eslint.config.js
import nestjsSecurity from 'eslint-plugin-nestjs-security';

export default [
  {
    ...nestjsSecurity.configs.recommended,
    rules: {
      // Tell ESLint: "We have app.useGlobalGuards() in main.ts"
      'nestjs-security/require-guards': ['warn', { assumeGlobalGuards: true }],

      // Tell ESLint: "We have app.useGlobalPipes(new ValidationPipe()) in main.ts"
      'nestjs-security/no-missing-validation-pipe': [
        'warn',
        { assumeGlobalPipes: true },
      ],

      // Tell ESLint: "We have ThrottlerModule.forRoot() in app.module.ts"
      'nestjs-security/require-throttler': [
        'warn',
        { assumeGlobalThrottler: true },
      ],

      // Or keep strict per-file checking and skip the project scan entirely
      // 'nestjs-security/require-guards': ['error', { detectGlobalGuards: false }],
    },
  },
];

可选:使用跳过装饰器

规则承认故意不受保护的端点的常见“旁路”装饰器:

// These bypass require-guards
@Public()        // nestjs-passport pattern
@SkipAuth()      // common custom decorator
@AllowAnonymous() // alternative naming
@NoAuth()        // alternative naming

// These bypass require-throttler
@SkipThrottle()  // @nestjs/throttler built-in

费率限制报告在哪里

require-throttler 报告 一次,在根模块上 (AppModule,或任何 @Module 类 app.module.ts)当没有时 ThrottlerModule 配置 在项目的任何地方。采用单模块注册限速, 因此,在每个路由上报告它将一行修复描述为数十个错误。


📦兼容性

包版本
ESLint^8.40.0 || ^9.0.0 || ^10.0.0
node . js>=18.0.0

看到 ESLint版本支持策略 -当前生态系统份额数据,20%门,以及v10的前瞻性例外。

规则

传说

图标描述
💼推荐:包含在推荐的预置中。
⚠️警告:在推荐设置中设置为警告。
🔧Auto-fixable:可自动修复 --fix CLI的选择。
💡建议在IDE中提供代码建议。
🚫弃用:该规则已弃用。
🟢Type-unaware: AST-only,运行在oxlint JS-plugin层。
🟡Type-aware(精炼):纯ast主路径;类型提高了精度。
🟠Type-aware(优雅):要求TS程序;没有它,寂静无声。
规则CWEOWASPCVSS描述🧠💼⚠️🔧💡🚫
no-exposed-private-fieldscwe - 200A01:2021该规则检测实体或DTO类中不存在的敏感字段(如密码、令牌、秘密)。🟢⚠️
no-hybrid-app-config-lossCWE-20A03:2021检测不带inheritAppConfig的connectMicroservice(),它会静默地丢弃每个全局管道和守卫……🟢💼
no-missing-validation-pipeCWE-20A03:2021该规则提供了llm优化的错误消息(紧凑的2行格式)和可操作的安全指导:🟡⚠️
no-permissive-corscwe - 942A05:2021标志CORS配置为接受任何原点-一个裸的enableCors(),原点‘*’,或反映原点为true。🟢💼
no-res-bypass-serializationcwe - 200A01:2021该规则检测的路由处理程序注入@Res()而不通过传递,然后写入一个对象,这仍然…🟢⚠️
no-unguarded-swaggercwe - 200A01:2021该规则检测在应用程序引导中无条件运行的SwaggerModule.setup(),它发布…🟢⚠️
no-unsafe-multer-filenameCWE-22A01:2021标记一个multer diskStorage文件名回调,该回调将上传存储在客户端选择的名称下。🟢💼
require-guardscwe - 306A01:2021该规则提供了llm优化的错误消息(紧凑的2行格式)和可操作的安全指导:🟢💼
require-throttlercwe - 770A05:2021该规则检测缺乏速率限制的NestJS控制器和路由处理程序,这会使应用程序…🟢⚠️
require-validation-pipe-whitelistcwe - 915A03:2021在ValidationPipe上要求白名单为true,因此DTO从未声明的属性将被剥离,而不是重新定义…🟢💼

部分 交错ESLint生态系统 -带有llm优化错误消息的ai本地规则;

安全

插件下载描述
eslint-plugin-anthropic-securitydownloadsAnthropic SDK安全性。
eslint-plugin-browser-securitydownloadsXSS, DOM安全性。
eslint-plugin-drizzle-securitydownloads细雨安全。
eslint-plugin-express-securitydownloads快速中间件加固。
eslint-plugin-gemini-securitydownloads谷歌Gemini SDK安全。
eslint-plugin-jwt-securitydownloads令牌的安全。
eslint-plugin-knex-securitydownloadsKnex安全。
eslint-plugin-lambda-securitydownloadsAWS Lambda加固。
eslint-plugin-mcp-sdk-securitydownloadsMCP SDK安全。
eslint-plugin-mongodb-securitydownloadsMongoDB注入。
eslint-plugin-mysql-securitydownloadsMySQL安全。
eslint-plugin-node-securitydownloads服务器端模式。
eslint-plugin-openai-securitydownloadsOpenAI SDK安全。
eslint-plugin-postgresql-securitydownloadsPostgreSQL的安全。
eslint-plugin-prisma-securitydownloads棱镜的安全。
eslint-plugin-secure-codingdownloads预防注射。
eslint-plugin-sequelize-securitydownloads序列化ORM安全性。
eslint-plugin-sqlite-securitydownloadsSQLite安全。
eslint-plugin-supabase-securitydownloadsSupabase安全。
eslint-plugin-typeorm-securitydownloadsTypeORM安全。
eslint-plugin-vercel-ai-securitydownloadsAI SDK安全。

代码质量

插件下载描述
eslint-plugin-conventionsdownloads团队特有的习惯和风格。
eslint-plugin-import-nextdownloads快速循环+导入图形分析。
eslint-plugin-maintainabilitydownloads认知负荷和干净的代码模式。
eslint-plugin-modernizationdownloadsESNext迁移+语法进化。
eslint-plugin-modularitydownloads结构完整性和DDD模式。
eslint-plugin-operabilitydownloads生产就绪和资源运行状况。
eslint-plugin-react-a11ydownloadsReact可访问性/ WCAG。
eslint-plugin-react-featuresdownloadsReact最佳实践和优化。
eslint-plugin-reliabilitydownloads运行时稳定性和错误安全性。

⭐支持与关注

如果这个插件为你捕获了一个真正的bug, 用星号标记回购 -星星是维持Interlace ESLint生态系统的信号-和 关注Dev.to上的文章 对于这些规则背后的基准和安全研究。

GitHub stars

📄许可证

麻省理工学院© Ofri佩雷茨

ESLint Interlace Plugin

Interlace

版本与发布记录

eslint-plugin-nestjs-security 共发布 30 个版本,首次发布于 2025/12/30;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.2.2latest2026/11/03今天
3.2.12026/11/03今天
3.2.02026/10/268 天前
3.1.32026/09/051 个月前
3.1.22026/09/032 个月前
3.1.12026/09/012 个月前

关于 eslint-plugin-nestjs-security 的常见问题

eslint-plugin-nestjs-security 是什么?

用于NestJS安全的ESLint插件——检测缺失的认证保护、缺失的验证管道、未节流的路由和暴露的私有字段。

如何安装 eslint-plugin-nestjs-security?

在项目目录运行 npm install eslint-plugin-nestjs-security,或使用 pnpm add / yarn add / bun add。

eslint-plugin-nestjs-security 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

eslint-plugin-nestjs-security 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

eslint-plugin-nestjs-security 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

eslint-plugin-nestjs-security 可以商用吗?

eslint-plugin-nestjs-security 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

eslint-plugin-nestjs-security 还在维护吗?

最新版本 3.2.2 发布于 2026/11/03(今天);至今共发布 30 个版本,其中过去 12 个月发布了 30 个;首次发布于 2025/12/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。