just-bash 2.14.3 → 2.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/bin/chunks/{awk2-DB6QZAC5.js → awk2-RSUCURL4.js} +1 -1
- package/dist/bin/chunks/chunk-AX6NCIX6.js +140 -0
- package/dist/bin/chunks/chunk-EFPTD2CS.js +36 -0
- package/dist/bin/{shell/chunks/chunk-A5UGPLEI.js → chunks/chunk-EWDHVLQL.js} +11 -11
- package/dist/bin/{shell/chunks/chunk-22NDZXS5.js → chunks/chunk-WUYP7DID.js} +1 -1
- package/dist/bin/chunks/{flag-coverage-QMWFYZRC.js → flag-coverage-IK7WVGOO.js} +1 -1
- package/dist/bin/chunks/{python3-JUJKPBFB.js → python3-V2HDKCNM.js} +1 -1
- package/dist/bin/{shell/chunks/rg-DK6FRF6Q.js → chunks/rg-LA2FQFMS.js} +1 -1
- package/dist/bin/chunks/{sqlite3-CVNFMP3Z.js → sqlite3-JPU62KSL.js} +1 -1
- package/dist/bin/chunks/sqlite3-worker.js +1728 -0
- package/dist/bin/{shell/chunks/xan-3G6YA5O6.js → chunks/xan-MM3YYGD7.js} +1 -1
- package/dist/bin/just-bash.js +238 -238
- package/dist/bin/shell/chunks/{awk2-DB6QZAC5.js → awk2-RSUCURL4.js} +1 -1
- package/dist/bin/shell/chunks/chunk-AX6NCIX6.js +140 -0
- package/dist/bin/shell/chunks/chunk-EFPTD2CS.js +36 -0
- package/dist/bin/{chunks/chunk-A5UGPLEI.js → shell/chunks/chunk-EWDHVLQL.js} +11 -11
- package/dist/bin/{chunks/chunk-22NDZXS5.js → shell/chunks/chunk-WUYP7DID.js} +1 -1
- package/dist/bin/shell/chunks/{flag-coverage-QMWFYZRC.js → flag-coverage-IK7WVGOO.js} +1 -1
- package/dist/bin/shell/chunks/{python3-RRCCYJKA.js → python3-TG6BXZCZ.js} +1 -1
- package/dist/bin/{chunks/rg-DK6FRF6Q.js → shell/chunks/rg-LA2FQFMS.js} +1 -1
- package/dist/bin/shell/chunks/{sqlite3-CVNFMP3Z.js → sqlite3-JPU62KSL.js} +1 -1
- package/dist/bin/{chunks/xan-3G6YA5O6.js → shell/chunks/xan-MM3YYGD7.js} +1 -1
- package/dist/bin/shell/shell.js +225 -225
- package/dist/bundle/browser.js +465 -465
- package/dist/bundle/chunks/{awk2-RRKCSTJR.js → awk2-JFGEGLFP.js} +1 -1
- package/dist/bundle/chunks/chunk-5KI4QWT5.js +139 -0
- package/dist/bundle/chunks/{chunk-HP7NBOKN.js → chunk-D6EWIDSU.js} +11 -11
- package/dist/bundle/chunks/{chunk-U6NMAV24.js → chunk-MIMEPCXY.js} +1 -1
- package/dist/bundle/chunks/chunk-XKWWSDBF.js +35 -0
- package/dist/bundle/chunks/{flag-coverage-XLSDDMFQ.js → flag-coverage-QFOIESUP.js} +1 -1
- package/dist/bundle/chunks/{python3-BFV3N3GY.js → python3-DNGS4G3E.js} +1 -1
- package/dist/bundle/chunks/{rg-KYSTAD4Q.js → rg-RYV2N6B5.js} +1 -1
- package/dist/bundle/chunks/{sqlite3-56UMWEY3.js → sqlite3-TE5AIOTF.js} +1 -1
- package/dist/bundle/chunks/sqlite3-worker.js +1728 -0
- package/dist/bundle/chunks/{xan-EY6QWK2V.js → xan-X2BIJPJV.js} +1 -1
- package/dist/bundle/index.cjs +713 -713
- package/dist/bundle/index.js +174 -174
- package/dist/commands/sqlite3/sqlite3.d.ts +1 -0
- package/dist/commands/sqlite3/worker.js +1728 -0
- package/dist/network/dns-pin.d.ts +43 -0
- package/package.json +3 -2
- package/dist/bin/chunks/chunk-CM4532DS.js +0 -36
- package/dist/bin/chunks/chunk-LTL632J4.js +0 -140
- package/dist/bin/shell/chunks/chunk-CM4532DS.js +0 -36
- package/dist/bin/shell/chunks/chunk-LTL632J4.js +0 -140
- package/dist/bundle/chunks/chunk-HWBSOZZR.js +0 -35
- package/dist/bundle/chunks/chunk-RAFHQYLP.js +0 -139
|
@@ -0,0 +1,1728 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// src/commands/sqlite3/worker.ts
|
|
9
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
10
|
+
import initSqlJs from "sql.js";
|
|
11
|
+
|
|
12
|
+
// src/fs/sanitize-error.ts
|
|
13
|
+
function sanitizeWithUnixPrefixes(message, includeHostRuntimePrefixes, includeFileUrls) {
|
|
14
|
+
if (!message) return message;
|
|
15
|
+
let sanitized = message.replace(/\n\s+at\s.*/g, "");
|
|
16
|
+
if (includeFileUrls) {
|
|
17
|
+
sanitized = sanitized.replace(/\bfile:\/\/\/?[^\s'",)}\]:]+/g, "<path>");
|
|
18
|
+
}
|
|
19
|
+
sanitized = sanitized.replace(
|
|
20
|
+
includeHostRuntimePrefixes ? /(?:\/(?:Users|home|private|var|opt|Library|System|usr|etc|tmp|nix|snap|workspace|root|srv|mnt|app))\b[^\s'",)}\]:]*/g : /(?:\/(?:Users|home|private|var|opt|Library|System|usr|etc|tmp|nix|snap))\b[^\s'",)}\]:]*/g,
|
|
21
|
+
"<path>"
|
|
22
|
+
);
|
|
23
|
+
sanitized = sanitized.replace(/node:internal\/[^\s'",)}\]:]+/g, "<internal>");
|
|
24
|
+
sanitized = sanitized.replace(/[A-Z]:\\[^\s'",)}\]:]+/g, "<path>");
|
|
25
|
+
if (includeFileUrls) {
|
|
26
|
+
sanitized = sanitized.replace(/\\\\[^\s\\]+\\[^\s'",)}\]:]+/g, "<path>");
|
|
27
|
+
}
|
|
28
|
+
return sanitized;
|
|
29
|
+
}
|
|
30
|
+
function sanitizeErrorMessage(message) {
|
|
31
|
+
return sanitizeWithUnixPrefixes(message, false, false);
|
|
32
|
+
}
|
|
33
|
+
function sanitizeHostErrorMessage(message) {
|
|
34
|
+
return sanitizeWithUnixPrefixes(message, true, true);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/security/blocked-globals.ts
|
|
38
|
+
function getBlockedGlobals() {
|
|
39
|
+
const globals = [
|
|
40
|
+
// Direct code execution vectors
|
|
41
|
+
{
|
|
42
|
+
prop: "Function",
|
|
43
|
+
target: globalThis,
|
|
44
|
+
violationType: "function_constructor",
|
|
45
|
+
strategy: "throw",
|
|
46
|
+
reason: "Function constructor allows arbitrary code execution"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
prop: "eval",
|
|
50
|
+
target: globalThis,
|
|
51
|
+
violationType: "eval",
|
|
52
|
+
strategy: "throw",
|
|
53
|
+
reason: "eval() allows arbitrary code execution"
|
|
54
|
+
},
|
|
55
|
+
// Timer functions with string argument allow code execution
|
|
56
|
+
{
|
|
57
|
+
prop: "setTimeout",
|
|
58
|
+
target: globalThis,
|
|
59
|
+
violationType: "setTimeout",
|
|
60
|
+
strategy: "throw",
|
|
61
|
+
reason: "setTimeout with string argument allows code execution"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
prop: "setInterval",
|
|
65
|
+
target: globalThis,
|
|
66
|
+
violationType: "setInterval",
|
|
67
|
+
strategy: "throw",
|
|
68
|
+
reason: "setInterval with string argument allows code execution"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
prop: "setImmediate",
|
|
72
|
+
target: globalThis,
|
|
73
|
+
violationType: "setImmediate",
|
|
74
|
+
strategy: "throw",
|
|
75
|
+
reason: "setImmediate could be used to escape sandbox context"
|
|
76
|
+
},
|
|
77
|
+
// Note: We intentionally do NOT block `process` entirely because:
|
|
78
|
+
// 1. Node.js internals (Promise resolution, etc.) use process.nextTick
|
|
79
|
+
// 2. Blocking process entirely breaks normal async operation
|
|
80
|
+
// 3. The primary code execution vectors (Function, eval) are already blocked
|
|
81
|
+
// However, we DO block specific dangerous process properties.
|
|
82
|
+
{
|
|
83
|
+
prop: "env",
|
|
84
|
+
target: process,
|
|
85
|
+
violationType: "process_env",
|
|
86
|
+
strategy: "throw",
|
|
87
|
+
reason: "process.env could leak sensitive environment variables",
|
|
88
|
+
// Node.js internals and bundled dependencies read these env vars
|
|
89
|
+
// during module loading, file watching, and I/O within the
|
|
90
|
+
// AsyncLocalStorage context. None are user secrets.
|
|
91
|
+
allowedKeys: /* @__PURE__ */ new Set([
|
|
92
|
+
// Node.js core
|
|
93
|
+
"NODE_V8_COVERAGE",
|
|
94
|
+
"NODE_DEBUG",
|
|
95
|
+
"NODE_DEBUG_NATIVE",
|
|
96
|
+
"NODE_COMPILE_CACHE",
|
|
97
|
+
"WATCH_REPORT_DEPENDENCIES",
|
|
98
|
+
// Dependencies
|
|
99
|
+
"FORCE_COLOR",
|
|
100
|
+
// chalk/supports-color
|
|
101
|
+
"DEBUG",
|
|
102
|
+
// debug package
|
|
103
|
+
"UNDICI_NO_FG",
|
|
104
|
+
// undici (Node.js fetch)
|
|
105
|
+
"JEST_WORKER_ID",
|
|
106
|
+
// jest/vitest worker detection
|
|
107
|
+
"__MINIMATCH_TESTING_PLATFORM__",
|
|
108
|
+
// minimatch
|
|
109
|
+
"LOG_TOKENS",
|
|
110
|
+
// query engine debug logging
|
|
111
|
+
"LOG_STREAM"
|
|
112
|
+
// query engine debug logging
|
|
113
|
+
])
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
prop: "binding",
|
|
117
|
+
target: process,
|
|
118
|
+
violationType: "process_binding",
|
|
119
|
+
strategy: "throw",
|
|
120
|
+
reason: "process.binding provides access to native Node.js modules"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
prop: "_linkedBinding",
|
|
124
|
+
target: process,
|
|
125
|
+
violationType: "process_binding",
|
|
126
|
+
strategy: "throw",
|
|
127
|
+
reason: "process._linkedBinding provides access to native Node.js modules"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
prop: "dlopen",
|
|
131
|
+
target: process,
|
|
132
|
+
violationType: "process_dlopen",
|
|
133
|
+
strategy: "throw",
|
|
134
|
+
reason: "process.dlopen allows loading native addons"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
prop: "getBuiltinModule",
|
|
138
|
+
target: process,
|
|
139
|
+
violationType: "process_get_builtin_module",
|
|
140
|
+
strategy: "throw",
|
|
141
|
+
reason: "process.getBuiltinModule allows loading native Node.js modules (fs, child_process, vm)"
|
|
142
|
+
},
|
|
143
|
+
// Note: process.mainModule is handled specially in defense-in-depth-box.ts
|
|
144
|
+
// and worker-defense-in-depth.ts because it may be undefined in ESM contexts
|
|
145
|
+
// but we still want to block both reading and setting it.
|
|
146
|
+
// Process control vectors
|
|
147
|
+
{
|
|
148
|
+
prop: "exit",
|
|
149
|
+
target: process,
|
|
150
|
+
violationType: "process_exit",
|
|
151
|
+
strategy: "throw",
|
|
152
|
+
reason: "process.exit could terminate the interpreter"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
prop: "abort",
|
|
156
|
+
target: process,
|
|
157
|
+
violationType: "process_exit",
|
|
158
|
+
strategy: "throw",
|
|
159
|
+
reason: "process.abort could crash the interpreter"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
prop: "kill",
|
|
163
|
+
target: process,
|
|
164
|
+
violationType: "process_kill",
|
|
165
|
+
strategy: "throw",
|
|
166
|
+
reason: "process.kill could signal other processes"
|
|
167
|
+
},
|
|
168
|
+
// Privilege escalation vectors
|
|
169
|
+
{
|
|
170
|
+
prop: "setuid",
|
|
171
|
+
target: process,
|
|
172
|
+
violationType: "process_setuid",
|
|
173
|
+
strategy: "throw",
|
|
174
|
+
reason: "process.setuid could escalate privileges"
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
prop: "setgid",
|
|
178
|
+
target: process,
|
|
179
|
+
violationType: "process_setuid",
|
|
180
|
+
strategy: "throw",
|
|
181
|
+
reason: "process.setgid could escalate privileges"
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
prop: "seteuid",
|
|
185
|
+
target: process,
|
|
186
|
+
violationType: "process_setuid",
|
|
187
|
+
strategy: "throw",
|
|
188
|
+
reason: "process.seteuid could escalate effective user privileges"
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
prop: "setegid",
|
|
192
|
+
target: process,
|
|
193
|
+
violationType: "process_setuid",
|
|
194
|
+
strategy: "throw",
|
|
195
|
+
reason: "process.setegid could escalate effective group privileges"
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
prop: "initgroups",
|
|
199
|
+
target: process,
|
|
200
|
+
violationType: "process_setuid",
|
|
201
|
+
strategy: "throw",
|
|
202
|
+
reason: "process.initgroups could modify supplementary group IDs"
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
prop: "setgroups",
|
|
206
|
+
target: process,
|
|
207
|
+
violationType: "process_setuid",
|
|
208
|
+
strategy: "throw",
|
|
209
|
+
reason: "process.setgroups could modify supplementary group IDs"
|
|
210
|
+
},
|
|
211
|
+
// File permission manipulation
|
|
212
|
+
{
|
|
213
|
+
prop: "umask",
|
|
214
|
+
target: process,
|
|
215
|
+
violationType: "process_umask",
|
|
216
|
+
strategy: "throw",
|
|
217
|
+
reason: "process.umask could modify file creation permissions"
|
|
218
|
+
},
|
|
219
|
+
// Information disclosure vectors
|
|
220
|
+
// Note: process.argv is an array (object) so gets an object proxy
|
|
221
|
+
{
|
|
222
|
+
prop: "argv",
|
|
223
|
+
target: process,
|
|
224
|
+
violationType: "process_argv",
|
|
225
|
+
strategy: "throw",
|
|
226
|
+
reason: "process.argv may contain secrets in CLI arguments"
|
|
227
|
+
},
|
|
228
|
+
// Note: process.execPath is a string primitive, handled specially
|
|
229
|
+
// in defense-in-depth-box.ts and worker-defense-in-depth.ts
|
|
230
|
+
// Note: process.connected is a boolean primitive, handled specially
|
|
231
|
+
// in defense-in-depth-box.ts and worker-defense-in-depth.ts
|
|
232
|
+
// Working directory access/manipulation
|
|
233
|
+
{
|
|
234
|
+
prop: "cwd",
|
|
235
|
+
target: process,
|
|
236
|
+
violationType: "process_chdir",
|
|
237
|
+
strategy: "throw",
|
|
238
|
+
reason: "process.cwd could disclose real host working directory path"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
prop: "chdir",
|
|
242
|
+
target: process,
|
|
243
|
+
violationType: "process_chdir",
|
|
244
|
+
strategy: "throw",
|
|
245
|
+
reason: "process.chdir could confuse the interpreter's CWD tracking"
|
|
246
|
+
},
|
|
247
|
+
// Diagnostic report (leaks full environment, host paths, system info)
|
|
248
|
+
{
|
|
249
|
+
prop: "report",
|
|
250
|
+
target: process,
|
|
251
|
+
violationType: "process_report",
|
|
252
|
+
strategy: "throw",
|
|
253
|
+
reason: "process.report could disclose full environment, host paths, and system info"
|
|
254
|
+
},
|
|
255
|
+
// Environment file loading (Node 21.7+)
|
|
256
|
+
{
|
|
257
|
+
prop: "loadEnvFile",
|
|
258
|
+
target: process,
|
|
259
|
+
violationType: "process_env",
|
|
260
|
+
strategy: "throw",
|
|
261
|
+
reason: "process.loadEnvFile could load env files bypassing env proxy"
|
|
262
|
+
},
|
|
263
|
+
// Exception handler manipulation
|
|
264
|
+
{
|
|
265
|
+
prop: "setUncaughtExceptionCaptureCallback",
|
|
266
|
+
target: process,
|
|
267
|
+
violationType: "process_exception_handler",
|
|
268
|
+
strategy: "throw",
|
|
269
|
+
reason: "setUncaughtExceptionCaptureCallback could intercept security errors"
|
|
270
|
+
},
|
|
271
|
+
// IPC communication vectors (may be undefined in non-IPC contexts)
|
|
272
|
+
{
|
|
273
|
+
prop: "send",
|
|
274
|
+
target: process,
|
|
275
|
+
violationType: "process_send",
|
|
276
|
+
strategy: "throw",
|
|
277
|
+
reason: "process.send could communicate with parent process in IPC contexts"
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
prop: "channel",
|
|
281
|
+
target: process,
|
|
282
|
+
violationType: "process_channel",
|
|
283
|
+
strategy: "throw",
|
|
284
|
+
reason: "process.channel could access IPC channel to parent process"
|
|
285
|
+
},
|
|
286
|
+
// Timing side-channel vectors
|
|
287
|
+
{
|
|
288
|
+
prop: "cpuUsage",
|
|
289
|
+
target: process,
|
|
290
|
+
violationType: "process_timing",
|
|
291
|
+
strategy: "throw",
|
|
292
|
+
reason: "process.cpuUsage could enable timing side-channel attacks"
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
prop: "memoryUsage",
|
|
296
|
+
target: process,
|
|
297
|
+
violationType: "process_timing",
|
|
298
|
+
strategy: "throw",
|
|
299
|
+
reason: "process.memoryUsage could enable timing side-channel attacks"
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
prop: "hrtime",
|
|
303
|
+
target: process,
|
|
304
|
+
violationType: "process_timing",
|
|
305
|
+
strategy: "throw",
|
|
306
|
+
reason: "process.hrtime could enable timing side-channel attacks"
|
|
307
|
+
},
|
|
308
|
+
// We also don't block `require` because:
|
|
309
|
+
// 1. It may not exist in all environments (ESM)
|
|
310
|
+
// 2. import() is the modern escape vector and can't be blocked this way
|
|
311
|
+
// Reference leak vectors
|
|
312
|
+
{
|
|
313
|
+
prop: "WeakRef",
|
|
314
|
+
target: globalThis,
|
|
315
|
+
violationType: "weak_ref",
|
|
316
|
+
strategy: "throw",
|
|
317
|
+
reason: "WeakRef could be used to leak references outside sandbox"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
prop: "FinalizationRegistry",
|
|
321
|
+
target: globalThis,
|
|
322
|
+
violationType: "finalization_registry",
|
|
323
|
+
strategy: "throw",
|
|
324
|
+
reason: "FinalizationRegistry could be used to leak references outside sandbox"
|
|
325
|
+
},
|
|
326
|
+
// Introspection/interception vectors (freeze instead of throw)
|
|
327
|
+
// SECURITY RATIONALE: Reflect is frozen (not blocked) because:
|
|
328
|
+
// 1. Defense infrastructure uses Reflect.apply/get/set/construct internally
|
|
329
|
+
// 2. Frozen Reflect cannot be mutated but remains fully functional
|
|
330
|
+
// 3. Reflect.construct(Function, ['code']) IS safe because globalThis.Function
|
|
331
|
+
// is replaced with a blocking proxy — Reflect.construct receives the proxy
|
|
332
|
+
// 4. Security depends on NEVER leaking original Function/eval references.
|
|
333
|
+
// If an unpatched Function ref leaked, Reflect.construct would bypass defense.
|
|
334
|
+
{
|
|
335
|
+
prop: "Reflect",
|
|
336
|
+
target: globalThis,
|
|
337
|
+
violationType: "reflect",
|
|
338
|
+
strategy: "freeze",
|
|
339
|
+
reason: "Reflect provides introspection capabilities"
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
prop: "Proxy",
|
|
343
|
+
target: globalThis,
|
|
344
|
+
violationType: "proxy",
|
|
345
|
+
strategy: "throw",
|
|
346
|
+
reason: "Proxy allows intercepting and modifying object behavior"
|
|
347
|
+
},
|
|
348
|
+
// WebAssembly allows arbitrary code execution
|
|
349
|
+
{
|
|
350
|
+
prop: "WebAssembly",
|
|
351
|
+
target: globalThis,
|
|
352
|
+
violationType: "webassembly",
|
|
353
|
+
strategy: "throw",
|
|
354
|
+
reason: "WebAssembly allows executing arbitrary compiled code"
|
|
355
|
+
},
|
|
356
|
+
// SharedArrayBuffer and Atomics can enable side-channel attacks
|
|
357
|
+
{
|
|
358
|
+
prop: "SharedArrayBuffer",
|
|
359
|
+
target: globalThis,
|
|
360
|
+
violationType: "shared_array_buffer",
|
|
361
|
+
strategy: "throw",
|
|
362
|
+
reason: "SharedArrayBuffer could enable side-channel communication or timing attacks"
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
prop: "Atomics",
|
|
366
|
+
target: globalThis,
|
|
367
|
+
violationType: "atomics",
|
|
368
|
+
strategy: "throw",
|
|
369
|
+
reason: "Atomics could enable side-channel communication or timing attacks"
|
|
370
|
+
},
|
|
371
|
+
// Note: Error.prepareStackTrace is handled specially in defense-in-depth-box.ts
|
|
372
|
+
// because we only want to block SETTING it, not reading (V8 reads it internally)
|
|
373
|
+
// Timing side-channel: performance.now() provides sub-millisecond resolution
|
|
374
|
+
// Note: Date.now() is intentionally NOT blocked — it's used for $SECONDS,
|
|
375
|
+
// date command, and has only ~1ms resolution (vs process.hrtime at ns).
|
|
376
|
+
{
|
|
377
|
+
prop: "performance",
|
|
378
|
+
target: globalThis,
|
|
379
|
+
violationType: "performance_timing",
|
|
380
|
+
strategy: "throw",
|
|
381
|
+
reason: "performance.now() provides sub-millisecond timing for side-channel attacks"
|
|
382
|
+
},
|
|
383
|
+
// Block direct access to process.stdout and process.stderr to prevent
|
|
384
|
+
// writing to the host's actual stdout/stderr, bypassing the interpreter's
|
|
385
|
+
// output accumulation.
|
|
386
|
+
{
|
|
387
|
+
prop: "stdout",
|
|
388
|
+
target: process,
|
|
389
|
+
violationType: "process_stdout",
|
|
390
|
+
strategy: "throw",
|
|
391
|
+
reason: "process.stdout could bypass interpreter output to write to host stdout"
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
prop: "stderr",
|
|
395
|
+
target: process,
|
|
396
|
+
violationType: "process_stderr",
|
|
397
|
+
strategy: "throw",
|
|
398
|
+
reason: "process.stderr could bypass interpreter output to write to host stderr"
|
|
399
|
+
},
|
|
400
|
+
// Prototype pollution vectors
|
|
401
|
+
{
|
|
402
|
+
prop: "__defineGetter__",
|
|
403
|
+
target: Object.prototype,
|
|
404
|
+
violationType: "prototype_mutation",
|
|
405
|
+
strategy: "throw",
|
|
406
|
+
reason: "__defineGetter__ allows prototype pollution via getter injection"
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
prop: "__defineSetter__",
|
|
410
|
+
target: Object.prototype,
|
|
411
|
+
violationType: "prototype_mutation",
|
|
412
|
+
strategy: "throw",
|
|
413
|
+
reason: "__defineSetter__ allows prototype pollution via setter injection"
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
prop: "__lookupGetter__",
|
|
417
|
+
target: Object.prototype,
|
|
418
|
+
violationType: "prototype_mutation",
|
|
419
|
+
strategy: "throw",
|
|
420
|
+
reason: "__lookupGetter__ enables introspection for prototype pollution attacks"
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
prop: "__lookupSetter__",
|
|
424
|
+
target: Object.prototype,
|
|
425
|
+
violationType: "prototype_mutation",
|
|
426
|
+
strategy: "throw",
|
|
427
|
+
reason: "__lookupSetter__ enables introspection for prototype pollution attacks"
|
|
428
|
+
},
|
|
429
|
+
// Freeze JSON and Math to prevent mutation of built-in utility objects
|
|
430
|
+
{
|
|
431
|
+
prop: "JSON",
|
|
432
|
+
target: globalThis,
|
|
433
|
+
violationType: "json_mutation",
|
|
434
|
+
strategy: "freeze",
|
|
435
|
+
reason: "Freeze JSON to prevent mutation of parsing/serialization"
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
prop: "Math",
|
|
439
|
+
target: globalThis,
|
|
440
|
+
violationType: "math_mutation",
|
|
441
|
+
strategy: "freeze",
|
|
442
|
+
reason: "Freeze Math to prevent mutation of math utilities"
|
|
443
|
+
}
|
|
444
|
+
];
|
|
445
|
+
try {
|
|
446
|
+
const AsyncFunction = Object.getPrototypeOf(async () => {
|
|
447
|
+
}).constructor;
|
|
448
|
+
if (AsyncFunction && AsyncFunction !== Function) {
|
|
449
|
+
globals.push({
|
|
450
|
+
prop: "constructor",
|
|
451
|
+
target: Object.getPrototypeOf(async () => {
|
|
452
|
+
}),
|
|
453
|
+
violationType: "async_function_constructor",
|
|
454
|
+
strategy: "throw",
|
|
455
|
+
reason: "AsyncFunction constructor allows arbitrary async code execution"
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
} catch {
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
const GeneratorFunction = Object.getPrototypeOf(
|
|
462
|
+
function* () {
|
|
463
|
+
}
|
|
464
|
+
).constructor;
|
|
465
|
+
if (GeneratorFunction && GeneratorFunction !== Function) {
|
|
466
|
+
globals.push({
|
|
467
|
+
prop: "constructor",
|
|
468
|
+
target: Object.getPrototypeOf(function* () {
|
|
469
|
+
}),
|
|
470
|
+
violationType: "generator_function_constructor",
|
|
471
|
+
strategy: "throw",
|
|
472
|
+
reason: "GeneratorFunction constructor allows arbitrary generator code execution"
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
} catch {
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
const AsyncGeneratorFunction = Object.getPrototypeOf(
|
|
479
|
+
async function* () {
|
|
480
|
+
}
|
|
481
|
+
).constructor;
|
|
482
|
+
if (AsyncGeneratorFunction && AsyncGeneratorFunction !== Function && AsyncGeneratorFunction !== Object.getPrototypeOf(async () => {
|
|
483
|
+
}).constructor) {
|
|
484
|
+
globals.push({
|
|
485
|
+
prop: "constructor",
|
|
486
|
+
target: Object.getPrototypeOf(async function* () {
|
|
487
|
+
}),
|
|
488
|
+
violationType: "async_generator_function_constructor",
|
|
489
|
+
strategy: "throw",
|
|
490
|
+
reason: "AsyncGeneratorFunction constructor allows arbitrary async generator code execution"
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
} catch {
|
|
494
|
+
}
|
|
495
|
+
return globals.filter((g) => {
|
|
496
|
+
try {
|
|
497
|
+
return g.target[g.prop] !== void 0;
|
|
498
|
+
} catch {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/security/defense-in-depth-box.ts
|
|
505
|
+
var IS_BROWSER = typeof __BROWSER__ !== "undefined" && __BROWSER__;
|
|
506
|
+
var AsyncLocalStorageClass = null;
|
|
507
|
+
if (!IS_BROWSER) {
|
|
508
|
+
try {
|
|
509
|
+
const { AsyncLocalStorage } = __require("node:async_hooks");
|
|
510
|
+
AsyncLocalStorageClass = AsyncLocalStorage;
|
|
511
|
+
} catch {
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
var executionContext = !IS_BROWSER && AsyncLocalStorageClass ? new AsyncLocalStorageClass() : null;
|
|
515
|
+
|
|
516
|
+
// src/security/worker-defense-in-depth.ts
|
|
517
|
+
var DEFENSE_IN_DEPTH_NOTICE = "\n\nThis is a defense-in-depth measure and indicates a bug in just-bash. Please report this at security@vercel.com";
|
|
518
|
+
var WorkerSecurityViolationError = class extends Error {
|
|
519
|
+
constructor(message, violation) {
|
|
520
|
+
super(message + DEFENSE_IN_DEPTH_NOTICE);
|
|
521
|
+
this.violation = violation;
|
|
522
|
+
this.name = "WorkerSecurityViolationError";
|
|
523
|
+
}
|
|
524
|
+
violation;
|
|
525
|
+
};
|
|
526
|
+
var MAX_STORED_VIOLATIONS = 1e3;
|
|
527
|
+
function generateExecutionId() {
|
|
528
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
529
|
+
return crypto.randomUUID();
|
|
530
|
+
}
|
|
531
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
532
|
+
const r = Math.random() * 16 | 0;
|
|
533
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
534
|
+
return v.toString(16);
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
var WorkerDefenseInDepth = class {
|
|
538
|
+
config;
|
|
539
|
+
isActivated = false;
|
|
540
|
+
originalDescriptors = [];
|
|
541
|
+
violations = [];
|
|
542
|
+
executionId;
|
|
543
|
+
/**
|
|
544
|
+
* Original Proxy constructor, captured before patching.
|
|
545
|
+
* This is captured at instance creation time to ensure we get the unpatched version.
|
|
546
|
+
*/
|
|
547
|
+
originalProxy;
|
|
548
|
+
/**
|
|
549
|
+
* Recursion guard to prevent infinite loops when proxy traps trigger
|
|
550
|
+
* code that accesses the same proxied object (e.g., process.env).
|
|
551
|
+
*/
|
|
552
|
+
inTrap = false;
|
|
553
|
+
/**
|
|
554
|
+
* Create and activate the worker defense layer.
|
|
555
|
+
*
|
|
556
|
+
* @param config - Configuration for the defense layer
|
|
557
|
+
*/
|
|
558
|
+
constructor(config) {
|
|
559
|
+
this.originalProxy = Proxy;
|
|
560
|
+
this.config = config;
|
|
561
|
+
this.executionId = generateExecutionId();
|
|
562
|
+
if (config.enabled !== false) {
|
|
563
|
+
this.activate();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Get statistics about the defense layer.
|
|
568
|
+
*/
|
|
569
|
+
getStats() {
|
|
570
|
+
return {
|
|
571
|
+
violationsBlocked: this.violations.length,
|
|
572
|
+
violations: [...this.violations],
|
|
573
|
+
isActive: this.isActivated
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Clear stored violations. Useful for testing.
|
|
578
|
+
*/
|
|
579
|
+
clearViolations() {
|
|
580
|
+
this.violations = [];
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Get the execution ID for this worker.
|
|
584
|
+
*/
|
|
585
|
+
getExecutionId() {
|
|
586
|
+
return this.executionId;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Deactivate the defense layer and restore original globals.
|
|
590
|
+
* Typically only needed for testing.
|
|
591
|
+
*/
|
|
592
|
+
deactivate() {
|
|
593
|
+
if (!this.isActivated) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
this.restorePatches();
|
|
597
|
+
this.isActivated = false;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Activate the defense layer by applying patches.
|
|
601
|
+
*/
|
|
602
|
+
activate() {
|
|
603
|
+
if (this.isActivated) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
this.applyPatches();
|
|
607
|
+
this.isActivated = true;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Get a human-readable path for a target object and property.
|
|
611
|
+
*/
|
|
612
|
+
getPathForTarget(target, prop) {
|
|
613
|
+
if (target === globalThis) {
|
|
614
|
+
return `globalThis.${prop}`;
|
|
615
|
+
}
|
|
616
|
+
if (typeof process !== "undefined" && target === process) {
|
|
617
|
+
return `process.${prop}`;
|
|
618
|
+
}
|
|
619
|
+
if (target === Error) {
|
|
620
|
+
return `Error.${prop}`;
|
|
621
|
+
}
|
|
622
|
+
if (target === Function.prototype) {
|
|
623
|
+
return `Function.prototype.${prop}`;
|
|
624
|
+
}
|
|
625
|
+
if (target === Object.prototype) {
|
|
626
|
+
return `Object.prototype.${prop}`;
|
|
627
|
+
}
|
|
628
|
+
return `<object>.${prop}`;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Record a violation and invoke the callback.
|
|
632
|
+
* In worker context, blocking always happens (no audit mode context check).
|
|
633
|
+
*/
|
|
634
|
+
recordViolation(type, path, message) {
|
|
635
|
+
const violation = {
|
|
636
|
+
timestamp: Date.now(),
|
|
637
|
+
type,
|
|
638
|
+
message,
|
|
639
|
+
path,
|
|
640
|
+
stack: new Error().stack,
|
|
641
|
+
executionId: this.executionId
|
|
642
|
+
};
|
|
643
|
+
if (this.violations.length < MAX_STORED_VIOLATIONS) {
|
|
644
|
+
this.violations.push(violation);
|
|
645
|
+
}
|
|
646
|
+
if (this.config.onViolation) {
|
|
647
|
+
try {
|
|
648
|
+
this.config.onViolation(violation);
|
|
649
|
+
} catch (e) {
|
|
650
|
+
console.debug(
|
|
651
|
+
"[WorkerDefenseInDepth] onViolation callback threw:",
|
|
652
|
+
e instanceof Error ? e.message : e
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return violation;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Create a blocking proxy for a function.
|
|
660
|
+
* In worker context, always blocks (no context check needed).
|
|
661
|
+
*/
|
|
662
|
+
createBlockingProxy(original, path, violationType) {
|
|
663
|
+
const self = this;
|
|
664
|
+
const auditMode = this.config.auditMode;
|
|
665
|
+
return new this.originalProxy(original, {
|
|
666
|
+
apply(target, thisArg, args) {
|
|
667
|
+
const message = `${path} is blocked in worker context`;
|
|
668
|
+
const violation = self.recordViolation(violationType, path, message);
|
|
669
|
+
if (!auditMode) {
|
|
670
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
671
|
+
}
|
|
672
|
+
return Reflect.apply(target, thisArg, args);
|
|
673
|
+
},
|
|
674
|
+
construct(target, args, newTarget) {
|
|
675
|
+
const message = `${path} constructor is blocked in worker context`;
|
|
676
|
+
const violation = self.recordViolation(violationType, path, message);
|
|
677
|
+
if (!auditMode) {
|
|
678
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
679
|
+
}
|
|
680
|
+
return Reflect.construct(target, args, newTarget);
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Create a blocking proxy for an object (blocks all property access).
|
|
686
|
+
*/
|
|
687
|
+
createBlockingObjectProxy(original, path, violationType, allowedKeys) {
|
|
688
|
+
const self = this;
|
|
689
|
+
const auditMode = this.config.auditMode;
|
|
690
|
+
return new this.originalProxy(original, {
|
|
691
|
+
get(target, prop, receiver) {
|
|
692
|
+
if (self.inTrap) {
|
|
693
|
+
return Reflect.get(target, prop, receiver);
|
|
694
|
+
}
|
|
695
|
+
if (allowedKeys && typeof prop === "string" && allowedKeys.has(prop)) {
|
|
696
|
+
return Reflect.get(target, prop, receiver);
|
|
697
|
+
}
|
|
698
|
+
self.inTrap = true;
|
|
699
|
+
try {
|
|
700
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
701
|
+
const message = `${fullPath} is blocked in worker context`;
|
|
702
|
+
const violation = self.recordViolation(
|
|
703
|
+
violationType,
|
|
704
|
+
fullPath,
|
|
705
|
+
message
|
|
706
|
+
);
|
|
707
|
+
if (!auditMode) {
|
|
708
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
709
|
+
}
|
|
710
|
+
return Reflect.get(target, prop, receiver);
|
|
711
|
+
} finally {
|
|
712
|
+
self.inTrap = false;
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
set(target, prop, value, receiver) {
|
|
716
|
+
if (self.inTrap) {
|
|
717
|
+
return Reflect.set(target, prop, value, receiver);
|
|
718
|
+
}
|
|
719
|
+
self.inTrap = true;
|
|
720
|
+
try {
|
|
721
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
722
|
+
const message = `${fullPath} modification is blocked in worker context`;
|
|
723
|
+
const violation = self.recordViolation(
|
|
724
|
+
violationType,
|
|
725
|
+
fullPath,
|
|
726
|
+
message
|
|
727
|
+
);
|
|
728
|
+
if (!auditMode) {
|
|
729
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
730
|
+
}
|
|
731
|
+
return Reflect.set(target, prop, value, receiver);
|
|
732
|
+
} finally {
|
|
733
|
+
self.inTrap = false;
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
ownKeys(target) {
|
|
737
|
+
if (self.inTrap) {
|
|
738
|
+
return Reflect.ownKeys(target);
|
|
739
|
+
}
|
|
740
|
+
self.inTrap = true;
|
|
741
|
+
try {
|
|
742
|
+
const message = `${path} enumeration is blocked in worker context`;
|
|
743
|
+
const violation = self.recordViolation(violationType, path, message);
|
|
744
|
+
if (!auditMode) {
|
|
745
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
746
|
+
}
|
|
747
|
+
return Reflect.ownKeys(target);
|
|
748
|
+
} finally {
|
|
749
|
+
self.inTrap = false;
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
753
|
+
if (self.inTrap) {
|
|
754
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
755
|
+
}
|
|
756
|
+
self.inTrap = true;
|
|
757
|
+
try {
|
|
758
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
759
|
+
const message = `${fullPath} descriptor access is blocked in worker context`;
|
|
760
|
+
const violation = self.recordViolation(
|
|
761
|
+
violationType,
|
|
762
|
+
fullPath,
|
|
763
|
+
message
|
|
764
|
+
);
|
|
765
|
+
if (!auditMode) {
|
|
766
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
767
|
+
}
|
|
768
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
769
|
+
} finally {
|
|
770
|
+
self.inTrap = false;
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
has(target, prop) {
|
|
774
|
+
if (self.inTrap) {
|
|
775
|
+
return Reflect.has(target, prop);
|
|
776
|
+
}
|
|
777
|
+
self.inTrap = true;
|
|
778
|
+
try {
|
|
779
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
780
|
+
const message = `${fullPath} existence check is blocked in worker context`;
|
|
781
|
+
const violation = self.recordViolation(
|
|
782
|
+
violationType,
|
|
783
|
+
fullPath,
|
|
784
|
+
message
|
|
785
|
+
);
|
|
786
|
+
if (!auditMode) {
|
|
787
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
788
|
+
}
|
|
789
|
+
return Reflect.has(target, prop);
|
|
790
|
+
} finally {
|
|
791
|
+
self.inTrap = false;
|
|
792
|
+
}
|
|
793
|
+
},
|
|
794
|
+
deleteProperty(target, prop) {
|
|
795
|
+
if (self.inTrap) {
|
|
796
|
+
return Reflect.deleteProperty(target, prop);
|
|
797
|
+
}
|
|
798
|
+
self.inTrap = true;
|
|
799
|
+
try {
|
|
800
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
801
|
+
const message = `${fullPath} deletion is blocked in worker context`;
|
|
802
|
+
const violation = self.recordViolation(
|
|
803
|
+
violationType,
|
|
804
|
+
fullPath,
|
|
805
|
+
message
|
|
806
|
+
);
|
|
807
|
+
if (!auditMode) {
|
|
808
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
809
|
+
}
|
|
810
|
+
return Reflect.deleteProperty(target, prop);
|
|
811
|
+
} finally {
|
|
812
|
+
self.inTrap = false;
|
|
813
|
+
}
|
|
814
|
+
},
|
|
815
|
+
setPrototypeOf(target, proto) {
|
|
816
|
+
if (self.inTrap) {
|
|
817
|
+
return Reflect.setPrototypeOf(target, proto);
|
|
818
|
+
}
|
|
819
|
+
self.inTrap = true;
|
|
820
|
+
try {
|
|
821
|
+
const message = `${path} setPrototypeOf is blocked in worker context`;
|
|
822
|
+
const violation = self.recordViolation(violationType, path, message);
|
|
823
|
+
if (!auditMode) {
|
|
824
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
825
|
+
}
|
|
826
|
+
return Reflect.setPrototypeOf(target, proto);
|
|
827
|
+
} finally {
|
|
828
|
+
self.inTrap = false;
|
|
829
|
+
}
|
|
830
|
+
},
|
|
831
|
+
defineProperty(target, prop, descriptor) {
|
|
832
|
+
if (self.inTrap) {
|
|
833
|
+
return Reflect.defineProperty(target, prop, descriptor);
|
|
834
|
+
}
|
|
835
|
+
self.inTrap = true;
|
|
836
|
+
try {
|
|
837
|
+
const fullPath = `${path}.${String(prop)}`;
|
|
838
|
+
const message = `${fullPath} defineProperty is blocked in worker context`;
|
|
839
|
+
const violation = self.recordViolation(
|
|
840
|
+
violationType,
|
|
841
|
+
fullPath,
|
|
842
|
+
message
|
|
843
|
+
);
|
|
844
|
+
if (!auditMode) {
|
|
845
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
846
|
+
}
|
|
847
|
+
return Reflect.defineProperty(target, prop, descriptor);
|
|
848
|
+
} finally {
|
|
849
|
+
self.inTrap = false;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Apply security patches to dangerous globals.
|
|
856
|
+
*/
|
|
857
|
+
applyPatches() {
|
|
858
|
+
const blockedGlobals = getBlockedGlobals();
|
|
859
|
+
const excludeTypes = new Set(this.config.excludeViolationTypes ?? []);
|
|
860
|
+
for (const blocked of blockedGlobals) {
|
|
861
|
+
if (excludeTypes.has(blocked.violationType)) {
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
this.applyPatch(blocked);
|
|
865
|
+
}
|
|
866
|
+
if (!excludeTypes.has("function_constructor")) {
|
|
867
|
+
this.protectConstructorChain(excludeTypes);
|
|
868
|
+
}
|
|
869
|
+
if (!excludeTypes.has("error_prepare_stack_trace")) {
|
|
870
|
+
this.protectErrorPrepareStackTrace();
|
|
871
|
+
}
|
|
872
|
+
if (!excludeTypes.has("module_load")) {
|
|
873
|
+
this.protectModuleLoad();
|
|
874
|
+
}
|
|
875
|
+
if (!excludeTypes.has("module_resolve_filename")) {
|
|
876
|
+
this.protectModuleResolveFilename();
|
|
877
|
+
}
|
|
878
|
+
if (!excludeTypes.has("process_main_module")) {
|
|
879
|
+
this.protectProcessMainModule();
|
|
880
|
+
}
|
|
881
|
+
if (!excludeTypes.has("process_exec_path")) {
|
|
882
|
+
this.protectProcessExecPath();
|
|
883
|
+
}
|
|
884
|
+
if (!excludeTypes.has("process_connected")) {
|
|
885
|
+
this.protectProcessConnected();
|
|
886
|
+
}
|
|
887
|
+
this.lockWellKnownSymbols();
|
|
888
|
+
if (!excludeTypes.has("proxy")) {
|
|
889
|
+
this.protectProxyRevocable();
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Lock well-known Symbol properties on built-in constructors/prototypes.
|
|
894
|
+
*/
|
|
895
|
+
lockWellKnownSymbols() {
|
|
896
|
+
const lock = (obj, sym) => {
|
|
897
|
+
try {
|
|
898
|
+
const desc = Object.getOwnPropertyDescriptor(obj, sym);
|
|
899
|
+
if (desc?.configurable) {
|
|
900
|
+
if ("value" in desc) {
|
|
901
|
+
Object.defineProperty(obj, sym, {
|
|
902
|
+
...desc,
|
|
903
|
+
configurable: false,
|
|
904
|
+
writable: false
|
|
905
|
+
});
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
Object.defineProperty(obj, sym, { ...desc, configurable: false });
|
|
909
|
+
}
|
|
910
|
+
} catch {
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
for (const ctor of [Array, Map, Set, RegExp, Promise]) {
|
|
914
|
+
lock(ctor, Symbol.species);
|
|
915
|
+
}
|
|
916
|
+
for (const proto of [
|
|
917
|
+
Array.prototype,
|
|
918
|
+
String.prototype,
|
|
919
|
+
Map.prototype,
|
|
920
|
+
Set.prototype
|
|
921
|
+
]) {
|
|
922
|
+
lock(proto, Symbol.iterator);
|
|
923
|
+
}
|
|
924
|
+
lock(Symbol.prototype, Symbol.toPrimitive);
|
|
925
|
+
lock(Date.prototype, Symbol.toPrimitive);
|
|
926
|
+
for (const sym of [
|
|
927
|
+
Symbol.match,
|
|
928
|
+
Symbol.matchAll,
|
|
929
|
+
Symbol.replace,
|
|
930
|
+
Symbol.search,
|
|
931
|
+
Symbol.split
|
|
932
|
+
]) {
|
|
933
|
+
lock(RegExp.prototype, sym);
|
|
934
|
+
}
|
|
935
|
+
lock(Function.prototype, Symbol.hasInstance);
|
|
936
|
+
lock(Array.prototype, Symbol.unscopables);
|
|
937
|
+
for (const proto of [
|
|
938
|
+
Map.prototype,
|
|
939
|
+
Set.prototype,
|
|
940
|
+
Promise.prototype,
|
|
941
|
+
ArrayBuffer.prototype
|
|
942
|
+
]) {
|
|
943
|
+
lock(proto, Symbol.toStringTag);
|
|
944
|
+
}
|
|
945
|
+
try {
|
|
946
|
+
const stackDesc = Object.getOwnPropertyDescriptor(
|
|
947
|
+
Error,
|
|
948
|
+
"stackTraceLimit"
|
|
949
|
+
);
|
|
950
|
+
this.originalDescriptors.push({
|
|
951
|
+
target: Error,
|
|
952
|
+
prop: "stackTraceLimit",
|
|
953
|
+
descriptor: stackDesc
|
|
954
|
+
});
|
|
955
|
+
Object.defineProperty(Error, "stackTraceLimit", {
|
|
956
|
+
value: Error.stackTraceLimit,
|
|
957
|
+
writable: false,
|
|
958
|
+
configurable: true
|
|
959
|
+
});
|
|
960
|
+
} catch {
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Block Proxy.revocable to prevent bypassing Proxy constructor blocking.
|
|
965
|
+
*
|
|
966
|
+
* Proxy.revocable internally uses the real Proxy constructor, so it bypasses
|
|
967
|
+
* our blocking proxy on globalThis.Proxy. We replace it with a wrapper that
|
|
968
|
+
* always blocks in worker context.
|
|
969
|
+
*/
|
|
970
|
+
protectProxyRevocable() {
|
|
971
|
+
const self = this;
|
|
972
|
+
const auditMode = this.config.auditMode;
|
|
973
|
+
try {
|
|
974
|
+
const originalRevocable = this.originalProxy.revocable;
|
|
975
|
+
if (typeof originalRevocable !== "function") return;
|
|
976
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
977
|
+
this.originalProxy,
|
|
978
|
+
"revocable"
|
|
979
|
+
);
|
|
980
|
+
this.originalDescriptors.push({
|
|
981
|
+
target: this.originalProxy,
|
|
982
|
+
prop: "revocable",
|
|
983
|
+
descriptor
|
|
984
|
+
});
|
|
985
|
+
Object.defineProperty(this.originalProxy, "revocable", {
|
|
986
|
+
value: function revocable(_target, _handler) {
|
|
987
|
+
const message = "Proxy.revocable is blocked in worker context";
|
|
988
|
+
const violation = self.recordViolation(
|
|
989
|
+
"proxy",
|
|
990
|
+
"Proxy.revocable",
|
|
991
|
+
message
|
|
992
|
+
);
|
|
993
|
+
if (!auditMode) {
|
|
994
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
995
|
+
}
|
|
996
|
+
return originalRevocable(_target, _handler);
|
|
997
|
+
},
|
|
998
|
+
writable: false,
|
|
999
|
+
configurable: true
|
|
1000
|
+
// Must be configurable for restoration
|
|
1001
|
+
});
|
|
1002
|
+
} catch {
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Protect against .constructor.constructor escape vector.
|
|
1007
|
+
* @param excludeTypes - Set of violation types to skip
|
|
1008
|
+
*/
|
|
1009
|
+
protectConstructorChain(excludeTypes) {
|
|
1010
|
+
let AsyncFunction = null;
|
|
1011
|
+
let GeneratorFunction = null;
|
|
1012
|
+
let AsyncGeneratorFunction = null;
|
|
1013
|
+
try {
|
|
1014
|
+
AsyncFunction = Object.getPrototypeOf(async () => {
|
|
1015
|
+
}).constructor;
|
|
1016
|
+
} catch {
|
|
1017
|
+
}
|
|
1018
|
+
try {
|
|
1019
|
+
GeneratorFunction = Object.getPrototypeOf(function* () {
|
|
1020
|
+
}).constructor;
|
|
1021
|
+
} catch {
|
|
1022
|
+
}
|
|
1023
|
+
try {
|
|
1024
|
+
AsyncGeneratorFunction = Object.getPrototypeOf(
|
|
1025
|
+
async function* () {
|
|
1026
|
+
}
|
|
1027
|
+
).constructor;
|
|
1028
|
+
} catch {
|
|
1029
|
+
}
|
|
1030
|
+
this.patchPrototypeConstructor(
|
|
1031
|
+
Function.prototype,
|
|
1032
|
+
"Function.prototype.constructor",
|
|
1033
|
+
"function_constructor"
|
|
1034
|
+
);
|
|
1035
|
+
if (!excludeTypes.has("async_function_constructor") && AsyncFunction && AsyncFunction !== Function) {
|
|
1036
|
+
this.patchPrototypeConstructor(
|
|
1037
|
+
AsyncFunction.prototype,
|
|
1038
|
+
"AsyncFunction.prototype.constructor",
|
|
1039
|
+
"async_function_constructor"
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
if (!excludeTypes.has("generator_function_constructor") && GeneratorFunction && GeneratorFunction !== Function) {
|
|
1043
|
+
this.patchPrototypeConstructor(
|
|
1044
|
+
GeneratorFunction.prototype,
|
|
1045
|
+
"GeneratorFunction.prototype.constructor",
|
|
1046
|
+
"generator_function_constructor"
|
|
1047
|
+
);
|
|
1048
|
+
}
|
|
1049
|
+
if (!excludeTypes.has("async_generator_function_constructor") && AsyncGeneratorFunction && AsyncGeneratorFunction !== Function && AsyncGeneratorFunction !== AsyncFunction) {
|
|
1050
|
+
this.patchPrototypeConstructor(
|
|
1051
|
+
AsyncGeneratorFunction.prototype,
|
|
1052
|
+
"AsyncGeneratorFunction.prototype.constructor",
|
|
1053
|
+
"async_generator_function_constructor"
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Protect Error.prepareStackTrace from being set.
|
|
1059
|
+
*/
|
|
1060
|
+
protectErrorPrepareStackTrace() {
|
|
1061
|
+
const self = this;
|
|
1062
|
+
const auditMode = this.config.auditMode;
|
|
1063
|
+
try {
|
|
1064
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
1065
|
+
Error,
|
|
1066
|
+
"prepareStackTrace"
|
|
1067
|
+
);
|
|
1068
|
+
this.originalDescriptors.push({
|
|
1069
|
+
target: Error,
|
|
1070
|
+
prop: "prepareStackTrace",
|
|
1071
|
+
descriptor: originalDescriptor
|
|
1072
|
+
});
|
|
1073
|
+
let currentValue = originalDescriptor?.value;
|
|
1074
|
+
Object.defineProperty(Error, "prepareStackTrace", {
|
|
1075
|
+
get() {
|
|
1076
|
+
return currentValue;
|
|
1077
|
+
},
|
|
1078
|
+
set(value) {
|
|
1079
|
+
const message = "Error.prepareStackTrace modification is blocked in worker context";
|
|
1080
|
+
const violation = self.recordViolation(
|
|
1081
|
+
"error_prepare_stack_trace",
|
|
1082
|
+
"Error.prepareStackTrace",
|
|
1083
|
+
message
|
|
1084
|
+
);
|
|
1085
|
+
if (!auditMode) {
|
|
1086
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1087
|
+
}
|
|
1088
|
+
currentValue = value;
|
|
1089
|
+
},
|
|
1090
|
+
configurable: true
|
|
1091
|
+
});
|
|
1092
|
+
} catch {
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Patch a prototype's constructor property.
|
|
1097
|
+
*
|
|
1098
|
+
* Returns a proxy that allows reading properties (like .name) but blocks
|
|
1099
|
+
* calling the constructor as a function (which would allow code execution).
|
|
1100
|
+
*/
|
|
1101
|
+
patchPrototypeConstructor(prototype, path, violationType) {
|
|
1102
|
+
const self = this;
|
|
1103
|
+
const auditMode = this.config.auditMode;
|
|
1104
|
+
try {
|
|
1105
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
1106
|
+
prototype,
|
|
1107
|
+
"constructor"
|
|
1108
|
+
);
|
|
1109
|
+
this.originalDescriptors.push({
|
|
1110
|
+
target: prototype,
|
|
1111
|
+
prop: "constructor",
|
|
1112
|
+
descriptor: originalDescriptor
|
|
1113
|
+
});
|
|
1114
|
+
const originalValue = originalDescriptor?.value;
|
|
1115
|
+
const constructorProxy = originalValue && typeof originalValue === "function" ? new this.originalProxy(originalValue, {
|
|
1116
|
+
apply(_target, _thisArg, _args) {
|
|
1117
|
+
const message = `${path} invocation is blocked in worker context`;
|
|
1118
|
+
const violation = self.recordViolation(
|
|
1119
|
+
violationType,
|
|
1120
|
+
path,
|
|
1121
|
+
message
|
|
1122
|
+
);
|
|
1123
|
+
if (!auditMode) {
|
|
1124
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1125
|
+
}
|
|
1126
|
+
return void 0;
|
|
1127
|
+
},
|
|
1128
|
+
construct(_target, _args, _newTarget) {
|
|
1129
|
+
const message = `${path} construction is blocked in worker context`;
|
|
1130
|
+
const violation = self.recordViolation(
|
|
1131
|
+
violationType,
|
|
1132
|
+
path,
|
|
1133
|
+
message
|
|
1134
|
+
);
|
|
1135
|
+
if (!auditMode) {
|
|
1136
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1137
|
+
}
|
|
1138
|
+
return {};
|
|
1139
|
+
},
|
|
1140
|
+
// Allow all property access (like .name, .prototype, etc.)
|
|
1141
|
+
get(target, prop, receiver) {
|
|
1142
|
+
return Reflect.get(target, prop, receiver);
|
|
1143
|
+
},
|
|
1144
|
+
getPrototypeOf(target) {
|
|
1145
|
+
return Reflect.getPrototypeOf(target);
|
|
1146
|
+
},
|
|
1147
|
+
has(target, prop) {
|
|
1148
|
+
return Reflect.has(target, prop);
|
|
1149
|
+
},
|
|
1150
|
+
ownKeys(target) {
|
|
1151
|
+
return Reflect.ownKeys(target);
|
|
1152
|
+
},
|
|
1153
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
1154
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
1155
|
+
}
|
|
1156
|
+
}) : originalValue;
|
|
1157
|
+
Object.defineProperty(prototype, "constructor", {
|
|
1158
|
+
get() {
|
|
1159
|
+
return constructorProxy;
|
|
1160
|
+
},
|
|
1161
|
+
set(value) {
|
|
1162
|
+
const message = `${path} modification is blocked in worker context`;
|
|
1163
|
+
const violation = self.recordViolation(violationType, path, message);
|
|
1164
|
+
if (!auditMode) {
|
|
1165
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1166
|
+
}
|
|
1167
|
+
Object.defineProperty(this, "constructor", {
|
|
1168
|
+
value,
|
|
1169
|
+
writable: true,
|
|
1170
|
+
configurable: true
|
|
1171
|
+
});
|
|
1172
|
+
},
|
|
1173
|
+
configurable: true
|
|
1174
|
+
});
|
|
1175
|
+
} catch {
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Protect process.mainModule from being accessed or set.
|
|
1180
|
+
*
|
|
1181
|
+
* The attack vector is:
|
|
1182
|
+
* ```
|
|
1183
|
+
* process.mainModule.require('child_process').execSync('whoami')
|
|
1184
|
+
* process.mainModule.constructor._load('vm')
|
|
1185
|
+
* ```
|
|
1186
|
+
*
|
|
1187
|
+
* process.mainModule may be undefined in ESM contexts but could exist in
|
|
1188
|
+
* CommonJS workers. We block both reading and setting.
|
|
1189
|
+
*/
|
|
1190
|
+
protectProcessMainModule() {
|
|
1191
|
+
if (typeof process === "undefined") return;
|
|
1192
|
+
const self = this;
|
|
1193
|
+
const auditMode = this.config.auditMode;
|
|
1194
|
+
try {
|
|
1195
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
1196
|
+
process,
|
|
1197
|
+
"mainModule"
|
|
1198
|
+
);
|
|
1199
|
+
this.originalDescriptors.push({
|
|
1200
|
+
target: process,
|
|
1201
|
+
prop: "mainModule",
|
|
1202
|
+
descriptor: originalDescriptor
|
|
1203
|
+
});
|
|
1204
|
+
const currentValue = originalDescriptor?.value;
|
|
1205
|
+
if (currentValue !== void 0) {
|
|
1206
|
+
Object.defineProperty(process, "mainModule", {
|
|
1207
|
+
get() {
|
|
1208
|
+
const message = "process.mainModule access is blocked in worker context";
|
|
1209
|
+
const violation = self.recordViolation(
|
|
1210
|
+
"process_main_module",
|
|
1211
|
+
"process.mainModule",
|
|
1212
|
+
message
|
|
1213
|
+
);
|
|
1214
|
+
if (!auditMode) {
|
|
1215
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1216
|
+
}
|
|
1217
|
+
return currentValue;
|
|
1218
|
+
},
|
|
1219
|
+
set(value) {
|
|
1220
|
+
const message = "process.mainModule modification is blocked in worker context";
|
|
1221
|
+
const violation = self.recordViolation(
|
|
1222
|
+
"process_main_module",
|
|
1223
|
+
"process.mainModule",
|
|
1224
|
+
message
|
|
1225
|
+
);
|
|
1226
|
+
if (!auditMode) {
|
|
1227
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1228
|
+
}
|
|
1229
|
+
Object.defineProperty(process, "mainModule", {
|
|
1230
|
+
value,
|
|
1231
|
+
writable: true,
|
|
1232
|
+
configurable: true
|
|
1233
|
+
});
|
|
1234
|
+
},
|
|
1235
|
+
configurable: true
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
} catch {
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Protect process.execPath from being read or set in worker context.
|
|
1243
|
+
*
|
|
1244
|
+
* process.execPath is a string primitive (not an object), so it cannot be
|
|
1245
|
+
* proxied via the normal blocked globals mechanism. We use Object.defineProperty
|
|
1246
|
+
* with getter/setter (same pattern as protectProcessMainModule).
|
|
1247
|
+
*/
|
|
1248
|
+
protectProcessExecPath() {
|
|
1249
|
+
if (typeof process === "undefined") return;
|
|
1250
|
+
const self = this;
|
|
1251
|
+
const auditMode = this.config.auditMode;
|
|
1252
|
+
try {
|
|
1253
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
1254
|
+
process,
|
|
1255
|
+
"execPath"
|
|
1256
|
+
);
|
|
1257
|
+
this.originalDescriptors.push({
|
|
1258
|
+
target: process,
|
|
1259
|
+
prop: "execPath",
|
|
1260
|
+
descriptor: originalDescriptor
|
|
1261
|
+
});
|
|
1262
|
+
const currentValue = originalDescriptor?.value ?? process.execPath;
|
|
1263
|
+
Object.defineProperty(process, "execPath", {
|
|
1264
|
+
get() {
|
|
1265
|
+
const message = "process.execPath access is blocked in worker context";
|
|
1266
|
+
const violation = self.recordViolation(
|
|
1267
|
+
"process_exec_path",
|
|
1268
|
+
"process.execPath",
|
|
1269
|
+
message
|
|
1270
|
+
);
|
|
1271
|
+
if (!auditMode) {
|
|
1272
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1273
|
+
}
|
|
1274
|
+
return currentValue;
|
|
1275
|
+
},
|
|
1276
|
+
set(value) {
|
|
1277
|
+
const message = "process.execPath modification is blocked in worker context";
|
|
1278
|
+
const violation = self.recordViolation(
|
|
1279
|
+
"process_exec_path",
|
|
1280
|
+
"process.execPath",
|
|
1281
|
+
message
|
|
1282
|
+
);
|
|
1283
|
+
if (!auditMode) {
|
|
1284
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1285
|
+
}
|
|
1286
|
+
Object.defineProperty(process, "execPath", {
|
|
1287
|
+
value,
|
|
1288
|
+
writable: true,
|
|
1289
|
+
configurable: true
|
|
1290
|
+
});
|
|
1291
|
+
},
|
|
1292
|
+
configurable: true
|
|
1293
|
+
});
|
|
1294
|
+
} catch {
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Protect process.connected from being read or set in worker context.
|
|
1299
|
+
*
|
|
1300
|
+
* process.connected is a boolean primitive (not an object), so it cannot be
|
|
1301
|
+
* proxied via the normal blocked globals mechanism. We use Object.defineProperty
|
|
1302
|
+
* with getter/setter (same pattern as protectProcessExecPath).
|
|
1303
|
+
*
|
|
1304
|
+
* Only protects if process.connected exists (IPC contexts).
|
|
1305
|
+
*/
|
|
1306
|
+
protectProcessConnected() {
|
|
1307
|
+
if (typeof process === "undefined") return;
|
|
1308
|
+
if (process.connected === void 0) return;
|
|
1309
|
+
const self = this;
|
|
1310
|
+
const auditMode = this.config.auditMode;
|
|
1311
|
+
try {
|
|
1312
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
1313
|
+
process,
|
|
1314
|
+
"connected"
|
|
1315
|
+
);
|
|
1316
|
+
this.originalDescriptors.push({
|
|
1317
|
+
target: process,
|
|
1318
|
+
prop: "connected",
|
|
1319
|
+
descriptor: originalDescriptor
|
|
1320
|
+
});
|
|
1321
|
+
const currentValue = originalDescriptor?.value ?? process.connected;
|
|
1322
|
+
Object.defineProperty(process, "connected", {
|
|
1323
|
+
get() {
|
|
1324
|
+
const message = "process.connected access is blocked in worker context";
|
|
1325
|
+
const violation = self.recordViolation(
|
|
1326
|
+
"process_connected",
|
|
1327
|
+
"process.connected",
|
|
1328
|
+
message
|
|
1329
|
+
);
|
|
1330
|
+
if (!auditMode) {
|
|
1331
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1332
|
+
}
|
|
1333
|
+
return currentValue;
|
|
1334
|
+
},
|
|
1335
|
+
set(value) {
|
|
1336
|
+
const message = "process.connected modification is blocked in worker context";
|
|
1337
|
+
const violation = self.recordViolation(
|
|
1338
|
+
"process_connected",
|
|
1339
|
+
"process.connected",
|
|
1340
|
+
message
|
|
1341
|
+
);
|
|
1342
|
+
if (!auditMode) {
|
|
1343
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1344
|
+
}
|
|
1345
|
+
Object.defineProperty(process, "connected", {
|
|
1346
|
+
value,
|
|
1347
|
+
writable: true,
|
|
1348
|
+
configurable: true
|
|
1349
|
+
});
|
|
1350
|
+
},
|
|
1351
|
+
configurable: true
|
|
1352
|
+
});
|
|
1353
|
+
} catch {
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* Protect Module._load from being called.
|
|
1358
|
+
*
|
|
1359
|
+
* The attack vector is:
|
|
1360
|
+
* ```
|
|
1361
|
+
* module.constructor._load('child_process')
|
|
1362
|
+
* require.main.constructor._load('vm')
|
|
1363
|
+
* ```
|
|
1364
|
+
*
|
|
1365
|
+
* We access the Module class and replace _load with a blocking proxy.
|
|
1366
|
+
*/
|
|
1367
|
+
protectModuleLoad() {
|
|
1368
|
+
const self = this;
|
|
1369
|
+
const auditMode = this.config.auditMode;
|
|
1370
|
+
try {
|
|
1371
|
+
let ModuleClass = null;
|
|
1372
|
+
if (typeof process !== "undefined") {
|
|
1373
|
+
const mainModule = process.mainModule;
|
|
1374
|
+
if (mainModule && typeof mainModule === "object") {
|
|
1375
|
+
ModuleClass = mainModule.constructor;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
if (!ModuleClass && typeof __require !== "undefined" && typeof __require.main !== "undefined") {
|
|
1379
|
+
ModuleClass = __require.main.constructor;
|
|
1380
|
+
}
|
|
1381
|
+
if (!ModuleClass || typeof ModuleClass._load !== "function") {
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1384
|
+
const original = ModuleClass._load;
|
|
1385
|
+
const descriptor = Object.getOwnPropertyDescriptor(ModuleClass, "_load");
|
|
1386
|
+
this.originalDescriptors.push({
|
|
1387
|
+
target: ModuleClass,
|
|
1388
|
+
prop: "_load",
|
|
1389
|
+
descriptor
|
|
1390
|
+
});
|
|
1391
|
+
const path = "Module._load";
|
|
1392
|
+
const proxy = new this.originalProxy(original, {
|
|
1393
|
+
apply(_target, _thisArg, _args) {
|
|
1394
|
+
const message = `${path} is blocked in worker context`;
|
|
1395
|
+
const violation = self.recordViolation("module_load", path, message);
|
|
1396
|
+
if (!auditMode) {
|
|
1397
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1398
|
+
}
|
|
1399
|
+
return Reflect.apply(_target, _thisArg, _args);
|
|
1400
|
+
}
|
|
1401
|
+
});
|
|
1402
|
+
Object.defineProperty(ModuleClass, "_load", {
|
|
1403
|
+
value: proxy,
|
|
1404
|
+
writable: true,
|
|
1405
|
+
configurable: true
|
|
1406
|
+
});
|
|
1407
|
+
} catch {
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1411
|
+
* Protect Module._resolveFilename from being called in worker context.
|
|
1412
|
+
*
|
|
1413
|
+
* Module._resolveFilename is called for both require() and import() resolution.
|
|
1414
|
+
* Blocking it catches file-based import() specifiers.
|
|
1415
|
+
*
|
|
1416
|
+
* data: and blob: URLs are handled by ESM loader hooks registered
|
|
1417
|
+
* in the main thread (DefenseInDepthBox.protectDynamicImport).
|
|
1418
|
+
*/
|
|
1419
|
+
protectModuleResolveFilename() {
|
|
1420
|
+
const self = this;
|
|
1421
|
+
const auditMode = this.config.auditMode;
|
|
1422
|
+
try {
|
|
1423
|
+
let ModuleClass = null;
|
|
1424
|
+
if (typeof process !== "undefined") {
|
|
1425
|
+
const mainModule = process.mainModule;
|
|
1426
|
+
if (mainModule && typeof mainModule === "object") {
|
|
1427
|
+
ModuleClass = mainModule.constructor;
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
if (!ModuleClass && typeof __require !== "undefined" && typeof __require.main !== "undefined") {
|
|
1431
|
+
ModuleClass = __require.main.constructor;
|
|
1432
|
+
}
|
|
1433
|
+
if (!ModuleClass || typeof ModuleClass._resolveFilename !== "function") {
|
|
1434
|
+
return;
|
|
1435
|
+
}
|
|
1436
|
+
const original = ModuleClass._resolveFilename;
|
|
1437
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
1438
|
+
ModuleClass,
|
|
1439
|
+
"_resolveFilename"
|
|
1440
|
+
);
|
|
1441
|
+
this.originalDescriptors.push({
|
|
1442
|
+
target: ModuleClass,
|
|
1443
|
+
prop: "_resolveFilename",
|
|
1444
|
+
descriptor
|
|
1445
|
+
});
|
|
1446
|
+
const path = "Module._resolveFilename";
|
|
1447
|
+
const proxy = new this.originalProxy(original, {
|
|
1448
|
+
apply(_target, _thisArg, _args) {
|
|
1449
|
+
const message = `${path} is blocked in worker context`;
|
|
1450
|
+
const violation = self.recordViolation(
|
|
1451
|
+
"module_resolve_filename",
|
|
1452
|
+
path,
|
|
1453
|
+
message
|
|
1454
|
+
);
|
|
1455
|
+
if (!auditMode) {
|
|
1456
|
+
throw new WorkerSecurityViolationError(message, violation);
|
|
1457
|
+
}
|
|
1458
|
+
return Reflect.apply(_target, _thisArg, _args);
|
|
1459
|
+
}
|
|
1460
|
+
});
|
|
1461
|
+
Object.defineProperty(ModuleClass, "_resolveFilename", {
|
|
1462
|
+
value: proxy,
|
|
1463
|
+
writable: true,
|
|
1464
|
+
configurable: true
|
|
1465
|
+
});
|
|
1466
|
+
} catch {
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
* Apply a single patch to a blocked global.
|
|
1471
|
+
*/
|
|
1472
|
+
applyPatch(blocked) {
|
|
1473
|
+
const { target, prop, violationType, strategy } = blocked;
|
|
1474
|
+
try {
|
|
1475
|
+
const original = target[prop];
|
|
1476
|
+
if (original === void 0) {
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
1480
|
+
this.originalDescriptors.push({ target, prop, descriptor });
|
|
1481
|
+
if (strategy === "freeze") {
|
|
1482
|
+
if (typeof original === "object" && original !== null) {
|
|
1483
|
+
Object.freeze(original);
|
|
1484
|
+
}
|
|
1485
|
+
} else {
|
|
1486
|
+
const path = this.getPathForTarget(target, prop);
|
|
1487
|
+
const proxy = typeof original === "function" ? this.createBlockingProxy(
|
|
1488
|
+
original,
|
|
1489
|
+
path,
|
|
1490
|
+
violationType
|
|
1491
|
+
) : this.createBlockingObjectProxy(
|
|
1492
|
+
original,
|
|
1493
|
+
path,
|
|
1494
|
+
violationType,
|
|
1495
|
+
blocked.allowedKeys
|
|
1496
|
+
);
|
|
1497
|
+
Object.defineProperty(target, prop, {
|
|
1498
|
+
value: proxy,
|
|
1499
|
+
writable: true,
|
|
1500
|
+
configurable: true
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
} catch {
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
/**
|
|
1507
|
+
* Restore all original values.
|
|
1508
|
+
*/
|
|
1509
|
+
restorePatches() {
|
|
1510
|
+
for (let i = this.originalDescriptors.length - 1; i >= 0; i--) {
|
|
1511
|
+
const { target, prop, descriptor } = this.originalDescriptors[i];
|
|
1512
|
+
try {
|
|
1513
|
+
if (descriptor) {
|
|
1514
|
+
Object.defineProperty(target, prop, descriptor);
|
|
1515
|
+
} else {
|
|
1516
|
+
delete target[prop];
|
|
1517
|
+
}
|
|
1518
|
+
} catch {
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
this.originalDescriptors = [];
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// src/security/wasm-callback.ts
|
|
1526
|
+
function sanitizeUnknownError(error) {
|
|
1527
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1528
|
+
return sanitizeErrorMessage(message);
|
|
1529
|
+
}
|
|
1530
|
+
function wrapWasmCallback(component, phase, callback) {
|
|
1531
|
+
return (...args) => {
|
|
1532
|
+
try {
|
|
1533
|
+
return callback(...args);
|
|
1534
|
+
} catch (error) {
|
|
1535
|
+
const message = sanitizeUnknownError(error);
|
|
1536
|
+
throw new Error(`${component} ${phase} callback failed: ${message}`);
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
// src/commands/sqlite3/worker.ts
|
|
1542
|
+
var cachedSQL = null;
|
|
1543
|
+
var defense = null;
|
|
1544
|
+
function wrapWorkerMessage(protocolToken, message) {
|
|
1545
|
+
const wrapped = /* @__PURE__ */ Object.create(null);
|
|
1546
|
+
if (!message || typeof message !== "object") {
|
|
1547
|
+
wrapped.success = false;
|
|
1548
|
+
wrapped.error = "Worker attempted to post non-object message";
|
|
1549
|
+
wrapped.protocolToken = protocolToken;
|
|
1550
|
+
return wrapped;
|
|
1551
|
+
}
|
|
1552
|
+
for (const [key, value] of Object.entries(message))
|
|
1553
|
+
wrapped[key] = value;
|
|
1554
|
+
wrapped.protocolToken = protocolToken;
|
|
1555
|
+
return wrapped;
|
|
1556
|
+
}
|
|
1557
|
+
function postWorkerMessage(protocolToken, message) {
|
|
1558
|
+
try {
|
|
1559
|
+
parentPort?.postMessage(wrapWorkerMessage(protocolToken, message));
|
|
1560
|
+
} catch (error) {
|
|
1561
|
+
console.debug(
|
|
1562
|
+
"[sqlite3-worker] failed to post worker message:",
|
|
1563
|
+
sanitizeUnknownError(error)
|
|
1564
|
+
);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
async function initializeWithDefense(protocolToken) {
|
|
1568
|
+
if (cachedSQL) {
|
|
1569
|
+
return cachedSQL;
|
|
1570
|
+
}
|
|
1571
|
+
cachedSQL = await initSqlJs();
|
|
1572
|
+
const onViolation = wrapWasmCallback(
|
|
1573
|
+
"sqlite3-worker",
|
|
1574
|
+
"onViolation",
|
|
1575
|
+
(v) => {
|
|
1576
|
+
postWorkerMessage(protocolToken, {
|
|
1577
|
+
type: "security-violation",
|
|
1578
|
+
violation: v
|
|
1579
|
+
});
|
|
1580
|
+
}
|
|
1581
|
+
);
|
|
1582
|
+
defense = new WorkerDefenseInDepth({ onViolation });
|
|
1583
|
+
return cachedSQL;
|
|
1584
|
+
}
|
|
1585
|
+
function stripLeadingNoise(sql) {
|
|
1586
|
+
let s = sql;
|
|
1587
|
+
for (; ; ) {
|
|
1588
|
+
const before = s;
|
|
1589
|
+
s = s.replace(/^\s+/, "");
|
|
1590
|
+
if (s.startsWith("--")) {
|
|
1591
|
+
const nl = s.indexOf("\n");
|
|
1592
|
+
s = nl === -1 ? "" : s.slice(nl + 1);
|
|
1593
|
+
} else if (s.startsWith("/*")) {
|
|
1594
|
+
const end = s.indexOf("*/");
|
|
1595
|
+
s = end === -1 ? "" : s.slice(end + 2);
|
|
1596
|
+
}
|
|
1597
|
+
if (s === before) return s;
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
function isReadOnlyStatement(sql) {
|
|
1601
|
+
const s = stripLeadingNoise(sql).toUpperCase();
|
|
1602
|
+
if (s.startsWith("SELECT")) return true;
|
|
1603
|
+
if (s.startsWith("EXPLAIN")) return true;
|
|
1604
|
+
if (s.startsWith("VALUES")) return true;
|
|
1605
|
+
if (s.startsWith("PRAGMA")) {
|
|
1606
|
+
const rest = s.slice("PRAGMA".length);
|
|
1607
|
+
return !/[=(]/.test(rest);
|
|
1608
|
+
}
|
|
1609
|
+
return false;
|
|
1610
|
+
}
|
|
1611
|
+
function isWriteStatement(sql) {
|
|
1612
|
+
const trimmed = stripLeadingNoise(sql).toUpperCase();
|
|
1613
|
+
return trimmed.startsWith("INSERT") || trimmed.startsWith("UPDATE") || trimmed.startsWith("DELETE") || trimmed.startsWith("CREATE") || trimmed.startsWith("DROP") || trimmed.startsWith("ALTER") || trimmed.startsWith("REPLACE") || trimmed.startsWith("VACUUM");
|
|
1614
|
+
}
|
|
1615
|
+
function splitStatements(sql) {
|
|
1616
|
+
const statements = [];
|
|
1617
|
+
let current = "";
|
|
1618
|
+
let inString = false;
|
|
1619
|
+
let stringChar = "";
|
|
1620
|
+
for (let i = 0; i < sql.length; i++) {
|
|
1621
|
+
const char = sql[i];
|
|
1622
|
+
if (inString) {
|
|
1623
|
+
current += char;
|
|
1624
|
+
if (char === stringChar) {
|
|
1625
|
+
if (sql[i + 1] === stringChar) {
|
|
1626
|
+
current += sql[++i];
|
|
1627
|
+
} else {
|
|
1628
|
+
inString = false;
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
} else if (char === "'" || char === '"') {
|
|
1632
|
+
current += char;
|
|
1633
|
+
inString = true;
|
|
1634
|
+
stringChar = char;
|
|
1635
|
+
} else if (char === ";") {
|
|
1636
|
+
const stmt2 = current.trim();
|
|
1637
|
+
if (stmt2) statements.push(stmt2);
|
|
1638
|
+
current = "";
|
|
1639
|
+
} else {
|
|
1640
|
+
current += char;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
const stmt = current.trim();
|
|
1644
|
+
if (stmt) statements.push(stmt);
|
|
1645
|
+
return statements;
|
|
1646
|
+
}
|
|
1647
|
+
async function executeQuery(data) {
|
|
1648
|
+
let db;
|
|
1649
|
+
try {
|
|
1650
|
+
const SQL = await initializeWithDefense(data.protocolToken);
|
|
1651
|
+
if (data.dbBuffer) {
|
|
1652
|
+
db = new SQL.Database(data.dbBuffer);
|
|
1653
|
+
} else {
|
|
1654
|
+
db = new SQL.Database();
|
|
1655
|
+
}
|
|
1656
|
+
} catch (e) {
|
|
1657
|
+
const message = sanitizeHostErrorMessage(e.message);
|
|
1658
|
+
return {
|
|
1659
|
+
success: false,
|
|
1660
|
+
error: message,
|
|
1661
|
+
defenseStats: defense?.getStats()
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
const results = [];
|
|
1665
|
+
let hasModifications = false;
|
|
1666
|
+
try {
|
|
1667
|
+
const statements = splitStatements(data.sql);
|
|
1668
|
+
for (const stmt of statements) {
|
|
1669
|
+
try {
|
|
1670
|
+
if (isWriteStatement(stmt)) {
|
|
1671
|
+
db.run(stmt);
|
|
1672
|
+
hasModifications = true;
|
|
1673
|
+
results.push({ type: "data", columns: [], rows: [] });
|
|
1674
|
+
} else {
|
|
1675
|
+
const prepared = db.prepare(stmt);
|
|
1676
|
+
const columns = prepared.getColumnNames();
|
|
1677
|
+
const rows = [];
|
|
1678
|
+
while (prepared.step()) {
|
|
1679
|
+
rows.push(prepared.get());
|
|
1680
|
+
}
|
|
1681
|
+
prepared.free();
|
|
1682
|
+
results.push({ type: "data", columns, rows });
|
|
1683
|
+
if (!isReadOnlyStatement(stmt)) {
|
|
1684
|
+
hasModifications = true;
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
} catch (e) {
|
|
1688
|
+
const error = e.message;
|
|
1689
|
+
results.push({ type: "error", error });
|
|
1690
|
+
if (data.options.bail) {
|
|
1691
|
+
break;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
let resultBuffer = null;
|
|
1696
|
+
if (hasModifications) {
|
|
1697
|
+
resultBuffer = db.export();
|
|
1698
|
+
}
|
|
1699
|
+
db.close();
|
|
1700
|
+
return {
|
|
1701
|
+
success: true,
|
|
1702
|
+
results,
|
|
1703
|
+
hasModifications,
|
|
1704
|
+
dbBuffer: resultBuffer,
|
|
1705
|
+
defenseStats: defense?.getStats()
|
|
1706
|
+
};
|
|
1707
|
+
} catch (e) {
|
|
1708
|
+
db.close();
|
|
1709
|
+
const message = sanitizeHostErrorMessage(e.message);
|
|
1710
|
+
return {
|
|
1711
|
+
success: false,
|
|
1712
|
+
error: message,
|
|
1713
|
+
defenseStats: defense?.getStats()
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
if (parentPort && workerData) {
|
|
1718
|
+
const input = workerData;
|
|
1719
|
+
executeQuery(input).then((result) => {
|
|
1720
|
+
postWorkerMessage(input.protocolToken, result);
|
|
1721
|
+
}).catch((error) => {
|
|
1722
|
+
postWorkerMessage(input.protocolToken, {
|
|
1723
|
+
success: false,
|
|
1724
|
+
error: sanitizeUnknownError(error),
|
|
1725
|
+
defenseStats: defense?.getStats()
|
|
1726
|
+
});
|
|
1727
|
+
});
|
|
1728
|
+
}
|