-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjavascript-software-data-integrity-failures.mdc
More file actions
662 lines (581 loc) · 26.3 KB
/
javascript-software-data-integrity-failures.mdc
File metadata and controls
662 lines (581 loc) · 26.3 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
---
description: Detect and prevent software and data integrity failures in JavaScript applications as defined in OWASP Top 10:2021-A08
globs: **/*.js, **/*.jsx, **/*.ts, **/*.tsx, !**/node_modules/**, !**/dist/**, !**/build/**, !**/coverage/**
---
# JavaScript Software and Data Integrity Failures (OWASP A08:2021)
<rule>
name: javascript_software_data_integrity_failures
description: Detect and prevent software and data integrity failures in JavaScript applications as defined in OWASP Top 10:2021-A08
actions:
- type: enforce
conditions:
# Pattern 1: Insecure Deserialization
- pattern: "(?:JSON\\.parse|eval)\\s*\\((?:[^)]|\\n)*(?:localStorage|sessionStorage|document\\.cookie|location|window\\.name|fetch|axios|\\$\\.(?:get|post)|XMLHttpRequest)"
message: "Insecure deserialization of user-controlled data detected. Validate and sanitize data before parsing JSON or using eval."
# Pattern 2: Missing Subresource Integrity
- pattern: "<script\\s+src=['\"][^'\"]+['\"]\\s*>"
negative_pattern: "integrity=['\"]sha(?:256|384|512)-[a-zA-Z0-9+/=]+"
message: "Script tag without Subresource Integrity (SRI) hash. Add integrity and crossorigin attributes for third-party scripts."
# Pattern 3: Insecure Package Installation
- pattern: "(?:npm|yarn)\\s+(?:install|add)\\s+(?:[\\w@\\-\\.\\/:]+\\s+)*--no-(?:verify|integrity|signature)"
message: "Package installation with integrity checks disabled. Always verify package integrity during installation."
# Pattern 4: Insecure Object Deserialization
- pattern: "(?:require|import)\\s+['\"](?:serialize-javascript|node-serialize|serialize|unserialize|deserialize)['\"]"
message: "Using potentially unsafe serialization/deserialization libraries. Ensure proper validation and sanitization of serialized data."
# Pattern 5: Missing Dependency Verification
- pattern: "package\\.json"
negative_pattern: "\"(?:scripts|devDependencies)\":\\s*{[^}]*\"(?:audit|verify|check)\":\\s*\"(?:npm|yarn)\\s+audit"
file_pattern: "package\\.json$"
message: "Missing dependency verification in package.json. Add npm/yarn audit to your scripts section."
# Pattern 6: Insecure Dynamic Imports
- pattern: "(?:import|require)\\s*\\(\\s*(?:variable|[a-zA-Z_$][a-zA-Z0-9_$]*|`[^`]*`|'[^']*'|\"[^\"]*\")\\s*\\)"
negative_pattern: "(?:allowlist|whitelist|validate)"
message: "Potentially insecure dynamic imports. Validate or restrict the modules that can be dynamically imported."
# Pattern 7: Prototype Pollution
- pattern: "Object\\.assign\\(\\s*(?:[^,]+)\\s*,\\s*(?:JSON\\.parse|req\\.body|req\\.query|req\\.params|formData\\.get)"
message: "Potential prototype pollution vulnerability. Use Object.create(null) or sanitize objects before merging."
# Pattern 8: Missing CI/CD Pipeline Integrity Checks
- pattern: "(?:\\.github\\/workflows\\/|\\.gitlab-ci\\.yml|azure-pipelines\\.yml|Jenkinsfile)"
negative_pattern: "(?:npm\\s+audit|yarn\\s+audit|checksum|integrity|verify|signature)"
file_pattern: "(?:\\.github\\/workflows\\/.*\\.ya?ml|\\.gitlab-ci\\.yml|azure-pipelines\\.yml|Jenkinsfile)$"
message: "Missing security checks in CI/CD pipeline. Add dependency scanning, integrity verification, and signature validation."
# Pattern 9: Insecure Update Mechanism
- pattern: "(?:update|upgrade|install)\\s*\\([^)]*\\)\\s*\\{[^}]*?\\}"
negative_pattern: "(?:verify|checksum|hash|signature|integrity)"
message: "Potentially insecure update mechanism. Implement integrity verification for all updates."
# Pattern 10: Insecure Plugin Loading
- pattern: "(?:plugin|addon|extension)\\.(?:load|register|install|add)\\s*\\([^)]*\\)"
negative_pattern: "(?:verify|validate|checksum|hash|signature|integrity)"
message: "Insecure plugin loading mechanism. Implement integrity verification for all plugins."
# Pattern 11: Insecure Data Binding
- pattern: "(?:eval|new\\s+Function|setTimeout|setInterval)\\s*\\(\\s*(?:[^,)]+\\.(?:value|innerHTML|innerText|textContent)|[^,)]+\\[[^\\]]+\\])"
message: "Insecure data binding using eval or Function constructor. Use safer alternatives like JSON.parse or template literals."
# Pattern 12: Insecure Object Property Assignment
- pattern: "(?:Object\\.assign|\\{\\s*\\.\\.\\.)"
negative_pattern: "Object\\.create\\(null\\)"
message: "Potential prototype pollution in object assignment. Use Object.create(null) as the target object or sanitize inputs."
# Pattern 13: Missing Lock File
- pattern: "package\\.json"
negative_pattern: "package-lock\\.json|yarn\\.lock"
file_pattern: "package\\.json$"
message: "Missing lock file for dependency management. Include package-lock.json or yarn.lock in version control."
# Pattern 14: Insecure Webpack Configuration
- pattern: "webpack\\.config\\.js"
negative_pattern: "(?:integrity|sri|subresource|hash|checksum)"
file_pattern: "webpack\\.config\\.js$"
message: "Webpack configuration without integrity checks. Consider enabling SRI for generated assets."
# Pattern 15: Insecure npm/yarn Configuration
- pattern: "\\.npmrc|\\.yarnrc"
negative_pattern: "(?:verify-store|integrity|signature)"
file_pattern: "(?:\\.npmrc|\\.yarnrc)$"
message: "npm/yarn configuration with potentially disabled security features. Ensure integrity checks are enabled."
- type: suggest
message: |
**JavaScript Software and Data Integrity Failures Best Practices:**
1. **Secure Deserialization:**
- Validate and sanitize data before deserialization
- Use schema validation for JSON data
- Example:
```javascript
import Ajv from 'ajv';
// Define a schema for expected data
const schema = {
type: 'object',
properties: {
id: { type: 'integer' },
name: { type: 'string' },
role: { type: 'string', enum: ['user', 'admin'] }
},
required: ['id', 'name', 'role'],
additionalProperties: false
};
// Validate data before parsing
function safelyParseJSON(data) {
try {
const parsed = JSON.parse(data);
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (validate(parsed)) {
return { valid: true, data: parsed };
} else {
return { valid: false, errors: validate.errors };
}
} catch (error) {
return { valid: false, errors: [error.message] };
}
}
```
2. **Subresource Integrity (SRI):**
- Add integrity hashes to external scripts and stylesheets
- Example:
```html
<script
src="https://cdn.example.com/library.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous">
</script>
```
```javascript
// Programmatically adding a script with SRI
function addScriptWithIntegrity(url, integrity) {
const script = document.createElement('script');
script.src = url;
script.integrity = integrity;
script.crossOrigin = 'anonymous';
document.head.appendChild(script);
}
```
3. **Dependency Verification:**
- Use npm/yarn audit regularly
- Implement lockfiles and version pinning
- Example:
```json
// package.json
{
"scripts": {
"audit": "npm audit --production",
"preinstall": "npm audit",
"verify": "npm audit && npm outdated"
}
}
```
```javascript
// Automated dependency verification in CI/CD
// .github/workflows/security.yml
// name: Security Checks
// on: [push, pull_request]
// jobs:
// security:
// runs-on: ubuntu-latest
// steps:
// - uses: actions/checkout@v3
// - uses: actions/setup-node@v3
// with:
// node-version: '16'
// - run: npm audit
```
4. **Secure Object Handling:**
- Prevent prototype pollution
- Use Object.create(null) for empty objects
- Example:
```javascript
// Prevent prototype pollution
function safeObjectMerge(target, source) {
// Start with a null prototype object
const result = Object.create(null);
// Copy properties from target
for (const key in target) {
if (Object.prototype.hasOwnProperty.call(target, key) &&
key !== '__proto__' &&
key !== 'constructor' &&
key !== 'prototype') {
result[key] = target[key];
}
}
// Copy properties from source
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key) &&
key !== '__proto__' &&
key !== 'constructor' &&
key !== 'prototype') {
result[key] = source[key];
}
}
return result;
}
```
5. **Secure Dynamic Imports:**
- Validate module paths before importing
- Use allowlists for dynamic imports
- Example:
```javascript
// Allowlist-based dynamic imports
const ALLOWED_MODULES = [
'./components/header',
'./components/footer',
'./components/sidebar'
];
async function safeImport(modulePath) {
if (!ALLOWED_MODULES.includes(modulePath)) {
throw new Error(`Module ${modulePath} is not in the allowlist`);
}
try {
return await import(modulePath);
} catch (error) {
console.error(`Failed to import ${modulePath}:`, error);
throw error;
}
}
```
6. **CI/CD Pipeline Security:**
- Implement integrity checks in build pipelines
- Verify dependencies and artifacts
- Example:
```yaml
# .github/workflows/build.yml
name: Build and Verify
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm ci
- name: Security audit
run: npm audit
- name: Build
run: npm run build
- name: Generate integrity hashes
run: |
cd dist
find . -type f -name "*.js" -exec sh -c 'echo "{}" $(sha384sum "{}" | cut -d " " -f 1)' \; > integrity.txt
- name: Upload artifacts with integrity manifest
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: |
dist
dist/integrity.txt
```
7. **Secure Update Mechanisms:**
- Verify integrity of updates before applying
- Use digital signatures when possible
- Example:
```javascript
import crypto from 'crypto';
import fs from 'fs';
async function verifyUpdate(updateFile, signatureFile, publicKeyFile) {
try {
const updateData = fs.readFileSync(updateFile);
const signature = fs.readFileSync(signatureFile);
const publicKey = fs.readFileSync(publicKeyFile);
const verify = crypto.createVerify('SHA256');
verify.update(updateData);
const isValid = verify.verify(publicKey, signature);
if (!isValid) {
throw new Error('Update signature verification failed');
}
return { valid: true, data: updateData };
} catch (error) {
console.error('Update verification failed:', error);
return { valid: false, error: error.message };
}
}
```
8. **Plugin/Extension Security:**
- Implement allowlists for plugins
- Verify plugin integrity before loading
- Example:
```javascript
class PluginManager {
constructor() {
this.plugins = new Map();
this.allowedPlugins = new Set(['logger', 'analytics', 'theme']);
}
async registerPlugin(name, pluginPath, expectedHash) {
if (!this.allowedPlugins.has(name)) {
throw new Error(`Plugin ${name} is not in the allowlist`);
}
// Verify plugin integrity
const pluginCode = await fetch(pluginPath).then(r => r.text());
const hash = crypto.createHash('sha256').update(pluginCode).digest('hex');
if (hash !== expectedHash) {
throw new Error(`Plugin integrity check failed for ${name}`);
}
// Safe loading using Function constructor instead of eval
// Still has security implications but better than direct eval
const sandboxedPlugin = new Function('exports', 'require', pluginCode);
const exports = {};
const safeRequire = (module) => {
// Implement a restricted require function
const allowedModules = ['lodash', 'dayjs'];
if (!allowedModules.includes(module)) {
throw new Error(`Module ${module} is not allowed in plugins`);
}
return require(module);
};
sandboxedPlugin(exports, safeRequire);
this.plugins.set(name, exports);
return exports;
}
}
```
9. **Secure Data Binding:**
- Avoid eval() and new Function()
- Use template literals or frameworks with safe binding
- Example:
```javascript
// Unsafe:
// function updateElement(id, data) {
// const element = document.getElementById(id);
// element.innerHTML = eval('`' + template + '`'); // DANGEROUS!
// }
// Safe alternative:
function updateElement(id, data) {
const element = document.getElementById(id);
// Use a template literal with explicit interpolation
const template = `<div class="user-card">
<h2>${escapeHTML(data.name)}</h2>
<p>${escapeHTML(data.bio)}</p>
</div>`;
element.innerHTML = template;
}
function escapeHTML(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
```
10. **Secure Configuration Management:**
- Validate configurations before use
- Use schema validation for config files
- Example:
```javascript
import Ajv from 'ajv';
import fs from 'fs';
function loadAndValidateConfig(configPath) {
// Define schema for configuration
const configSchema = {
type: 'object',
properties: {
server: {
type: 'object',
properties: {
port: { type: 'integer', minimum: 1024, maximum: 65535 },
host: { type: 'string', format: 'hostname' }
},
required: ['port', 'host']
},
database: {
type: 'object',
properties: {
url: { type: 'string' },
maxConnections: { type: 'integer', minimum: 1 }
},
required: ['url']
}
},
required: ['server', 'database'],
additionalProperties: false
};
try {
const configData = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configData);
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(configSchema);
if (validate(config)) {
return { valid: true, config };
} else {
return { valid: false, errors: validate.errors };
}
} catch (error) {
return { valid: false, errors: [error.message] };
}
}
```
11. **Secure Webpack Configuration:**
- Enable SRI in webpack
- Use content hashing for cache busting
- Example:
```javascript
// webpack.config.js
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
module.exports = {
output: {
filename: '[name].[contenthash].js',
crossOriginLoading: 'anonymous' // Required for SRI
},
plugins: [
new SubresourceIntegrityPlugin({
hashFuncNames: ['sha384'],
enabled: process.env.NODE_ENV === 'production'
})
]
};
```
12. **Secure npm/yarn Configuration:**
- Enable integrity checks
- Use lockfiles and exact versions
- Example:
```
# .npmrc
audit=true
audit-level=moderate
save-exact=true
verify-store=true
# .yarnrc.yml
enableStrictSsl: true
enableImmutableInstalls: true
checksumBehavior: "throw"
```
13. **Secure JSON Parsing:**
- Use reviver functions with JSON.parse
- Example:
```javascript
function parseUserData(data) {
return JSON.parse(data, (key, value) => {
// Sanitize specific fields
if (key === 'role' && !['user', 'admin', 'editor'].includes(value)) {
return 'user'; // Default to safe value
}
// Prevent Date objects from being reconstructed from strings
if (typeof value === 'string' &&
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(value)) {
// Return as string, not Date object
return value;
}
return value;
});
}
```
14. **Content Security Policy (CSP):**
- Implement strict CSP headers
- Use nonce-based CSP for inline scripts
- Example:
```javascript
// Express.js example
import crypto from 'crypto';
import helmet from 'helmet';
app.use((req, res, next) => {
// Generate a new nonce for each request
res.locals.cspNonce = crypto.randomBytes(16).toString('base64');
next();
});
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
(req, res) => `'nonce-${res.locals.cspNonce}'`,
'https://cdn.jsdelivr.net'
],
styleSrc: ["'self'", 'https://cdn.jsdelivr.net'],
// Add other directives as needed
}
}));
// In your template engine, use the nonce:
// <script nonce="<%= cspNonce %>">
// // Inline JavaScript
// </script>
```
15. **Secure Local Storage:**
- Validate data before storing and after retrieving
- Consider encryption for sensitive data
- Example:
```javascript
// Simple encryption/decryption for localStorage
// Note: This is still client-side and not fully secure
class SecureStorage {
constructor(secret) {
this.secret = secret;
}
// Set item with validation and encryption
setItem(key, value, schema) {
// Validate with schema if provided
if (schema) {
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(value)) {
throw new Error(`Invalid data for ${key}: ${ajv.errorsText(validate.errors)}`);
}
}
// Simple encryption (not for truly sensitive data)
const valueStr = JSON.stringify(value);
const encrypted = this.encrypt(valueStr);
localStorage.setItem(key, encrypted);
}
// Get item with decryption and validation
getItem(key, schema) {
const encrypted = localStorage.getItem(key);
if (!encrypted) return null;
try {
const decrypted = this.decrypt(encrypted);
const value = JSON.parse(decrypted);
// Validate with schema if provided
if (schema) {
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(value)) {
console.error(`Retrieved invalid data for ${key}`);
return null;
}
}
return value;
} catch (error) {
console.error(`Failed to retrieve ${key}:`, error);
return null;
}
}
// Simple XOR encryption (not for production use with sensitive data)
encrypt(text) {
let result = '';
for (let i = 0; i < text.length; i++) {
result += String.fromCharCode(text.charCodeAt(i) ^ this.secret.charCodeAt(i % this.secret.length));
}
return btoa(result);
}
decrypt(encoded) {
const text = atob(encoded);
let result = '';
for (let i = 0; i < text.length; i++) {
result += String.fromCharCode(text.charCodeAt(i) ^ this.secret.charCodeAt(i % this.secret.length));
}
return result;
}
}
```
- type: validate
conditions:
# Check 1: Subresource Integrity
- pattern: "<script\\s+[^>]*?integrity=['\"]sha(?:256|384|512)-[a-zA-Z0-9+/=]+['\"][^>]*?>"
message: "Using Subresource Integrity (SRI) for external scripts."
# Check 2: Dependency Verification
- pattern: "\"scripts\":\\s*{[^}]*\"(?:audit|verify|check)\":\\s*\"(?:npm|yarn)\\s+audit"
message: "Implementing dependency verification in package.json scripts."
# Check 3: Lock File Usage
- pattern: "(?:package-lock\\.json|yarn\\.lock)"
file_pattern: "(?:package-lock\\.json|yarn\\.lock)$"
message: "Using lock files for dependency management."
# Check 4: Safe Object Creation
- pattern: "Object\\.create\\(null\\)"
message: "Using Object.create(null) to prevent prototype pollution."
# Check 5: Schema Validation
- pattern: "(?:ajv|joi|yup|zod|jsonschema|validate)"
message: "Implementing schema validation for data integrity."
metadata:
priority: high
version: 1.0
tags:
- security
- javascript
- nodejs
- browser
- integrity
- owasp
- language:javascript
- framework:express
- framework:react
- framework:vue
- framework:angular
- category:security
- subcategory:integrity
- standard:owasp-top10
- risk:a08-software-data-integrity-failures
references:
- "https://owasp.org/Top10/A08_2021-Software_and_Data_Integrity_Failures/"
- "https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html"
- "https://cheatsheetseries.owasp.org/cheatsheets/Third_Party_Javascript_Management_Cheat_Sheet.html"
- "https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity"
- "https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/11-Client-side_Testing/13-Testing_for_Subresource_Integrity"
- "https://snyk.io/blog/prototype-pollution-javascript/"
- "https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/NPM_Security_Cheat_Sheet.md"
- "https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html"
- "https://owasp.org/www-community/attacks/Prototype_pollution"
</rule>