fix(authz): audit getResource denials; fail closed on a malformed denies shape

guardPermission's getResource catch returned 403 directly, never reaching
decideFor -> decide -> finish, so the audit sink never saw it — an attacker
probing ids that make the resource loader throw got a clean 403 stream
invisible to the audit trail. The audit sink is now stashed on the
per-request RequestAuthz object (authzMiddleware already receives it via
AuthzResolverOptions), and the catch records an "allowed: false" event with
an opaque reason before returning the 403.

Also: the explicit-deny check sat outside decide()'s try/catch, and
deniedBy() guarded on denies.length rather than Array.isArray(denies). A
store returning denies as a bare string let new Set(denies) iterate
characters instead of the permission, so the deny matched nothing and was
silently discarded; a store omitting denies entirely threw straight out of
decide(). Both are now validated and handled inside the try, denying via the
same "Authorization store unavailable" path as any other store failure.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-05 02:10:11 +05:30
co-authored by Claude Opus 5
parent a7255fa1bd
commit 3867e7c183
4 changed files with 137 additions and 10 deletions
+44
View File
@@ -312,6 +312,50 @@ describe("createAuthzResolver fail-closed regressions", () => {
expect(decision.reason).toMatch(/explicit deny/i);
});
test("a store returning a non-array `denies` (e.g. a string) denies rather than silently allowing", async () => {
// new Set("post:write") would iterate CHARACTERS, not the permission, so
// a store returning a malformed `denies` shape must not let an otherwise
// role-granted permission slip through as allowed. Uses "post:comment:delete"
// (granted via the "moderator" role's "post:comment:*" wildcard) rather
// than "post:write", specifically because "post:write" is bound to the
// "ownsPost" policy in this test catalog — a resource-ownership check
// that would itself deny an unowned resource and mask the exact bug this
// test exists to catch, passing for the wrong reason even without the fix.
const store = memoryPermissionStore();
await store.assignRole("u1", "moderator"); // moderator -> post:comment:* wildcard grant
const malformed = {
...store,
assignmentsFor: async (subjectId: string, scope?: { tenantId?: string }) => {
const real = await store.assignmentsFor(subjectId, scope);
return { ...real, denies: "post:comment:delete" as unknown as string[] };
},
};
const resolver = createAuthzResolver({ catalog, store: malformed, strict: false });
const result = await resolver.decide({
subject: { id: "u1" },
permission: "post:comment:delete",
});
expect(result.allowed).toBe(false);
});
test("a store omitting `denies` entirely denies rather than throwing out of decide()", async () => {
const store = memoryPermissionStore();
await store.assignRole("u1", "editor");
const malformed = {
...store,
assignmentsFor: async (subjectId: string, scope?: { tenantId?: string }) => {
const real = await store.assignmentsFor(subjectId, scope);
const { denies: _denies, ...withoutDenies } = real;
return withoutDenies as unknown as typeof real;
},
};
const resolver = createAuthzResolver({ catalog, store: malformed, strict: false });
// If decide() still threw/rejected instead of denying, this `await` would
// reject and fail the test right here rather than reaching the assertion.
const result = await resolver.decide({ subject: { id: "u1" }, permission: "post:write" });
expect(result.allowed).toBe(false);
});
test("non-string subject ids deny rather than falling back to anonymous", async () => {
const { resolver } = make();
const invalidIds: unknown[] = [0, "", 123, {}];