为NestJS应用量身定制的安全规则(控制器、提供者、装饰器)。
⭐如果这个插件为你捕获了一个真正的错误, 用星号标记回购 -这是维持这些规则的信号。
描述
这个插件提供了针对NestJS应用(控制器、提供者、装饰器)量身定制的安全规则。
- 为什么 ——没人读的书什么也保护不了。我们宁愿错过一个发现 而不是把你的注意力花在一个不真实的人身上。
- 如何 -证据,不是名字。一个规则触发什么代码 做,解决 通过AST和ESLint自己的作用域分析。
- 什么 ——每一个发现都有它的意义,对一个人来说,都是结构化的 代理的JSON。安全规则添加一个CWE映射,并在分配时添加一个CVSS分数。
这种贸易成本让我们回想起来,我们对它进行了衡量: 方法 · 结果 · 误报是错误的。
开始
- 要查看 指南,请访问 eslint.interlace.tools. 📚
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/common | decorator,警卫 | ||
@nestjs/core | 应用程序配置 | ||
class-validator | DTO验证 | ||
@nestjs/throttler | 速度限制 |
⚠️全局配置处理
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程序;没有它,寂静无声。 |
| 规则 | CWE | OWASP | CVSS | 描述 | 🧠 | 💼 | ⚠️ | 🔧 | 💡 | 🚫 |
|---|---|---|---|---|---|---|---|---|---|---|
| no-exposed-private-fields | cwe - 200 | A01:2021 | 该规则检测实体或DTO类中不存在的敏感字段(如密码、令牌、秘密)。 | 🟢 | ⚠️ | |||||
| no-hybrid-app-config-loss | CWE-20 | A03:2021 | 检测不带inheritAppConfig的connectMicroservice(),它会静默地丢弃每个全局管道和守卫…… | 🟢 | 💼 | |||||
| no-missing-validation-pipe | CWE-20 | A03:2021 | 该规则提供了llm优化的错误消息(紧凑的2行格式)和可操作的安全指导: | 🟡 | ⚠️ | |||||
| no-permissive-cors | cwe - 942 | A05:2021 | 标志CORS配置为接受任何原点-一个裸的enableCors(),原点‘*’,或反映原点为true。 | 🟢 | 💼 | |||||
| no-res-bypass-serialization | cwe - 200 | A01:2021 | 该规则检测的路由处理程序注入@Res()而不通过传递,然后写入一个对象,这仍然… | 🟢 | ⚠️ | |||||
| no-unguarded-swagger | cwe - 200 | A01:2021 | 该规则检测在应用程序引导中无条件运行的SwaggerModule.setup(),它发布… | 🟢 | ⚠️ | |||||
| no-unsafe-multer-filename | CWE-22 | A01:2021 | 标记一个multer diskStorage文件名回调,该回调将上传存储在客户端选择的名称下。 | 🟢 | 💼 | |||||
| require-guards | cwe - 306 | A01:2021 | 该规则提供了llm优化的错误消息(紧凑的2行格式)和可操作的安全指导: | 🟢 | 💼 | |||||
| require-throttler | cwe - 770 | A05:2021 | 该规则检测缺乏速率限制的NestJS控制器和路由处理程序,这会使应用程序… | 🟢 | ⚠️ | |||||
| require-validation-pipe-whitelist | cwe - 915 | A03:2021 | 在ValidationPipe上要求白名单为true,因此DTO从未声明的属性将被剥离,而不是重新定义… | 🟢 | 💼 | |||||
🔗相关ESLint插件
部分 交错ESLint生态系统 -带有llm优化错误消息的ai本地规则;
安全
| 插件 | 下载 | 描述 |
|---|---|---|
eslint-plugin-anthropic-security | Anthropic SDK安全性。 | |
eslint-plugin-browser-security | XSS, DOM安全性。 | |
eslint-plugin-drizzle-security | 细雨安全。 | |
eslint-plugin-express-security | 快速中间件加固。 | |
eslint-plugin-gemini-security | 谷歌Gemini SDK安全。 | |
eslint-plugin-jwt-security | 令牌的安全。 | |
eslint-plugin-knex-security | Knex安全。 | |
eslint-plugin-lambda-security | AWS Lambda加固。 | |
eslint-plugin-mcp-sdk-security | MCP SDK安全。 | |
eslint-plugin-mongodb-security | MongoDB注入。 | |
eslint-plugin-mysql-security | MySQL安全。 | |
eslint-plugin-node-security | 服务器端模式。 | |
eslint-plugin-openai-security | OpenAI SDK安全。 | |
eslint-plugin-postgresql-security | PostgreSQL的安全。 | |
eslint-plugin-prisma-security | 棱镜的安全。 | |
eslint-plugin-secure-coding | 预防注射。 | |
eslint-plugin-sequelize-security | 序列化ORM安全性。 | |
eslint-plugin-sqlite-security | SQLite安全。 | |
eslint-plugin-supabase-security | Supabase安全。 | |
eslint-plugin-typeorm-security | TypeORM安全。 | |
eslint-plugin-vercel-ai-security | AI SDK安全。 |
代码质量
| 插件 | 下载 | 描述 |
|---|---|---|
eslint-plugin-conventions | 团队特有的习惯和风格。 | |
eslint-plugin-import-next | 快速循环+导入图形分析。 | |
eslint-plugin-maintainability | 认知负荷和干净的代码模式。 | |
eslint-plugin-modernization | ESNext迁移+语法进化。 | |
eslint-plugin-modularity | 结构完整性和DDD模式。 | |
eslint-plugin-operability | 生产就绪和资源运行状况。 | |
eslint-plugin-react-a11y | React可访问性/ WCAG。 | |
eslint-plugin-react-features | React最佳实践和优化。 | |
eslint-plugin-reliability | 运行时稳定性和错误安全性。 |
⭐支持与关注
如果这个插件为你捕获了一个真正的bug, 用星号标记回购 -星星是维持Interlace ESLint生态系统的信号-和 关注Dev.to上的文章 对于这些规则背后的基准和安全研究。
📄许可证
麻省理工学院© Ofri佩雷茨
