-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
262 lines (242 loc) · 8.7 KB
/
index.js
File metadata and controls
262 lines (242 loc) · 8.7 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
/**
* hardstop-patterns — Security patterns for command and file access validation
*
* Single source of truth for HardStop detection patterns.
* Requires Node.js 16+.
*/
const fs = require('fs');
const path = require('path');
const patternsDir = path.join(__dirname, 'patterns');
/**
* Load and parse a pattern JSON file. Wraps in try/catch so a single
* corrupted file doesn't hard-crash the entire process.
*/
function loadPatterns(filename) {
const filepath = path.join(patternsDir, filename);
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (err) {
const wrapped = new Error(`hardstop-patterns: failed to load ${filename}: ${err.message}`);
wrapped.cause = err;
throw wrapped;
}
}
// Pre-compiled regex cache: patternFile object -> Array<{pattern, regex}>
const _regexCache = new Map();
/**
* Compile all patterns in a file, respecting match_mode.
* - "fullmatch": wraps pattern in ^(?:...)$ if not already anchored
* - "search": uses pattern as-is
* Individual bad regexes are skipped (logged to stderr) rather than crashing.
*/
function getCompiledPatterns(patternFile) {
if (_regexCache.has(patternFile)) return _regexCache.get(patternFile);
const isFullMatch = patternFile.match_mode === 'fullmatch';
const compiled = [];
for (const p of patternFile.patterns) {
let src = p.pattern;
if (isFullMatch) {
// Enforce full-match semantics: wrap if not already anchored
const hasStart = src.startsWith('^');
const hasEnd = src.endsWith('$');
if (!hasStart || !hasEnd) {
src = '^(?:' + src.replace(/^\^/, '').replace(/\$$/, '') + ')$';
}
}
try {
compiled.push({ pattern: p, regex: new RegExp(src, 'i') });
} catch (err) {
// Skip bad regex rather than crashing — log for debugging
if (typeof process !== 'undefined' && process.stderr) {
process.stderr.write(`hardstop-patterns: bad regex in ${p.id}: ${err.message}\n`);
}
}
}
_regexCache.set(patternFile, compiled);
return compiled;
}
// Lazy-loaded pattern sets
let _bashDangerous, _bashSafe, _readDangerous, _readSensitive, _readSafe, _meta;
Object.defineProperties(module.exports, {
bashDangerous: {
get() { return _bashDangerous || (_bashDangerous = loadPatterns('bash-dangerous.json')); },
enumerable: true
},
bashSafe: {
get() { return _bashSafe || (_bashSafe = loadPatterns('bash-safe.json')); },
enumerable: true
},
readDangerous: {
get() { return _readDangerous || (_readDangerous = loadPatterns('read-dangerous.json')); },
enumerable: true
},
readSensitive: {
get() { return _readSensitive || (_readSensitive = loadPatterns('read-sensitive.json')); },
enumerable: true
},
readSafe: {
get() { return _readSafe || (_readSafe = loadPatterns('read-safe.json')); },
enumerable: true
},
meta: {
get() { return _meta || (_meta = loadPatterns('meta.json')); },
enumerable: true
},
version: {
get() { return require('./package.json').version; },
enumerable: true
}
});
/**
* Detect current platform as a pattern platform string.
* @returns {'linux'|'macos'|'windows'}
*/
function detectPlatform() {
if (typeof process === 'undefined') return 'linux';
switch (process.platform) {
case 'win32': return 'windows';
case 'darwin': return 'macos';
default: return 'linux';
}
}
/** @type {'linux'|'macos'|'windows'} */
let _detectedPlatform;
function getCurrentPlatform() {
return _detectedPlatform || (_detectedPlatform = detectPlatform());
}
/**
* Check if a pattern applies to the given platform.
* @param {object} pattern - Pattern object with platforms array
* @param {string|null} platform - Platform to check, or null to skip filtering
*/
function matchesPlatform(pattern, platform) {
if (!platform) return true;
if (!pattern.platforms || pattern.platforms.length === 0) return true;
return pattern.platforms.includes(platform);
}
/**
* Check if a command matches any dangerous bash pattern.
* @param {string} command - The shell command to check
* @param {{ platform?: string|null|'auto' }} [options] - Options. platform: 'auto' (default) uses OS detection, null disables filtering, or specify 'linux'|'macos'|'windows'
* @returns {{ matched: boolean, pattern?: object }}
*/
module.exports.checkBashDangerous = function checkBashDangerous(command, options) {
if (typeof command !== 'string') return { matched: false };
const platform = resolvePlatform(options);
const compiled = getCompiledPatterns(module.exports.bashDangerous);
for (const { pattern, regex } of compiled) {
if (matchesPlatform(pattern, platform) && regex.test(command)) {
return { matched: true, pattern };
}
}
return { matched: false };
};
/**
* Check if a command matches a safe bash pattern (full match enforced).
* @param {string} command - The shell command to check
* @param {{ platform?: string|null|'auto' }} [options]
* @returns {{ matched: boolean, pattern?: object }}
*/
module.exports.checkBashSafe = function checkBashSafe(command, options) {
if (typeof command !== 'string') return { matched: false };
const trimmed = command.trim();
const platform = resolvePlatform(options);
const compiled = getCompiledPatterns(module.exports.bashSafe);
for (const { pattern, regex } of compiled) {
if (matchesPlatform(pattern, platform) && regex.test(trimmed)) {
return { matched: true, pattern };
}
}
return { matched: false };
};
/**
* Check if a file path matches any dangerous read pattern.
* @param {string} filePath - The file path to check
* @param {{ platform?: string|null|'auto' }} [options]
* @returns {{ matched: boolean, pattern?: object }}
*/
module.exports.checkReadDangerous = function checkReadDangerous(filePath, options) {
if (typeof filePath !== 'string') return { matched: false };
const normalized = filePath.replace(/\\/g, '/');
const platform = resolvePlatform(options);
const compiled = getCompiledPatterns(module.exports.readDangerous);
for (const { pattern, regex } of compiled) {
if (matchesPlatform(pattern, platform) && regex.test(normalized)) {
return { matched: true, pattern };
}
}
return { matched: false };
};
/**
* Check if a file path matches any sensitive read pattern.
* @param {string} filePath - The file path to check
* @param {{ platform?: string|null|'auto' }} [options]
* @returns {{ matched: boolean, pattern?: object }}
*/
module.exports.checkReadSensitive = function checkReadSensitive(filePath, options) {
if (typeof filePath !== 'string') return { matched: false };
const normalized = filePath.replace(/\\/g, '/');
const platform = resolvePlatform(options);
const compiled = getCompiledPatterns(module.exports.readSensitive);
for (const { pattern, regex } of compiled) {
if (matchesPlatform(pattern, platform) && regex.test(normalized)) {
return { matched: true, pattern };
}
}
return { matched: false };
};
/**
* Check if a file path matches any safe read pattern.
* @param {string} filePath - The file path to check
* @param {{ platform?: string|null|'auto' }} [options]
* @returns {{ matched: boolean, pattern?: object }}
*/
module.exports.checkReadSafe = function checkReadSafe(filePath, options) {
if (typeof filePath !== 'string') return { matched: false };
const normalized = filePath.replace(/\\/g, '/');
const platform = resolvePlatform(options);
const compiled = getCompiledPatterns(module.exports.readSafe);
for (const { pattern, regex } of compiled) {
if (matchesPlatform(pattern, platform) && regex.test(normalized)) {
return { matched: true, pattern };
}
}
return { matched: false };
};
/**
* Preload and compile all pattern files. Call this at startup to avoid
* sync I/O latency on first check. Returns a promise that resolves
* when all patterns are loaded and compiled.
* @returns {Promise<void>}
*/
module.exports.preload = function preload() {
return new Promise((resolve, reject) => {
try {
// Touch all lazy getters to trigger loading
const files = [
module.exports.bashDangerous,
module.exports.bashSafe,
module.exports.readDangerous,
module.exports.readSensitive,
module.exports.readSafe,
module.exports.meta,
];
// Pre-compile all regex caches
files.forEach(f => { if (f.patterns) getCompiledPatterns(f); });
resolve();
} catch (err) {
reject(err);
}
});
};
/**
* Resolve the platform option.
* @param {{ platform?: string|null|'auto' }} [options]
* @returns {string|null}
*/
function resolvePlatform(options) {
if (!options || options.platform === undefined || options.platform === 'auto') {
return getCurrentPlatform();
}
return options.platform; // null = no filtering, or explicit 'linux'|'macos'|'windows'
}