fix(rpc): close prototype-chain permission bypass, add server/client/transport tests
C1 CRITICAL: implement() looked up procedures/handlers with plain property indexing, so any Object.prototype member name (constructor, toString, etc.) resolved truthy and skipped the permission gate entirely. Fixed with Object.hasOwn checks in packages/rpc/src/server.ts. Defense-in-depth guard added in packages/dev-server/src/rpc-dispatch.ts constraining URL path segments to a safe charset before they reach service/procedure lookups. Added missing direct test coverage for packages/rpc/src/transport.ts, server.ts and client.ts (previously untested), including a prototype-name sweep in both server.test.ts and dev-server's rpc-endpoint.test.ts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,9 +27,12 @@ export async function handleRpcRequest(
|
||||
if (!isInternalCaller(req)) return new Response("Not found", { status: 404 });
|
||||
if (req.method !== "POST") return new Response("Method not allowed", { status: 405 });
|
||||
const segments = url.pathname.split("/");
|
||||
const service = segments[3] ? services.get(segments[3]) : undefined;
|
||||
const SAFE_SEGMENT = /^[A-Za-z0-9_-]+$/;
|
||||
const serviceName = segments[3];
|
||||
const procedure = segments[4];
|
||||
if (!service || !procedure || segments.length !== 5) {
|
||||
const service =
|
||||
serviceName && SAFE_SEGMENT.test(serviceName) ? services.get(serviceName) : undefined;
|
||||
if (!service || !procedure || !SAFE_SEGMENT.test(procedure) || segments.length !== 5) {
|
||||
return json({ ok: false, code: "RPC_UNKNOWN", message: "Unknown procedure", retryable: false });
|
||||
}
|
||||
let payload: unknown;
|
||||
|
||||
@@ -48,4 +48,29 @@ describe("RPC endpoint", () => {
|
||||
expect(isInternalCaller(forwarded)).toBe(false);
|
||||
expect((await handleRpcRequest(forwarded, new URL(forwarded.url), services))!.status).toBe(404);
|
||||
});
|
||||
|
||||
describe("C1: prototype-chain procedure names cannot bypass the permission gate", () => {
|
||||
const PROTO_NAMES = [
|
||||
"constructor",
|
||||
"toString",
|
||||
"valueOf",
|
||||
"hasOwnProperty",
|
||||
"__proto__",
|
||||
"isPrototypeOf",
|
||||
];
|
||||
|
||||
for (const name of PROTO_NAMES) {
|
||||
test(`"${name}" in the URL path yields RPC_UNKNOWN`, async () => {
|
||||
const req = request(`/__wrnexus/rpc/demo/${name}`, { "x-wrnexus-internal": "1" });
|
||||
const res = await handleRpcRequest(req, new URL(req.url), services);
|
||||
expect(await res!.json()).toMatchObject({ ok: false, code: "RPC_UNKNOWN" });
|
||||
});
|
||||
}
|
||||
|
||||
test(`"constructor" as the SERVICE segment also yields RPC_UNKNOWN`, async () => {
|
||||
const req = request("/__wrnexus/rpc/constructor/add", { "x-wrnexus-internal": "1" });
|
||||
const res = await handleRpcRequest(req, new URL(req.url), services);
|
||||
expect(await res!.json()).toMatchObject({ ok: false, code: "RPC_UNKNOWN" });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user