-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity-check.js
executable file
·76 lines (66 loc) · 1.95 KB
/
security-check.js
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
#!/usr/bin/env node
/**
* Script to check for sensitive files that should not be committed
* Run this before committing or as part of a pre-commit hook
*/
import fs from 'fs';
import { execSync } from 'child_process';
// Files that should never be committed
const SENSITIVE_FILES = [
'.env',
'.env.local',
'.env.development',
'.env.production',
'.env.test',
'credentials.json',
'secrets.json',
'token.json',
'keys.json',
'*.pem',
'*.key',
'*.crt',
'*.pfx',
'*.p12'
];
console.log('Running security check...');
// Check for sensitive files in the git working directory
let filesFound = false;
for (const pattern of SENSITIVE_FILES) {
// Skip glob patterns
if (pattern.includes('*')) continue;
// Check if file exists and is not ignored by git
if (fs.existsSync(pattern)) {
try {
// Check if file is already ignored by git
execSync(`git check-ignore -q ${pattern}`);
} catch (error) {
// File exists and is not ignored
console.error(`❌ Error: Sensitive file "${pattern}" found and is not ignored by git.`);
filesFound = true;
}
}
}
// Check for pattern matches (like *.pem files)
try {
const allFiles = execSync('git ls-files').toString().split('\n').filter(Boolean);
for (const file of allFiles) {
for (const pattern of SENSITIVE_FILES) {
if (pattern.includes('*')) {
const regex = new RegExp(pattern.replace('.', '\\.').replace('*', '.*'));
if (regex.test(file)) {
console.error(`❌ Error: Sensitive file "${file}" matching pattern "${pattern}" is tracked by git.`);
filesFound = true;
}
}
}
}
} catch (error) {
console.error('❌ Error checking git files:', error.message);
}
if (filesFound) {
console.error('❌ Security check failed. Please remove the sensitive files or add them to .gitignore.');
process.exit(1);
} else {
console.log('✅ Security check passed. No sensitive files detected.');
process.exit(0);
}