fix: enforce explicit grant invariants

Explicit-grant management could produce states an operator could not reason
about, and pushed request-shaped problems into the database.

Two identical createGrant calls produced two grants; five concurrent identical
calls produced five. Revoking one returned success while the equivalent kept
authorizing the same user — an operator could believe they had removed access
that was still live. Two issuances are now the same authorization source when
user_id, permission_id, scope, source_kind, and source_ref are all equal, with
NULL equal only to NULL; expiry, creation metadata, actor, and grant id do not
distinguish them. Creating one while an equivalent is currently effective fails
with AUTHORIZATION_GRANT_ALREADY_EXISTS (HTTP 409, no-store) naming nothing about
the grant that already exists, and writing no grant and no audit row. A revoked
or expired equivalent does not block a fresh issuance. Conflict rather than
idempotent return: the caller asked to record a new authorization with their own
actor and expiry, and answering "done" while recording neither would hide that
nothing happened.

Enforcement is database-backed, since nothing constrains the number of API
processes: every create and revoke touching one equivalence tuple takes a
transaction-scoped advisory lock keyed by a hash of that tuple, encoded as a JSON
array so NULL stays distinct from the empty string and a crafted scope cannot
forge a separator. A collision can only make unrelated tuples serialize, never
hide a conflict, because the decision is always an exact SQL predicate. Its
single-key lock space is verified disjoint from the two-key space the migration
runner and bootstrap use. A time-dependent partial unique index was rejected:
now() is not immutable and cannot be indexed.

One revocation now clears every unrevoked equivalent — including expired-but-
unrevoked legacy rows, so the history is left in one state — with one audit row
per grant actually revoked, each carrying its own id, in one transaction.
Differently scoped or sourced grants are untouched. Historical duplicates are not
silently revoked by migration; the store prevents new effective equivalents and
an operator's explicit revocation resolves the class.

The request boundary no longer leaks into PostgreSQL. GrantId accepts exactly the
canonical lowercase hyphenated UUID the database emits, with no trimming and no
case folding, so a malformed path parameter is a 400 decided after authorization
but before any store call instead of reaching the UUID comparison and returning
an indistinguishable 503; a canonical unknown id still returns 404. expiresAt
accepts exactly YYYY-MM-DDTHH:mm:ss.sssZ, byte-for-byte equal to the parsed
date's own toISOString, and must be strictly future — bare new Date silently
rewrote 2026-02-31 to March 3rd and accepted locale, date-only, offset, and
millisecond-free forms, and a past expiry was stored as a grant dead on arrival.
The store validates the same thing before any SQL so trusted internal callers get
the guarantee too.

The generic HTTP surface now rejects a scope field in every form, including null.
No route middleware supplies a scope to the evaluator, so a scope accepted here
produced a grant that persisted, listed, and authorized nothing. Core's scope
semantics are unchanged for internal callers; no resolver was added.

Listing was unbounded and returned a user's entire history. It is now keyset
pagination over created_at DESC, id DESC with a strict boundary on both values,
so rows sharing a timestamp are neither skipped nor repeated, at most limit + 1
rows read, explicit-source filtering server-side on every page, and an opaque
unsigned position cursor that is strictly decoded and reveals nothing on
rejection. Core's list method requires a validated finite bound with no
unbounded fallback anywhere.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
ekklesia
2026-07-26 21:56:32 -05:00
co-authored by Claude Opus 5
parent 532ce3803b
commit 5c589d8fec
15 changed files with 2177 additions and 107 deletions
+259 -31
View File
@@ -28,7 +28,10 @@ import {
} from "./schema.js";
export type AuthorizationStoreErrorCode =
| "AUTHORIZATION_GRANT_ALREADY_EXISTS"
| "AUTHORIZATION_GRANT_CREATE_FAILED"
| "AUTHORIZATION_GRANT_EXPIRATION_INVALID"
| "AUTHORIZATION_GRANT_LIMIT_INVALID"
| "AUTHORIZATION_GRANT_LIST_FAILED"
| "AUTHORIZATION_GRANT_LOOKUP_FAILED"
| "AUTHORIZATION_GRANT_NOT_FOUND"
@@ -38,7 +41,12 @@ export type AuthorizationStoreErrorCode =
| "AUTHORIZATION_PLATFORM_OWNER_NOT_FOUND";
const storeErrorMessages: Readonly<Record<AuthorizationStoreErrorCode, string>> = Object.freeze({
AUTHORIZATION_GRANT_ALREADY_EXISTS:
"An equivalent authorization grant is already effective for this user.",
AUTHORIZATION_GRANT_CREATE_FAILED: "The authorization grant could not be created.",
AUTHORIZATION_GRANT_EXPIRATION_INVALID:
"The authorization grant expiration must be a valid instant in the future.",
AUTHORIZATION_GRANT_LIMIT_INVALID: "The authorization grant page size is invalid.",
AUTHORIZATION_GRANT_LIST_FAILED: "The authorization grant list could not be retrieved.",
AUTHORIZATION_GRANT_LOOKUP_FAILED: "The authorization grant lookup could not be completed.",
AUTHORIZATION_GRANT_NOT_FOUND: "The authorization grant was not found or is already revoked.",
@@ -49,6 +57,10 @@ const storeErrorMessages: Readonly<Record<AuthorizationStoreErrorCode, string>>
"The platform-owner record was not found or is already revoked.",
});
/** The largest page any caller may request from `listGrantsForUser`, enforced identically by the
* store and by the HTTP surface so there is exactly one bound, not two that could drift. */
export const AUTHORIZATION_GRANT_LIST_MAX_LIMIT = 100;
/** Never carries the underlying database error, SQL text, host, port, path, or stack. */
export class AuthorizationStoreError extends Error {
readonly code: AuthorizationStoreErrorCode;
@@ -101,11 +113,38 @@ export interface RevokeGrantInput {
readonly sourceKind?: GrantSourceKind;
}
/** One row's position in the `created_at DESC, id DESC` ordering, used as a strict keyset
* boundary. Both values are required: comparing on the timestamp alone would skip or repeat rows
* whenever two grants share a `created_at`, which concurrent creation makes routine. */
export interface GrantPageBoundary {
readonly createdAt: Date;
readonly id: GrantId;
}
export interface ListGrantsForUserOptions {
/** Required. The maximum number of rows to return, 1 through
* `AUTHORIZATION_GRANT_LIST_MAX_LIMIT`. There is deliberately no default and no unbounded
* fallback anywhere: an unbounded listing returned a user's entire authorization history in one
* response, and a bound a caller may simply omit is not a bound. */
readonly limit: number;
/** When provided, only grants strictly *after* this position in the ordering are returned. */
readonly after?: GrantPageBoundary;
/** When provided, only grants with this exact `sourceKind` are returned. */
readonly sourceKind?: GrantSourceKind;
}
export interface GrantPage {
readonly grants: readonly GrantRecord[];
/** The last returned row's position when more rows exist, otherwise `undefined`. */
readonly nextBoundary?: GrantPageBoundary;
}
export interface CreateAuthorizationStoreOptions {
/** Injected only so expiration validation is deterministic under test. Production composition
* leaves this unset and the store reads real time. */
readonly now?: () => Date;
}
/**
* Core's Postgres-backed authorization persistence: grants, platform-owner status, and the
* append-only audit log, all behind one explicit factory — no environment reads, no connection
@@ -117,12 +156,9 @@ export interface ListGrantsForUserOptions {
export interface AuthorizationStore extends AuthorizationGrantPort, AuthorizationPlatformOwnerPort {
createGrant(input: CreateGrantInput): Promise<GrantRecord>;
revokeGrant(input: RevokeGrantInput): Promise<void>;
/** Every grant ever recorded for the user, active or revoked, newest first — the caller decides
* what to display. Never includes another user's grants. */
listGrantsForUser(
userId: EkklesiaUserId,
options?: ListGrantsForUserOptions,
): Promise<readonly GrantRecord[]>;
/** One bounded page of the user's grants, active or revoked, ordered `created_at DESC, id DESC`.
* Never includes another user's grants. */
listGrantsForUser(userId: EkklesiaUserId, options: ListGrantsForUserOptions): Promise<GrantPage>;
grantPlatformOwner(userId: EkklesiaUserId, actor: AuthorizationActor): Promise<void>;
revokePlatformOwner(userId: EkklesiaUserId, actor: AuthorizationActor): Promise<void>;
}
@@ -160,11 +196,141 @@ function toGrantRecord(row: GrantRow): GrantRecord {
});
}
export function createAuthorizationStore(db: AuthorizationDrizzleDatabase): AuthorizationStore {
/**
* The five values that make two grant issuances the *same authorization source*: subject,
* permission, scope, source kind, and source reference — with `NULL` equal only to `NULL`.
* `expires_at`, creation metadata, actor metadata, and the grant id deliberately do **not**
* distinguish them: re-issuing the identical authority with a different expiry or by a different
* administrator is the same grant, not a second one.
*/
interface GrantEquivalence {
readonly userId: string;
readonly permissionId: string;
readonly scope: string | null;
readonly sourceKind: string;
readonly sourceRef: string | null;
}
function equivalenceOf(input: CreateGrantInput): GrantEquivalence {
return {
permissionId: input.permission,
scope: input.scope ?? null,
sourceKind: input.sourceKind,
sourceRef: input.sourceRef ?? null,
userId: input.userId,
};
}
/**
* Serializes every create/revoke touching one equivalence tuple against a transaction-scoped
* PostgreSQL advisory lock, so concurrent callers cannot both observe "no effective equivalent"
* and both insert. A process-local mutex would not do this — nothing constrains the number of API
* processes — and neither would an unlocked check-then-insert.
*
* The key is a 64-bit hash of the tuple encoded as a JSON array, which keeps `NULL` and the empty
* string distinct and makes separator forgery through a crafted scope or source reference
* impossible. A hash collision is harmless by construction: it can only make two unrelated tuples
* serialize against each other, never make a real conflict go unnoticed, because the authoritative
* decision is always the exact SQL predicate below and never the hash.
*
* `pg_advisory_xact_lock(bigint)` uses PostgreSQL's *single-key* advisory-lock space, which is
* verified to be disjoint from the two-key space used by the migration runner
* (`core/src/migrations/postgres-database.ts`) and by platform-owner bootstrap. Grant serialization
* therefore can never block, or be blocked by, migration or bootstrap locking. Being
* transaction-scoped, it is released on COMMIT or ROLLBACK with no manual unlock.
*/
async function lockEquivalence(
tx: Parameters<Parameters<AuthorizationDrizzleDatabase["transaction"]>[0]>[0],
equivalence: GrantEquivalence,
): Promise<void> {
await tx.execute(
sql`select pg_advisory_xact_lock(hashtextextended(jsonb_build_array(
${equivalence.userId}::text,
${equivalence.permissionId}::text,
${equivalence.scope}::text,
${equivalence.sourceKind}::text,
${equivalence.sourceRef}::text
)::text, 0))`,
);
}
/** Matches exactly the rows sharing an equivalence tuple. `is not distinct from` is what gives
* `NULL = NULL` for the two nullable members; plain `=` would silently match nothing. */
function equivalenceCondition(equivalence: GrantEquivalence) {
return and(
eq(ekklesiaCoreGrant.userId, equivalence.userId),
eq(ekklesiaCoreGrant.permissionId, equivalence.permissionId),
sql`${ekklesiaCoreGrant.scope} is not distinct from ${equivalence.scope}::text`,
eq(ekklesiaCoreGrant.sourceKind, equivalence.sourceKind),
sql`${ekklesiaCoreGrant.sourceRef} is not distinct from ${equivalence.sourceRef}::text`,
);
}
/** A supplied expiration must be a real instant strictly later than the acceptance clock. A past
* or equal-to-now expiration produces a grant that is dead the moment it is written, which is
* never what a caller meant; rejecting it here means trusted internal callers get the same
* guarantee the HTTP surface enforces, rather than each validating separately. */
function assertValidExpiration(expiresAt: Date | undefined, now: Date): void {
if (expiresAt === undefined) {
return;
}
if (
!(expiresAt instanceof Date) ||
Number.isNaN(expiresAt.getTime()) ||
expiresAt.getTime() <= now.getTime()
) {
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_EXPIRATION_INVALID");
}
}
function assertValidLimit(limit: number): number {
if (
typeof limit !== "number" ||
!Number.isSafeInteger(limit) ||
limit < 1 ||
limit > AUTHORIZATION_GRANT_LIST_MAX_LIMIT
) {
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_LIMIT_INVALID");
}
return limit;
}
export function createAuthorizationStore(
db: AuthorizationDrizzleDatabase,
options: CreateAuthorizationStoreOptions = {},
): AuthorizationStore {
const now = options.now ?? ((): Date => new Date());
return Object.freeze({
createGrant: async (input: CreateGrantInput): Promise<GrantRecord> => {
// Validated before any transaction opens, so a rejected expiration performs no database work
// at all and can never leave a grant or an audit row behind.
assertValidExpiration(input.expiresAt, now());
const equivalence = equivalenceOf(input);
try {
return await db.transaction(async (tx) => {
await lockEquivalence(tx, equivalence);
// Under the lock, "currently effective" is authoritative: an equivalent that is revoked,
// or whose expiry has already passed, does not block a fresh issuance.
const effective = await tx
.select({ id: ekklesiaCoreGrant.id })
.from(ekklesiaCoreGrant)
.where(
and(
equivalenceCondition(equivalence),
isNull(ekklesiaCoreGrant.revokedAt),
or(
isNull(ekklesiaCoreGrant.expiresAt),
gt(ekklesiaCoreGrant.expiresAt, sql`now()`),
),
),
)
.limit(1);
if (effective.length > 0) {
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_ALREADY_EXISTS");
}
const [row] = await tx
.insert(ekklesiaCoreGrant)
.values({
@@ -202,16 +368,32 @@ export function createAuthorizationStore(db: AuthorizationDrizzleDatabase): Auth
}
},
/**
* Revoking one grant revokes *every* currently unrevoked issuance sharing its equivalence
* tuple, in one transaction, with one `grant.revoke` audit row per grant actually changed.
*
* Addressing a single id and revoking only that row was actively misleading: an operator who
* revoked a grant received `success` while an equivalent duplicate kept authorizing the same
* user. Expired-but-unrevoked equivalents are included deliberately — they are already
* ineffective, but leaving them unrevoked would keep the history in two states for what is one
* authorization source. Grants with a different subject, permission, scope, source kind, or
* source reference are untouched.
*/
revokeGrant: async (input: RevokeGrantInput): Promise<void> => {
try {
await db.transaction(async (tx) => {
const [row] = await tx
.update(ekklesiaCoreGrant)
.set({
revokedAt: sql`now()`,
revokedByActorId: actorId(input.actor),
revokedByActorType: input.actor.type,
// The tuple columns are immutable once written, so reading them before taking the lock
// is safe; whether the target is still unrevoked is re-decided under the lock by the
// update predicate below.
const [target] = await tx
.select({
permissionId: ekklesiaCoreGrant.permissionId,
scope: ekklesiaCoreGrant.scope,
sourceKind: ekklesiaCoreGrant.sourceKind,
sourceRef: ekklesiaCoreGrant.sourceRef,
userId: ekklesiaCoreGrant.userId,
})
.from(ekklesiaCoreGrant)
.where(
and(
eq(ekklesiaCoreGrant.id, input.grantId),
@@ -221,20 +403,38 @@ export function createAuthorizationStore(db: AuthorizationDrizzleDatabase): Auth
: [eq(ekklesiaCoreGrant.sourceKind, input.sourceKind)]),
),
)
.returning();
if (!row) {
.limit(1);
if (!target) {
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_NOT_FOUND");
}
await tx.insert(ekklesiaCoreAuthorizationAudit).values({
action: "grant.revoke",
actorId: actorId(input.actor),
actorType: input.actor.type,
grantId: row.id,
permissionId: row.permissionId,
scope: row.scope,
subjectUserId: row.userId,
});
await lockEquivalence(tx, target);
const rows = await tx
.update(ekklesiaCoreGrant)
.set({
revokedAt: sql`now()`,
revokedByActorId: actorId(input.actor),
revokedByActorType: input.actor.type,
})
.where(and(equivalenceCondition(target), isNull(ekklesiaCoreGrant.revokedAt)))
.returning();
if (rows.length === 0) {
// A concurrent revocation committed between the lookup and the lock.
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_NOT_FOUND");
}
await tx.insert(ekklesiaCoreAuthorizationAudit).values(
rows.map((row) => ({
action: "grant.revoke",
actorId: actorId(input.actor),
actorType: input.actor.type,
grantId: row.id,
permissionId: row.permissionId,
scope: row.scope,
subjectUserId: row.userId,
})),
);
});
} catch (error) {
if (isAuthorizationStoreError(error)) {
@@ -269,10 +469,21 @@ export function createAuthorizationStore(db: AuthorizationDrizzleDatabase): Auth
}
},
/**
* Deterministic keyset pagination over `created_at DESC, id DESC`. The boundary is a strict
* row-value comparison on *both* columns, so grants sharing a `created_at` — which concurrent
* creation makes routine — can neither be skipped nor repeated across pages. Offset pagination
* would do exactly that under concurrent writes, which is why it is not used.
*
* At most `limit + 1` rows are read: the extra row only answers "is there another page" and is
* never returned.
*/
listGrantsForUser: async (
userId: EkklesiaUserId,
options: ListGrantsForUserOptions = {},
): Promise<readonly GrantRecord[]> => {
listOptions: ListGrantsForUserOptions,
): Promise<GrantPage> => {
const limit = assertValidLimit(listOptions.limit);
const after = listOptions.after;
try {
const rows = await db
.select()
@@ -280,14 +491,31 @@ export function createAuthorizationStore(db: AuthorizationDrizzleDatabase): Auth
.where(
and(
eq(ekklesiaCoreGrant.userId, userId),
...(options.sourceKind === undefined
...(listOptions.sourceKind === undefined
? []
: [eq(ekklesiaCoreGrant.sourceKind, options.sourceKind)]),
: [eq(ekklesiaCoreGrant.sourceKind, listOptions.sourceKind)]),
...(after === undefined
? []
: [
sql`(${ekklesiaCoreGrant.createdAt}, ${ekklesiaCoreGrant.id}) < (${after.createdAt}::timestamptz, ${after.id}::uuid)`,
]),
),
)
.orderBy(desc(ekklesiaCoreGrant.createdAt));
return Object.freeze(rows.map(toGrantRecord));
} catch {
.orderBy(desc(ekklesiaCoreGrant.createdAt), desc(ekklesiaCoreGrant.id))
.limit(limit + 1);
const page = rows.slice(0, limit).map(toGrantRecord);
const last = page.at(-1);
return Object.freeze({
grants: Object.freeze(page),
...(rows.length > limit && last !== undefined
? { nextBoundary: Object.freeze({ createdAt: last.createdAt, id: last.id }) }
: {}),
});
} catch (error) {
if (isAuthorizationStoreError(error)) {
throw error;
}
throw new AuthorizationStoreError("AUTHORIZATION_GRANT_LIST_FAILED");
}
},
+556 -10
View File
@@ -9,6 +9,7 @@ import {
type GrantId,
parseAuthorizationScope,
parseEkklesiaUserId,
parseGrantId,
parsePermissionId,
SYSTEM_ACTOR,
} from "@ekklesia/contracts";
@@ -341,23 +342,56 @@ describe.sequential("live Core authorization store", () => {
});
});
it("denies a grant once it expires, evaluated against a real elapsed instant", async () => {
const expiringPermission = parsePermissionId("calendar.events.export");
const past = new Date(Date.now() - 1_000);
it("refuses to create a grant that is already expired on arrival", async () => {
// The store no longer accepts a past expiration at all: such a grant is dead the moment it is
// written, which is never what a caller meant. This is validated before any transaction opens.
let captured: unknown;
try {
await store.createGrant({
actor,
expiresAt: new Date(Date.now() - 1_000),
permission: parsePermissionId("calendar.events.export"),
sourceKind: "explicit",
userId: userOneId,
});
} catch (error) {
captured = error;
}
expect(captured).toMatchObject({ code: "AUTHORIZATION_GRANT_EXPIRATION_INVALID" });
expect(
await safeQuery(admin, `select id from "ekklesia_core_grant" where permission_id = $1`, [
"calendar.events.export",
]),
).toHaveLength(0);
});
it("denies a grant once its expiry has passed, evaluated against a real elapsed instant", async () => {
const expiringPermission = parsePermissionId("calendar.events.expiring");
const expiresAt = new Date(Date.now() + 60_000);
await store.createGrant({
actor,
expiresAt: past,
expiresAt,
permission: expiringPermission,
sourceKind: "explicit",
userId: userOneId,
});
const active = await store.hasActiveGrant({
now: new Date(),
permission: expiringPermission,
userId: userOneId,
});
expect(active).toBe(false);
// Deterministic rather than sleeping: the read port evaluates expiration against the instant
// it is given, so advancing that instant past the stored expiry is a real elapsed-time check.
expect(
await store.hasActiveGrant({
now: new Date(expiresAt.getTime() - 1_000),
permission: expiringPermission,
userId: userOneId,
}),
).toBe(true);
expect(
await store.hasActiveGrant({
now: new Date(expiresAt.getTime() + 1_000),
permission: expiringPermission,
userId: userOneId,
}),
).toBe(false);
});
it("allows a grant that has not yet expired", async () => {
@@ -539,3 +573,515 @@ describe.sequential("live Core authorization store — no automatic migration",
}
});
});
/**
* Pass 3 grant-integrity scenarios, all against real PostgreSQL because every property under test
* is a database-concurrency or database-constraint property that a fake cannot establish.
*
* The administrative client each scenario uses (`openAdministrativeClient`) is **test-only
* provisioning and inspection infrastructure**: it seeds users, plants legacy duplicate rows that
* the production store would now refuse to create, and reads back the resulting state. It is never
* an API pool or a runtime connection, and it is closed in `afterAll` along with its disposable
* database.
*/
describe.sequential("live Core authorization store — grant equivalence and revocation", () => {
let database: { name: string; url: string };
let admin: Client;
let store: ReturnType<typeof createAuthorizationStore>;
let close: () => Promise<void>;
const subject = parseEkklesiaUserId("equiv_subject");
const other = parseEkklesiaUserId("equiv_other");
const actor = createUserActor("equiv_other");
const permission = parsePermissionId("calendar.events.read");
async function effectiveCount(userId: string, permissionId: string): Promise<number> {
const rows = await safeQuery<{ n: string }>(
admin,
`select count(*)::int as n from "ekklesia_core_grant"
where user_id = $1 and permission_id = $2 and revoked_at is null
and (expires_at is null or expires_at > now())`,
[userId, permissionId],
);
return Number(rows[0]?.n ?? 0);
}
it("prepares a disposable database with the three Core migrations applied", async () => {
database = await createDisposableDatabase("equivalence");
await applyCoreMigrations(database.url);
admin = await openAdministrativeClient(database.url);
await seedUser(admin, subject, "[email protected]");
await seedUser(admin, other, "[email protected]");
const handle = createAuthorizationDrizzleDatabase(database.url);
close = handle.close;
store = createAuthorizationStore(handle.db);
expect(await effectiveCount(subject, permission)).toBe(0);
});
it("creates the first unscoped explicit grant and writes exactly one create audit row", async () => {
const grant = await store.createGrant({
actor,
permission,
sourceKind: "explicit",
userId: subject,
});
expect(grant.scope).toBeUndefined();
const audit = await safeQuery<{ action: string }>(
admin,
`select action from "ekklesia_core_authorization_audit" where grant_id = $1`,
[grant.id],
);
expect(audit.map((row) => row.action)).toEqual(["grant.create"]);
});
it("refuses a sequential equivalent creation, leaving no second grant and no second audit row", async () => {
const auditBefore = await safeQuery(
admin,
`select id from "ekklesia_core_authorization_audit" where subject_user_id = $1 and permission_id = $2`,
[subject, permission],
);
let captured: unknown;
try {
await store.createGrant({ actor, permission, sourceKind: "explicit", userId: subject });
} catch (error) {
captured = error;
}
expect(captured).toBeInstanceOf(AuthorizationStoreError);
expect(captured).toMatchObject({ code: "AUTHORIZATION_GRANT_ALREADY_EXISTS" });
expect(await effectiveCount(subject, permission)).toBe(1);
const auditAfter = await safeQuery(
admin,
`select id from "ekklesia_core_authorization_audit" where subject_user_id = $1 and permission_id = $2`,
[subject, permission],
);
expect(auditAfter).toHaveLength(auditBefore.length);
});
it("a differing expiration does not make an otherwise identical issuance distinct", async () => {
let captured: unknown;
try {
await store.createGrant({
actor,
expiresAt: new Date(Date.now() + 3_600_000),
permission,
sourceKind: "explicit",
userId: subject,
});
} catch (error) {
captured = error;
}
expect(captured).toMatchObject({ code: "AUTHORIZATION_GRANT_ALREADY_EXISTS" });
expect(await effectiveCount(subject, permission)).toBe(1);
});
it("a different subject, permission, scope, source kind, or source reference is not equivalent", async () => {
const distinct = [
{ permission, sourceKind: "explicit" as const, userId: other },
{
permission: parsePermissionId("calendar.events.write"),
sourceKind: "explicit" as const,
userId: subject,
},
{
permission,
scope: parseAuthorizationScope("calendar-event:1"),
sourceKind: "explicit" as const,
userId: subject,
},
{ permission, sourceKind: "relationship" as const, userId: subject },
{
permission,
sourceKind: "relationship" as const,
sourceRef: "roster:7",
userId: subject,
},
];
for (const input of distinct) {
const grant = await store.createGrant({ actor, ...input });
expect(grant.id).toBeDefined();
}
// A second issuance of one of those distinct tuples is itself a duplicate.
let captured: unknown;
try {
await store.createGrant({
actor,
permission,
scope: parseAuthorizationScope("calendar-event:1"),
sourceKind: "explicit",
userId: subject,
});
} catch (error) {
captured = error;
}
expect(captured).toMatchObject({ code: "AUTHORIZATION_GRANT_ALREADY_EXISTS" });
});
it("serializes concurrent equivalent creations to exactly one grant and one conflict", async () => {
const concurrentPermission = parsePermissionId("calendar.events.concurrent");
// Six genuinely concurrent transactions against the same tuple. The transaction-scoped
// advisory lock is what makes this deterministic: no sleeps, no retries, no process-local
// mutex — a second API process would be serialized identically.
const results = await Promise.allSettled(
Array.from({ length: 6 }, () =>
store.createGrant({
actor,
permission: concurrentPermission,
sourceKind: "explicit",
userId: subject,
}),
),
);
const fulfilled = results.filter((result) => result.status === "fulfilled");
const rejected = results.filter((result) => result.status === "rejected");
expect(fulfilled).toHaveLength(1);
expect(rejected).toHaveLength(5);
for (const result of rejected) {
expect((result as PromiseRejectedResult).reason).toMatchObject({
code: "AUTHORIZATION_GRANT_ALREADY_EXISTS",
});
}
expect(await effectiveCount(subject, concurrentPermission)).toBe(1);
const audit = await safeQuery(
admin,
`select id from "ekklesia_core_authorization_audit"
where subject_user_id = $1 and permission_id = $2 and action = 'grant.create'`,
[subject, concurrentPermission],
);
expect(audit).toHaveLength(1);
});
it("allows a fresh issuance once the previous equivalent is revoked, and again once one expires", async () => {
const cyclePermission = parsePermissionId("calendar.events.cycle");
const first = await store.createGrant({
actor,
permission: cyclePermission,
sourceKind: "explicit",
userId: subject,
});
await store.revokeGrant({ actor, grantId: first.id, sourceKind: "explicit" });
// Revoked equivalents do not block a new issuance.
const second = await store.createGrant({
actor,
expiresAt: new Date(Date.now() + 60_000),
permission: cyclePermission,
sourceKind: "explicit",
userId: subject,
});
expect(second.id).not.toBe(first.id);
// Force the second grant past its expiry through the administrative client, which is the only
// deterministic way to observe "expired equivalent" without sleeping.
await safeQuery(
admin,
`update "ekklesia_core_grant" set expires_at = now() - interval '1 second' where id = $1`,
[second.id],
);
const third = await store.createGrant({
actor,
permission: cyclePermission,
sourceKind: "explicit",
userId: subject,
});
expect(third.id).not.toBe(second.id);
expect(await effectiveCount(subject, cyclePermission)).toBe(1);
});
it("one revocation revokes every unrevoked equivalent, audits each by its own id, and leaves nothing authorizing", async () => {
const legacyPermission = parsePermissionId("calendar.events.legacy");
// Plant the exact state the old store could produce: several equivalent issuances, including
// one already expired but never revoked, plus two deliberately non-equivalent neighbours.
const legacyIds: string[] = [];
for (const expires of ["null", "now() + interval '1 hour'", "now() - interval '1 hour'"]) {
const rows = await safeQuery<{ id: string }>(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id, expires_at)
values ($1, $2, null, 'explicit', null, 'user', $3, ${expires}) returning id`,
[subject, legacyPermission, other],
);
legacyIds.push(rows[0]?.id as string);
}
const scopedNeighbour = await safeQuery<{ id: string }>(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id)
values ($1, $2, 'calendar-event:9', 'explicit', null, 'user', $3) returning id`,
[subject, legacyPermission, other],
);
// Deliberately on its own permission: an unscoped relationship grant for `legacyPermission`
// would legitimately keep authorizing after this revocation (it is a different equivalence
// tuple), which would make the "nothing authorizes" assertion below meaningless rather than
// wrong. The scoped neighbour above stays on the same permission because a scoped grant never
// satisfies an unscoped check.
const relationshipNeighbour = await safeQuery<{ id: string }>(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id)
values ($1, 'calendar.events.legacyrel', null, 'relationship', 'roster:9', 'user', $2) returning id`,
[subject, other],
);
expect(await effectiveCount(subject, legacyPermission)).toBe(3);
await store.revokeGrant({
actor,
grantId: parseGrantId(legacyIds[0] as string),
sourceKind: "explicit",
});
const revoked = await safeQuery<{ id: string; revoked_at: Date | null }>(
admin,
`select id, revoked_at from "ekklesia_core_grant" where id = any($1::uuid[])`,
[legacyIds],
);
// Every unrevoked equivalent, including the expired-but-unrevoked one, is now revoked.
expect(revoked.every((row) => row.revoked_at !== null)).toBe(true);
const auditRows = await safeQuery<{ grant_id: string }>(
admin,
`select grant_id from "ekklesia_core_authorization_audit"
where action = 'grant.revoke' and grant_id = any($1::uuid[])`,
[legacyIds],
);
// One audit row per grant actually revoked, each carrying that row's own id.
expect(auditRows).toHaveLength(legacyIds.length);
expect([...new Set(auditRows.map((row) => row.grant_id))].sort()).toEqual(
[...legacyIds].sort(),
);
// Differently scoped and differently sourced grants are untouched.
const untouched = await safeQuery<{ revoked_at: Date | null }>(
admin,
`select revoked_at from "ekklesia_core_grant" where id = any($1::uuid[])`,
[[scopedNeighbour[0]?.id, relationshipNeighbour[0]?.id]],
);
expect(untouched.every((row) => row.revoked_at === null)).toBe(true);
// No equivalent issuance continues authorizing the user.
expect(
await store.hasActiveGrant({
now: new Date(),
permission: legacyPermission,
userId: subject,
}),
).toBe(false);
});
it("serializes a concurrent create and revoke on the same tuple without leaving an unexplained duplicate", async () => {
const racePermission = parsePermissionId("calendar.events.race");
const seed = await store.createGrant({
actor,
permission: racePermission,
sourceKind: "explicit",
userId: subject,
});
const [revokeResult, createResult] = await Promise.allSettled([
store.revokeGrant({ actor, grantId: seed.id, sourceKind: "explicit" }),
store.createGrant({
actor,
permission: racePermission,
sourceKind: "explicit",
userId: subject,
}),
]);
// Whichever order the two transactions serialize in, the end state is consistent with that
// order: either the create lost to an effective equivalent (0 effective), or it ran after the
// revoke (1 effective). Two effective equivalents is the state this pass makes impossible.
expect(revokeResult.status).toBe("fulfilled");
const effective = await effectiveCount(subject, racePermission);
expect(effective).toBeLessThanOrEqual(1);
if (createResult.status === "rejected") {
expect(createResult.reason).toMatchObject({ code: "AUTHORIZATION_GRANT_ALREADY_EXISTS" });
expect(effective).toBe(0);
} else {
expect(effective).toBe(1);
}
});
it("rolls back every grant and audit change when the multi-row revocation transaction fails", async () => {
const doomedPermission = parsePermissionId("calendar.events.doomed");
const rows = await safeQuery<{ id: string }>(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id)
values ($1, $2, null, 'explicit', null, 'user', $3), ($1, $2, null, 'explicit', null, 'user', $3)
returning id`,
[subject, doomedPermission, other],
);
// Force the audit insert inside the revoke transaction to violate a real constraint: the audit
// action CHECK only permits the four known actions, so a temporary trigger that rewrites the
// action makes the whole multi-row revocation fail after the grant updates were staged.
await safeQuery(
admin,
`create function ekklesia_test_break_audit() returns trigger as $$
begin new.action := 'not.a.real.action'; return new; end;
$$ language plpgsql`,
);
await safeQuery(
admin,
`create trigger ekklesia_test_break_audit_trigger before insert on "ekklesia_core_authorization_audit"
for each row execute function ekklesia_test_break_audit()`,
);
try {
let captured: unknown;
try {
await store.revokeGrant({
actor,
grantId: parseGrantId(rows[0]?.id as string),
sourceKind: "explicit",
});
} catch (error) {
captured = error;
}
expect(captured).toMatchObject({ code: "AUTHORIZATION_GRANT_REVOKE_FAILED" });
const after = await safeQuery<{ revoked_at: Date | null }>(
admin,
`select revoked_at from "ekklesia_core_grant" where id = any($1::uuid[])`,
[rows.map((row) => row.id)],
);
expect(after.every((row) => row.revoked_at === null)).toBe(true);
const audit = await safeQuery(
admin,
`select id from "ekklesia_core_authorization_audit" where permission_id = $1`,
[doomedPermission],
);
expect(audit).toHaveLength(0);
} finally {
// Always restored, even when an assertion above fails.
await safeQuery(
admin,
`drop trigger if exists ekklesia_test_break_audit_trigger on "ekklesia_core_authorization_audit"`,
);
await safeQuery(admin, `drop function if exists ekklesia_test_break_audit()`);
}
});
it("reports duplicate conflict, not-found, and infrastructure failure as three distinct sanitized outcomes", async () => {
const codes: string[] = [];
try {
await store.createGrant({ actor, permission, sourceKind: "explicit", userId: subject });
} catch (error) {
codes.push((error as AuthorizationStoreError).code);
}
try {
await store.revokeGrant({
actor,
grantId: parseGrantId("00000000-0000-4000-8000-000000000000"),
sourceKind: "explicit",
});
} catch (error) {
codes.push((error as AuthorizationStoreError).code);
}
await close();
try {
await store.hasActiveGrant({ now: new Date(), permission, userId: subject });
} catch (error) {
codes.push((error as AuthorizationStoreError).code);
}
expect(codes).toEqual([
"AUTHORIZATION_GRANT_ALREADY_EXISTS",
"AUTHORIZATION_GRANT_NOT_FOUND",
"AUTHORIZATION_GRANT_LOOKUP_FAILED",
]);
await safeEnd(admin);
});
});
describe.sequential("live Core authorization store — bounded keyset pagination", () => {
let database: { name: string; url: string };
let admin: Client;
let store: ReturnType<typeof createAuthorizationStore>;
let close: () => Promise<void>;
const subject = parseEkklesiaUserId("page_subject");
it("seeds explicit and relationship grants, including several sharing one created_at", async () => {
database = await createDisposableDatabase("pagination");
await applyCoreMigrations(database.url);
admin = await openAdministrativeClient(database.url);
await seedUser(admin, subject, "[email protected]");
const handle = createAuthorizationDrizzleDatabase(database.url);
close = handle.close;
store = createAuthorizationStore(handle.db);
// Seven explicit grants all sharing one identical `created_at`, which is exactly the case a
// timestamp-only keyset boundary would skip or repeat, plus relationship grants that must
// never appear on any page.
await safeQuery(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id, created_at)
select $1, 'calendar.events.p' || generation, null, 'explicit', null, 'user', $1,
timestamptz '2026-01-01 00:00:00Z'
from generate_series(1, 7) as generation`,
[subject],
);
await safeQuery(
admin,
`insert into "ekklesia_core_grant"
(user_id, permission_id, scope, source_kind, source_ref, created_by_actor_type, created_by_actor_id, created_at)
select $1, 'calendar.events.r' || generation, null, 'relationship', 'roster:' || generation, 'user', $1,
timestamptz '2026-01-01 00:00:00Z'
from generate_series(1, 3) as generation`,
[subject],
);
expect(
await safeQuery(admin, `select id from "ekklesia_core_grant" where user_id = $1`, [subject]),
).toHaveLength(10);
});
it("traverses every explicit grant exactly once across pages, excluding relationship grants", async () => {
const seen: string[] = [];
let after: { createdAt: Date; id: GrantId } | undefined;
let pages = 0;
for (;;) {
const page = await store.listGrantsForUser(subject, {
...(after === undefined ? {} : { after }),
limit: 3,
sourceKind: "explicit",
});
pages += 1;
seen.push(...page.grants.map((grant) => grant.id));
expect(page.grants.every((grant) => grant.sourceKind === "explicit")).toBe(true);
if (page.nextBoundary === undefined) {
expect(page.grants.length).toBeLessThanOrEqual(3);
break;
}
after = page.nextBoundary;
expect(pages).toBeLessThan(10);
}
// 7 explicit rows at limit 3 → pages of 3, 3, 1. No duplicates, no omissions, deterministic
// despite every row sharing one created_at.
expect(pages).toBe(3);
expect(seen).toHaveLength(7);
expect(new Set(seen).size).toBe(7);
// Ordering is created_at DESC, id DESC — with equal timestamps that is a pure descending id
// sort, which is exactly what makes traversal total and repeatable.
expect([...seen].sort().reverse()).toEqual(seen);
});
it("honours the 1 and 100 boundaries and rejects anything outside them", async () => {
expect(
(await store.listGrantsForUser(subject, { limit: 1, sourceKind: "explicit" })).grants,
).toHaveLength(1);
const all = await store.listGrantsForUser(subject, { limit: 100, sourceKind: "explicit" });
expect(all.grants).toHaveLength(7);
expect(all.nextBoundary).toBeUndefined();
for (const limit of [0, 101, -1, 1.5]) {
await expect(store.listGrantsForUser(subject, { limit })).rejects.toMatchObject({
code: "AUTHORIZATION_GRANT_LIMIT_INVALID",
});
}
await close();
await safeEnd(admin);
});
});
+78 -1
View File
@@ -4,8 +4,13 @@ const poolConstructor = vi.hoisted(() => vi.fn());
vi.mock("pg", () => ({ Pool: poolConstructor }));
import { parseEkklesiaUserId, parsePermissionId, SYSTEM_ACTOR } from "@ekklesia/contracts";
import { createAuthorizationDrizzleDatabase } from "../src/authorization/database.js";
import { AuthorizationStoreError, createAuthorizationStore } from "../src/authorization/store.js";
import {
AUTHORIZATION_GRANT_LIST_MAX_LIMIT,
AuthorizationStoreError,
createAuthorizationStore,
} from "../src/authorization/store.js";
describe("Core authorization store — import and construction safety", () => {
it("does not construct a pool or open a connection merely because the module is imported", () => {
@@ -33,7 +38,11 @@ describe("Core authorization store — import and construction safety", () => {
describe("AuthorizationStoreError", () => {
it.each([
"AUTHORIZATION_GRANT_ALREADY_EXISTS",
"AUTHORIZATION_GRANT_CREATE_FAILED",
"AUTHORIZATION_GRANT_EXPIRATION_INVALID",
"AUTHORIZATION_GRANT_LIMIT_INVALID",
"AUTHORIZATION_GRANT_LIST_FAILED",
"AUTHORIZATION_GRANT_LOOKUP_FAILED",
"AUTHORIZATION_GRANT_NOT_FOUND",
"AUTHORIZATION_GRANT_REVOKE_FAILED",
@@ -56,3 +65,71 @@ describe("AuthorizationStoreError", () => {
);
});
});
/**
* Input validation the store performs *before* any transaction opens, so a rejected value performs
* no database work at all. The fake database below records every call it receives; asserting it
* stayed empty is what proves "no grant and no audit row" rather than merely "an error was
* thrown". `createAuthorizationStore`'s injectable clock keeps this deterministic without
* arbitrary sleeps or real-time flakiness.
*/
describe("Core authorization store — pre-database input validation", () => {
const fixedNow = new Date("2026-07-26T12:00:00.000Z");
function recordingStore() {
const calls: string[] = [];
poolConstructor.mockImplementation(function fakePool() {
return { end: async () => undefined, on: vi.fn(), query: vi.fn() };
});
const { db } = createAuthorizationDrizzleDatabase("postgresql://example.test/ekklesia");
const instrumented = new Proxy(db, {
get(target, property, receiver) {
if (property === "transaction" || property === "select" || property === "execute") {
calls.push(String(property));
}
return Reflect.get(target, property, receiver) as unknown;
},
});
return { calls, store: createAuthorizationStore(instrumented, { now: () => fixedNow }) };
}
it.each([
["a past instant", new Date("2020-01-01T00:00:00.000Z")],
["exactly the acceptance clock", new Date("2026-07-26T12:00:00.000Z")],
["an invalid Date", new Date("nonsense")],
])("rejects %s expiration with no database work at all", async (_label, expiresAt) => {
const { calls, store } = recordingStore();
await expect(
store.createGrant({
actor: SYSTEM_ACTOR,
expiresAt,
permission: parsePermissionId("calendar.events.read"),
sourceKind: "explicit",
userId: parseEkklesiaUserId("subject_1"),
}),
).rejects.toMatchObject({ code: "AUTHORIZATION_GRANT_EXPIRATION_INVALID" });
expect(calls).toEqual([]);
});
it.each([
0,
-1,
101,
1.5,
Number.NaN,
Number.POSITIVE_INFINITY,
Number.MAX_SAFE_INTEGER + 2,
])("rejects the unusable page size %s with no database work at all", async (limit) => {
const { calls, store } = recordingStore();
await expect(
store.listGrantsForUser(parseEkklesiaUserId("subject_1"), { limit }),
).rejects.toMatchObject({ code: "AUTHORIZATION_GRANT_LIMIT_INVALID" });
expect(calls).toEqual([]);
});
it("exposes one shared maximum page size rather than two that could drift", () => {
expect(AUTHORIZATION_GRANT_LIST_MAX_LIMIT).toBe(100);
});
});