|
| 1 | +# ESLint Configuration Guide |
| 2 | + |
| 3 | +This project uses ESLint with comprehensive security plugins to maintain code quality and prevent common security vulnerabilities. |
| 4 | + |
| 5 | +## 📦 Installed Plugins |
| 6 | + |
| 7 | +### Core |
| 8 | +- **@eslint/js** - ESLint's recommended JavaScript rules |
| 9 | +- **@typescript-eslint/eslint-plugin** - TypeScript-specific linting rules |
| 10 | +- **@typescript-eslint/parser** - TypeScript parser for ESLint |
| 11 | + |
| 12 | +### Security Plugins |
| 13 | +- **eslint-plugin-security** - Detects common security vulnerabilities |
| 14 | + - Object injection prevention |
| 15 | + - RegEx DoS protection |
| 16 | + - Buffer security checks |
| 17 | + - Eval detection |
| 18 | + - CSRF protection |
| 19 | + |
| 20 | +- **eslint-plugin-no-secrets** - Prevents hardcoded secrets and credentials |
| 21 | + - API keys detection |
| 22 | + - Passwords and tokens |
| 23 | + - Custom patterns for webhook secrets |
| 24 | + |
| 25 | +### Best Practices |
| 26 | +- **eslint-plugin-n** - Node.js best practices and deprecated API detection |
| 27 | +- **eslint-plugin-import** - Validates ES6 import/export syntax |
| 28 | +- **eslint-plugin-promise** - Ensures proper promise handling |
| 29 | + |
| 30 | +## 🚀 Usage |
| 31 | + |
| 32 | +### Run Linting |
| 33 | + |
| 34 | +```bash |
| 35 | +# Check for issues |
| 36 | +yarn lint |
| 37 | + |
| 38 | +# Automatically fix issues |
| 39 | +yarn lint:fix |
| 40 | + |
| 41 | +# Security-focused check |
| 42 | +yarn lint:security |
| 43 | + |
| 44 | +# CI mode (fails on warnings) |
| 45 | +yarn lint:ci |
| 46 | +``` |
| 47 | + |
| 48 | +### VSCode Integration |
| 49 | + |
| 50 | +ESLint is automatically integrated with VSCode: |
| 51 | +- Real-time linting feedback |
| 52 | +- Auto-fix on save (configurable) |
| 53 | +- Inline error/warning display |
| 54 | + |
| 55 | +Make sure you have the ESLint extension installed: |
| 56 | +``` |
| 57 | +ext install dbaeumer.vscode-eslint |
| 58 | +``` |
| 59 | + |
| 60 | +## 🔒 Key Security Rules |
| 61 | + |
| 62 | +### Critical (Error Level) |
| 63 | + |
| 64 | +- ✅ **No hardcoded secrets** - Detects API keys, tokens, passwords |
| 65 | +- ✅ **Safe regular expressions** - Prevents ReDoS attacks |
| 66 | +- ✅ **Secure random generation** - Enforces crypto.randomBytes |
| 67 | +- ✅ **No eval** - Prevents code injection via eval() |
| 68 | +- ✅ **Buffer security** - Checks for unsafe buffer operations |
| 69 | +- ✅ **Promise handling** - All promises must be properly handled |
| 70 | + |
| 71 | +### Warnings |
| 72 | + |
| 73 | +- ⚠️ **Object injection** - Warns about dynamic property access |
| 74 | +- ⚠️ **Child processes** - Flags subprocess creation |
| 75 | +- ⚠️ **File system operations** - Non-literal file paths |
| 76 | +- ⚠️ **Timing attacks** - Potential timing-based vulnerabilities |
| 77 | +- ⚠️ **process.exit()** - Suggests throwing errors instead |
| 78 | + |
| 79 | +## ⚙️ Configuration |
| 80 | + |
| 81 | +The ESLint configuration uses the modern flat config format (`eslint.config.js`). |
| 82 | + |
| 83 | +### Ignored Patterns |
| 84 | + |
| 85 | +The following are automatically ignored: |
| 86 | +- `node_modules/` |
| 87 | +- `dist/` |
| 88 | +- `build/` |
| 89 | +- `coverage/` |
| 90 | +- `*.min.js` |
| 91 | +- `.yarn/` |
| 92 | +- `tmp/`, `temp/` |
| 93 | + |
| 94 | +### Custom Rules |
| 95 | + |
| 96 | +Some rules are customized for this project: |
| 97 | + |
| 98 | +```javascript |
| 99 | +// Security rules - adjusted for false positives |
| 100 | +'security/detect-object-injection': 'warn', // Warn instead of error |
| 101 | +'security/detect-non-literal-fs-filename': 'warn', |
| 102 | +'security/detect-non-literal-require': 'warn', |
| 103 | + |
| 104 | +// TypeScript rules |
| 105 | +'@typescript-eslint/no-explicit-any': 'warn', // Allow with justification |
| 106 | +'@typescript-eslint/no-unused-vars': ['error', { |
| 107 | + argsIgnorePattern: '^_', // Allow unused vars prefixed with _ |
| 108 | + varsIgnorePattern: '^_', |
| 109 | +}], |
| 110 | +``` |
| 111 | + |
| 112 | +## 💡 Best Practices |
| 113 | + |
| 114 | +### When to Disable Rules |
| 115 | + |
| 116 | +Use ESLint disable comments sparingly and only with justification: |
| 117 | + |
| 118 | +```typescript |
| 119 | +// Good - documented reason |
| 120 | +// eslint-disable-next-line security/detect-object-injection |
| 121 | +if (!event[field]) { // field is from typed array, safe |
| 122 | + // ... |
| 123 | +} |
| 124 | + |
| 125 | +// Bad - no justification |
| 126 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 127 | +const data: any = response; |
| 128 | +``` |
| 129 | + |
| 130 | +### Acceptable Uses of `any` |
| 131 | + |
| 132 | +The `any` type is acceptable in these cases: |
| 133 | +- External API responses (Unthread webhook payloads) |
| 134 | +- Dynamic data structures where type is truly unknown |
| 135 | +- Express.js Request generics |
| 136 | +- Always document why `any` is necessary |
| 137 | + |
| 138 | +### Handling Warnings |
| 139 | + |
| 140 | +- **Fix all errors** before committing |
| 141 | +- **Address warnings** unless there's a documented reason |
| 142 | +- **Security warnings** should be taken seriously |
| 143 | +- Use `yarn lint:fix` to auto-fix style issues |
| 144 | + |
| 145 | +## 🔄 CI/CD Integration |
| 146 | + |
| 147 | +ESLint runs automatically in GitHub Actions: |
| 148 | + |
| 149 | +```yaml |
| 150 | +- name: Run linting |
| 151 | + run: yarn lint |
| 152 | +``` |
| 153 | +
|
| 154 | +Builds will fail if there are linting errors. Warnings are allowed but should be minimized. |
| 155 | +
|
| 156 | +## 🔧 Troubleshooting |
| 157 | +
|
| 158 | +### "Parsing error: parserOptions.project" |
| 159 | +
|
| 160 | +Make sure `tsconfig.json` exists and is valid. |
| 161 | + |
| 162 | +### "Cannot find module 'eslint-plugin-xxx'" |
| 163 | + |
| 164 | +Run `yarn install` to ensure all dependencies are installed. |
| 165 | + |
| 166 | +### Too many warnings |
| 167 | + |
| 168 | +Use `yarn lint:fix` to automatically fix style issues. |
| 169 | + |
| 170 | +### False positives |
| 171 | + |
| 172 | +For security false positives: |
| 173 | +1. Verify the code is actually safe |
| 174 | +2. Add ESLint disable comment with justification |
| 175 | +3. Document in PR why the disable is necessary |
| 176 | + |
| 177 | +## 📚 Resources |
| 178 | + |
| 179 | +- [ESLint Documentation](https://eslint.org/) |
| 180 | +- [TypeScript ESLint](https://typescript-eslint.io/) |
| 181 | +- [eslint-plugin-security](https://github.com/eslint-community/eslint-plugin-security) |
| 182 | +- [eslint-plugin-no-secrets](https://github.com/nickdeis/eslint-plugin-no-secrets) |
0 commit comments