opencode-fs-plugin 3.3.0 → 3.5.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 +173 -119
- package/SPEC.md +61 -39
- package/package.json +2 -2
- package/skills/filesystem/SKILL.md +93 -53
- package/src/core/format.ts +3 -9
- package/src/core/permissions.ts +32 -14
- package/src/core/relay-control.ts +343 -0
- package/src/core/relay-protocol.ts +229 -0
- package/src/core/types.ts +0 -3
- package/src/index.ts +55 -64
- package/src/tools/cat.ts +31 -46
- package/src/tools/command.ts +533 -0
- package/src/tools/{http_request.ts → curl.ts} +4 -4
- package/src/tools/pipe.ts +7 -7
- package/src/core/spill.ts +0 -121
- package/src/tools/exec.ts +0 -483
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as fs from "node:fs/promises";
|
|
7
|
+
import * as net from "node:net";
|
|
8
|
+
import * as os from "node:os";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { FsError, errorMessage } from "./errors.js";
|
|
11
|
+
import {
|
|
12
|
+
RELAY_DECIDE_REQUEST,
|
|
13
|
+
RELAY_HELLO,
|
|
14
|
+
RelayFrameReader,
|
|
15
|
+
RelayProtocolError,
|
|
16
|
+
decodeDecisionRequest,
|
|
17
|
+
decodeHello,
|
|
18
|
+
encodeDecisionResponse,
|
|
19
|
+
encodeHelloAck,
|
|
20
|
+
type RelayDecisionRequest,
|
|
21
|
+
type RelayVerdict,
|
|
22
|
+
} from "./relay-protocol.js";
|
|
23
|
+
|
|
24
|
+
export const RELAY_CONTROL_MAX_INFLIGHT = 64;
|
|
25
|
+
/**
|
|
26
|
+
* Completed decision ids retained to reject a replay. procage allocates ids
|
|
27
|
+
* monotonically and never reuses one, so only a recent duplicate can be a
|
|
28
|
+
* protocol error; older ids are forgotten to keep a long run's memory
|
|
29
|
+
* bounded. The Rust client keeps its own bounded late-answer queue.
|
|
30
|
+
*/
|
|
31
|
+
export const RELAY_CONTROL_DECIDED_HISTORY = 1024;
|
|
32
|
+
/**
|
|
33
|
+
* Permission prompts are interactive, so the default is generous. The caller
|
|
34
|
+
* can lower it; every timeout resolves to a deny.
|
|
35
|
+
*/
|
|
36
|
+
export const RELAY_CONTROL_REQUEST_TIMEOUT_MS = 60_000;
|
|
37
|
+
export const RELAY_CONTROL_HANDSHAKE_TIMEOUT_MS = 5_000;
|
|
38
|
+
|
|
39
|
+
const DIR_PREFIX = "opencode-relay-";
|
|
40
|
+
const SOCKET_NAME = "control.sock";
|
|
41
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
42
|
+
|
|
43
|
+
export interface RelayControlOptions {
|
|
44
|
+
/** The run id HELLO must carry and HELLO_ACK echoes. */
|
|
45
|
+
readonly runId: bigint;
|
|
46
|
+
/** Injected permission assertion; rejection is a deny. */
|
|
47
|
+
readonly assertNetwork: (resource: string) => Promise<void>;
|
|
48
|
+
/** Concurrent decisions allowed before further requests are denied. */
|
|
49
|
+
readonly maxInflight?: number;
|
|
50
|
+
/** Per-decision timeout; a timeout is a deny. */
|
|
51
|
+
readonly requestTimeoutMs?: number;
|
|
52
|
+
/** Time allowed for HELLO after connect; a timeout closes the control link. */
|
|
53
|
+
readonly handshakeTimeoutMs?: number;
|
|
54
|
+
/** Parent for the per-run 0700 directory. Defaults to `os.tmpdir()`. */
|
|
55
|
+
readonly baseDir?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface PendingDecision {
|
|
59
|
+
readonly timer: NodeJS.Timeout;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface ControlSession {
|
|
63
|
+
readonly socket: net.Socket;
|
|
64
|
+
readonly reader: RelayFrameReader;
|
|
65
|
+
readonly handshakeTimer: NodeJS.Timeout;
|
|
66
|
+
readonly active: Map<bigint, PendingDecision>;
|
|
67
|
+
/** Recent completed ids, oldest first, bounded by the history cap. */
|
|
68
|
+
readonly decided: Map<bigint, true>;
|
|
69
|
+
hello: boolean;
|
|
70
|
+
closed: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A fail-closed per-run network decision server for procage's relay control
|
|
75
|
+
* client. It listens on a private Unix SOCK_STREAM socket in a fresh 0700
|
|
76
|
+
* directory, accepts exactly one connection for the run, and answers only
|
|
77
|
+
* DECIDE_REQ messages with a permission verdict.
|
|
78
|
+
*
|
|
79
|
+
* Cancellation, a closed control link, a handshake failure, a malformed frame,
|
|
80
|
+
* or a decision timeout all deny. The socket path must be passed to procage as
|
|
81
|
+
* `relay_control_socket`; it must never be added to `ipc.unix_sockets` and
|
|
82
|
+
* must not be inside the child's TMPDIR or workspace.
|
|
83
|
+
*/
|
|
84
|
+
export class RelayControlServer {
|
|
85
|
+
readonly runId: bigint;
|
|
86
|
+
readonly maxInflight: number;
|
|
87
|
+
|
|
88
|
+
/** The Unix socket path once started; empty before start and after close. */
|
|
89
|
+
socketPath = "";
|
|
90
|
+
|
|
91
|
+
private readonly assertNetwork: (resource: string) => Promise<void>;
|
|
92
|
+
private readonly requestTimeoutMs: number;
|
|
93
|
+
private readonly handshakeTimeoutMs: number;
|
|
94
|
+
private readonly baseDir: string;
|
|
95
|
+
private dir = "";
|
|
96
|
+
private server!: net.Server;
|
|
97
|
+
private session: ControlSession | undefined;
|
|
98
|
+
private connected = false;
|
|
99
|
+
private started = false;
|
|
100
|
+
|
|
101
|
+
constructor(options: RelayControlOptions) {
|
|
102
|
+
this.runId = normalizeRunId(options.runId);
|
|
103
|
+
this.maxInflight = integerOption(options.maxInflight, RELAY_CONTROL_MAX_INFLIGHT, 1, 0xffffffff, "maxInflight");
|
|
104
|
+
this.requestTimeoutMs = integerOption(
|
|
105
|
+
options.requestTimeoutMs,
|
|
106
|
+
RELAY_CONTROL_REQUEST_TIMEOUT_MS,
|
|
107
|
+
1,
|
|
108
|
+
MAX_TIMER_MS,
|
|
109
|
+
"requestTimeoutMs",
|
|
110
|
+
);
|
|
111
|
+
this.handshakeTimeoutMs = integerOption(
|
|
112
|
+
options.handshakeTimeoutMs,
|
|
113
|
+
RELAY_CONTROL_HANDSHAKE_TIMEOUT_MS,
|
|
114
|
+
1,
|
|
115
|
+
MAX_TIMER_MS,
|
|
116
|
+
"handshakeTimeoutMs",
|
|
117
|
+
);
|
|
118
|
+
this.assertNetwork = options.assertNetwork;
|
|
119
|
+
this.baseDir = options.baseDir ?? os.tmpdir();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Number of decisions currently awaiting a permission result. */
|
|
123
|
+
get pendingCount(): number {
|
|
124
|
+
return this.session === undefined ? 0 : this.session.active.size;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Number of recent completed ids kept to reject a replay. */
|
|
128
|
+
get decidedCount(): number {
|
|
129
|
+
return this.session === undefined ? 0 : this.session.decided.size;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Create the private directory, bind the socket, and begin listening. */
|
|
133
|
+
async start(): Promise<string> {
|
|
134
|
+
if (this.started) {
|
|
135
|
+
throw new FsError("relay control server is already started.", "exists");
|
|
136
|
+
}
|
|
137
|
+
this.started = true;
|
|
138
|
+
|
|
139
|
+
let dir = "";
|
|
140
|
+
try {
|
|
141
|
+
dir = await fs.mkdtemp(path.join(this.baseDir, DIR_PREFIX));
|
|
142
|
+
await fs.chmod(dir, 0o700);
|
|
143
|
+
const socketPath = path.join(dir, SOCKET_NAME);
|
|
144
|
+
const server = net.createServer((socket) => this.accept(socket));
|
|
145
|
+
server.on("error", () => {});
|
|
146
|
+
this.server = server;
|
|
147
|
+
await new Promise<void>((resolve, reject) => {
|
|
148
|
+
const onError = (error: Error) => {
|
|
149
|
+
reject(error);
|
|
150
|
+
};
|
|
151
|
+
server.once("error", onError);
|
|
152
|
+
server.listen(socketPath, () => {
|
|
153
|
+
server.off("error", onError);
|
|
154
|
+
resolve();
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
await fs.chmod(socketPath, 0o600);
|
|
158
|
+
this.dir = dir;
|
|
159
|
+
this.socketPath = socketPath;
|
|
160
|
+
return socketPath;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
// A bind that fails after accepting a client must not leak the session or
|
|
163
|
+
// its socket: end it first, then close the listener and remove the dir.
|
|
164
|
+
if (this.session !== undefined) this.endSession(this.session);
|
|
165
|
+
this.connected = false;
|
|
166
|
+
const server = this.server as net.Server | undefined;
|
|
167
|
+
if (server !== undefined) {
|
|
168
|
+
await new Promise<void>((resolve) => {
|
|
169
|
+
server.close(() => resolve());
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
this.started = false;
|
|
173
|
+
this.dir = "";
|
|
174
|
+
this.socketPath = "";
|
|
175
|
+
if (dir.length > 0) await fs.rm(dir, { recursive: true, force: true });
|
|
176
|
+
throw new FsError(`relay control server failed to start: ${errorMessage(error)}`, "io");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Stop listening, close the control link, and remove the socket directory. */
|
|
181
|
+
async close(): Promise<void> {
|
|
182
|
+
if (!this.started) return;
|
|
183
|
+
this.started = false;
|
|
184
|
+
const session = this.session;
|
|
185
|
+
if (session !== undefined) this.endSession(session);
|
|
186
|
+
await new Promise<void>((resolve) => {
|
|
187
|
+
this.server.close(() => resolve());
|
|
188
|
+
});
|
|
189
|
+
const dir = this.dir;
|
|
190
|
+
this.dir = "";
|
|
191
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
192
|
+
this.socketPath = "";
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private accept(socket: net.Socket): void {
|
|
196
|
+
if (this.connected) {
|
|
197
|
+
socket.destroy();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this.connected = true;
|
|
201
|
+
socket.setNoDelay(true);
|
|
202
|
+
|
|
203
|
+
let session: ControlSession;
|
|
204
|
+
const handshakeTimer = setTimeout(() => this.endSession(session), this.handshakeTimeoutMs);
|
|
205
|
+
handshakeTimer.unref();
|
|
206
|
+
session = {
|
|
207
|
+
socket,
|
|
208
|
+
reader: new RelayFrameReader(),
|
|
209
|
+
handshakeTimer,
|
|
210
|
+
active: new Map(),
|
|
211
|
+
decided: new Map(),
|
|
212
|
+
hello: false,
|
|
213
|
+
closed: false,
|
|
214
|
+
};
|
|
215
|
+
this.session = session;
|
|
216
|
+
|
|
217
|
+
socket.on("data", (chunk) => this.onData(session, chunk));
|
|
218
|
+
/* v8 ignore next 2 -- an errored socket also emits close */
|
|
219
|
+
socket.on("error", () => this.endSession(session));
|
|
220
|
+
socket.on("close", () => this.endSession(session));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private onData(session: ControlSession, chunk: Buffer): void {
|
|
224
|
+
try {
|
|
225
|
+
for (const payload of session.reader.push(chunk)) {
|
|
226
|
+
this.dispatch(session, payload);
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
this.endSession(session);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private dispatch(session: ControlSession, payload: Buffer): void {
|
|
234
|
+
const type = payload.readUInt8(0);
|
|
235
|
+
if (type === RELAY_HELLO) {
|
|
236
|
+
this.onHello(session, payload);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (type === RELAY_DECIDE_REQUEST) {
|
|
240
|
+
this.onRequest(session, payload);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
throw new RelayProtocolError(`unexpected relay message type: ${type}`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private onHello(session: ControlSession, payload: Buffer): void {
|
|
247
|
+
const hello = decodeHello(payload);
|
|
248
|
+
if (session.hello) {
|
|
249
|
+
throw new RelayProtocolError("duplicate HELLO");
|
|
250
|
+
}
|
|
251
|
+
if (hello.runId !== this.runId) {
|
|
252
|
+
throw new RelayProtocolError("HELLO run id does not match this relay run");
|
|
253
|
+
}
|
|
254
|
+
session.hello = true;
|
|
255
|
+
clearTimeout(session.handshakeTimer);
|
|
256
|
+
session.socket.write(encodeHelloAck(this.runId, this.maxInflight));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private onRequest(session: ControlSession, payload: Buffer): void {
|
|
260
|
+
if (!session.hello) {
|
|
261
|
+
throw new RelayProtocolError("DECIDE_REQ before HELLO");
|
|
262
|
+
}
|
|
263
|
+
const request = decodeDecisionRequest(payload);
|
|
264
|
+
if (session.active.has(request.id)) {
|
|
265
|
+
throw new RelayProtocolError(`duplicate in-flight decision id: ${request.id}`);
|
|
266
|
+
}
|
|
267
|
+
if (session.decided.has(request.id)) {
|
|
268
|
+
throw new RelayProtocolError(`stale decision id: ${request.id}`);
|
|
269
|
+
}
|
|
270
|
+
if (session.active.size >= this.maxInflight) {
|
|
271
|
+
this.settle(session, request.id, 0, "inflight limit");
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const timer = setTimeout(() => this.settle(session, request.id, 0, "decision timeout"), this.requestTimeoutMs);
|
|
276
|
+
timer.unref();
|
|
277
|
+
session.active.set(request.id, { timer });
|
|
278
|
+
void this.evaluate(session, request);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private async evaluate(session: ControlSession, request: RelayDecisionRequest): Promise<void> {
|
|
282
|
+
try {
|
|
283
|
+
await this.assertNetwork(request.resource);
|
|
284
|
+
this.settle(session, request.id, 1, "allowed");
|
|
285
|
+
} catch (error) {
|
|
286
|
+
this.settle(session, request.id, 0, denyReason(error));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private settle(session: ControlSession, id: bigint, verdict: RelayVerdict, reason: string): void {
|
|
291
|
+
const pending = session.active.get(id);
|
|
292
|
+
if (pending !== undefined) {
|
|
293
|
+
session.active.delete(id);
|
|
294
|
+
clearTimeout(pending.timer);
|
|
295
|
+
}
|
|
296
|
+
if (session.decided.has(id)) return;
|
|
297
|
+
session.decided.set(id, true);
|
|
298
|
+
while (session.decided.size > RELAY_CONTROL_DECIDED_HISTORY) {
|
|
299
|
+
const oldest = session.decided.keys().next();
|
|
300
|
+
/* v8 ignore next -- size > cap guarantees one entry */
|
|
301
|
+
if (oldest.done === true) break;
|
|
302
|
+
session.decided.delete(oldest.value);
|
|
303
|
+
}
|
|
304
|
+
if (session.closed) return;
|
|
305
|
+
session.socket.write(encodeDecisionResponse({ id, verdict, reason }));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private endSession(session: ControlSession): void {
|
|
309
|
+
if (session.closed) return;
|
|
310
|
+
session.closed = true;
|
|
311
|
+
clearTimeout(session.handshakeTimer);
|
|
312
|
+
for (const pending of session.active.values()) clearTimeout(pending.timer);
|
|
313
|
+
session.active.clear();
|
|
314
|
+
session.socket.destroy();
|
|
315
|
+
this.session = undefined;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function normalizeRunId(runId: bigint): bigint {
|
|
320
|
+
if (runId < 0n || runId > 0xffff_ffff_ffff_ffffn) {
|
|
321
|
+
throw new FsError("relay control runId must be an unsigned 64-bit integer.", "invalid");
|
|
322
|
+
}
|
|
323
|
+
return runId;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function integerOption(
|
|
327
|
+
value: number | undefined,
|
|
328
|
+
fallback: number,
|
|
329
|
+
min: number,
|
|
330
|
+
max: number,
|
|
331
|
+
name: string,
|
|
332
|
+
): number {
|
|
333
|
+
if (value === undefined) return fallback;
|
|
334
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
335
|
+
throw new FsError(`relay control ${name} must be an integer between ${min} and ${max}.`, "invalid");
|
|
336
|
+
}
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function denyReason(error: unknown): string {
|
|
341
|
+
const text = errorMessage(error).trim();
|
|
342
|
+
return text.length === 0 ? "permission denied" : text;
|
|
343
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wire format shared with procage's relay control client.
|
|
8
|
+
*
|
|
9
|
+
* Every frame is a 4-byte big-endian payload length followed by the payload.
|
|
10
|
+
* Payloads start with a one-byte message type. All integers are big-endian.
|
|
11
|
+
*
|
|
12
|
+
* HELLO (3) u8 type, u8 version, u64 run_id, u32 pid
|
|
13
|
+
* HELLO_ACK (4) u8 type, u8 version, u32 protocol_version,
|
|
14
|
+
* u32 max_inflight, u64 run_id
|
|
15
|
+
* DECIDE_REQ (1) u8 type, u64 id, u16 resource_len, resource UTF-8,
|
|
16
|
+
* u16 port, u8 proto (1 TCP, 2 UDP)
|
|
17
|
+
* DECIDE_RESP (2) u8 type, u64 id, u8 verdict (0 deny, 1 allow),
|
|
18
|
+
* u16 reason_len, reason UTF-8
|
|
19
|
+
*
|
|
20
|
+
* A payload is at most 4096 bytes. The resource is the only network detail
|
|
21
|
+
* Node sees: a DNS QNAME, TLS SNI, HTTP Host, QUIC SNI, or an IP literal.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const RELAY_DECIDE_REQUEST = 1;
|
|
25
|
+
export const RELAY_DECIDE_RESPONSE = 2;
|
|
26
|
+
export const RELAY_HELLO = 3;
|
|
27
|
+
export const RELAY_HELLO_ACK = 4;
|
|
28
|
+
|
|
29
|
+
/** Envelope version carried by HELLO and HELLO_ACK. */
|
|
30
|
+
export const RELAY_MESSAGE_VERSION = 1;
|
|
31
|
+
/** Semantic protocol version announced by HELLO_ACK. */
|
|
32
|
+
export const RELAY_PROTOCOL_VERSION = 1;
|
|
33
|
+
export const RELAY_MAX_FRAME = 4096;
|
|
34
|
+
/**
|
|
35
|
+
* The relay never names a resource longer than this: a DNS name is at most
|
|
36
|
+
* 253 bytes and an IP literal is far shorter. procage enforces the same bound
|
|
37
|
+
* before it asks; the server rejects an oversized request instead of
|
|
38
|
+
* answering a resource no permission rule can cover.
|
|
39
|
+
*/
|
|
40
|
+
export const RELAY_MAX_RESOURCE = 255;
|
|
41
|
+
/** Reasons are diagnostic only and are truncated before they reach the wire. */
|
|
42
|
+
export const RELAY_MAX_REASON_BYTES = 512;
|
|
43
|
+
|
|
44
|
+
export const RELAY_PROTO_TCP = 1;
|
|
45
|
+
export const RELAY_PROTO_UDP = 2;
|
|
46
|
+
|
|
47
|
+
export type RelayProto = typeof RELAY_PROTO_TCP | typeof RELAY_PROTO_UDP;
|
|
48
|
+
export type RelayVerdict = 0 | 1;
|
|
49
|
+
|
|
50
|
+
export interface RelayHello {
|
|
51
|
+
readonly version: number;
|
|
52
|
+
readonly runId: bigint;
|
|
53
|
+
readonly pid: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface RelayDecisionRequest {
|
|
57
|
+
readonly id: bigint;
|
|
58
|
+
readonly resource: string;
|
|
59
|
+
readonly port: number;
|
|
60
|
+
readonly proto: RelayProto;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RelayDecisionResponse {
|
|
64
|
+
readonly id: bigint;
|
|
65
|
+
readonly verdict: RelayVerdict;
|
|
66
|
+
readonly reason: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A malformed or unsupported frame; the connection is closed. */
|
|
70
|
+
export class RelayProtocolError extends Error {
|
|
71
|
+
constructor(message: string) {
|
|
72
|
+
super(message);
|
|
73
|
+
this.name = "RelayProtocolError";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const UTF8 = new TextDecoder("utf-8", { fatal: true });
|
|
78
|
+
const FORBIDDEN_RESOURCE = /[\u0000-\u0020\u007f]/;
|
|
79
|
+
|
|
80
|
+
/** Wrap one payload in the length prefix. */
|
|
81
|
+
export function encodeFrame(payload: Buffer): Buffer {
|
|
82
|
+
if (payload.length === 0 || payload.length > RELAY_MAX_FRAME) {
|
|
83
|
+
throw new RelayProtocolError(`relay frame length out of range: ${payload.length}`);
|
|
84
|
+
}
|
|
85
|
+
const frame = Buffer.allocUnsafe(4 + payload.length);
|
|
86
|
+
frame.writeUInt32BE(payload.length, 0);
|
|
87
|
+
payload.copy(frame, 4);
|
|
88
|
+
return frame;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Read and validate the length prefix from a complete 4-byte header. */
|
|
92
|
+
export function readFrameLength(header: Buffer): number {
|
|
93
|
+
if (header.length < 4) {
|
|
94
|
+
throw new RelayProtocolError("relay frame header is incomplete");
|
|
95
|
+
}
|
|
96
|
+
const length = header.readUInt32BE(0);
|
|
97
|
+
if (length === 0 || length > RELAY_MAX_FRAME) {
|
|
98
|
+
throw new RelayProtocolError(`relay frame length out of range: ${length}`);
|
|
99
|
+
}
|
|
100
|
+
return length;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Incremental frame parser. A socket may deliver a header or payload in any
|
|
105
|
+
* number of chunks, and several frames may arrive in one chunk.
|
|
106
|
+
*/
|
|
107
|
+
export class RelayFrameReader {
|
|
108
|
+
private buffer: Buffer = Buffer.alloc(0);
|
|
109
|
+
|
|
110
|
+
push(chunk: Buffer): Buffer[] {
|
|
111
|
+
this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
|
|
112
|
+
const frames: Buffer[] = [];
|
|
113
|
+
for (;;) {
|
|
114
|
+
if (this.buffer.length < 4) break;
|
|
115
|
+
const length = readFrameLength(this.buffer.subarray(0, 4));
|
|
116
|
+
if (this.buffer.length < length + 4) break;
|
|
117
|
+
frames.push(this.buffer.subarray(4, length + 4));
|
|
118
|
+
this.buffer = this.buffer.subarray(length + 4);
|
|
119
|
+
}
|
|
120
|
+
return frames;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function decodeHello(payload: Buffer): RelayHello {
|
|
125
|
+
assertType(payload, RELAY_HELLO, "HELLO");
|
|
126
|
+
if (payload.length !== 14) {
|
|
127
|
+
throw new RelayProtocolError(`HELLO payload length is ${payload.length}, expected 14`);
|
|
128
|
+
}
|
|
129
|
+
const version = payload.readUInt8(1);
|
|
130
|
+
if (version !== RELAY_MESSAGE_VERSION) {
|
|
131
|
+
throw new RelayProtocolError(`unsupported relay message version: ${version}`);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
version,
|
|
135
|
+
runId: payload.readBigUInt64BE(2),
|
|
136
|
+
pid: payload.readUInt32BE(10),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function encodeHelloAck(runId: bigint, maxInflight: number): Buffer {
|
|
141
|
+
const payload = Buffer.allocUnsafe(18);
|
|
142
|
+
payload.writeUInt8(RELAY_HELLO_ACK, 0);
|
|
143
|
+
payload.writeUInt8(RELAY_MESSAGE_VERSION, 1);
|
|
144
|
+
payload.writeUInt32BE(RELAY_PROTOCOL_VERSION, 2);
|
|
145
|
+
payload.writeUInt32BE(maxInflight, 6);
|
|
146
|
+
payload.writeBigUInt64BE(runId, 10);
|
|
147
|
+
return encodeFrame(payload);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function decodeDecisionRequest(payload: Buffer): RelayDecisionRequest {
|
|
151
|
+
assertType(payload, RELAY_DECIDE_REQUEST, "DECIDE_REQ");
|
|
152
|
+
if (payload.length < 14) {
|
|
153
|
+
throw new RelayProtocolError(`DECIDE_REQ payload length is ${payload.length}, expected at least 14`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const id = payload.readBigUInt64BE(1);
|
|
157
|
+
const resourceLength = payload.readUInt16BE(9);
|
|
158
|
+
if (resourceLength === 0 || resourceLength > RELAY_MAX_RESOURCE) {
|
|
159
|
+
throw new RelayProtocolError(`DECIDE_REQ resource length out of range: ${resourceLength}`);
|
|
160
|
+
}
|
|
161
|
+
const resourceStart = 11;
|
|
162
|
+
const portOffset = resourceStart + resourceLength;
|
|
163
|
+
const protoOffset = portOffset + 2;
|
|
164
|
+
if (payload.length !== protoOffset + 1) {
|
|
165
|
+
throw new RelayProtocolError("DECIDE_REQ length does not match its resource length");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let resource: string;
|
|
169
|
+
try {
|
|
170
|
+
resource = UTF8.decode(payload.subarray(resourceStart, portOffset));
|
|
171
|
+
} catch {
|
|
172
|
+
throw new RelayProtocolError("DECIDE_REQ resource is not valid UTF-8");
|
|
173
|
+
}
|
|
174
|
+
if (FORBIDDEN_RESOURCE.test(resource)) {
|
|
175
|
+
throw new RelayProtocolError("DECIDE_REQ resource contains whitespace or control characters");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const port = payload.readUInt16BE(portOffset);
|
|
179
|
+
if (port === 0) {
|
|
180
|
+
throw new RelayProtocolError("DECIDE_REQ port is zero");
|
|
181
|
+
}
|
|
182
|
+
const proto = payload.readUInt8(protoOffset);
|
|
183
|
+
if (proto !== RELAY_PROTO_TCP && proto !== RELAY_PROTO_UDP) {
|
|
184
|
+
throw new RelayProtocolError(`unsupported DECIDE_REQ protocol: ${proto}`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { id, resource, port, proto };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function encodeDecisionRequest(request: RelayDecisionRequest): Buffer {
|
|
191
|
+
const resource = Buffer.from(request.resource, "utf8");
|
|
192
|
+
if (resource.length === 0 || resource.length > RELAY_MAX_RESOURCE) {
|
|
193
|
+
throw new RelayProtocolError(`DECIDE_REQ resource length out of range: ${resource.length}`);
|
|
194
|
+
}
|
|
195
|
+
const payload = Buffer.allocUnsafe(14 + resource.length);
|
|
196
|
+
payload.writeUInt8(RELAY_DECIDE_REQUEST, 0);
|
|
197
|
+
payload.writeBigUInt64BE(request.id, 1);
|
|
198
|
+
payload.writeUInt16BE(resource.length, 9);
|
|
199
|
+
resource.copy(payload, 11);
|
|
200
|
+
payload.writeUInt16BE(request.port, 11 + resource.length);
|
|
201
|
+
payload.writeUInt8(request.proto, 13 + resource.length);
|
|
202
|
+
return encodeFrame(payload);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function encodeDecisionResponse(response: RelayDecisionResponse): Buffer {
|
|
206
|
+
const reason = truncateUtf8(response.reason, RELAY_MAX_REASON_BYTES);
|
|
207
|
+
const payload = Buffer.allocUnsafe(12 + reason.length);
|
|
208
|
+
payload.writeUInt8(RELAY_DECIDE_RESPONSE, 0);
|
|
209
|
+
payload.writeBigUInt64BE(response.id, 1);
|
|
210
|
+
payload.writeUInt8(response.verdict, 9);
|
|
211
|
+
payload.writeUInt16BE(reason.length, 10);
|
|
212
|
+
reason.copy(payload, 12);
|
|
213
|
+
return encodeFrame(payload);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function assertType(payload: Buffer, expected: number, name: string): void {
|
|
217
|
+
if (payload.length === 0 || payload.readUInt8(0) !== expected) {
|
|
218
|
+
throw new RelayProtocolError(`${name} payload has an unexpected message type`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Truncate on a UTF-8 character boundary, never in the middle of a code point. */
|
|
223
|
+
function truncateUtf8(text: string, maxBytes: number): Buffer {
|
|
224
|
+
const bytes = Buffer.from(text, "utf8");
|
|
225
|
+
if (bytes.length <= maxBytes) return bytes;
|
|
226
|
+
let end = maxBytes;
|
|
227
|
+
while ((bytes[end]! & 0xc0) === 0x80) end--;
|
|
228
|
+
return bytes.subarray(0, end);
|
|
229
|
+
}
|
package/src/core/types.ts
CHANGED
|
@@ -30,9 +30,6 @@ export interface RunContext {
|
|
|
30
30
|
readonly concurrency: number;
|
|
31
31
|
readonly signal: AbortSignal;
|
|
32
32
|
readonly tmpdir: string;
|
|
33
|
-
readonly execAllowlist: readonly string[];
|
|
34
|
-
/** `proxy` forces egress through the run's allowlist proxy; `ip` is all-IP. */
|
|
35
|
-
readonly execNetworkMode: "proxy" | "ip";
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
export interface ToolSpec<I> {
|