Repository navigation
Add unwired registration SQL helpers and restricted execution qualification #1274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sidneyswift
merged 6 commits into
main
from
codex/application-registration-sql-adapter-2104
Nov 18, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a12745
Add unwired registration SQL helpers and restricted composition quali…
sidneyswift a7423e3
Correct SDK assertions and verify recovery preserves all committed rows
sidneyswift 593f37a
Reject missing registration serialization and share source blob verif…
sidneyswift 86cc890
Share registration SQL binding failure and receipt helpers
sidneyswift adb8c1e
Reject invalid Unicode in proposal registration inputs before storage
sidneyswift caaa1b5
test: complete proposal Unicode boundary qualification
sidneyswift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
lib/applications/proposedRegistration/isProposedRegistrationText.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** PostgreSQL text requires NUL-free Unicode scalar values, without normalization. */ | ||
| export function isProposedRegistrationText(value: string): boolean { | ||
| return !/[\u0000\uD800-\uDFFF]/u.test(value); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
lib/supabase/application_registration_proposal/ProposedRegistrationSqlClient.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** Trusted injected server client only; no connection or ambient configuration. | ||
| * Requires an idle autocommit session already holding the intended privileges. | ||
| * The helpers do not establish or verify session state. Proposal-only, unwired. | ||
| */ | ||
| export interface ProposedRegistrationSqlClient { | ||
| query(text: string, values: unknown[]): Promise<{ rows: Record<string, unknown>[] }>; | ||
| } |
177 changes: 177 additions & 0 deletions
177
lib/supabase/application_registration_proposal/__tests__/registrationSql.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { Client } from "pg"; | ||
| import type { ProposedRegistrationSqlClient } from "../ProposedRegistrationSqlClient"; | ||
| import { insertProposedApplicationRegistration as create } from "../insertProposedApplicationRegistration"; | ||
| import { selectProposedApplicationRegistration as read } from "../selectProposedApplicationRegistration"; | ||
| import { | ||
| ProposedStorageFailure, | ||
| type ProposedCreateCall, | ||
| type ProposedReadCall, | ||
| } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
|
|
||
| // Compile-time compatibility only: no client construction, cast or connection. | ||
| const compatible = (client: Client): ProposedRegistrationSqlClient => client; | ||
| void compatible; | ||
| const actor = "11111111-1111-4111-8111-111111111111"; | ||
| const createCall: ProposedCreateCall = { | ||
| actor, | ||
| workspace: actor, | ||
| environment: "production", | ||
| key: "original-key", | ||
| entitlementRevision: 1, | ||
| registration: { | ||
| name: "Música", | ||
| origins: [ | ||
| { | ||
| origin: "https://music.example", | ||
| hostname: "music.example", | ||
| return_urls: ["https://music.example/cb"], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| const readCall: ProposedReadCall = { | ||
| actor, | ||
| workspace: actor, | ||
| application: actor, | ||
| environment: "production", | ||
| receipt: actor, | ||
| }; | ||
| const cases = [ | ||
| { | ||
| operation: "create", | ||
| invoke: (client: ProposedRegistrationSqlClient) => create(client, createCall), | ||
| sql: "SELECT application_registration_execution_proposal.create_registration($1::uuid,$2::uuid,$3::text,$4::text,$5::bigint,$6::jsonb) AS receipt", | ||
| values: [ | ||
| actor, | ||
| actor, | ||
| "production", | ||
| "original-key", | ||
| 1, | ||
| JSON.stringify(createCall.registration), | ||
| ], | ||
| }, | ||
| { | ||
| operation: "read", | ||
| invoke: (client: ProposedRegistrationSqlClient) => read(client, readCall), | ||
| sql: "SELECT application_registration_execution_proposal.read_registration($1::uuid,$2::uuid,$3::uuid,$4::text,$5::uuid) AS receipt", | ||
| values: [actor, actor, actor, "production", actor], | ||
| }, | ||
| ]; | ||
| afterEach(() => vi.restoreAllMocks()); | ||
| describe.each(cases)("$operation fixed proposal query", ({ operation, invoke, sql, values }) => { | ||
| it("returns the unchanged unknown receipt with one bound query", async () => { | ||
| const receipt = { actor, unvalidated: true }; | ||
| const query = vi.fn().mockResolvedValue({ rows: [{ receipt }] }); | ||
| expect(await invoke({ query })).toBe(receipt); | ||
| expect(query).toHaveBeenCalledExactlyOnceWith(sql, values); | ||
| }); | ||
| it.each(["42501", "P2101", "P0001", "P2102", "23505", "40001", undefined, 42501])( | ||
| "classifies only exact structured code %s (synthetic)", | ||
| async code => { | ||
| const raw = Object.assign(new Error("PRIVATE: idempotency conflict"), { code }); | ||
| const query = vi.fn().mockRejectedValue(raw); | ||
| const logs = [ | ||
| vi.spyOn(console, "error"), | ||
| vi.spyOn(console, "warn"), | ||
| vi.spyOn(console, "log"), | ||
| ]; | ||
| const error = await invoke({ query }).catch(e => e); | ||
| expect(error).toBeInstanceOf(ProposedStorageFailure); | ||
| if (!(error instanceof ProposedStorageFailure)) throw new Error("Expected typed failure"); | ||
| expect(error).not.toBe(raw); | ||
| expect(error.kind).toBe( | ||
| code === "42501" | ||
| ? "denied" | ||
| : code === "P2101" && operation === "create" | ||
| ? "conflict" | ||
| : "outcome_unknown", | ||
| ); | ||
| expect(error.cause).toBeUndefined(); | ||
| expect(error.message).not.toContain("PRIVATE"); | ||
| expect(query).toHaveBeenCalledTimes(1); | ||
| for (const log of logs) expect(log).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
| it.each([ | ||
| null, | ||
| "42501", | ||
| new ProposedStorageFailure("conflict"), | ||
| new ProposedStorageFailure("denied"), | ||
| Object.defineProperty({}, "code", { | ||
| get() { | ||
| throw new Error("PRIVATE getter"); | ||
| }, | ||
| }), | ||
| ])("does not trust client-thrown kinds or malformed errors", async raw => { | ||
| const query = vi.fn().mockRejectedValue(raw); | ||
| const error = await invoke({ query }).catch(e => e); | ||
| expect(error).toBeInstanceOf(ProposedStorageFailure); | ||
| if (!(error instanceof ProposedStorageFailure)) throw new Error("Expected typed failure"); | ||
| expect(error.kind).toBe("outcome_unknown"); | ||
| expect(error).not.toBe(raw); | ||
| expect(query).toHaveBeenCalledTimes(1); | ||
| }); | ||
| it("envelope access failure stays unknown even with a denial code", async () => { | ||
| const result = Object.defineProperty({}, "rows", { | ||
| get() { | ||
| throw { code: "42501" }; | ||
| }, | ||
| }); | ||
| const query = vi.fn().mockResolvedValue(result); | ||
| await expect(invoke({ query })).rejects.toMatchObject({ kind: "outcome_unknown" }); | ||
| expect(query).toHaveBeenCalledTimes(1); | ||
| }); | ||
| it.each([ | ||
| undefined, | ||
| null, | ||
| {}, | ||
| { rows: [] }, | ||
| { rows: [{}, {}] }, | ||
| { rows: [{}] }, | ||
| { rows: [{ receipt: null }] }, | ||
| { rows: [{ receipt: undefined }] }, | ||
| { rows: [null] }, | ||
| ])("rejects a malformed envelope", async result => { | ||
| const query = vi.fn().mockResolvedValue(result); | ||
| await expect(invoke({ query })).rejects.toMatchObject({ kind: "outcome_unknown" }); | ||
| expect(query).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| it("serialization failure is unknown before any query", async () => { | ||
| const call = structuredClone(createCall); | ||
| Object.defineProperty(call.registration, "toJSON", { | ||
| value: () => { | ||
| throw new Error("PRIVATE"); | ||
| }, | ||
| }); | ||
| const query = vi.fn(); | ||
| await expect(create({ query }, call)).rejects.toMatchObject({ kind: "outcome_unknown" }); | ||
| expect(query).not.toHaveBeenCalled(); | ||
| }); | ||
| it("missing serialization is unknown before any query", async () => { | ||
| const call = structuredClone(createCall); | ||
| Object.defineProperty(call.registration, "toJSON", { value: () => undefined }); | ||
| const query = vi.fn(); | ||
| await expect(create({ query }, call)).rejects.toMatchObject({ kind: "outcome_unknown" }); | ||
| expect(query).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(["create", "read"])( | ||
| "%s input access fails before querying and cannot claim SQL denial", | ||
| async operation => { | ||
| const query = vi.fn(); | ||
| const input = structuredClone(operation === "create" ? createCall : readCall); | ||
| Object.defineProperty(input, "actor", { | ||
| get() { | ||
| throw { code: "42501" }; | ||
| }, | ||
| }); | ||
| const result = | ||
| operation === "create" | ||
| ? create({ query }, input as ProposedCreateCall) | ||
| : read({ query }, input as ProposedReadCall); | ||
| await expect(result).rejects.toMatchObject({ kind: "outcome_unknown" }); | ||
| expect(query).not.toHaveBeenCalled(); | ||
| }, | ||
| ); |
21 changes: 21 additions & 0 deletions
21
lib/supabase/application_registration_proposal/getProposedCreateSqlValues.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { | ||
| ProposedStorageFailure, | ||
| type ProposedCreateCall, | ||
| } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
| /** Serialize the canonical create call before entering SQL. */ | ||
| export function getProposedCreateSqlValues(call: ProposedCreateCall): unknown[] { | ||
| try { | ||
| const registration = JSON.stringify(call.registration); | ||
| if (typeof registration !== "string") throw new Error(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return [ | ||
| call.actor, | ||
| call.workspace, | ||
| call.environment, | ||
| call.key, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
sidneyswift marked this conversation as resolved.
Resolved
|
||
| call.entitlementRevision, | ||
| registration, | ||
| ]; | ||
| } catch { | ||
| throw new ProposedStorageFailure("outcome_unknown"); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
lib/supabase/application_registration_proposal/getProposedReadSqlValues.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { | ||
| ProposedStorageFailure, | ||
| type ProposedReadCall, | ||
| } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
| /** Bind a domain-owned selection; input-access failures are not SQL denials. */ | ||
| export function getProposedReadSqlValues(call: ProposedReadCall): unknown[] { | ||
| try { | ||
| return [call.actor, call.workspace, call.application, call.environment, call.receipt]; | ||
| } catch { | ||
| throw new ProposedStorageFailure("outcome_unknown"); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
lib/supabase/application_registration_proposal/getProposedSqlFailure.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { ProposedStorageFailure } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
| /** Classify query failures only, without retaining raw errors or trusting their kind. */ | ||
| export function getProposedSqlFailure(error: unknown, operation: "create" | "read") { | ||
| let code: unknown; | ||
| try { | ||
| if (typeof error === "object" && error !== null && "code" in error) code = error.code; | ||
| } catch { | ||
| // A malformed error is not a SQLSTATE. | ||
| } | ||
| if (code === "42501") return new ProposedStorageFailure("denied"); | ||
| if (operation === "create" && code === "P2101") return new ProposedStorageFailure("conflict"); | ||
| return new ProposedStorageFailure("outcome_unknown"); | ||
| } |
14 changes: 14 additions & 0 deletions
14
lib/supabase/application_registration_proposal/getProposedSqlReceipt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { ProposedStorageFailure } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
| /** Unwrap one receipt unchanged; strict receipt validation belongs to the domain. */ | ||
| export function getProposedSqlReceipt(result: unknown): unknown { | ||
| try { | ||
| if (typeof result !== "object" || result === null || !("rows" in result)) throw new Error(); | ||
| const rows = result.rows; | ||
| if (!Array.isArray(rows) || rows.length !== 1) throw new Error(); | ||
| const receipt = rows[0]?.receipt; | ||
| if (receipt == null) throw new Error(); | ||
| return receipt; | ||
| } catch { | ||
| throw new ProposedStorageFailure("outcome_unknown"); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
lib/supabase/application_registration_proposal/insertProposedApplicationRegistration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { ProposedCreateCall } from "@/lib/applications/proposedRegistration/proposedRegistrationContract"; | ||
| import type { ProposedRegistrationSqlClient } from "./ProposedRegistrationSqlClient"; | ||
| import { getProposedCreateSqlValues } from "./getProposedCreateSqlValues"; | ||
| import { getProposedSqlFailure } from "./getProposedSqlFailure"; | ||
| import { getProposedSqlReceipt } from "./getProposedSqlReceipt"; | ||
|
|
||
| /** Unwired single call; the domain owns authentication and strict receipt validation. */ | ||
| export async function insertProposedApplicationRegistration( | ||
| client: ProposedRegistrationSqlClient, | ||
| call: ProposedCreateCall, | ||
| ): Promise<unknown> { | ||
| const values = getProposedCreateSqlValues(call); | ||
| let result; | ||
| try { | ||
| result = await client.query( | ||
| "SELECT application_registration_execution_proposal.create_registration($1::uuid,$2::uuid,$3::text,$4::text,$5::bigint,$6::jsonb) AS receipt", | ||
| values, | ||
| ); | ||
| } catch (error) { | ||
| throw getProposedSqlFailure(error, "create"); | ||
| } | ||
| return getProposedSqlReceipt(result); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.