Skip to content

Commit c998a9e

Browse files
committed
refactor: validate worker messages at the catalog boundary
1 parent 5471188 commit c998a9e

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

‎src/agents/prepared-model-catalog.worker.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Worker-thread entrypoint for complete model-catalog discovery. */
22
import { parentPort, workerData } from "node:worker_threads";
33
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
4+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
45
import {
56
copyConfigResolutionFacts,
67
restoreConfigResolutionFacts,
@@ -235,14 +236,30 @@ export async function runPreparedModelCatalogWorkerRequest(
235236
}
236237
}
237238

239+
function isWorkerRequest(value: unknown): value is PreparedModelWorkerRequest {
240+
return (
241+
isRecord(value) &&
242+
(value.kind === "catalog" ||
243+
(value.kind === "auth-refresh" &&
244+
Array.isArray(value.providerIds) &&
245+
value.providerIds.every((providerId) => typeof providerId === "string") &&
246+
(value.profileIds === undefined ||
247+
(Array.isArray(value.profileIds) &&
248+
value.profileIds.every((profileId) => typeof profileId === "string")))))
249+
);
250+
}
251+
238252
if (parentPort) {
239253
const value = workerData as PreparedModelCatalogWorkerInput;
240254
let preparedGeneration: ReturnType<typeof prepareWorkerGeneration> | undefined;
241-
serveWorkerTasks((request: PreparedModelWorkerRequest) =>
242-
runPreparedModelCatalogWorkerRequest(
255+
serveWorkerTasks((request) => {
256+
if (!isWorkerRequest(request)) {
257+
throw new Error("invalid prepared model catalog worker request");
258+
}
259+
return runPreparedModelCatalogWorkerRequest(
243260
value,
244261
request,
245262
(preparedGeneration ??= prepareWorkerGeneration(value)),
246-
),
247-
);
263+
);
264+
});
248265
}

src/infra/worker-task-pool.test-fixture.ts renamed to src/infra/worker-task-pool.test-support.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import assert from "node:assert/strict";
12
import { threadId } from "node:worker_threads";
3+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
24
import { serveWorkerTasks } from "./worker-task-pool.js";
35

46
export type PoolFixtureInput = {
@@ -16,19 +18,24 @@ export type PoolFixtureResult = {
1618
};
1719

1820
let previousBuffer: ArrayBuffer | undefined;
19-
serveWorkerTasks<PoolFixtureInput, PoolFixtureResult>(
21+
serveWorkerTasks<PoolFixtureResult>(
2022
(input) => {
23+
assert.ok(isRecord(input));
24+
assert.ok(typeof input.label === "string");
2125
if (input.exitCode !== undefined) {
26+
assert.ok(typeof input.exitCode === "number");
2227
process.exit(input.exitCode);
2328
}
2429
if (input.counters) {
30+
assert.ok(input.counters instanceof SharedArrayBuffer);
2531
const counters = new Int32Array(input.counters);
2632
Atomics.add(counters, 0, 1);
2733
if (input.wait) {
2834
Atomics.wait(counters, 1, 0);
2935
}
3036
}
3137
const previousBufferBytes = previousBuffer?.byteLength;
38+
assert.ok(input.buffer === undefined || input.buffer instanceof ArrayBuffer);
3239
previousBuffer = input.buffer;
3340
return { label: input.label, threadId, buffer: input.buffer, previousBufferBytes };
3441
},

‎src/infra/worker-task-pool.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { promisify } from "node:util";
55
import { Worker } from "node:worker_threads";
66
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import { WorkerTaskPool } from "./worker-task-pool.js";
8-
import type { PoolFixtureInput, PoolFixtureResult } from "./worker-task-pool.test-fixture.js";
8+
import type { PoolFixtureInput, PoolFixtureResult } from "./worker-task-pool.test-support.js";
99

10-
const workerUrl = new URL("./worker-task-pool.test-fixture.ts", import.meta.url);
10+
const workerUrl = new URL("./worker-task-pool.test-support.ts", import.meta.url);
1111
const pools: WorkerTaskPool<PoolFixtureInput, PoolFixtureResult>[] = [];
1212
const workers: Worker[] = [];
1313
const workerChannel = channel("worker_threads");

‎src/infra/worker-task-pool.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,15 @@ export class WorkerTaskPool<Input, Output> {
257257
}
258258

259259
/** Pool dispatch is serial per worker; handlers finish cleanup before returning their result. */
260-
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- The handler owns the private input type.
261-
export function serveWorkerTasks<Input, Output>(
262-
handler: (input: Input) => Output | Promise<Output>,
260+
export function serveWorkerTasks<Output>(
261+
handler: (input: unknown) => Output | Promise<Output>,
263262
options: { transferList?: (value: Output) => Transferable[] } = {},
264263
): void {
265264
const port = parentPort;
266265
if (!port) {
267266
return;
268267
}
269-
port.on("message", ({ input }: { input: Input }) => {
268+
port.on("message", ({ input }: { input: unknown }) => {
270269
void Promise.resolve()
271270
.then(() => handler(input))
272271
.then((value) =>

0 commit comments

Comments
 (0)