Skip to content
Draft
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
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.1083.0",
"@hasna/contracts": "0.5.2",
"@hasna/contracts": "0.10.0",
"@hasna/events": "^0.1.9",
"@modelcontextprotocol/sdk": "^1.29.0",
"@openrouter/ai-sdk-provider": "2.9.1",
Expand Down
44 changes: 44 additions & 0 deletions src/lib/auth/tenant-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,48 @@
);
expect(decision).toMatchObject({ ok: false, status: 401, reason: "key_record_mismatch" });
});

test("a polluted Object.prototype.agent cannot satisfy the principal binding", async () => {
// Minted with NO agent claim: `agent` is optional in ApiKeyClaims, so the
// own property is absent and a bare `claims.agent` read falls through to
// Object.prototype.
const token = mintApiKey({ app: "loops", scopes: ["loops:read"], signingSecret: secret, kid: "kid-poison", nowMs: now });
expect(Object.hasOwn(token.claims, "agent")).toBe(false);

const { client } = clientFor(token);
let decision: Awaited<ReturnType<TenantApiAuthenticator["authenticate"]>>;
try {
// The attacker sets the polluted value to the TARGET row's principal_id.
(Object.prototype as unknown as Record<string, unknown>).agent = "principal-a";
decision = await new TenantApiAuthenticator(client, secret, () => now).authenticate(
headers(token.token),
{ method: "GET", path: "/v1/loops", policy: readPolicy },
);
} finally {
// Restore in finally so pollution cannot leak into sibling suites and
// resurface later as unrelated flakiness.
delete (Object.prototype as unknown as Record<string, unknown>).agent;
}
expect("agent" in {}).toBe(false); // prototype actually restored
expect(decision.ok).toBe(false);

Check failure on line 295 in src/lib/auth/tenant-auth.test.ts

View workflow job for this annotation

GitHub Actions / bun (macos-latest)

error: expect(received).toBe(expected)

Expected: false Received: true at <anonymous> (/Users/runner/work/loops/loops/src/lib/auth/tenant-auth.test.ts:295:25)

Check failure on line 295 in src/lib/auth/tenant-auth.test.ts

View workflow job for this annotation

GitHub Actions / bun (ubuntu-latest)

error: expect(received).toBe(expected)

Expected: false Received: true at <anonymous> (/home/runner/work/loops/loops/src/lib/auth/tenant-auth.test.ts:295:25)
expect(decision).toMatchObject({ status: 401, reason: "key_record_mismatch" });
});

test("the principal binding still denies a real mismatch and allows a real match", async () => {
// Guards the other direction: a fix that denied everything would pass the
// pollution test above and break every legitimate caller.
const mismatch = mintApiKey({ app: "loops", agent: "principal-b", scopes: ["loops:read"], signingSecret: secret, kid: "kid-mismatch", nowMs: now });
const denied = await new TenantApiAuthenticator(clientFor(mismatch).client, secret, () => now).authenticate(
headers(mismatch.token),
{ method: "GET", path: "/v1/loops", policy: readPolicy },
);
expect(denied).toMatchObject({ ok: false, status: 401, reason: "key_record_mismatch" });

const match = mintApiKey({ app: "loops", agent: "principal-a", scopes: ["loops:read"], signingSecret: secret, kid: "kid-match", nowMs: now });
const allowed = await new TenantApiAuthenticator(clientFor(match).client, secret, () => now).authenticate(
headers(match.token),
{ method: "GET", path: "/v1/loops", policy: readPolicy },
);
expect(allowed.ok).toBe(true);
});
});
Loading