@cotal-ai/core 0.168.0 → 0.169.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/dist/agent-file.d.ts.map +1 -1
- package/dist/agent-file.js +12 -9
- package/dist/agent-file.js.map +1 -1
- package/dist/connector-setup.d.ts +2 -0
- package/dist/connector-setup.d.ts.map +1 -1
- package/dist/connector.d.ts +8 -11
- package/dist/connector.d.ts.map +1 -1
- package/dist/control-exchange.d.ts +114 -0
- package/dist/control-exchange.d.ts.map +1 -0
- package/dist/control-exchange.js +416 -0
- package/dist/control-exchange.js.map +1 -0
- package/dist/endpoint-call.d.ts +2 -1
- package/dist/endpoint-call.d.ts.map +1 -1
- package/dist/endpoint-call.js +14 -7
- package/dist/endpoint-call.js.map +1 -1
- package/dist/endpoint-delivery-plane.d.ts +337 -0
- package/dist/endpoint-delivery-plane.d.ts.map +1 -0
- package/dist/endpoint-delivery-plane.js +1291 -0
- package/dist/endpoint-delivery-plane.js.map +1 -0
- package/dist/endpoint-envelope.d.ts +2 -1
- package/dist/endpoint-envelope.d.ts.map +1 -1
- package/dist/endpoint-envelope.js +9 -3
- package/dist/endpoint-envelope.js.map +1 -1
- package/dist/endpoint-history.d.ts +370 -0
- package/dist/endpoint-history.d.ts.map +1 -0
- package/dist/endpoint-history.js +969 -0
- package/dist/endpoint-history.js.map +1 -0
- package/dist/endpoint-inbound.d.ts +10 -0
- package/dist/endpoint-inbound.d.ts.map +1 -0
- package/dist/endpoint-inbound.js +28 -0
- package/dist/endpoint-inbound.js.map +1 -0
- package/dist/endpoint-serve.d.ts +11 -1
- package/dist/endpoint-serve.d.ts.map +1 -1
- package/dist/endpoint-serve.js +42 -5
- package/dist/endpoint-serve.js.map +1 -1
- package/dist/endpoint-subjects.d.ts +3 -0
- package/dist/endpoint-subjects.d.ts.map +1 -1
- package/dist/endpoint-subjects.js +3 -0
- package/dist/endpoint-subjects.js.map +1 -1
- package/dist/endpoint.d.ts +64 -253
- package/dist/endpoint.d.ts.map +1 -1
- package/dist/endpoint.js +177 -2015
- package/dist/endpoint.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/launch-material.d.ts +9 -3
- package/dist/launch-material.d.ts.map +1 -1
- package/dist/launch-material.js +22 -2
- package/dist/launch-material.js.map +1 -1
- package/dist/provision-permissions.d.ts.map +1 -1
- package/dist/provision-permissions.js +7 -1
- package/dist/provision-permissions.js.map +1 -1
- package/dist/remote-manager-authority.d.ts +1 -1
- package/dist/run-journal.d.ts +2 -0
- package/dist/run-journal.d.ts.map +1 -1
- package/dist/run-journal.js +1 -1
- package/dist/run-journal.js.map +1 -1
- package/package.json +5 -1
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The local control exchange: how a connector's control server and each of its clients (the
|
|
3
|
+
* lifecycle hook relay, the Manager's stop and session calls) prove to each other that both hold the
|
|
4
|
+
* endpoint's key. Neither side ever sends the key.
|
|
5
|
+
*
|
|
6
|
+
* A client used to send the key in its first frame and print whatever came back. A stand-in that
|
|
7
|
+
* took the name of a server that had exited (another local user on a shared `/tmp`, or any process
|
|
8
|
+
* on the Windows pipe namespace) received the key and answered the hook. With remote permission
|
|
9
|
+
* answers, a hook's reply can be "allow this tool call". So the exchange proves both ends:
|
|
10
|
+
*
|
|
11
|
+
* C → S {"v":1,"nonce":"<Nc>"}\n
|
|
12
|
+
* S → C {"v":1,"nonce":"<Ns>","mac":"<Ms>"}\n
|
|
13
|
+
* C → S <Mc> <request JSON>\n
|
|
14
|
+
* S → C <Mr> <reply JSON>\n
|
|
15
|
+
* C → S <Mk>\n (only when the request asked for a delivery receipt)
|
|
16
|
+
*
|
|
17
|
+
* `Nc` and `Ns` are 32 fresh random bytes each, base64url without padding (43 characters). Every
|
|
18
|
+
* MAC is HMAC-SHA256 keyed with the UTF-8 bytes of the endpoint key, base64url without padding (43
|
|
19
|
+
* characters from `A-Z a-z 0-9 - _`), over these exact bytes (`\0` is one zero byte; the request and
|
|
20
|
+
* reply are the bytes between the single space and the newline, and must be valid UTF-8):
|
|
21
|
+
*
|
|
22
|
+
* Ms = HMAC(K, "cotal-control/1\0server\0" Nc "\0" Ns)
|
|
23
|
+
* Mc = HMAC(K, "cotal-control/1\0client\0" Nc "\0" Ns "\0" request)
|
|
24
|
+
* Mr = HMAC(K, "cotal-control/1\0reply\0" Nc "\0" Ns "\0" reply)
|
|
25
|
+
* Mk = HMAC(K, "cotal-control/1\0receipt\0" Nc "\0" Ns "\0" reply)
|
|
26
|
+
*
|
|
27
|
+
* The role word keeps one side's MAC from being reflected as the other's, and both nonces make each
|
|
28
|
+
* MAC good for one connection only. `Mk` binds the receipt to the reply it confirms, so a process
|
|
29
|
+
* that forwards the connection without the key cannot confirm a reply it dropped. MACs are compared
|
|
30
|
+
* in constant time, after the presented one is checked against the alphabet and length above. The
|
|
31
|
+
* exchange authenticates both ends, the request, the reply and the receipt; it does not encrypt.
|
|
32
|
+
*
|
|
33
|
+
* Every line is bounded in bytes, complete or not, before it is parsed or a MAC is computed over
|
|
34
|
+
* it: the hello and the challenge at 1 KiB, a request's JSON at {@link MAX_CONTROL_REQUEST_BYTES},
|
|
35
|
+
* a reply's JSON at the caller's `maxReplyBytes`. The 44 bytes of MAC and space before a request or
|
|
36
|
+
* reply do not count against its bound.
|
|
37
|
+
*
|
|
38
|
+
* The version travels with the endpoint and its key (the launch material, `LaunchSpec.control`), and
|
|
39
|
+
* a build speaks exactly one. A client and server of different versions refuse each other: the hello
|
|
40
|
+
* and the challenge each carry `v`, a mismatch drops the connection, and the version is inside every
|
|
41
|
+
* MAC. Nothing is negotiated on the wire and there is no fallback.
|
|
42
|
+
*
|
|
43
|
+
* Off Windows the socket also sits in a directory only its user can enter, made fresh for each
|
|
44
|
+
* launch under an unguessable name, and checked for owner and mode by the server before it binds and
|
|
45
|
+
* by every client before it connects, so another user cannot create a name there at all. On Windows
|
|
46
|
+
* the pipe namespace is global, and the exchange is the boundary.
|
|
47
|
+
*/
|
|
48
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
49
|
+
import { lstatSync } from "node:fs";
|
|
50
|
+
import { connect } from "node:net";
|
|
51
|
+
import { dirname } from "node:path";
|
|
52
|
+
/** The one exchange version this build speaks. */
|
|
53
|
+
export const CONTROL_PROTOCOL_VERSION = 1;
|
|
54
|
+
/** How long a server keeps a connection that has not yet proved it holds the key: from accept until
|
|
55
|
+
* a request with a good MAC has arrived. A silent or slow client is dropped here, so a local process
|
|
56
|
+
* cannot camp on a finite pipe instance. */
|
|
57
|
+
export const CONTROL_SERVER_AUTH_DEADLINE_MS = 5_000;
|
|
58
|
+
/** How long a client waits, from connect, for the server to prove it holds the key. Separate from
|
|
59
|
+
* the server's deadline and shorter than any caller's reply budget, so a stand-in that says nothing
|
|
60
|
+
* cannot hold a hook for its whole budget. */
|
|
61
|
+
export const CONTROL_CLIENT_PROOF_DEADLINE_MS = 1_000;
|
|
62
|
+
/** Cap on the hello and the challenge, which are a version and a nonce or two. */
|
|
63
|
+
const MAX_HANDSHAKE_LINE_BYTES = 1024;
|
|
64
|
+
/** Cap on a request's JSON. A request is one small lifecycle event or an op. */
|
|
65
|
+
export const MAX_CONTROL_REQUEST_BYTES = 1 << 20;
|
|
66
|
+
const BASE64URL_43 = /^[A-Za-z0-9_-]{43}$/;
|
|
67
|
+
const MAC_CHARS = 43;
|
|
68
|
+
/** The MAC and the single space before a request or reply body. */
|
|
69
|
+
const MAC_PREFIX_BYTES = MAC_CHARS + 1;
|
|
70
|
+
const NEWLINE = 0x0a;
|
|
71
|
+
const SPACE = 0x20;
|
|
72
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true });
|
|
73
|
+
/** Throw unless `version` is the one this build speaks. `where` names the carrier for the message. */
|
|
74
|
+
export function assertControlVersion(version, where) {
|
|
75
|
+
if (version === CONTROL_PROTOCOL_VERSION)
|
|
76
|
+
return;
|
|
77
|
+
throw new Error(`${where} carries control protocol version ${JSON.stringify(version)}, and this build speaks only ` +
|
|
78
|
+
`version ${CONTROL_PROTOCOL_VERSION}. A launcher and connector from different releases refuse each ` +
|
|
79
|
+
`other instead of falling back; upgrade both to the same release.`);
|
|
80
|
+
}
|
|
81
|
+
/** Refuse a reply cap that bounds nothing. The type requires one, but a JavaScript caller can omit it
|
|
82
|
+
* or pass `NaN` or `Infinity`, and every length comparison against those lets the reply through. */
|
|
83
|
+
function assertReplyCap(opts) {
|
|
84
|
+
const cap = opts?.maxReplyBytes;
|
|
85
|
+
if (typeof cap !== "number" || !Number.isSafeInteger(cap) || cap < 0)
|
|
86
|
+
throw new Error(`maxReplyBytes must be a finite, non-negative whole number of bytes, not ${typeof cap === "number" ? cap : typeof cap}`);
|
|
87
|
+
}
|
|
88
|
+
/** Refuse an endpoint this build cannot speak to, before any socket is opened or bound. */
|
|
89
|
+
export function assertControlEndpoint(endpoint) {
|
|
90
|
+
assertControlVersion(endpoint.version, "the control endpoint");
|
|
91
|
+
if (typeof endpoint.token !== "string" || !endpoint.token)
|
|
92
|
+
throw new Error("the control endpoint has no key");
|
|
93
|
+
}
|
|
94
|
+
function transcriptMac(token, role, nc, ns, body) {
|
|
95
|
+
const mac = createHmac("sha256", Buffer.from(token, "utf8"));
|
|
96
|
+
mac.update(`cotal-control/${CONTROL_PROTOCOL_VERSION}\0${role}\0${nc}\0${ns}`, "utf8");
|
|
97
|
+
if (body !== undefined)
|
|
98
|
+
mac.update("\0", "utf8").update(body);
|
|
99
|
+
return mac.digest("base64url");
|
|
100
|
+
}
|
|
101
|
+
/** Constant-time match of a presented MAC against the expected one. The presented MAC must be 43
|
|
102
|
+
* characters of the base64url alphabet first, so no second spelling of the same bytes passes. */
|
|
103
|
+
function macMatches(presented, expected) {
|
|
104
|
+
if (typeof presented !== "string" || !BASE64URL_43.test(presented))
|
|
105
|
+
return false;
|
|
106
|
+
return timingSafeEqual(Buffer.from(presented, "ascii"), Buffer.from(expected, "ascii"));
|
|
107
|
+
}
|
|
108
|
+
/** Split `<MAC> <body>` and verify the MAC over the body's exact bytes. Returns the body as text, or
|
|
109
|
+
* undefined when the shape, the MAC or the UTF-8 is wrong. */
|
|
110
|
+
function openSealed(line, expected) {
|
|
111
|
+
if (line.length < MAC_PREFIX_BYTES || line[MAC_CHARS] !== SPACE)
|
|
112
|
+
return undefined;
|
|
113
|
+
const body = line.subarray(MAC_PREFIX_BYTES);
|
|
114
|
+
if (!macMatches(line.subarray(0, MAC_CHARS).toString("latin1"), expected(body)))
|
|
115
|
+
return undefined;
|
|
116
|
+
try {
|
|
117
|
+
return utf8.decode(body);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Parse a handshake line as a JSON object, or undefined when it is not valid UTF-8 JSON. */
|
|
124
|
+
function parseHandshake(line) {
|
|
125
|
+
try {
|
|
126
|
+
const value = JSON.parse(utf8.decode(line));
|
|
127
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Feed a byte stream in, get newline-terminated lines out. `cap()` is the byte bound of the line
|
|
135
|
+
* being read now, MAC included; a line longer than that, complete or not, calls `oversize` instead.
|
|
136
|
+
* `onLine` returns false to stop reading.
|
|
137
|
+
*/
|
|
138
|
+
function lineReader(cap, onLine, oversize) {
|
|
139
|
+
let buf = Buffer.alloc(0);
|
|
140
|
+
let stopped = false;
|
|
141
|
+
return (chunk) => {
|
|
142
|
+
if (stopped)
|
|
143
|
+
return;
|
|
144
|
+
buf = buf.length ? Buffer.concat([buf, chunk]) : chunk;
|
|
145
|
+
for (;;) {
|
|
146
|
+
const limit = cap();
|
|
147
|
+
const nl = buf.indexOf(NEWLINE);
|
|
148
|
+
const length = nl < 0 ? buf.length : nl;
|
|
149
|
+
if (length > limit) {
|
|
150
|
+
stopped = true;
|
|
151
|
+
return oversize();
|
|
152
|
+
}
|
|
153
|
+
if (nl < 0)
|
|
154
|
+
return;
|
|
155
|
+
const line = buf.subarray(0, nl);
|
|
156
|
+
buf = buf.subarray(nl + 1);
|
|
157
|
+
if (!onLine(line)) {
|
|
158
|
+
stopped = true;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const nonce = () => randomBytes(32).toString("base64url");
|
|
165
|
+
/** The directory a POSIX control socket lives in, or undefined where no directory applies. */
|
|
166
|
+
function socketDir(path) {
|
|
167
|
+
return process.platform === "win32" ? undefined : dirname(path);
|
|
168
|
+
}
|
|
169
|
+
/** Throw unless `dir` is a real directory owned by this user and closed to group and others. The
|
|
170
|
+
* message names what is wrong and how to recover. */
|
|
171
|
+
function assertPrivateDir(dir) {
|
|
172
|
+
const st = lstatSync(dir);
|
|
173
|
+
const uid = process.getuid();
|
|
174
|
+
const problem = st.isSymbolicLink()
|
|
175
|
+
? "is a symbolic link, not a directory"
|
|
176
|
+
: !st.isDirectory()
|
|
177
|
+
? "is not a directory"
|
|
178
|
+
: st.uid !== uid
|
|
179
|
+
? `is owned by uid ${st.uid}, not by this user (uid ${uid})`
|
|
180
|
+
: (st.mode & 0o077) !== 0
|
|
181
|
+
? `has mode ${(st.mode & 0o7777).toString(8)}, which lets group or others in`
|
|
182
|
+
: undefined;
|
|
183
|
+
if (problem)
|
|
184
|
+
throw new Error(`control socket directory ${dir} ${problem}, so it is not a private directory of this user. ` +
|
|
185
|
+
`Remove it if you did not create it, or point TMPDIR at a directory only you can write, and relaunch.`);
|
|
186
|
+
}
|
|
187
|
+
/** Check the directory a control socket is about to be bound in: it must already exist, as a real
|
|
188
|
+
* directory owned by this user and closed to group and others. A server calls this before it binds.
|
|
189
|
+
* It never makes the directory, because by then the path is public: the launcher makes it when it
|
|
190
|
+
* mints the path, and a hand-driven path must name a directory its user made. No-op on Windows. */
|
|
191
|
+
export function assertControlSocketDir(path) {
|
|
192
|
+
if (path.startsWith("\0"))
|
|
193
|
+
throw new Error("control socket path is in the Linux abstract namespace, which any local user can bind; use a path in a private directory");
|
|
194
|
+
const dir = socketDir(path);
|
|
195
|
+
if (!dir)
|
|
196
|
+
return;
|
|
197
|
+
try {
|
|
198
|
+
assertPrivateDir(dir);
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
if (e.code !== "ENOENT")
|
|
202
|
+
throw e;
|
|
203
|
+
throw new Error(`control socket directory ${dir} does not exist. The launcher makes it when it mints the endpoint; ` +
|
|
204
|
+
`for a hand-driven COTAL_CONTROL_SOCKET, make it yourself with mode 0700.`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** Check the directory a control socket lives in before connecting to it. Every client calls this
|
|
208
|
+
* right before each dial. Always ok on Windows, where the exchange is the only boundary. */
|
|
209
|
+
export function checkControlSocketDir(path) {
|
|
210
|
+
if (path.startsWith("\0"))
|
|
211
|
+
return { ok: false, absent: false, reason: "the path is in the Linux abstract namespace, which any local user can bind" };
|
|
212
|
+
const dir = socketDir(path);
|
|
213
|
+
if (!dir)
|
|
214
|
+
return { ok: true };
|
|
215
|
+
try {
|
|
216
|
+
assertPrivateDir(dir);
|
|
217
|
+
return { ok: true };
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
const code = e.code;
|
|
221
|
+
return { ok: false, absent: code === "ENOENT", reason: e.message };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The client half, on a socket that has just connected. Sends the hello, waits up to
|
|
226
|
+
* {@link CONTROL_CLIENT_PROOF_DEADLINE_MS} for the server's proof, sends the MAC'd `request`, and
|
|
227
|
+
* calls `done` with the reply's JSON text once its MAC verifies, plus the receipt line (`<Mk>\n`) a
|
|
228
|
+
* caller writes back when it asked for one and the reply has reached its own output. Every caller
|
|
229
|
+
* bounds the reply: `maxReplyBytes` caps its JSON in bytes, complete or not, and a cap that is not a
|
|
230
|
+
* finite, non-negative whole number throws before anything is sent. `onRequestSent` runs once
|
|
231
|
+
* the request has been written, so a caller whose own deadline or socket error ends the exchange
|
|
232
|
+
* later can say the server may have handled it.
|
|
233
|
+
*
|
|
234
|
+
* Any failure (a wrong version, a bad MAC or nonce, a malformed or oversized line, the proof
|
|
235
|
+
* deadline, the connection closing first) destroys the socket and calls `done` with the reason.
|
|
236
|
+
* Nothing from an unproven server is ever handed back. On success the socket stays open, so a caller
|
|
237
|
+
* can send its receipt; the caller owns it from there, and owns the overall reply deadline.
|
|
238
|
+
*/
|
|
239
|
+
export function clientExchange(sock, endpoint, request, done, opts) {
|
|
240
|
+
assertReplyCap(opts);
|
|
241
|
+
assertControlEndpoint(endpoint);
|
|
242
|
+
const nc = nonce();
|
|
243
|
+
let ns;
|
|
244
|
+
let requestSent = false;
|
|
245
|
+
let settled = false;
|
|
246
|
+
const finish = (result) => {
|
|
247
|
+
if (settled)
|
|
248
|
+
return;
|
|
249
|
+
settled = true;
|
|
250
|
+
clearTimeout(proof);
|
|
251
|
+
sock.off("data", onData);
|
|
252
|
+
sock.off("end", onEnd);
|
|
253
|
+
sock.off("close", onEnd);
|
|
254
|
+
if (!result.ok)
|
|
255
|
+
sock.destroy();
|
|
256
|
+
done(result);
|
|
257
|
+
};
|
|
258
|
+
const refuse = (reason) => (finish({ ok: false, reason, requestSent }), false);
|
|
259
|
+
const proof = setTimeout(() => refuse("the server did not prove it holds the key in time"), opts.proofDeadlineMs ?? CONTROL_CLIENT_PROOF_DEADLINE_MS);
|
|
260
|
+
proof.unref?.();
|
|
261
|
+
const onEnd = () => void refuse("the connection closed before a proven reply");
|
|
262
|
+
const onLine = (line) => {
|
|
263
|
+
if (ns === undefined) {
|
|
264
|
+
const challenge = parseHandshake(line);
|
|
265
|
+
if (!challenge)
|
|
266
|
+
return refuse("the server's challenge is not JSON");
|
|
267
|
+
if (challenge.v !== CONTROL_PROTOCOL_VERSION)
|
|
268
|
+
return refuse(`the server speaks control protocol ${JSON.stringify(challenge.v)}, not ${CONTROL_PROTOCOL_VERSION}`);
|
|
269
|
+
if (typeof challenge.nonce !== "string" || !BASE64URL_43.test(challenge.nonce))
|
|
270
|
+
return refuse("the server's nonce is malformed");
|
|
271
|
+
if (!macMatches(challenge.mac, transcriptMac(endpoint.token, "server", nc, challenge.nonce)))
|
|
272
|
+
return refuse("the server did not prove it holds the key");
|
|
273
|
+
ns = challenge.nonce;
|
|
274
|
+
clearTimeout(proof);
|
|
275
|
+
const body = Buffer.from(JSON.stringify(request), "utf8");
|
|
276
|
+
try {
|
|
277
|
+
requestSent = true;
|
|
278
|
+
sock.write(Buffer.concat([Buffer.from(`${transcriptMac(endpoint.token, "client", nc, ns, body)} `, "ascii"), body, Buffer.from("\n")]));
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return refuse("the connection closed before the request was sent");
|
|
282
|
+
}
|
|
283
|
+
opts.onRequestSent?.();
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
const proven = ns;
|
|
287
|
+
const reply = openSealed(line, (body) => transcriptMac(endpoint.token, "reply", nc, proven, body));
|
|
288
|
+
if (reply === undefined)
|
|
289
|
+
return refuse("the reply's MAC does not verify");
|
|
290
|
+
const receipt = `${transcriptMac(endpoint.token, "receipt", nc, proven, line.subarray(MAC_PREFIX_BYTES))}\n`;
|
|
291
|
+
finish({ ok: true, reply, receipt });
|
|
292
|
+
return false;
|
|
293
|
+
};
|
|
294
|
+
const onData = lineReader(() => (ns === undefined ? MAX_HANDSHAKE_LINE_BYTES : MAC_PREFIX_BYTES + opts.maxReplyBytes), onLine, () => void refuse(ns === undefined ? "the server's challenge is oversized" : "the reply is oversized"));
|
|
295
|
+
sock.on("data", onData);
|
|
296
|
+
sock.on("end", onEnd);
|
|
297
|
+
sock.on("close", onEnd);
|
|
298
|
+
try {
|
|
299
|
+
sock.write(JSON.stringify({ v: CONTROL_PROTOCOL_VERSION, nonce: nc }) + "\n");
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
refuse("the connection closed before the hello was sent");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* The server half, on a freshly accepted socket. Answers the hello with the server's proof, then
|
|
307
|
+
* verifies the client's MAC'd request and calls `onRequest`. The socket's listeners are detached
|
|
308
|
+
* first, so the caller reads the connection from there on, as raw bytes.
|
|
309
|
+
*
|
|
310
|
+
* Anything else is dropped before `onRequest` runs: a wrong version, a malformed hello, a bad MAC,
|
|
311
|
+
* an oversized line, invalid UTF-8, or no proven request within
|
|
312
|
+
* {@link CONTROL_SERVER_AUTH_DEADLINE_MS} of accept. A dropped connection gets no reply. The deadline
|
|
313
|
+
* is absolute, not idle, so a client dribbling one byte at a time cannot keep resetting it.
|
|
314
|
+
*/
|
|
315
|
+
export function serverExchange(sock, endpoint, onRequest, opts = {}) {
|
|
316
|
+
assertControlEndpoint(endpoint);
|
|
317
|
+
let nc;
|
|
318
|
+
const ns = nonce();
|
|
319
|
+
let settled = false;
|
|
320
|
+
const drop = () => {
|
|
321
|
+
if (!settled) {
|
|
322
|
+
settled = true;
|
|
323
|
+
clearTimeout(deadline);
|
|
324
|
+
sock.destroy();
|
|
325
|
+
}
|
|
326
|
+
return false;
|
|
327
|
+
};
|
|
328
|
+
const deadline = setTimeout(drop, opts.deadlineMs ?? CONTROL_SERVER_AUTH_DEADLINE_MS);
|
|
329
|
+
deadline.unref?.(); // never hold the process open on an unproven connection
|
|
330
|
+
const onClose = () => {
|
|
331
|
+
settled = true;
|
|
332
|
+
clearTimeout(deadline);
|
|
333
|
+
};
|
|
334
|
+
const onLine = (line) => {
|
|
335
|
+
if (settled)
|
|
336
|
+
return false;
|
|
337
|
+
if (nc === undefined) {
|
|
338
|
+
const hello = parseHandshake(line);
|
|
339
|
+
if (!hello || hello.v !== CONTROL_PROTOCOL_VERSION || typeof hello.nonce !== "string" || !BASE64URL_43.test(hello.nonce))
|
|
340
|
+
return drop();
|
|
341
|
+
nc = hello.nonce;
|
|
342
|
+
try {
|
|
343
|
+
sock.write(JSON.stringify({ v: CONTROL_PROTOCOL_VERSION, nonce: ns, mac: transcriptMac(endpoint.token, "server", nc, ns) }) + "\n");
|
|
344
|
+
}
|
|
345
|
+
catch {
|
|
346
|
+
return drop();
|
|
347
|
+
}
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
const proven = nc;
|
|
351
|
+
const request = openSealed(line, (body) => transcriptMac(endpoint.token, "client", proven, ns, body));
|
|
352
|
+
if (request === undefined)
|
|
353
|
+
return drop();
|
|
354
|
+
settled = true;
|
|
355
|
+
clearTimeout(deadline);
|
|
356
|
+
sock.off("data", onData);
|
|
357
|
+
sock.off("close", onClose);
|
|
358
|
+
onRequest({
|
|
359
|
+
request,
|
|
360
|
+
seal: (text) => {
|
|
361
|
+
const body = Buffer.from(text, "utf8");
|
|
362
|
+
const receipt = transcriptMac(endpoint.token, "receipt", proven, ns, body);
|
|
363
|
+
return {
|
|
364
|
+
line: `${transcriptMac(endpoint.token, "reply", proven, ns, body)} ${text}\n`,
|
|
365
|
+
receiptMatches: (presented) => macMatches(presented, receipt),
|
|
366
|
+
};
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
return false;
|
|
370
|
+
};
|
|
371
|
+
const onData = lineReader(() => (nc === undefined ? MAX_HANDSHAKE_LINE_BYTES : MAC_PREFIX_BYTES + MAX_CONTROL_REQUEST_BYTES), onLine, () => void drop());
|
|
372
|
+
sock.on("data", onData);
|
|
373
|
+
sock.on("close", onClose);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* One request and its proven reply over a fresh connection, for a client that needs nothing more
|
|
377
|
+
* (the Manager's stop and session calls). Resolves with the reply's JSON text. Rejects before dialing
|
|
378
|
+
* when `maxReplyBytes` is not a finite, non-negative whole number, and rejects when the socket's
|
|
379
|
+
* directory is not private, the dial fails, the server does not prove itself, the reply's
|
|
380
|
+
* JSON is over `maxReplyBytes`, or nothing proven arrives within `timeoutMs`. The key is never sent.
|
|
381
|
+
*/
|
|
382
|
+
export function controlCall(endpoint, request, opts) {
|
|
383
|
+
return new Promise((resolve, reject) => {
|
|
384
|
+
assertReplyCap(opts);
|
|
385
|
+
assertControlEndpoint(endpoint);
|
|
386
|
+
const dir = checkControlSocketDir(endpoint.path);
|
|
387
|
+
if (!dir.ok)
|
|
388
|
+
return reject(new Error(dir.reason));
|
|
389
|
+
let sock;
|
|
390
|
+
let settled = false;
|
|
391
|
+
const finish = (error, reply) => {
|
|
392
|
+
if (settled)
|
|
393
|
+
return;
|
|
394
|
+
settled = true;
|
|
395
|
+
clearTimeout(timer);
|
|
396
|
+
sock?.destroy();
|
|
397
|
+
if (error)
|
|
398
|
+
reject(error);
|
|
399
|
+
else
|
|
400
|
+
resolve(reply);
|
|
401
|
+
};
|
|
402
|
+
const timer = setTimeout(() => finish(new Error("control call timed out")), opts.timeoutMs);
|
|
403
|
+
timer.unref?.();
|
|
404
|
+
try {
|
|
405
|
+
sock = connect(endpoint.path);
|
|
406
|
+
}
|
|
407
|
+
catch (e) {
|
|
408
|
+
return finish(e instanceof Error ? e : new Error(String(e)));
|
|
409
|
+
}
|
|
410
|
+
sock.on("error", (e) => finish(e));
|
|
411
|
+
sock.on("connect", () => clientExchange(sock, endpoint, request, (result) => (result.ok ? finish(undefined, result.reply) : finish(new Error(result.reason))), {
|
|
412
|
+
maxReplyBytes: opts.maxReplyBytes,
|
|
413
|
+
}));
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
//# sourceMappingURL=control-exchange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"control-exchange.js","sourceRoot":"","sources":["../src/control-exchange.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,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AC;QACD,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CACtB,cAAc,CAAC,IAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;YACrI,aAAa,EAAE,IAAI,CAAC,aAAa;SAClC,CAAC,CACH,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/endpoint-call.d.ts
CHANGED
|
@@ -177,7 +177,8 @@ export declare function buildPrincipalRequest(space: string, to: {
|
|
|
177
177
|
* pinned contract.
|
|
178
178
|
*
|
|
179
179
|
* Application-level failure is the returned reply's `ok: false`. This boundary throws only for its
|
|
180
|
-
* own refusals: invalid args
|
|
180
|
+
* own refusals: invalid args or a `deadlineMs` over {@link EP_PRINCIPAL_MAX_DEADLINE_MS}
|
|
181
|
+
* `bad-request`, `not-executed`, before anything is published; no responder on the sentinel `unavailable`,
|
|
181
182
|
* `not-executed`; a refused publish `permission-denied`, `not-executed` (the caller holds no row);
|
|
182
183
|
* the deadline `deadline-exceeded`, `unknown`; and a reply that does not parse, echo or validate
|
|
183
184
|
* `internal`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-call.d.ts","sourceRoot":"","sources":["../src/endpoint-call.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAO,cAAc,EAAgB,MAAM,yBAAyB,CAAC;AAKjF,OAAO,
|
|
1
|
+
{"version":3,"file":"endpoint-call.d.ts","sourceRoot":"","sources":["../src/endpoint-call.ts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EAAE,kBAAkB,CAAA;CAAE,EAC7D,EAAE,EAAE,QAAQ,EACZ,IAAI,EAAE;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,GAC3B;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAWrE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,eAAe,CACnC,EAAE,EAAE,cAAc,EAClB,KAAK,EAAE,MAAM,EACb,EAAE,EAAE;IAAE,SAAS,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,kBAAkB,CAAA;CAAE,EAC7D,EAAE,EAAE,QAAQ,EACZ,IAAI,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,GACjD,OAAO,CAAC,aAAa,CAAC,CA0BxB;AAID;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,MAAM,CAC1B,EAAE,EAAE,cAAc,EAClB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,OAAO,EACd,EAAE,EAAE,QAAQ,EACZ,IAAI,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAO,GACjC,OAAO,CAAC,IAAI,CAAC,CA4Bf"}
|
package/dist/endpoint-call.js
CHANGED
|
@@ -5,7 +5,7 @@ import { PermissionViolationError } from "@nats-io/nats-core";
|
|
|
5
5
|
import { BROKER_REFUSAL_CAUSE, openPublishDenialWatch } from "./endpoint-publish-denial.js";
|
|
6
6
|
import { carriesStoredReplyProvenance } from "./endpoint-provenance.js";
|
|
7
7
|
import { spacePrefix } from "./subjects.js";
|
|
8
|
-
import { epRequestSubject, parseEpSubject, callerRailTokens, epPlaneTokens, assertIdToken, assertLifecycleToken, assertBoundedOwner, epPrincipalRequestSubject, epPrincipalReplySubject, epPrincipalCallerReplyFilter, epPrincipalNoRespondersSubject, isIssuedCaller } from "./endpoint-subjects.js";
|
|
8
|
+
import { epRequestSubject, parseEpSubject, callerRailTokens, epPlaneTokens, assertIdToken, assertLifecycleToken, assertBoundedOwner, epPrincipalRequestSubject, epPrincipalReplySubject, epPrincipalCallerReplyFilter, epPrincipalNoRespondersSubject, isIssuedCaller, EP_PRINCIPAL_MAX_DEADLINE_MS } from "./endpoint-subjects.js";
|
|
9
9
|
import { EpEnvelopeError, EP_UNBOUND_RESPONDER, EP_UNANSWERED, EP_REGISTRY_READ_FAILED, EP_UNDECLARED_ARG, EP_BIND_REFUSED, bindRefusalMarked, parseEndpointReply, assertArgsValid, assertOutputValid } from "./endpoint-envelope.js";
|
|
10
10
|
const nonce = () => base64url(globalThis.crypto.getRandomValues(new Uint8Array(24))); // 32 tokens of [A-Za-z0-9_-], 192 bits
|
|
11
11
|
function subjectTarget(t) {
|
|
@@ -416,8 +416,8 @@ export async function epCall(nc, space, route, op, opts) {
|
|
|
416
416
|
* JetStream read, such as a Direct Get, delivers one on any subject its requester names.
|
|
417
417
|
* With `accepts`, a message on a reply subject it refuses does not count either, and the wait goes
|
|
418
418
|
* on. The broker's no-responders status counts only on the sentinel, which no responder holds a
|
|
419
|
-
* publish grant for: there it is `unavailable`, `not-executed
|
|
420
|
-
* responder frame and takes the ordinary reply path. A refused publish is `permission-denied`
|
|
419
|
+
* publish grant for: there it is `unavailable`, `not-executed`, and any other message there is
|
|
420
|
+
* dropped; a 503 on any other subject is a responder frame and takes the ordinary reply path. A refused publish is `permission-denied`
|
|
421
421
|
* (the watch is registered before the publish it watches), the deadline is `deadline-exceeded`,
|
|
422
422
|
* `unknown`, and an abort is `unavailable`, `unknown`. Subscription and publication share one
|
|
423
423
|
* promise executor, so a closed or draining connection cannot strand a rejected reply wait.
|
|
@@ -428,6 +428,8 @@ async function exchange(nc, x) {
|
|
|
428
428
|
let denialWatch;
|
|
429
429
|
let onAbort;
|
|
430
430
|
let published = false;
|
|
431
|
+
/** A refused reply read, waiting for its flush. */
|
|
432
|
+
let readRefusal;
|
|
431
433
|
const cancelled = new Promise((_, reject) => {
|
|
432
434
|
onAbort = () => reject(new EpEnvelopeError("unavailable", `${x.what} observation cancelled; the request may have executed`, published ? [{ ...x.unanswered, requestId: x.requestId }] : undefined, "unknown"));
|
|
433
435
|
x.signal?.addEventListener("abort", onAbort, { once: true });
|
|
@@ -439,7 +441,8 @@ async function exchange(nc, x) {
|
|
|
439
441
|
callback: (err, msg) => {
|
|
440
442
|
if (err) {
|
|
441
443
|
if (x.readRefused === "permission" && err instanceof PermissionViolationError) {
|
|
442
|
-
|
|
444
|
+
const refusal = readRefusal = new EpEnvelopeError("permission-denied", `the broker refused this caller's reply read "${x.replyFilter}" for ${x.what}: the request was published and may have run, and its reply cannot be read here; ${BROKER_REFUSAL_CAUSE}`, [{ ...x.unanswered, requestId: x.requestId }], "unknown");
|
|
445
|
+
void nc.flush().catch(() => undefined).then(() => new Promise((resolve) => setImmediate(resolve))).then(() => reject(refusal));
|
|
443
446
|
return;
|
|
444
447
|
}
|
|
445
448
|
reject(new EpEnvelopeError("unavailable", `the caller's reply subscription failed: ${err.message}`));
|
|
@@ -459,7 +462,8 @@ async function exchange(nc, x) {
|
|
|
459
462
|
reject(new EpEnvelopeError("unavailable", `no responder for ${x.what} (SPEC 13.5)`, [x.unanswered], "not-executed"));
|
|
460
463
|
return;
|
|
461
464
|
}
|
|
462
|
-
|
|
465
|
+
// Anything else there means nothing: a peer can name the sentinel as the reply subject of
|
|
466
|
+
// a JetStream API request, which the broker answers there. The wait goes on.
|
|
463
467
|
return;
|
|
464
468
|
}
|
|
465
469
|
if (x.accepts && !x.accepts(msg.subject))
|
|
@@ -471,7 +475,7 @@ async function exchange(nc, x) {
|
|
|
471
475
|
nc.publish(x.subject, x.body, { reply: x.sentinel });
|
|
472
476
|
published = true;
|
|
473
477
|
});
|
|
474
|
-
const timeout = new Promise((_, reject) => { timer = setTimeout(() => reject(new EpEnvelopeError("deadline-exceeded", `no reply to ${x.what} within the ${x.deadlineMs}ms budget (SPEC 13.5)`, [{ ...x.unanswered, requestId: x.requestId }], "unknown")), x.deadlineMs); });
|
|
478
|
+
const timeout = new Promise((_, reject) => { timer = setTimeout(() => reject(readRefusal ?? new EpEnvelopeError("deadline-exceeded", `no reply to ${x.what} within the ${x.deadlineMs}ms budget (SPEC 13.5)`, [{ ...x.unanswered, requestId: x.requestId }], "unknown")), x.deadlineMs); });
|
|
475
479
|
return await Promise.race([outcome, timeout, denialWatch.denied, cancelled]);
|
|
476
480
|
}
|
|
477
481
|
finally {
|
|
@@ -495,6 +499,8 @@ export function buildPrincipalRequest(space, to, op, verb) {
|
|
|
495
499
|
throw new EpEnvelopeError("bad-request", "a principal-route request carries no bind: the runtime id in the subject pins the process (SPEC 13.21)", undefined, "not-executed");
|
|
496
500
|
if (isIssuedCaller(op.caller))
|
|
497
501
|
throw new EpEnvelopeError("bad-request", "the principal route has no versioned form; it carries the caller triple only (SPEC 13.21)", undefined, "not-executed");
|
|
502
|
+
if (verb.deadlineMs > EP_PRINCIPAL_MAX_DEADLINE_MS)
|
|
503
|
+
throw new EpEnvelopeError("bad-request", `deadlineMs ${verb.deadlineMs} exceeds the principal route's ${EP_PRINCIPAL_MAX_DEADLINE_MS}ms maximum (SPEC 13.21)`, undefined, "not-executed");
|
|
498
504
|
const args = validatedArgs(op);
|
|
499
505
|
const n = nonce();
|
|
500
506
|
const subject = epPrincipalRequestSubject(space, {
|
|
@@ -512,7 +518,8 @@ export function buildPrincipalRequest(space, to, op, verb) {
|
|
|
512
518
|
* pinned contract.
|
|
513
519
|
*
|
|
514
520
|
* Application-level failure is the returned reply's `ok: false`. This boundary throws only for its
|
|
515
|
-
* own refusals: invalid args
|
|
521
|
+
* own refusals: invalid args or a `deadlineMs` over {@link EP_PRINCIPAL_MAX_DEADLINE_MS}
|
|
522
|
+
* `bad-request`, `not-executed`, before anything is published; no responder on the sentinel `unavailable`,
|
|
516
523
|
* `not-executed`; a refused publish `permission-denied`, `not-executed` (the caller holds no row);
|
|
517
524
|
* the deadline `deadline-exceeded`, `unknown`; and a reply that does not parse, echo or validate
|
|
518
525
|
* `internal`.
|