Skip to content

Commit e33f92e

Browse files
authored
Merge pull request #15 from wgtechlabs/copilot/fix-2f50d303-2814-4614-bbb9-3d384f318448
feat: Implement ESLint with comprehensive security plugins and best practices
2 parents d761cd7 + 8e297c7 commit e33f92e

File tree

19 files changed

+2542
-34
lines changed

19 files changed

+2542
-34
lines changed

.github/workflows/validate.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
- name: Install dependencies
2828
run: pnpm install --frozen-lockfile
2929

30+
- name: Run linting
31+
run: pnpm lint
32+
3033
- name: Type checking
3134
run: pnpm type-check
3235

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ node_modules
113113
# Users Environment Variables
114114
.lock-wscript
115115

116-
# IDEs and editors (shim for help)
117-
.vscode/
116+
# IDEs and editors
117+
.vscode/*
118+
!.vscode/settings.json
119+
!.vscode/extensions.json
118120
.idea/
119121
*.swp
120122
*.swo

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"recommendations": [
3+
"dbaeumer.vscode-eslint",
34
"bierner.markdown-preview-github-styles",
45
"bierner.github-markdown-preview",
56
"bierner.markdown-shiki",
67
"bierner.color-info",
78
"oderwat.indent-rainbow",
8-
"warengonzaga.bini-theme",
9+
"warengonzaga.bini-theme"
910
]
1011
}

.vscode/settings.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"editor.formatOnSave": false,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll.eslint": "explicit"
5+
},
6+
"eslint.validate": [
7+
"javascript",
8+
"typescript"
9+
],
10+
"eslint.workingDirectories": [
11+
{
12+
"mode": "auto"
13+
}
14+
],
15+
"eslint.lintTask.enable": true,
16+
"typescript.tsdk": "node_modules/typescript/lib",
17+
"typescript.enablePromptUseWorkspaceTsdk": true,
18+
"[typescript]": {
19+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
20+
},
21+
"[javascript]": {
22+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
23+
}
24+
}

CONTRIBUTING.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ pnpm build
6969
# Type checking only
7070
pnpm type-check
7171

72+
# Linting
73+
pnpm lint # Run ESLint on all source files
74+
pnpm lint:fix # Run ESLint with auto-fix
75+
pnpm lint:security # Focus on security-related issues
76+
pnpm lint:ci # CI-friendly linting (fails on warnings)
77+
7278
# Clean build artifacts
7379
pnpm clean
7480

@@ -101,6 +107,7 @@ src/
101107
#### 🎯 Development Guidelines
102108

103109
- **TypeScript First**: All code must be written in TypeScript with strict type checking
110+
- **Code Quality**: Follow ESLint rules and security best practices enforced by automated linting
104111
- **Structured Logging**: Use `@wgtechlabs/log-engine` for all logging with built-in PII protection and security features
105112
- **Error Handling**: Implement comprehensive error handling with detailed logging
106113
- **Package Manager**: Use pnpm exclusively (enforced via preinstall script)
@@ -109,6 +116,75 @@ src/
109116
- **Redis Integration**: Ensure Redis connectivity for all webhook-related features
110117
- **Webhook Integration**: Ensure compatibility with [`wgtechlabs/unthread-telegram-bot`](https://github.com/wgtechlabs/unthread-telegram-bot)
111118

119+
#### 🔍 Code Quality and Linting
120+
121+
This project uses **ESLint** with comprehensive security plugins to maintain code quality and prevent common security vulnerabilities.
122+
123+
**Security Plugins Enabled:**
124+
125+
- **eslint-plugin-security** - Detects common security vulnerabilities
126+
- **eslint-plugin-no-secrets** - Prevents hardcoded secrets and credentials
127+
- **eslint-plugin-n** - Node.js best practices and deprecated API detection
128+
- **eslint-plugin-import** - Validates ES6 import/export syntax
129+
- **eslint-plugin-promise** - Ensures proper promise handling
130+
131+
**Running Linting:**
132+
133+
```bash
134+
# Check for issues
135+
pnpm lint
136+
137+
# Automatically fix issues
138+
pnpm lint:fix
139+
140+
# Security-focused check
141+
pnpm lint:security
142+
143+
# CI mode (fails on warnings)
144+
pnpm lint:ci
145+
```
146+
147+
**Comprehensive ESLint Configuration:**
148+
149+
This project uses a modern flat config format (`eslint.config.js`) with the following capabilities:
150+
- **TypeScript-first**: Full TypeScript-ESLint integration with strict type checking
151+
- **Security-focused**: Multiple security plugins working together to prevent vulnerabilities
152+
- **Customizable**: Tailored rules for webhook server security requirements
153+
- **IDE Integration**: Works seamlessly with VSCode ESLint extension
154+
155+
For complete configuration details, see [ESLINT.md](./ESLINT.md).
156+
157+
**Key Security Rules:**
158+
159+
- **No hardcoded secrets** - Detects API keys, tokens, passwords, webhook secrets in code
160+
- **Safe regular expressions** - Prevents ReDoS attacks
161+
- **Secure random generation** - Enforces crypto.randomBytes over Math.random
162+
- **Object injection protection** - Warns about unsafe object property access
163+
- **Child process security** - Flags potentially unsafe child process usage
164+
- **Promise handling** - Ensures all promises are properly handled
165+
166+
**Best Practices:**
167+
168+
- **Fix all linting errors** before submitting PRs (required)
169+
- **Address security warnings** unless there's a documented reason to ignore them
170+
- **Use ESLint disable comments sparingly** and only with proper justification
171+
- **Run `pnpm lint:fix`** to auto-fix style issues before committing
172+
- **Test security rules** with `pnpm lint:security` for security-focused checks
173+
- **VSCode users** get automatic linting and auto-fix on save with ESLint extension
174+
- **Document any rule disables** in code comments explaining why they're necessary
175+
176+
**When to Disable Rules:**
177+
178+
```typescript
179+
// ✅ Good - documented reason
180+
// eslint-disable-next-line security/detect-object-injection
181+
const value = obj[key]; // key is from typed enum, safe
182+
183+
// ❌ Bad - no justification
184+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
185+
const data: any = response;
186+
```
187+
112188
#### 🧪 Testing Guidelines
113189

114190
While this project doesn't currently have a test suite, when contributing:
@@ -124,16 +200,19 @@ While this project doesn't currently have a test suite, when contributing:
124200
1. **Pre-submission checks**:
125201
- [ ] Code builds without errors (`pnpm build`)
126202
- [ ] TypeScript type checking passes (`pnpm type-check`)
203+
- [ ] Linting passes without errors (`pnpm lint`)
127204
- [ ] Development server starts successfully (`pnpm dev`)
128205
- [ ] Redis integration works properly
129206
- [ ] Error handling is comprehensive
207+
- [ ] No security warnings from `pnpm lint:security`
130208

131209
2. **Pull Request Requirements**:
132210
- [ ] Target the `dev` branch (PRs to `main` will be rejected)
133211
- [ ] Include clear description of changes
134-
- [ ] Follow existing code patterns
212+
- [ ] Follow existing code patterns and ESLint rules
135213
- [ ] Update documentation if needed
136214
- [ ] Test webhook functionality manually
215+
- [ ] All linting issues resolved or properly justified
137216

138217
### 📖 Documentation
139218

ESLINT.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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)

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Server runs on `http://localhost:3000` with endpoints:
6868
- **Comprehensive Logging**: Detailed operation logs with emoji indicators
6969
- **Health Monitoring**: Built-in health checks for system status
7070
- **TypeScript**: Full type safety throughout the codebase
71+
- **Security-First Linting**: ESLint with comprehensive security plugins (security, no-secrets, promise handling)
72+
- **Code Quality**: Automated code quality checks with TypeScript-ESLint integration
7173

7274
## 🚂 One-Click Deploy
7375

@@ -222,6 +224,26 @@ pnpm dev # Development with hot-reload
222224
pnpm start # Run production build
223225
```
224226

227+
### Code Quality & Linting
228+
229+
This project enforces strict code quality and security standards using ESLint with comprehensive security plugins.
230+
231+
```bash
232+
pnpm lint # Run ESLint on all source files
233+
pnpm lint:fix # Run ESLint with auto-fix
234+
pnpm lint:security # Focus on security-related issues
235+
pnpm lint:ci # CI-friendly linting (fails on warnings)
236+
```
237+
238+
**Security Plugins Enabled:**
239+
- `eslint-plugin-security` - Detects common security vulnerabilities
240+
- `eslint-plugin-no-secrets` - Prevents hardcoded secrets and credentials
241+
- `eslint-plugin-n` - Node.js best practices and deprecated API detection
242+
- `eslint-plugin-import` - Validates ES6 import/export syntax
243+
- `eslint-plugin-promise` - Ensures proper promise handling
244+
245+
For detailed ESLint configuration and security rules, see [ESLINT.md](./ESLINT.md).
246+
225247
### Project Structure
226248

227249
```

0 commit comments

Comments
 (0)