-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjavascript-security-misconfiguration.mdc
More file actions
467 lines (402 loc) · 18.8 KB
/
javascript-security-misconfiguration.mdc
File metadata and controls
467 lines (402 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
---
description: Detect and prevent security misconfigurations in JavaScript applications as defined in OWASP Top 10:2021-A05
globs: **/*.js, **/*.jsx, **/*.ts, **/*.tsx, !**/node_modules/**, !**/dist/**, !**/build/**, !**/coverage/**
---
# JavaScript Security Misconfiguration (OWASP A05:2021)
<rule>
name: javascript_security_misconfiguration
description: Detect and prevent security misconfigurations in JavaScript applications as defined in OWASP Top 10:2021-A05
actions:
- type: enforce
conditions:
# Pattern 1: Missing or Insecure HTTP Security Headers
- pattern: "app\\.use\\([^)]*?\\)\\s*(?!.*(?:helmet|frameguard|hsts|noSniff|xssFilter|contentSecurityPolicy))"
location: "(?:app|server|index)\\.(?:js|ts)$"
message: "Missing HTTP security headers. Consider using Helmet.js to set secure HTTP headers."
# Pattern 2: Insecure CORS Configuration
- pattern: "app\\.use\\(cors\\(\\{[^}]*?origin\\s*:\\s*['\"]\\*['\"]\\s*\\}\\)\\)"
message: "Insecure CORS configuration. Avoid using wildcard (*) for CORS origin in production environments."
# Pattern 3: Exposed Environment Variables in Client-Side Code
- pattern: "process\\.env\\.[A-Z_]+"
location: "(?:src|components|pages)"
message: "Exposing environment variables in client-side code. Only use environment variables with NEXT_PUBLIC_, REACT_APP_, or VITE_ prefixes for client-side code."
# Pattern 4: Insecure Cookie Settings
- pattern: "(?:cookie|cookies|session)\\([^)]*?\\{[^}]*?(?:secure\\s*:\\s*false|httpOnly\\s*:\\s*false|sameSite\\s*:\\s*['\"]none['\"])"
message: "Insecure cookie configuration. Set secure:true, httpOnly:true, and appropriate sameSite value for cookies."
# Pattern 5: Missing Content Security Policy
- pattern: "app\\.use\\([^)]*?helmet\\([^)]*?\\{[^}]*?contentSecurityPolicy\\s*:\\s*false"
message: "Content Security Policy (CSP) is disabled. Enable and configure CSP to prevent XSS attacks."
# Pattern 6: Debug Information Exposure
- pattern: "app\\.use\\([^)]*?morgan\\(['\"]dev['\"]\\)|console\\.(?:log|debug|info|warn|error)\\("
location: "(?:app|server|index)\\.(?:js|ts)$"
message: "Debug information might be exposed in production. Ensure logging is properly configured based on the environment."
# Pattern 7: Insecure Server Configuration
- pattern: "app\\.disable\\(['\"]x-powered-by['\"]\\)"
negative_pattern: true
location: "(?:app|server|index)\\.(?:js|ts)$"
message: "X-Powered-By header is not disabled. Use app.disable('x-powered-by') to hide technology information."
# Pattern 8: Directory Listing Enabled
- pattern: "express\\.static\\([^)]*?\\{[^}]*?index\\s*:\\s*false"
message: "Directory listing might be enabled. Set index:true or provide an index file to prevent directory listing."
# Pattern 9: Missing Rate Limiting
- pattern: "app\\.(?:get|post|put|delete|patch)\\([^)]*?['\"](?:/api|/login|/register|/auth)['\"]"
negative_pattern: "(?:rateLimit|rateLimiter|limiter|throttle)"
message: "Missing rate limiting for sensitive endpoints. Implement rate limiting to prevent brute force attacks."
# Pattern 10: Insecure WebSocket Configuration
- pattern: "new\\s+WebSocket\\([^)]*?\\)|io\\.on\\(['\"]connection['\"]"
negative_pattern: "(?:wss://|https://)"
message: "Potentially insecure WebSocket connection. Use secure WebSocket (wss://) in production."
# Pattern 11: Hardcoded Configuration Values
- pattern: "(?:apiKey|secret|password|token|credentials)\\s*=\\s*['\"][^'\"]+['\"]"
message: "Hardcoded configuration values. Use environment variables or a secure configuration management system."
# Pattern 12: Insecure SSL/TLS Configuration
- pattern: "https\\.createServer\\([^)]*?\\{[^}]*?rejectUnauthorized\\s*:\\s*false"
message: "Insecure SSL/TLS configuration. Never set rejectUnauthorized:false in production."
# Pattern 13: Missing Security Middleware
- pattern: "express\\(\\)|require\\(['\"]express['\"]\\)"
negative_pattern: "(?:helmet|cors|rateLimit|bodyParser\\.json\\(\\{\\s*limit|express\\.json\\(\\{\\s*limit)"
location: "(?:app|server|index)\\.(?:js|ts)$"
message: "Missing essential security middleware. Consider using helmet, cors, rate limiting, and request size limiting."
# Pattern 14: Insecure Error Handling
- pattern: "app\\.use\\([^)]*?function\\s*\\([^)]*?err[^)]*?\\)\\s*\\{[^}]*?res\\.status[^}]*?err(?:\\.message|\\.stack)"
message: "Insecure error handling. Avoid exposing error details like stack traces to clients in production."
# Pattern 15: Outdated Dependencies Warning
- pattern: "(?:\"dependencies\"|\"devDependencies\")\\s*:\\s*\\{[^}]*?['\"](?:express|react|vue|angular|next|nuxt|axios)['\"]\\s*:\\s*['\"]\\^?\\d+\\.\\d+\\.\\d+['\"]"
location: "package\\.json$"
message: "Check for outdated dependencies. Regularly update dependencies to avoid known vulnerabilities."
- type: suggest
message: |
**JavaScript Security Configuration Best Practices:**
1. **HTTP Security Headers:**
- Use Helmet.js to set secure HTTP headers
- Configure Content Security Policy (CSP)
- Example:
```javascript
const helmet = require('helmet');
// Basic usage
app.use(helmet());
// Custom CSP configuration
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", 'trusted-cdn.com'],
styleSrc: ["'self'", "'unsafe-inline'", 'trusted-cdn.com'],
imgSrc: ["'self'", 'data:', 'trusted-cdn.com'],
connectSrc: ["'self'", 'api.yourdomain.com'],
fontSrc: ["'self'", 'trusted-cdn.com'],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
upgradeInsecureRequests: [],
},
})
);
```
2. **Secure CORS Configuration:**
- Specify allowed origins explicitly
- Configure appropriate CORS options
- Example:
```javascript
const cors = require('cors');
// Define allowed origins
const allowedOrigins = [
'https://yourdomain.com',
'https://app.yourdomain.com',
'https://admin.yourdomain.com'
];
// Configure CORS
app.use(cors({
origin: function(origin, callback) {
// Allow requests with no origin (like mobile apps, curl, etc.)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
credentials: true,
maxAge: 86400 // 24 hours
}));
```
3. **Environment-Based Configuration:**
- Use different configurations for development and production
- Validate configuration at startup
- Example:
```javascript
const express = require('express');
const helmet = require('helmet');
const morgan = require('morgan');
const app = express();
// Environment-specific configuration
if (process.env.NODE_ENV === 'production') {
// Production settings
app.use(helmet());
app.use(morgan('combined'));
app.set('trust proxy', 1); // Trust first proxy
// Disable X-Powered-By header
app.disable('x-powered-by');
} else {
// Development settings
app.use(morgan('dev'));
}
// Validate required environment variables
const requiredEnvVars = ['DATABASE_URL', 'JWT_SECRET'];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.error(`Error: Environment variable ${envVar} is required`);
process.exit(1);
}
}
```
4. **Secure Cookie Configuration:**
- Set secure, httpOnly, and sameSite attributes
- Use signed cookies when appropriate
- Example:
```javascript
const session = require('express-session');
app.use(session({
secret: process.env.SESSION_SECRET,
name: 'sessionId', // Custom cookie name instead of default
cookie: {
secure: process.env.NODE_ENV === 'production', // HTTPS only in production
httpOnly: true, // Prevents client-side JS from reading the cookie
sameSite: 'lax', // Controls when cookies are sent with cross-site requests
maxAge: 3600000, // 1 hour in milliseconds
domain: process.env.NODE_ENV === 'production' ? '.yourdomain.com' : undefined
},
resave: false,
saveUninitialized: false
}));
```
5. **Request Size Limiting:**
- Limit request body size to prevent DoS attacks
- Example:
```javascript
// Using express built-in middleware
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
// Or using body-parser
const bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '10kb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '10kb' }));
```
6. **Proper Error Handling:**
- Use a centralized error handler
- Don't expose sensitive information in error responses
- Example:
```javascript
// Custom error class
class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
// Global error handling middleware
app.use((err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || 'error';
// Different handling for development and production
if (process.env.NODE_ENV === 'development') {
res.status(err.statusCode).json({
status: err.status,
error: err,
message: err.message,
stack: err.stack
});
} else if (process.env.NODE_ENV === 'production') {
// Only send operational errors to the client
if (err.isOperational) {
res.status(err.statusCode).json({
status: err.status,
message: err.message
});
} else {
// Log programming or unknown errors
console.error('ERROR 💥', err);
// Send generic message
res.status(500).json({
status: 'error',
message: 'Something went wrong'
});
}
}
});
```
7. **Rate Limiting:**
- Apply rate limiting to sensitive endpoints
- Use different limits for different endpoints
- Example:
```javascript
const rateLimit = require('express-rate-limit');
// Create a rate limiter for API endpoints
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: 'Too many requests from this IP, please try again after 15 minutes'
});
// Create a stricter rate limiter for authentication endpoints
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // limit each IP to 5 login attempts per windowMs
standardHeaders: true,
legacyHeaders: false,
message: 'Too many login attempts from this IP, please try again after 15 minutes'
});
// Apply rate limiters to routes
app.use('/api/', apiLimiter);
app.use('/api/auth/', authLimiter);
```
8. **Secure WebSocket Configuration:**
- Use secure WebSocket connections (wss://)
- Implement authentication for WebSocket connections
- Example:
```javascript
const http = require('http');
const https = require('https');
const socketIo = require('socket.io');
const fs = require('fs');
let server;
// Create secure server in production
if (process.env.NODE_ENV === 'production') {
const options = {
key: fs.readFileSync('/path/to/private.key'),
cert: fs.readFileSync('/path/to/certificate.crt')
};
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
const io = socketIo(server, {
cors: {
origin: process.env.NODE_ENV === 'production'
? 'https://yourdomain.com'
: 'http://localhost:3000',
methods: ['GET', 'POST'],
credentials: true
}
});
// WebSocket authentication middleware
io.use((socket, next) => {
const token = socket.handshake.auth.token;
if (!token) {
return next(new Error('Authentication error'));
}
// Verify token
// ...
next();
});
```
9. **Security Dependency Management:**
- Regularly update dependencies
- Use tools like npm audit or Snyk
- Example:
```javascript
// package.json scripts
{
"scripts": {
"audit": "npm audit",
"audit:fix": "npm audit fix",
"outdated": "npm outdated",
"update": "npm update",
"prestart": "npm audit --production"
}
}
```
10. **Secure Logging Configuration:**
- Configure logging based on environment
- Avoid logging sensitive information
- Example:
```javascript
const winston = require('winston');
// Define log levels
const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
debug: 4,
};
// Define log level based on environment
const level = () => {
const env = process.env.NODE_ENV || 'development';
return env === 'development' ? 'debug' : 'warn';
};
// Define log format
const format = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
);
// Define transports
const transports = [
new winston.transports.Console(),
new winston.transports.File({
filename: 'logs/error.log',
level: 'error',
}),
new winston.transports.File({ filename: 'logs/all.log' }),
];
// Create the logger
const logger = winston.createLogger({
level: level(),
levels,
format,
transports,
});
module.exports = logger;
```
- type: validate
conditions:
# Check 1: Helmet usage
- pattern: "helmet\\(\\)|frameguard\\(\\)|hsts\\(\\)|noSniff\\(\\)|xssFilter\\(\\)|contentSecurityPolicy\\(\\)"
message: "Using Helmet.js or individual HTTP security headers middleware."
# Check 2: Secure CORS configuration
- pattern: "cors\\(\\{[^}]*?origin\\s*:\\s*(?!['\"]*\\*)['\"]"
message: "Using secure CORS configuration with specific origins."
# Check 3: Environment-based configuration
- pattern: "process\\.env\\.NODE_ENV\\s*===\\s*['\"]production['\"]"
message: "Implementing environment-specific configuration."
# Check 4: Secure cookie settings
- pattern: "cookie\\s*:\\s*\\{[^}]*?secure\\s*:\\s*true[^}]*?httpOnly\\s*:\\s*true"
message: "Using secure cookie configuration."
# Check 5: Request size limiting
- pattern: "(?:express|bodyParser)\\.json\\(\\{[^}]*?limit\\s*:"
message: "Implementing request size limiting."
metadata:
priority: high
version: 1.0
tags:
- security
- javascript
- nodejs
- browser
- configuration
- owasp
- language:javascript
- framework:express
- framework:react
- framework:vue
- framework:angular
- category:security
- subcategory:misconfiguration
- standard:owasp-top10
- risk:a05-security-misconfiguration
references:
- "https://owasp.org/Top10/A05_2021-Security_Misconfiguration/"
- "https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html"
- "https://expressjs.com/en/advanced/best-practice-security.html"
- "https://helmetjs.github.io/"
- "https://github.com/OWASP/NodeGoat"
- "https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html"
- "https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html"
</rule>