@cedarjs/api 7.1.1-next.471 → 7.2.0-rc.33
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 +0 -12
- package/dist/__tests__/mergePrismaWithSdlTypes.test-d.d.ts +2 -0
- package/dist/__tests__/mergePrismaWithSdlTypes.test-d.d.ts.map +1 -0
- package/dist/cache/clients/MemcachedClient.d.ts +2 -7
- package/dist/cache/clients/MemcachedClient.d.ts.map +1 -1
- package/dist/cache/clients/MemcachedClient.js +0 -5
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -5
- package/dist/transforms.d.ts +4 -22
- package/dist/transforms.d.ts.map +1 -1
- package/dist/transforms.js +9 -11
- package/dist/types.d.ts +14 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -17
- package/dist/__tests__/removeNulls.test-d.d.ts +0 -2
- package/dist/__tests__/removeNulls.test-d.d.ts.map +0 -1
- package/dist/sandbox/concurrencyLimiter.d.ts +0 -22
- package/dist/sandbox/concurrencyLimiter.d.ts.map +0 -1
- package/dist/sandbox/concurrencyLimiter.js +0 -33
- package/dist/sandbox/findChildScript.d.ts +0 -29
- package/dist/sandbox/findChildScript.d.ts.map +0 -1
- package/dist/sandbox/findChildScript.js +0 -35
- package/dist/sandbox/index.d.ts +0 -11
- package/dist/sandbox/index.d.ts.map +0 -1
- package/dist/sandbox/index.js +0 -22
- package/dist/sandbox/noNetworkPreload.d.ts +0 -16
- package/dist/sandbox/noNetworkPreload.d.ts.map +0 -1
- package/dist/sandbox/noNetworkPreload.js +0 -109
- package/dist/sandbox/runSandboxedChild.d.ts +0 -186
- package/dist/sandbox/runSandboxedChild.d.ts.map +0 -1
- package/dist/sandbox/runSandboxedChild.js +0 -213
- package/dist/sandbox/sandboxPackages.d.ts +0 -16
- package/dist/sandbox/sandboxPackages.d.ts.map +0 -1
- package/dist/sandbox/sandboxPackages.js +0 -53
- package/dist/signedTokens/index.d.ts +0 -2
- package/dist/signedTokens/index.d.ts.map +0 -1
- package/dist/signedTokens/index.js +0 -1
- package/dist/signedTokens/signedTokens.d.ts +0 -99
- package/dist/signedTokens/signedTokens.d.ts.map +0 -1
- package/dist/signedTokens/signedTokens.js +0 -100
- package/dist/webhooks/__tests__/webhooks.test-d.d.ts +0 -2
- package/dist/webhooks/__tests__/webhooks.test-d.d.ts.map +0 -1
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
/** The heap cap for a sandboxed child, in MB, unless `heapMb` is given */
|
|
2
|
-
export declare const DEFAULT_SANDBOX_HEAP_MB = 256;
|
|
3
|
-
/** How long a sandboxed child may run, unless `timeoutMs` is given */
|
|
4
|
-
export declare const DEFAULT_SANDBOX_TIMEOUT_MS = 10000;
|
|
5
|
-
/** The most bytes sent to a sandboxed child, unless `maxInputBytes` is given */
|
|
6
|
-
export declare const DEFAULT_SANDBOX_MAX_INPUT_BYTES: number;
|
|
7
|
-
/** The most bytes read back from a sandboxed child, unless `maxOutputBytes` is given */
|
|
8
|
-
export declare const DEFAULT_SANDBOX_MAX_OUTPUT_BYTES: number;
|
|
9
|
-
export interface SandboxedChildArgsOptions {
|
|
10
|
-
/**
|
|
11
|
-
* The Node script the child runs: a `.js`/`.mjs`/`.cjs` file, or a `.ts`
|
|
12
|
-
* file Node can run by stripping its types. It may read itself.
|
|
13
|
-
*/
|
|
14
|
-
script: string;
|
|
15
|
-
/**
|
|
16
|
-
* The packages the child imports, by name (`['sharp']`). The child may
|
|
17
|
-
* read each one's folder and those of everything it depends on
|
|
18
|
-
* (dependencies, and installed optional and peer dependencies, which is
|
|
19
|
-
* where native packages keep their platform's binaries), resolved from
|
|
20
|
-
* the script's folder the way Node will resolve its imports. Nothing
|
|
21
|
-
* else in `node_modules` is readable. A package that isn't installed
|
|
22
|
-
* there fails the run (`'spawn-error'`) before a child starts.
|
|
23
|
-
*/
|
|
24
|
-
packages?: string[];
|
|
25
|
-
/**
|
|
26
|
-
* Files and directories (and everything under them) the child may read
|
|
27
|
-
* besides its own script and its `packages`. Everything else on disk is
|
|
28
|
-
* unreadable to the child: `.env`, your app's source, uploaded files.
|
|
29
|
-
*/
|
|
30
|
-
readable?: string[];
|
|
31
|
-
/**
|
|
32
|
-
* Whether the child may load native addons (`.node` files), as a library
|
|
33
|
-
* like sharp needs. Off by default. Node's permission model does not
|
|
34
|
-
* apply to native code: an addon, or an attacker who takes over one, can
|
|
35
|
-
* do anything the OS lets the process do. Turn this on only for fixed,
|
|
36
|
-
* known libraries, never for an addon the input could choose.
|
|
37
|
-
*/
|
|
38
|
-
allowAddons?: boolean;
|
|
39
|
-
/** The child's heap cap (`--max-old-space-size`), in MB. Default 256. */
|
|
40
|
-
heapMb?: number;
|
|
41
|
-
/** Arguments for the child script, in `process.argv.slice(2)` */
|
|
42
|
-
args?: string[];
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* The `node` command-line arguments for running `script` as a sandboxed
|
|
46
|
-
* child process: Node's permission model (`--permission`) with read access
|
|
47
|
-
* to `script`, its `packages` and `readable` only, no network, a heap cap, and code generation from
|
|
48
|
-
* strings (`eval`, `new Function`) turned off. No file writes, no child
|
|
49
|
-
* processes, no worker threads, no WASI, no inspector, and native addons
|
|
50
|
-
* only with `allowAddons`.
|
|
51
|
-
*
|
|
52
|
-
* On Node 25 and later the permission model blocks the network. On Node 24
|
|
53
|
-
* it doesn't, so the arguments also load a preload that takes the network
|
|
54
|
-
* away from JavaScript in the child (see `permissionModelBlocksNetwork()`).
|
|
55
|
-
*
|
|
56
|
-
* `runSandboxedChild()` uses this; it's exported for spawning the child
|
|
57
|
-
* yourself (e.g. in a test probing what the child can do). Pass an empty
|
|
58
|
-
* `env` when you do: the arguments restrict what the child may touch, not
|
|
59
|
-
* what it inherits.
|
|
60
|
-
*
|
|
61
|
-
* The permission model checks the path a file is opened by, and Node opens
|
|
62
|
-
* the script and the modules it imports by their real paths, so the
|
|
63
|
-
* script is given by its real path, and other paths by both (an app
|
|
64
|
-
* deployed behind a `current -> releases/…` symlink would otherwise be
|
|
65
|
-
* refused its own files). Throws if a path doesn't exist.
|
|
66
|
-
*/
|
|
67
|
-
export declare const sandboxedChildArgs: ({ script, packages, readable, allowAddons, heapMb, args, }: SandboxedChildArgsOptions) => string[];
|
|
68
|
-
export interface RunSandboxedChildOptions extends SandboxedChildArgsOptions {
|
|
69
|
-
/** The untrusted bytes, written to the child's stdin */
|
|
70
|
-
input: Uint8Array;
|
|
71
|
-
/**
|
|
72
|
-
* The child is killed (SIGKILL) if it hasn't exited after this many
|
|
73
|
-
* milliseconds. Default 10 000. Like the byte limits, it must be a finite
|
|
74
|
-
* number from 0 up; anything else (`NaN` from a missing setting, say) is
|
|
75
|
-
* a `'spawn-error'` and no child is started.
|
|
76
|
-
*/
|
|
77
|
-
timeoutMs?: number;
|
|
78
|
-
/**
|
|
79
|
-
* Larger input is refused without starting the child. Default 25 MiB.
|
|
80
|
-
*/
|
|
81
|
-
maxInputBytes?: number;
|
|
82
|
-
/**
|
|
83
|
-
* The child is killed as soon as it has written more than this many bytes
|
|
84
|
-
* to stdout. Default 25 MiB.
|
|
85
|
-
*/
|
|
86
|
-
maxOutputBytes?: number;
|
|
87
|
-
/** The child's working directory. Default: the script's folder. */
|
|
88
|
-
cwd?: string;
|
|
89
|
-
}
|
|
90
|
-
/** Why a sandboxed child produced no output */
|
|
91
|
-
export type SandboxedChildFailure =
|
|
92
|
-
/** The input was over `maxInputBytes`; no child was started */
|
|
93
|
-
'input-too-large'
|
|
94
|
-
/** The child ran past `timeoutMs` and was killed */
|
|
95
|
-
| 'timeout'
|
|
96
|
-
/** The child wrote more than `maxOutputBytes` and was killed */
|
|
97
|
-
| 'output-too-large'
|
|
98
|
-
/** The child exited non-zero or was killed by a signal */
|
|
99
|
-
| 'exit'
|
|
100
|
-
/** The child couldn't be started, or a limit isn't a valid number */
|
|
101
|
-
| 'spawn-error';
|
|
102
|
-
export type SandboxedChildResult = {
|
|
103
|
-
ok: true;
|
|
104
|
-
/** Everything the child wrote to stdout */
|
|
105
|
-
output: Buffer;
|
|
106
|
-
/** What the child wrote to stderr (the last 4000 characters) */
|
|
107
|
-
stderr: string;
|
|
108
|
-
} | {
|
|
109
|
-
ok: false;
|
|
110
|
-
failure: SandboxedChildFailure;
|
|
111
|
-
/** A one-line description, for a log */
|
|
112
|
-
message: string;
|
|
113
|
-
/** What the child wrote to stderr (the last 4000 characters) */
|
|
114
|
-
stderr: string;
|
|
115
|
-
exitCode: number | null;
|
|
116
|
-
signal: NodeJS.Signals | null;
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Runs a Node script on untrusted bytes in a locked-down (sandboxed) child
|
|
120
|
-
* process, so a crafted input that exploits a parser bug lands in a process
|
|
121
|
-
* that can't read your secrets or touch your files. Use it for parsing,
|
|
122
|
-
* decoding or converting files your users or third parties send you:
|
|
123
|
-
* resizing a photo with sharp, rendering a PDF page with pdf.js, unpacking
|
|
124
|
-
* an archive, parsing a spreadsheet.
|
|
125
|
-
*
|
|
126
|
-
* The child:
|
|
127
|
-
*
|
|
128
|
-
* - gets an **empty environment** — no `DATABASE_URL`, no API keys, none of
|
|
129
|
-
* your `.env`;
|
|
130
|
-
* - runs under **Node's permission model**: it may read its own script,
|
|
131
|
-
* the `packages` it imports and the `readable` paths,
|
|
132
|
-
* nothing else; it may not write files, spawn processes, start workers,
|
|
133
|
-
* or `eval`; it may load native addons only with `allowAddons`;
|
|
134
|
-
* - has **no network**: no sockets, no `fetch`, no DNS (on Node 24 through
|
|
135
|
-
* a preload, see `permissionModelBlocksNetwork()`);
|
|
136
|
-
* - gets the input on **stdin**, and its **stdout** is the result, used only
|
|
137
|
-
* if it exits with code 0;
|
|
138
|
-
* - is **bounded**: a heap cap, a hard timeout (SIGKILL), and caps on input
|
|
139
|
-
* and output size.
|
|
140
|
-
*
|
|
141
|
-
* It never rejects: every failure — a non-zero exit, a timeout, too much
|
|
142
|
-
* output, a crash — is an `{ ok: false }` result with a `failure` and a
|
|
143
|
-
* `message` for your log. Treat the output as untrusted too, and check it
|
|
144
|
-
* (e.g. that it's really a PNG) before using it.
|
|
145
|
-
*
|
|
146
|
-
* Limit how many run at once with `createConcurrencyLimiter()`: each child
|
|
147
|
-
* is a whole Node process.
|
|
148
|
-
*
|
|
149
|
-
* **This is defense in depth, not a security boundary.** Node documents
|
|
150
|
-
* that its permission model does not protect against malicious code that
|
|
151
|
-
* is already running in the process. None of it constrains native code: a
|
|
152
|
-
* native addon (`allowAddons`), or an exploit in one, makes its own system
|
|
153
|
-
* calls, so for those children what holds is the empty environment, the
|
|
154
|
-
* separate process and its limits. An external binary like ffmpeg needs an
|
|
155
|
-
* OS-level sandbox (a container, seccomp, a separate user).
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```ts
|
|
159
|
-
* import {
|
|
160
|
-
* createConcurrencyLimiter,
|
|
161
|
-
* findChildScript,
|
|
162
|
-
* runSandboxedChild,
|
|
163
|
-
* } from '@cedarjs/api/sandbox'
|
|
164
|
-
*
|
|
165
|
-
* const limit = createConcurrencyLimiter(2)
|
|
166
|
-
*
|
|
167
|
-
* const result = await limit(() =>
|
|
168
|
-
* runSandboxedChild({
|
|
169
|
-
* script: findChildScript(import.meta.dirname, 'resize.child'),
|
|
170
|
-
* packages: ['sharp'],
|
|
171
|
-
* allowAddons: true,
|
|
172
|
-
* input: uploadedBytes,
|
|
173
|
-
* timeoutMs: 10_000,
|
|
174
|
-
* maxOutputBytes: 4 * 1024 * 1024,
|
|
175
|
-
* }),
|
|
176
|
-
* )
|
|
177
|
-
*
|
|
178
|
-
* if (result.ok) {
|
|
179
|
-
* // result.output: Buffer
|
|
180
|
-
* } else {
|
|
181
|
-
* logger.warn({ failure: result.failure }, result.message)
|
|
182
|
-
* }
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
export declare const runSandboxedChild: ({ input, timeoutMs, maxInputBytes, maxOutputBytes, cwd, ...argsOptions }: RunSandboxedChildOptions) => Promise<SandboxedChildResult>;
|
|
186
|
-
//# sourceMappingURL=runSandboxedChild.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runSandboxedChild.d.ts","sourceRoot":"","sources":["../../src/sandbox/runSandboxedChild.ts"],"names":[],"mappings":"AAaA,0EAA0E;AAC1E,eAAO,MAAM,uBAAuB,MAAM,CAAA;AAC1C,sEAAsE;AACtE,eAAO,MAAM,0BAA0B,QAAS,CAAA;AAChD,gFAAgF;AAChF,eAAO,MAAM,+BAA+B,QAAW,CAAA;AACvD,wFAAwF;AACxF,eAAO,MAAM,gCAAgC,QAAW,CAAA;AAQxD,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;IACnB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;IACnB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,yEAAyE;IACzE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,kBAAkB,GAAI,4DAOhC,yBAAyB,KAAG,MAAM,EA6BpC,CAAA;AAED,MAAM,WAAW,wBAAyB,SAAQ,yBAAyB;IACzE,wDAAwD;IACxD,KAAK,EAAE,UAAU,CAAA;IACjB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,mEAAmE;IACnE,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,+CAA+C;AAC/C,MAAM,MAAM,qBAAqB;AAC/B,+DAA+D;AAC7D,iBAAiB;AACnB,oDAAoD;GAClD,SAAS;AACX,gEAAgE;GAC9D,kBAAkB;AACpB,0DAA0D;GACxD,MAAM;AACR,qEAAqE;GACnE,aAAa,CAAA;AAEjB,MAAM,MAAM,oBAAoB,GAC5B;IACE,EAAE,EAAE,IAAI,CAAA;IACR,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAA;IACd,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAA;CACf,GACD;IACE,EAAE,EAAE,KAAK,CAAA;IACT,OAAO,EAAE,qBAAqB,CAAA;IAC9B,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAA;IACf,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,IAAI,CAAA;CAC9B,CAAA;AAEL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AACH,eAAO,MAAM,iBAAiB,GAAI,0EAO/B,wBAAwB,KAAG,OAAO,CAAC,oBAAoB,CA+LzD,CAAA"}
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
noNetworkPreloadArg,
|
|
6
|
-
permissionModelBlocksNetwork
|
|
7
|
-
} from "./noNetworkPreload.js";
|
|
8
|
-
import { sandboxPackagePaths } from "./sandboxPackages.js";
|
|
9
|
-
const MIB = 1024 * 1024;
|
|
10
|
-
const DEFAULT_SANDBOX_HEAP_MB = 256;
|
|
11
|
-
const DEFAULT_SANDBOX_TIMEOUT_MS = 1e4;
|
|
12
|
-
const DEFAULT_SANDBOX_MAX_INPUT_BYTES = 25 * MIB;
|
|
13
|
-
const DEFAULT_SANDBOX_MAX_OUTPUT_BYTES = 25 * MIB;
|
|
14
|
-
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
15
|
-
const MAX_STDERR_CHARS = 4e3;
|
|
16
|
-
const sandboxedChildArgs = ({
|
|
17
|
-
script,
|
|
18
|
-
packages = [],
|
|
19
|
-
readable = [],
|
|
20
|
-
allowAddons = false,
|
|
21
|
-
heapMb = DEFAULT_SANDBOX_HEAP_MB,
|
|
22
|
-
args = []
|
|
23
|
-
}) => {
|
|
24
|
-
if (!Number.isInteger(heapMb) || heapMb <= 0) {
|
|
25
|
-
throw new Error(`heapMb must be a positive integer, got ${heapMb}`);
|
|
26
|
-
}
|
|
27
|
-
const realScript = fs.realpathSync(script);
|
|
28
|
-
const readPaths = [
|
|
29
|
-
.../* @__PURE__ */ new Set([
|
|
30
|
-
realScript,
|
|
31
|
-
...sandboxPackagePaths(packages, realScript),
|
|
32
|
-
// Node may read a file through the path it was given or through
|
|
33
|
-
// its real path
|
|
34
|
-
...readable.flatMap((p) => [path.resolve(p), fs.realpathSync(p)])
|
|
35
|
-
])
|
|
36
|
-
];
|
|
37
|
-
return [
|
|
38
|
-
`--max-old-space-size=${heapMb}`,
|
|
39
|
-
"--disallow-code-generation-from-strings",
|
|
40
|
-
"--permission",
|
|
41
|
-
...allowAddons ? ["--allow-addons"] : [],
|
|
42
|
-
// Node warns that the permission model is experimental, and about
|
|
43
|
-
// --allow-addons; the warnings would end up in the failure message
|
|
44
|
-
"--no-warnings",
|
|
45
|
-
...readPaths.map((p) => `--allow-fs-read=${p}`),
|
|
46
|
-
...permissionModelBlocksNetwork() ? [] : [noNetworkPreloadArg()],
|
|
47
|
-
realScript,
|
|
48
|
-
...args
|
|
49
|
-
];
|
|
50
|
-
};
|
|
51
|
-
const runSandboxedChild = ({
|
|
52
|
-
input,
|
|
53
|
-
timeoutMs = DEFAULT_SANDBOX_TIMEOUT_MS,
|
|
54
|
-
maxInputBytes = DEFAULT_SANDBOX_MAX_INPUT_BYTES,
|
|
55
|
-
maxOutputBytes = DEFAULT_SANDBOX_MAX_OUTPUT_BYTES,
|
|
56
|
-
cwd,
|
|
57
|
-
...argsOptions
|
|
58
|
-
}) => {
|
|
59
|
-
const fail = (failure, message, rest = {}) => ({
|
|
60
|
-
ok: false,
|
|
61
|
-
failure,
|
|
62
|
-
message,
|
|
63
|
-
stderr: rest.stderr ?? "",
|
|
64
|
-
exitCode: rest.exitCode ?? null,
|
|
65
|
-
signal: rest.signal ?? null
|
|
66
|
-
});
|
|
67
|
-
const invalidLimit = [
|
|
68
|
-
["timeoutMs", timeoutMs, MAX_TIMEOUT_MS],
|
|
69
|
-
["maxInputBytes", maxInputBytes, Number.MAX_SAFE_INTEGER],
|
|
70
|
-
["maxOutputBytes", maxOutputBytes, Number.MAX_SAFE_INTEGER]
|
|
71
|
-
].find(
|
|
72
|
-
([, value, max]) => typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > Number(max)
|
|
73
|
-
);
|
|
74
|
-
if (invalidLimit) {
|
|
75
|
-
const [name, value, max] = invalidLimit;
|
|
76
|
-
return Promise.resolve(
|
|
77
|
-
fail(
|
|
78
|
-
"spawn-error",
|
|
79
|
-
`${name} must be a number from 0 to ${max}, got ${String(value)}`
|
|
80
|
-
)
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
if (input.length > maxInputBytes) {
|
|
84
|
-
return Promise.resolve(
|
|
85
|
-
fail(
|
|
86
|
-
"input-too-large",
|
|
87
|
-
`input is ${input.length} bytes, over the ${maxInputBytes} byte limit`
|
|
88
|
-
)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
let args;
|
|
92
|
-
try {
|
|
93
|
-
args = sandboxedChildArgs(argsOptions);
|
|
94
|
-
} catch (e) {
|
|
95
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
96
|
-
return Promise.resolve(fail("spawn-error", message));
|
|
97
|
-
}
|
|
98
|
-
return new Promise((resolve) => {
|
|
99
|
-
let settled = false;
|
|
100
|
-
let stderr = "";
|
|
101
|
-
const stderrTail = () => stderr.slice(-MAX_STDERR_CHARS);
|
|
102
|
-
let child;
|
|
103
|
-
const settle = (result) => {
|
|
104
|
-
if (settled) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
settled = true;
|
|
108
|
-
clearTimeout(timer);
|
|
109
|
-
resolve(result);
|
|
110
|
-
};
|
|
111
|
-
let killedFor = null;
|
|
112
|
-
const kill = (failure, message) => {
|
|
113
|
-
if (killedFor || settled) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
killedFor = { failure, message };
|
|
117
|
-
clearTimeout(timer);
|
|
118
|
-
child?.kill("SIGKILL");
|
|
119
|
-
};
|
|
120
|
-
const timer = setTimeout(
|
|
121
|
-
() => kill("timeout", `timed out after ${timeoutMs} ms`),
|
|
122
|
-
timeoutMs
|
|
123
|
-
);
|
|
124
|
-
try {
|
|
125
|
-
child = spawn(process.execPath, args, {
|
|
126
|
-
// Nothing of the parent's environment: no database URL, no keys
|
|
127
|
-
env: {},
|
|
128
|
-
cwd: cwd ?? path.dirname(fs.realpathSync(argsOptions.script)),
|
|
129
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
130
|
-
windowsHide: true
|
|
131
|
-
});
|
|
132
|
-
} catch (e) {
|
|
133
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
134
|
-
settle(fail("spawn-error", message));
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const output = [];
|
|
138
|
-
let outputSize = 0;
|
|
139
|
-
child.stdout?.on("data", (chunk) => {
|
|
140
|
-
if (settled || killedFor) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
outputSize += chunk.length;
|
|
144
|
-
if (outputSize > maxOutputBytes) {
|
|
145
|
-
kill(
|
|
146
|
-
"output-too-large",
|
|
147
|
-
`output is over the ${maxOutputBytes} byte limit`
|
|
148
|
-
);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
output.push(chunk);
|
|
152
|
-
});
|
|
153
|
-
child.stderr?.setEncoding("utf8");
|
|
154
|
-
child.stderr?.on("data", (chunk) => {
|
|
155
|
-
stderr = (stderr + chunk).slice(-MAX_STDERR_CHARS * 2);
|
|
156
|
-
});
|
|
157
|
-
child.on("error", (e) => {
|
|
158
|
-
if (child?.pid === void 0) {
|
|
159
|
-
settle(fail("spawn-error", e.message, { stderr: stderrTail() }));
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
child.on("exit", (exitCode, signal) => {
|
|
163
|
-
if (killedFor) {
|
|
164
|
-
settle(
|
|
165
|
-
fail(killedFor.failure, killedFor.message, {
|
|
166
|
-
stderr: stderrTail(),
|
|
167
|
-
exitCode,
|
|
168
|
-
signal
|
|
169
|
-
})
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
child.on("close", (exitCode, signal) => {
|
|
174
|
-
if (killedFor) {
|
|
175
|
-
settle(
|
|
176
|
-
fail(killedFor.failure, killedFor.message, {
|
|
177
|
-
stderr: stderrTail(),
|
|
178
|
-
exitCode,
|
|
179
|
-
signal
|
|
180
|
-
})
|
|
181
|
-
);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
if (exitCode === 0) {
|
|
185
|
-
settle({
|
|
186
|
-
ok: true,
|
|
187
|
-
output: Buffer.concat(output),
|
|
188
|
-
stderr: stderrTail()
|
|
189
|
-
});
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const lastLine = stderrTail().trim().split("\n").filter((line) => line.trim()).pop() ?? "";
|
|
193
|
-
settle(
|
|
194
|
-
fail(
|
|
195
|
-
"exit",
|
|
196
|
-
`exited with ${exitCode ?? signal}${lastLine ? `: ${lastLine}` : ""}`,
|
|
197
|
-
{ stderr: stderrTail(), exitCode, signal }
|
|
198
|
-
)
|
|
199
|
-
);
|
|
200
|
-
});
|
|
201
|
-
child.stdin?.on("error", () => {
|
|
202
|
-
});
|
|
203
|
-
child.stdin?.end(input);
|
|
204
|
-
});
|
|
205
|
-
};
|
|
206
|
-
export {
|
|
207
|
-
DEFAULT_SANDBOX_HEAP_MB,
|
|
208
|
-
DEFAULT_SANDBOX_MAX_INPUT_BYTES,
|
|
209
|
-
DEFAULT_SANDBOX_MAX_OUTPUT_BYTES,
|
|
210
|
-
DEFAULT_SANDBOX_TIMEOUT_MS,
|
|
211
|
-
runSandboxedChild,
|
|
212
|
-
sandboxedChildArgs
|
|
213
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The folders a sandboxed child needs to read to import `packages` from
|
|
3
|
-
* `fromFile` (its script): each package's folder and, recursively, those
|
|
4
|
-
* of its `dependencies` and installed `optionalDependencies` and
|
|
5
|
-
* `peerDependencies`, each resolved from the folder of the package that
|
|
6
|
-
* needs it, as Node will at run time. That covers nested versions, pnpm's
|
|
7
|
-
* layout and native packages' platform binaries (`@img/sharp-<platform>`)
|
|
8
|
-
* without naming a platform. Optional and peer dependencies that aren't
|
|
9
|
-
* installed are skipped; a missing package or regular dependency throws.
|
|
10
|
-
*
|
|
11
|
-
* A package reached through a symlink is granted at both paths: Node reads
|
|
12
|
-
* its `package.json` through the path it looked in, and its modules
|
|
13
|
-
* through their real paths.
|
|
14
|
-
*/
|
|
15
|
-
export declare const sandboxPackagePaths: (packages: string[], fromFile: string) => string[];
|
|
16
|
-
//# sourceMappingURL=sandboxPackages.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sandboxPackages.d.ts","sourceRoot":"","sources":["../../src/sandbox/sandboxPackages.ts"],"names":[],"mappings":"AAmCA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,EAAE,EAAE,UAAU,MAAM,aAoCvE,CAAA"}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
const findPackage = (name, fromFile) => {
|
|
5
|
-
for (const dir of createRequire(fromFile).resolve.paths(name) ?? []) {
|
|
6
|
-
const found = path.join(dir, name);
|
|
7
|
-
if (!fs.existsSync(path.join(found, "package.json"))) {
|
|
8
|
-
continue;
|
|
9
|
-
}
|
|
10
|
-
const realDir = fs.realpathSync(found);
|
|
11
|
-
const pkg = JSON.parse(
|
|
12
|
-
fs.readFileSync(path.join(realDir, "package.json"), "utf8")
|
|
13
|
-
);
|
|
14
|
-
return { found, realDir, pkg };
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
};
|
|
18
|
-
const sandboxPackagePaths = (packages, fromFile) => {
|
|
19
|
-
const paths = /* @__PURE__ */ new Set();
|
|
20
|
-
const visited = /* @__PURE__ */ new Set();
|
|
21
|
-
const visit = (name, from, required, by) => {
|
|
22
|
-
const found = findPackage(name, from);
|
|
23
|
-
if (!found) {
|
|
24
|
-
if (required) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
`Can't find package "${name}" (needed by ${by}) in any node_modules folder Node looks in from ${path.dirname(from)}`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
paths.add(found.found).add(found.realDir);
|
|
32
|
-
if (visited.has(found.realDir)) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
visited.add(found.realDir);
|
|
36
|
-
const manifest = path.join(found.realDir, "package.json");
|
|
37
|
-
const { dependencies = {}, optionalDependencies = {} } = found.pkg;
|
|
38
|
-
const optional = { ...optionalDependencies, ...found.pkg.peerDependencies };
|
|
39
|
-
for (const dep of Object.keys(dependencies)) {
|
|
40
|
-
visit(dep, manifest, !(dep in optional), name);
|
|
41
|
-
}
|
|
42
|
-
for (const dep of Object.keys(optional)) {
|
|
43
|
-
visit(dep, manifest, false, name);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
for (const name of packages) {
|
|
47
|
-
visit(name, fromFile, true, path.basename(fromFile));
|
|
48
|
-
}
|
|
49
|
-
return [...paths];
|
|
50
|
-
};
|
|
51
|
-
export {
|
|
52
|
-
sandboxPackagePaths
|
|
53
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signedTokens/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./signedTokens.js";
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type { SignOptions } from 'jsonwebtoken';
|
|
2
|
-
/**
|
|
3
|
-
* Name of the environment variable that holds the default secret used to
|
|
4
|
-
* sign and verify tokens when no `secret` option is passed.
|
|
5
|
-
*/
|
|
6
|
-
export declare const SIGNED_TOKEN_SECRET_ENV_VAR = "SIGNED_TOKEN_SECRET";
|
|
7
|
-
/**
|
|
8
|
-
* Machine-readable reason a `SignedTokenError` was thrown. Use it to tell an
|
|
9
|
-
* expired link apart from a tampered one without matching on the message.
|
|
10
|
-
*/
|
|
11
|
-
export type SignedTokenErrorCode = 'MISSING_SECRET' | 'MISSING_PURPOSE' | 'MISSING_TOKEN' | 'SIGN_FAILED' | 'INVALID' | 'EXPIRED' | 'PURPOSE_MISMATCH';
|
|
12
|
-
/**
|
|
13
|
-
* Thrown by `createSignedToken` and `verifySignedToken` whenever a token
|
|
14
|
-
* cannot be created or cannot be trusted. Verification never returns `null`
|
|
15
|
-
* or `false` for a bad token; it always throws so a missing check cannot
|
|
16
|
-
* silently pass.
|
|
17
|
-
*/
|
|
18
|
-
export declare class SignedTokenError extends Error {
|
|
19
|
-
code: SignedTokenErrorCode;
|
|
20
|
-
constructor(code: SignedTokenErrorCode, message: string, cause?: unknown);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* How long a token stays valid. A number is a count of seconds. A string is
|
|
24
|
-
* a duration such as `'10m'`, `'2h'`, or `'7 days'`.
|
|
25
|
-
*/
|
|
26
|
-
export type SignedTokenExpiresIn = NonNullable<SignOptions['expiresIn']>;
|
|
27
|
-
export interface CreateSignedTokenOptions<TPayload extends object> {
|
|
28
|
-
/**
|
|
29
|
-
* The claims to carry inside the token. Serialized as JSON, so only
|
|
30
|
-
* JSON-compatible values survive the round trip (a `Date` comes back as a
|
|
31
|
-
* string).
|
|
32
|
-
*/
|
|
33
|
-
payload: TPayload;
|
|
34
|
-
/**
|
|
35
|
-
* What the token is for, such as `'google-oauth-state'` or
|
|
36
|
-
* `'email-confirmation'`. A token only verifies when the same purpose is
|
|
37
|
-
* passed to `verifySignedToken`, so a token minted for one flow cannot be
|
|
38
|
-
* replayed in another.
|
|
39
|
-
*/
|
|
40
|
-
purpose: string;
|
|
41
|
-
/** How long the token stays valid. See `SignedTokenExpiresIn`. */
|
|
42
|
-
expiresIn: SignedTokenExpiresIn;
|
|
43
|
-
/**
|
|
44
|
-
* The secret to sign with. Defaults to the `SIGNED_TOKEN_SECRET`
|
|
45
|
-
* environment variable. There is no built-in fallback: when neither is set
|
|
46
|
-
* a `SignedTokenError` with code `MISSING_SECRET` is thrown.
|
|
47
|
-
*/
|
|
48
|
-
secret?: string;
|
|
49
|
-
}
|
|
50
|
-
export interface VerifySignedTokenOptions {
|
|
51
|
-
/** The purpose the token must have been created with. */
|
|
52
|
-
purpose: string;
|
|
53
|
-
/**
|
|
54
|
-
* The secret to verify with. Defaults to the `SIGNED_TOKEN_SECRET`
|
|
55
|
-
* environment variable. There is no built-in fallback: when neither is set
|
|
56
|
-
* a `SignedTokenError` with code `MISSING_SECRET` is thrown.
|
|
57
|
-
*/
|
|
58
|
-
secret?: string;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Creates a compact, URL-safe token that carries `payload`, is bound to
|
|
62
|
-
* `purpose`, expires after `expiresIn`, and is signed so it can only have
|
|
63
|
-
* come from this application.
|
|
64
|
-
*
|
|
65
|
-
* Use it wherever a value has to leave the server and come back untouched:
|
|
66
|
-
* OAuth `state`, email confirmation and password-set links, unsubscribe
|
|
67
|
-
* links, or short-lived capability tokens.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
*
|
|
71
|
-
* const state = createSignedToken({
|
|
72
|
-
* payload: { organizationId, userId },
|
|
73
|
-
* purpose: 'google-oauth-state',
|
|
74
|
-
* expiresIn: '10m',
|
|
75
|
-
* })
|
|
76
|
-
*/
|
|
77
|
-
export declare function createSignedToken<TPayload extends object>({ payload, purpose, expiresIn, secret, }: CreateSignedTokenOptions<TPayload>): string;
|
|
78
|
-
/**
|
|
79
|
-
* Verifies a token created by `createSignedToken` and returns its payload.
|
|
80
|
-
*
|
|
81
|
-
* Throws a `SignedTokenError` when the token is missing, was signed with a
|
|
82
|
-
* different secret, has been tampered with, has expired, or was created for
|
|
83
|
-
* a different `purpose`. The error's `code` says which.
|
|
84
|
-
*
|
|
85
|
-
* The token argument accepts `null` and `undefined` on purpose so a value
|
|
86
|
-
* read straight from a query string or a header can be passed in as-is. A
|
|
87
|
-
* missing token is a verification failure, never a skipped check.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
*
|
|
91
|
-
* const { organizationId, userId } = verifySignedToken<{
|
|
92
|
-
* organizationId: string
|
|
93
|
-
* userId: string
|
|
94
|
-
* }>(event.queryStringParameters?.state, {
|
|
95
|
-
* purpose: 'google-oauth-state',
|
|
96
|
-
* })
|
|
97
|
-
*/
|
|
98
|
-
export declare function verifySignedToken<TPayload extends object = Record<string, unknown>>(token: string | null | undefined, { purpose, secret }: VerifySignedTokenOptions): TPayload;
|
|
99
|
-
//# sourceMappingURL=signedTokens.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"signedTokens.d.ts","sourceRoot":"","sources":["../../src/signedTokens/signedTokens.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE/C;;;GAGG;AACH,eAAO,MAAM,2BAA2B,wBAAwB,CAAA;AAEhE;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAC5B,gBAAgB,GAChB,iBAAiB,GACjB,eAAe,GACf,aAAa,GACb,SAAS,GACT,SAAS,GACT,kBAAkB,CAAA;AAEtB;;;;;GAKG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,IAAI,EAAE,oBAAoB,CAAA;gBAEd,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAKzE;AAED;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,CAAA;AAExE,MAAM,WAAW,wBAAwB,CAAC,QAAQ,SAAS,MAAM;IAC/D;;;;OAIG;IACH,OAAO,EAAE,QAAQ,CAAA;IACjB;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAA;IACf,kEAAkE;IAClE,SAAS,EAAE,oBAAoB,CAAA;IAC/B;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,wBAAwB;IACvC,yDAAyD;IACzD,OAAO,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAiED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,SAAS,MAAM,EAAE,EACzD,OAAO,EACP,OAAO,EACP,SAAS,EACT,MAAM,GACP,EAAE,wBAAwB,CAAC,QAAQ,CAAC,GAAG,MAAM,CAkB7C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAEjD,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAChC,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,wBAAwB,GAC5C,QAAQ,CA2CV"}
|