tslaws 0.9.0 → 1.0.0
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/README.md +8 -2
- package/bin/tslaws.js +8 -2
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +36 -7
- package/dist/bundled.js +2 -2
- package/dist/bundlerPlugins.d.ts +2 -0
- package/dist/bundlerPlugins.js +7 -0
- package/dist/certificates.d.ts +139 -0
- package/dist/certificates.js +433 -0
- package/dist/chc.d.ts +85 -0
- package/dist/chc.js +298 -0
- package/dist/check.d.ts +13 -1
- package/dist/check.js +215 -28
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +324 -41
- package/dist/compile.d.ts +17 -1
- package/dist/compile.js +244 -25
- package/dist/coverage.d.ts +55 -3
- package/dist/coverage.js +231 -12
- package/dist/e2eApi.js +3 -1
- package/dist/generators.js +12 -2
- package/dist/github.d.ts +49 -5
- package/dist/github.js +107 -24
- package/dist/githubCli.d.ts +28 -0
- package/dist/githubCli.js +1039 -211
- package/dist/hints.js +2 -1
- package/dist/init.js +42 -0
- package/dist/isolate.d.ts +28 -0
- package/dist/isolate.js +262 -0
- package/dist/isolatedCheck.d.ts +1 -0
- package/dist/isolatedCheck.js +16 -0
- package/dist/lawHost.d.ts +50 -0
- package/dist/lawHost.js +199 -0
- package/dist/lawRuntime.d.ts +39 -0
- package/dist/lawRuntime.js +242 -0
- package/dist/lock.d.ts +49 -9
- package/dist/lock.js +132 -21
- package/dist/mcp.js +28 -7
- package/dist/mutate.d.ts +4 -0
- package/dist/mutate.js +7 -1
- package/dist/plan.d.ts +1 -0
- package/dist/plan.js +4 -0
- package/dist/pluginHooks.d.ts +16 -0
- package/dist/pluginHooks.js +48 -0
- package/dist/pluginHost.d.ts +5 -0
- package/dist/pluginHost.js +43 -0
- package/dist/pluginWorker.d.ts +1 -0
- package/dist/pluginWorker.js +40 -0
- package/dist/plural.d.ts +2 -0
- package/dist/plural.js +4 -0
- package/dist/pool.d.ts +80 -7
- package/dist/pool.js +222 -11
- package/dist/projects.d.ts +70 -0
- package/dist/projects.js +457 -0
- package/dist/promote.d.ts +56 -2
- package/dist/promote.js +429 -13
- package/dist/property.js +13 -2
- package/dist/propertyWorker.js +11 -1
- package/dist/proposals.d.ts +52 -4
- package/dist/proposals.js +484 -21
- package/dist/reporters.js +5 -1
- package/dist/runtimeEnv.d.ts +32 -0
- package/dist/runtimeEnv.js +146 -0
- package/dist/sandbox.d.ts +98 -0
- package/dist/sandbox.js +673 -0
- package/dist/sandboxGuard.d.ts +56 -0
- package/dist/sandboxGuard.js +249 -0
- package/dist/seal.d.ts +9 -0
- package/dist/seal.js +426 -0
- package/dist/security/cli.d.ts +6 -0
- package/dist/security/cli.js +39 -0
- package/dist/security/cwe.d.ts +14 -0
- package/dist/security/cwe.js +40 -0
- package/dist/security/deps/cache.d.ts +28 -0
- package/dist/security/deps/cache.js +107 -0
- package/dist/security/deps/config.d.ts +16 -0
- package/dist/security/deps/config.js +43 -0
- package/dist/security/deps/imports.d.ts +4 -0
- package/dist/security/deps/imports.js +33 -0
- package/dist/security/deps/lockfiles.d.ts +23 -0
- package/dist/security/deps/lockfiles.js +153 -0
- package/dist/security/deps/osv.d.ts +66 -0
- package/dist/security/deps/osv.js +137 -0
- package/dist/security/deps/run.d.ts +54 -0
- package/dist/security/deps/run.js +135 -0
- package/dist/security/deps/severity.d.ts +26 -0
- package/dist/security/deps/severity.js +78 -0
- package/dist/security/generators.d.ts +9 -0
- package/dist/security/generators.js +36 -0
- package/dist/security/index.d.ts +27 -0
- package/dist/security/index.js +18 -0
- package/dist/security/integers.d.ts +31 -0
- package/dist/security/integers.js +105 -0
- package/dist/security/law.d.ts +54 -0
- package/dist/security/law.js +54 -0
- package/dist/security/pathTraversal.d.ts +29 -0
- package/dist/security/pathTraversal.js +146 -0
- package/dist/security/prototypePollution.d.ts +30 -0
- package/dist/security/prototypePollution.js +142 -0
- package/dist/security/redirect.d.ts +21 -0
- package/dist/security/redirect.js +154 -0
- package/dist/security/redos.d.ts +21 -0
- package/dist/security/redos.js +93 -0
- package/dist/security/validation.d.ts +25 -0
- package/dist/security/validation.js +89 -0
- package/dist/security/xss.d.ts +21 -0
- package/dist/security/xss.js +90 -0
- package/dist/settings.d.ts +20 -0
- package/dist/settings.js +32 -0
- package/dist/smt.d.ts +29 -1
- package/dist/smt.js +239 -107
- package/dist/smtWorker.d.ts +1 -0
- package/dist/smtWorker.js +26 -0
- package/dist/solvers.d.ts +9 -0
- package/dist/solvers.js +11 -5
- package/dist/symbolic.d.ts +54 -1
- package/dist/symbolic.js +239 -13
- package/dist/tsconfigResolve.d.ts +28 -0
- package/dist/tsconfigResolve.js +141 -0
- package/dist/types.d.ts +28 -0
- package/env.d.ts +17 -0
- package/hooks/claude-hook.mjs +2 -0
- package/package.json +22 -6
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** What the parent decided is reviewed. Every path is absolute, with forward slashes. */
|
|
2
|
+
export interface GuardConfig {
|
|
3
|
+
/** Trees of code the build wrote from reviewed files (`.tslaws/out`). */
|
|
4
|
+
trees: string[];
|
|
5
|
+
/** Single files: the laws files, the files their seals cover and the project files they import. */
|
|
6
|
+
files: string[];
|
|
7
|
+
/** Trees of trusted code outside `node_modules`: the tslaws package and workspace packages the project declares. */
|
|
8
|
+
packages: string[];
|
|
9
|
+
/** The package directories under `node_modules` that the manifests of the project and of tslaws declare. */
|
|
10
|
+
modules: string[];
|
|
11
|
+
}
|
|
12
|
+
/** The Node modules the code of a law and of the project may load: no files, processes, sockets, code or threads. */
|
|
13
|
+
export declare const lawBuiltins: readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
* What a declared package may load as well: it reads its own files and may use the usual helpers. Never `module` (the
|
|
16
|
+
* loader's own internals; see `builtin` for tslaws's own files) or `process`, and none of what starts a process, a
|
|
17
|
+
* thread or a socket.
|
|
18
|
+
*/
|
|
19
|
+
export declare const packageBuiltins: readonly string[];
|
|
20
|
+
/** A path as the guard compares it: forward slashes, and no case on Windows. */
|
|
21
|
+
export declare function normal(path: string): string;
|
|
22
|
+
/** The package directory a file under `node_modules` belongs to (`.../node_modules/@scope/name`), if it is under one. */
|
|
23
|
+
export declare function packageDirOf(path: string): string | undefined;
|
|
24
|
+
/** The rule: what may be loaded, and by whom. */
|
|
25
|
+
export declare class Guard {
|
|
26
|
+
private trees;
|
|
27
|
+
private packages;
|
|
28
|
+
private modules;
|
|
29
|
+
private files;
|
|
30
|
+
private configured;
|
|
31
|
+
/** Sets the reviewed set. Once: a second call (a forged message) changes nothing and throws. */
|
|
32
|
+
configure(config: GuardConfig): void;
|
|
33
|
+
/** Whether `path` is code of a trusted package: tslaws, or a package the manifests declare. */
|
|
34
|
+
isPackageFile(path: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Throws unless the Node module `request` may be loaded. The callers are the files on the call stack; `parent` is what
|
|
37
|
+
* the loader says imports it (a `file:` URL or a path), consulted as well. The strictest wins: if any of them is a law or
|
|
38
|
+
* project file the request is a law's, whatever package code sits between (a law that has a trusted helper `require`
|
|
39
|
+
* `child_process` for it is refused). One thing is not a law's request: the loader linking the `import` statements of a
|
|
40
|
+
* package that a law loads (`linking`). The specifiers are text in the package, which the law cannot change, and no code
|
|
41
|
+
* of a package is running (a package function that calls `import()` is on the stack, and is judged like any other).
|
|
42
|
+
*/
|
|
43
|
+
builtin(request: string, parent?: string, linking?: boolean): void;
|
|
44
|
+
/** Throws unless the file `path` is in the reviewed set. */
|
|
45
|
+
file(path: string, request: string): void;
|
|
46
|
+
/** Checks one resolution: `request` as written, `resolved` as Node found it (a `file:` or `node:` URL, or a path). */
|
|
47
|
+
resolved(request: string, resolved: string, parent?: string, linking?: boolean): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Puts the guard in front of every way a law can load a module: the CommonJS loader (`require`, `createRequire`,
|
|
51
|
+
* `import()` compiled to `require`) and the synchronous hooks that cover `import` as well (Node 22.15, 23.5 or later, which
|
|
52
|
+
* the sandbox requires). Then it closes the doors to the loader itself, so that law code can neither take the guard out
|
|
53
|
+
* nor put a hook in front of it: the loader's functions and tables are made read-only, `registerHooks` and `register`
|
|
54
|
+
* throw. `process.getBuiltinModule` passes the same check.
|
|
55
|
+
*/
|
|
56
|
+
export declare function installGuard(guard: Guard): void;
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// The reviewed set, as a rule about module loading. A sandboxed law process (lawHost.ts) runs inside Node's permission
|
|
2
|
+
// model, which stops what the process can do to the machine; this decides what code it may load at all. Anything a
|
|
3
|
+
// resolve step names that is outside the set is refused before it is read: a file nobody reviewed (a proposal that was
|
|
4
|
+
// not passed, a helper the law reaches by a computed path), a `data:` or network URL, and a Node module a law has no
|
|
5
|
+
// use for (`child_process`, `net`, `vm`, `module`, ...). The permission model is the hard fence; this one makes a
|
|
6
|
+
// refusal a plain error with the reason, and keeps an unreviewed file from running even where it could be read.
|
|
7
|
+
import nodeModule from 'node:module';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
/** The Node modules the code of a law and of the project may load: no files, processes, sockets, code or threads. */
|
|
10
|
+
export const lawBuiltins = [
|
|
11
|
+
'assert',
|
|
12
|
+
'assert/strict',
|
|
13
|
+
'buffer',
|
|
14
|
+
'crypto',
|
|
15
|
+
'events',
|
|
16
|
+
'path',
|
|
17
|
+
'path/posix',
|
|
18
|
+
'path/win32',
|
|
19
|
+
'punycode',
|
|
20
|
+
'querystring',
|
|
21
|
+
'string_decoder',
|
|
22
|
+
'timers',
|
|
23
|
+
'timers/promises',
|
|
24
|
+
'url',
|
|
25
|
+
'util',
|
|
26
|
+
'util/types',
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* What a declared package may load as well: it reads its own files and may use the usual helpers. Never `module` (the
|
|
30
|
+
* loader's own internals; see `builtin` for tslaws's own files) or `process`, and none of what starts a process, a
|
|
31
|
+
* thread or a socket.
|
|
32
|
+
*/
|
|
33
|
+
export const packageBuiltins = [
|
|
34
|
+
...lawBuiltins,
|
|
35
|
+
'async_hooks',
|
|
36
|
+
'console',
|
|
37
|
+
'constants',
|
|
38
|
+
'diagnostics_channel',
|
|
39
|
+
'fs',
|
|
40
|
+
'fs/promises',
|
|
41
|
+
'os',
|
|
42
|
+
'perf_hooks',
|
|
43
|
+
'stream',
|
|
44
|
+
'stream/promises',
|
|
45
|
+
'tty',
|
|
46
|
+
'zlib',
|
|
47
|
+
];
|
|
48
|
+
const refusal = (what, why) => Object.assign(new Error(`tslaws sandbox: ${what} ${why}`), { code: 'ERR_TSLAWS_SANDBOX' });
|
|
49
|
+
/** A path as the guard compares it: forward slashes, and no case on Windows. */
|
|
50
|
+
export function normal(path) {
|
|
51
|
+
const slashed = path.replaceAll('\\', '/');
|
|
52
|
+
return process.platform === 'win32' ? slashed.toLowerCase() : slashed;
|
|
53
|
+
}
|
|
54
|
+
const within = (path, tree) => path === tree || path.startsWith(tree.endsWith('/') ? tree : `${tree}/`);
|
|
55
|
+
/** The package directory a file under `node_modules` belongs to (`.../node_modules/@scope/name`), if it is under one. */
|
|
56
|
+
export function packageDirOf(path) {
|
|
57
|
+
const marker = '/node_modules/';
|
|
58
|
+
const at = path.lastIndexOf(marker);
|
|
59
|
+
if (at < 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
const start = at + marker.length;
|
|
62
|
+
const parts = path.slice(start).split('/');
|
|
63
|
+
const name = parts.slice(0, parts[0]?.startsWith('@') ? 2 : 1).join('/');
|
|
64
|
+
return name === '' ? undefined : path.slice(0, start) + name;
|
|
65
|
+
}
|
|
66
|
+
// What the guard needs from V8 is taken once, before any law code runs, so a law that replaces `Error.captureStackTrace`
|
|
67
|
+
// or the `Error` global cannot change what the guard sees.
|
|
68
|
+
const ErrorClass = Error;
|
|
69
|
+
const captureStackTrace = Error.captureStackTrace.bind(Error);
|
|
70
|
+
const selfPath = normal(fileURLToPath(import.meta.url));
|
|
71
|
+
/** tslaws's own code (`dist` or `src`, not the rest of the package directory): it alone may read `module.builtinModules`. */
|
|
72
|
+
const selfRoot = selfPath.slice(0, selfPath.lastIndexOf('/'));
|
|
73
|
+
/**
|
|
74
|
+
* The files that are really asking: every frame of the call stack that is neither Node's own nor this file, closest
|
|
75
|
+
* first. A `parent` handed to `Module._load` is the caller's claim (`module.constructor._load('fs', { filename })` makes
|
|
76
|
+
* any), so the stack, which the caller cannot write, decides. All frames count, not the closest: a trusted package that a
|
|
77
|
+
* law calls must not vouch for what the law asked it to load.
|
|
78
|
+
*/
|
|
79
|
+
function callerFiles() {
|
|
80
|
+
const limit = ErrorClass.stackTraceLimit;
|
|
81
|
+
const prepare = ErrorClass.prepareStackTrace;
|
|
82
|
+
ErrorClass.stackTraceLimit = 100;
|
|
83
|
+
ErrorClass.prepareStackTrace = (_error, sites) => sites;
|
|
84
|
+
let sites = [];
|
|
85
|
+
try {
|
|
86
|
+
const holder = {};
|
|
87
|
+
captureStackTrace(holder, callerFiles);
|
|
88
|
+
sites = holder.stack ?? [];
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
ErrorClass.prepareStackTrace = prepare;
|
|
92
|
+
ErrorClass.stackTraceLimit = limit;
|
|
93
|
+
}
|
|
94
|
+
const files = [];
|
|
95
|
+
for (const site of sites) {
|
|
96
|
+
const name = site.getFileName();
|
|
97
|
+
if (typeof name !== 'string' || name === '' || name.startsWith('node:'))
|
|
98
|
+
continue;
|
|
99
|
+
const path = name.startsWith('file:') ? fileURLToPath(name) : name;
|
|
100
|
+
if (normal(path) === selfPath)
|
|
101
|
+
continue;
|
|
102
|
+
files.push(path);
|
|
103
|
+
}
|
|
104
|
+
return files;
|
|
105
|
+
}
|
|
106
|
+
/** The rule: what may be loaded, and by whom. */
|
|
107
|
+
export class Guard {
|
|
108
|
+
trees = [];
|
|
109
|
+
packages = [];
|
|
110
|
+
modules = new Set();
|
|
111
|
+
files = new Set();
|
|
112
|
+
configured = false;
|
|
113
|
+
/** Sets the reviewed set. Once: a second call (a forged message) changes nothing and throws. */
|
|
114
|
+
configure(config) {
|
|
115
|
+
if (this.configured)
|
|
116
|
+
throw refusal('the reviewed set', 'is already fixed');
|
|
117
|
+
this.configured = true;
|
|
118
|
+
this.trees = config.trees.map(normal);
|
|
119
|
+
this.packages = config.packages.map(normal);
|
|
120
|
+
this.modules = new Set(config.modules.map(normal));
|
|
121
|
+
this.files = new Set(config.files.map(normal));
|
|
122
|
+
}
|
|
123
|
+
/** Whether `path` is code of a trusted package: tslaws, or a package the manifests declare. */
|
|
124
|
+
isPackageFile(path) {
|
|
125
|
+
if (this.packages.some((tree) => within(path, tree)))
|
|
126
|
+
return true;
|
|
127
|
+
const dir = packageDirOf(path);
|
|
128
|
+
return dir !== undefined && this.modules.has(dir);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Throws unless the Node module `request` may be loaded. The callers are the files on the call stack; `parent` is what
|
|
132
|
+
* the loader says imports it (a `file:` URL or a path), consulted as well. The strictest wins: if any of them is a law or
|
|
133
|
+
* project file the request is a law's, whatever package code sits between (a law that has a trusted helper `require`
|
|
134
|
+
* `child_process` for it is refused). One thing is not a law's request: the loader linking the `import` statements of a
|
|
135
|
+
* package that a law loads (`linking`). The specifiers are text in the package, which the law cannot change, and no code
|
|
136
|
+
* of a package is running (a package function that calls `import()` is on the stack, and is judged like any other).
|
|
137
|
+
*/
|
|
138
|
+
builtin(request, parent, linking = false) {
|
|
139
|
+
const name = request.startsWith('node:') ? request.slice(5) : request;
|
|
140
|
+
const declared = parent === undefined ? undefined : parent.startsWith('file:') ? fileURLToPath(parent) : parent;
|
|
141
|
+
const frames = callerFiles();
|
|
142
|
+
const callers = declared === undefined ? frames : [...frames, declared];
|
|
143
|
+
const linked = linking &&
|
|
144
|
+
declared !== undefined &&
|
|
145
|
+
this.isPackageFile(normal(declared)) &&
|
|
146
|
+
!frames.some((file) => !within(normal(file), selfRoot) && this.isPackageFile(normal(file)));
|
|
147
|
+
const fromPackage = linked || (callers.length > 0 && callers.every((file) => this.isPackageFile(normal(file))));
|
|
148
|
+
const asking = frames.find((file) => !this.isPackageFile(normal(file))) ?? frames[0];
|
|
149
|
+
if ((fromPackage ? packageBuiltins : lawBuiltins).includes(name))
|
|
150
|
+
return;
|
|
151
|
+
// tslaws itself asks what Node's modules are called (`builtinModules`, to tell a package from a built-in). The loader's
|
|
152
|
+
// functions that could load anything are locked, so naming the module gives its caller nothing it could use.
|
|
153
|
+
const naming = linked ? [declared] : callers;
|
|
154
|
+
if (name === 'module' && naming.length > 0 && naming.every((file) => within(normal(file), selfRoot)))
|
|
155
|
+
return;
|
|
156
|
+
throw refusal(`"${request}"`, `is not a module a law may load${fromPackage ? '' : ' (allowed: ' + lawBuiltins.join(', ') + ')'}` +
|
|
157
|
+
(asking === undefined ? '' : `; requested by ${asking}`) +
|
|
158
|
+
(frames.length > 1 ? ` (call stack: ${frames.slice(0, 6).join(' <- ')})` : ''));
|
|
159
|
+
}
|
|
160
|
+
/** Throws unless the file `path` is in the reviewed set. */
|
|
161
|
+
file(path, request) {
|
|
162
|
+
const at = normal(path);
|
|
163
|
+
if (this.files.has(at) || this.trees.some((tree) => within(at, tree)) || this.isPackageFile(at))
|
|
164
|
+
return;
|
|
165
|
+
throw refusal(`"${request}" resolves to ${path}, which`, 'is outside the reviewed set of files (the laws, what their seals cover, declared packages)');
|
|
166
|
+
}
|
|
167
|
+
/** Checks one resolution: `request` as written, `resolved` as Node found it (a `file:` or `node:` URL, or a path). */
|
|
168
|
+
resolved(request, resolved, parent, linking = false) {
|
|
169
|
+
if (resolved.startsWith('node:') || nodeModule.isBuiltin(resolved))
|
|
170
|
+
return this.builtin(resolved, parent, linking);
|
|
171
|
+
if (resolved.startsWith('file:'))
|
|
172
|
+
return this.file(fileURLToPath(resolved), request);
|
|
173
|
+
if (/^[a-z][a-z0-9+.-]+:/i.test(resolved) && !/^[a-z]:[\\/]/i.test(resolved))
|
|
174
|
+
throw refusal(`"${request}"`, `resolves to ${resolved.slice(0, 40)}, which is not a file`);
|
|
175
|
+
return this.file(resolved, request);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const locked = (target, name, value) => {
|
|
179
|
+
Object.defineProperty(target, name, { value, writable: false, configurable: false });
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Puts the guard in front of every way a law can load a module: the CommonJS loader (`require`, `createRequire`,
|
|
183
|
+
* `import()` compiled to `require`) and the synchronous hooks that cover `import` as well (Node 22.15, 23.5 or later, which
|
|
184
|
+
* the sandbox requires). Then it closes the doors to the loader itself, so that law code can neither take the guard out
|
|
185
|
+
* nor put a hook in front of it: the loader's functions and tables are made read-only, `registerHooks` and `register`
|
|
186
|
+
* throw. `process.getBuiltinModule` passes the same check.
|
|
187
|
+
*/
|
|
188
|
+
export function installGuard(guard) {
|
|
189
|
+
const internals = nodeModule;
|
|
190
|
+
const load = internals._load;
|
|
191
|
+
const resolveFilename = internals._resolveFilename;
|
|
192
|
+
const register = internals.registerHooks;
|
|
193
|
+
if (typeof register !== 'function')
|
|
194
|
+
throw refusal('the loader', 'has no synchronous hooks, so it cannot be guarded');
|
|
195
|
+
// `require('node:fs')` is answered before `_resolveFilename` is asked, so the request is checked here as well. The
|
|
196
|
+
// parent is not consulted: it is whatever the caller passed.
|
|
197
|
+
const guardedLoad = function (request, parent, isMain) {
|
|
198
|
+
if (typeof request === 'string' && nodeModule.isBuiltin(request))
|
|
199
|
+
guard.builtin(request);
|
|
200
|
+
return load.call(this, request, parent, isMain);
|
|
201
|
+
};
|
|
202
|
+
const guardedResolve = function (request, parent, ...rest) {
|
|
203
|
+
const found = resolveFilename.call(this, request, parent, ...rest);
|
|
204
|
+
guard.resolved(request, found);
|
|
205
|
+
return found;
|
|
206
|
+
};
|
|
207
|
+
locked(nodeModule, '_load', guardedLoad);
|
|
208
|
+
locked(nodeModule, '_resolveFilename', guardedResolve);
|
|
209
|
+
register({
|
|
210
|
+
resolve(specifier, context, nextResolve) {
|
|
211
|
+
const result = nextResolve(specifier, context);
|
|
212
|
+
// Only the loader's own `import` resolution names the importing module itself; `require` passes what its caller says.
|
|
213
|
+
guard.resolved(specifier, result.url, context.parentURL, context.conditions?.includes('import') === true);
|
|
214
|
+
return result;
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
const getBuiltin = process.getBuiltinModule?.bind(process);
|
|
218
|
+
if (getBuiltin !== undefined)
|
|
219
|
+
locked(process, 'getBuiltinModule', (id) => {
|
|
220
|
+
guard.builtin(id);
|
|
221
|
+
return getBuiltin(id);
|
|
222
|
+
});
|
|
223
|
+
const refuse = (what) => () => {
|
|
224
|
+
throw refusal(what, 'is not available to a law');
|
|
225
|
+
};
|
|
226
|
+
for (const name of ['registerHooks', 'register'])
|
|
227
|
+
locked(nodeModule, name, refuse(`module.${name}`));
|
|
228
|
+
// The tables and entry points the loader works through: read-only, so no law swaps in its own.
|
|
229
|
+
Object.freeze(internals._extensions);
|
|
230
|
+
const loader = nodeModule;
|
|
231
|
+
for (const name of ['_extensions', '_cache', '_pathCache', '_findPath', 'createRequire'])
|
|
232
|
+
locked(nodeModule, name, loader[name]);
|
|
233
|
+
const proto = nodeModule.prototype;
|
|
234
|
+
for (const name of ['_compile', 'require', 'load'])
|
|
235
|
+
locked(nodeModule.prototype, name, proto[name]);
|
|
236
|
+
// Not closed, because Node fixes them: `Module.wrap` (its setter makes later loads use another wrapper) and the
|
|
237
|
+
// `constructor` of a module, which is how a law reaches `Module` without `require('module')`. What a law gains is the
|
|
238
|
+
// text around files not yet loaded; every request those make is still checked, by the real caller on the stack, and the
|
|
239
|
+
// permission model denies writes, processes, threads and sockets whatever any code asks for.
|
|
240
|
+
// What reads the stack must not be redirected: the properties stay writable data but cannot become accessors.
|
|
241
|
+
for (const name of ['prepareStackTrace', 'stackTraceLimit'])
|
|
242
|
+
Object.defineProperty(ErrorClass, name, {
|
|
243
|
+
value: ErrorClass[name],
|
|
244
|
+
writable: true,
|
|
245
|
+
enumerable: false,
|
|
246
|
+
configurable: false,
|
|
247
|
+
});
|
|
248
|
+
locked(ErrorClass, 'captureStackTrace', ErrorClass.captureStackTrace);
|
|
249
|
+
}
|
package/dist/seal.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ManifestLaw } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The files the seals of `laws` cover: each law's file and every module it reaches, absolute and real. This is the set a
|
|
4
|
+
* sandboxed law process may load besides the build's output (see sandboxGuard.ts), so the code that runs is the code the
|
|
5
|
+
* lock digests, no more.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sealedFiles(cwd: string, laws: ManifestLaw[]): string[];
|
|
8
|
+
/** The laws with their `seal` set. */
|
|
9
|
+
export declare function sealLaws(cwd: string, laws: ManifestLaw[]): ManifestLaw[];
|