eslint-plugin-nestjs-security
v1.2.3
Published
Security-focused ESLint rules for NestJS applications. Detects missing guards, validation pipes, throttling, exposed sensitive fields, and more.
Maintainers
Readme
Description
This plugin provides Security rules tailored for NestJS applications (Controllers, Providers, Decorators). By using this plugin, you can proactively identify and mitigate security risks across your entire codebase.
Philosophy
Interlace fosters strength through integration. Instead of stacking isolated rules, we interlace security directly into your workflow to create a resilient fabric of code. We believe tools should guide rather than gatekeep, providing educational feedback that strengthens the developer with every interaction.
Getting Started
- To check out the guide, visit eslint.interlace.tools. 📚
- 要查看中文 指南, 请访问 eslint.interlace.tools. 📚
- 가이드 문서는 eslint.interlace.tools에서 확인하실 수 있습니다. 📚
- ガイドは eslint.interlace.toolsでご確認ください。 📚
- Para ver la guía, visita eslint.interlace.tools. 📚
- للاطلاع على الدليل، قم بزيارة eslint.interlace.tools. 📚
npm install eslint-plugin-nestjs-security --save-dev⚙️ Configuration Presets
| Preset | Description |
| :------------ | :------------------------------------------------------- |
| recommended | Enables all security rules with sensible severity levels |
| strict | All security rules set to 'error' for maximum protection |
📚 Supported Libraries
| Library | npm | Downloads | Detection |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| @nestjs/common | |
| Decorators, Guards |
|
@nestjs/core | |
| App Config |
|
class-validator | |
| DTO Validation |
|
@nestjs/throttler | |
| Rate Limiting |
⚠️ Global Configuration Handling
Static Analysis Limitation: ESLint analyzes files independently. It cannot detect cross-file configurations like
app.useGlobalGuards()inmain.tswhile lintingusers.controller.ts.
Understanding the Problem
NestJS supports two security configuration approaches:
| Approach | Example | ESLint Can See? |
| -------------------- | ------------------------------------------------- | :-------------: |
| Per-Controller | @UseGuards(AuthGuard) on class | ✅ |
| Per-Method | @UseGuards(AuthGuard) on method | ✅ |
| Global (main.ts) | app.useGlobalGuards(new AuthGuard()) | ❌ |
| Global (Module) | ThrottlerModule.forRoot({ ttl: 60, limit: 10 }) | ❌ |
Solution: assumeGlobal* Options
For teams using global configuration, set assumeGlobal*: true to disable per-file checks:
// 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 },
],
},
},
];Alternative: Use Skip Decorators
The rules recognize common "bypass" decorators for intentionally unprotected endpoints:
// 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🔮 Future: Cross-File Global Detection (Planned)
We're planning dedicated rules to verify global configuration exists:
require-global-guards→ Ensuresmain.tscontainsapp.useGlobalGuards()require-global-validation-pipe→ Ensuresmain.tscontainsapp.useGlobalPipes()require-global-throttler→ Ensuresapp.module.tsimportsThrottlerModule
This will enable a "trust but verify" approach for teams using global configuration.
Rules
Legend
| Icon | Description |
| :---: | :--- |
| 💼 | Recommended: Included in the recommended preset. |
| ⚠️ | Warns: Set towarn in recommended preset. |
| 🔧 | Auto-fixable: Automatically fixable by the --fix CLI option. |
| 💡 | Suggestions: Providing code suggestions in IDE. |
| 🚫 | Deprecated: This rule is deprecated. |
| Rule | CWE | OWASP | CVSS | Description | 💼 | ⚠️ | 🔧 | 💡 | 🚫 | | :--- | :---: | :---: | :---: | :--- | :---: | :---: | :---: | :---: | :---: | | no-exposed-debug-endpoints | | | | ESLint rule documentation for no-exposed-debug-endpoints | | | | | | | no-exposed-private-fields | CWE-200 | | 7.5 | ESLint rule documentation for no-exposed-private-fields | 💼 | ⚠️ | | 💡 | | | no-missing-validation-pipe | CWE-20 | | 8.6 | ESLint rule documentation for no-missing-validation-pipe | 💼 | ⚠️ | | 💡 | | | require-class-validator | CWE-20 | | 7.5 | ESLint rule documentation for require-class-validator | 💼 | ⚠️ | | 💡 | | | require-guards | CWE-284 | | 9.8 | ESLint rule documentation for require-guards | 💼 | | | 💡 | | | require-throttler | CWE-770 | | 7.5 | ESLint rule documentation for require-throttler | 💼 | ⚠️ | | 💡 | |
🔗 Related ESLint Plugins
Part of the Interlace ESLint Ecosystem — AI-native security plugins with LLM-optimized error messages:
| Plugin | Downloads | Description |
| :--- | :---: | :--- |
| eslint-plugin-secure-coding | | General security rules & OWASP guidelines. |
|
eslint-plugin-pg | | PostgreSQL security & best practices. |
|
eslint-plugin-crypto | | NodeJS Cryptography security rules. |
|
eslint-plugin-jwt | | JWT security & best practices. |
|
eslint-plugin-browser-security | | Browser-specific security & XSS prevention. |
|
eslint-plugin-express-security | | Express.js security hardening rules. |
|
eslint-plugin-lambda-security | | AWS Lambda security best practices. |
|
eslint-plugin-nestjs-security | | NestJS security rules & patterns. |
|
eslint-plugin-mongodb-security | | MongoDB security best practices. |
|
eslint-plugin-vercel-ai-security | | Vercel AI SDK security hardening. |
|
eslint-plugin-import-next | | Next-gen import sorting & architecture. |
📄 License
MIT © Ofri Peretz
