Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_deferred_tools_worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,14 @@ test("HTTP call preflight permits an unadvertised grant and returns real permiss
const denied = await handleDeferredMcp(body, env, principal, entries, { service: f.service, validate: () => true });
assert.equal(denied.status, 403); assert.equal((await denied.json()).error.code, "tool_not_granted");
});

test("loading the module makes no random values, which Workers forbid in global scope", async () => {
const original = Object.getOwnPropertyDescriptor(globalThis.crypto, "randomUUID");
globalThis.crypto.randomUUID = () => { throw new Error("randomUUID called while loading the module"); };
try {
await import(`../worker/knowledge/deferred-tools.mjs?global-scope=${Date.now()}`);
} finally {
if (original) Object.defineProperty(globalThis.crypto, "randomUUID", original);
else delete globalThis.crypto.randomUUID;
}
});
8 changes: 5 additions & 3 deletions worker/knowledge/deferred-tools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const MAX_DISCOVERY_BYTES = 64 * 1024;
export const CURSOR_TTL_SECONDS = 120;
const MAX_GRANTS = 4096;
const RPC_ENVELOPE_RESERVE = 4096;
const processRevision = crypto.randomUUID();
// Workers forbid random values in global scope, so the per-isolate revision is made on first use.
let processRevision;
const currentProcessRevision = () => processRevision ??= crypto.randomUUID();
const encoder = new TextEncoder();
const services = new WeakMap();
let warned = false;
Expand Down Expand Up @@ -86,7 +88,7 @@ export async function readGrantSnapshot(env, principalId) {
const grants = results[0].results.map(row => ({ principal_id: row.principal_id, tool_name: row.tool_name,
scopes: JSON.parse(row.scopes_json), allowed: row.allowed, revision: row.revision }));
validatedGrants(grants, principalId);
const revision = revisionEnabled ? results[1].results[0]?.revision : processRevision;
const revision = revisionEnabled ? results[1].results[0]?.revision : currentProcessRevision();
if (revisionEnabled && (!Number.isSafeInteger(revision) || revision < 0)) throw unavailable();
return { grants, revision };
} catch { throw unavailable(); }
Expand All @@ -102,7 +104,7 @@ export class DeferredTools {
try {
const value = await this.readGrants(principal.id);
const grants = validatedGrants(Array.isArray(value) ? value : value.grants, principal.id);
const revision = this.revision ? await this.revision() : value.revision ?? processRevision;
const revision = this.revision ? await this.revision() : value.revision ?? currentProcessRevision();
if (!(typeof revision === "string" && revision || Number.isSafeInteger(revision) && revision >= 0)) throw unavailable();
const rows = [...grants.entries()].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([, row]) => row);
return { grants, revision, stamp: await hash(rows) };
Expand Down
Loading