Startup proves the database can take Heimdall's writes

Soul pass 4, F4: the startup check passed on databases where every request
still failed: a role granted SELECT only (O2), and a table whose primary
key and key column were dropped (N), including on a start that applied the
schema, since CREATE ... IF NOT EXISTS leaves a damaged table as it is.

Every start now ends in checkSchema, after applying the schema or not. It
requires, read from the schema SQL: every relation; every column the
created tables declare, not only the added one (read from pg_attribute, so
a column the role may not see is not reported as missing); every primary
key and unique constraint as a non-partial unique index on exactly its
columns, which is what the store's ON CONFLICT clauses and the foreign
keys rely on; and SELECT, INSERT and UPDATE on every table. A missing
piece refuses with SCHEMA_MISSING, a missing privilege with
SCHEMA_PRIVILEGES, through the same fixed startup message.

A live-Postgres block drives createStore itself, skipped unless
HEIMDALL_TEST_PG_ADMIN_URL names a role that may create databases and
roles: a read-only role (and its canary password stays out of the
message), a dropped primary key with and without applying, a unique key
replaced by a partial one, and a dropped column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 17:15:34 +02:00
commit 8541ae3f2f
4 changed files with 261 additions and 40 deletions

View file

@ -14,10 +14,15 @@ export async function createStore(config: HeimdallConfig): Promise<HeimdallStore
const store = createPostgresStore(config.storage.databaseUrl ?? "postgres://127.0.0.1/heimdall");
try {
// Every start connects, and one that does not apply the schema proves it
// is there, so a misbound URL or an empty database fails here rather than
// Every start connects and proves the database can take its requests,
// whether or not it applied the schema first (CREATE ... IF NOT EXISTS
// leaves a damaged table as it is), so a misbound URL, an empty or
// damaged database, or a role that may not write fails here rather than
// in the first request.
await (config.storage.applySchemaOnStartup ? store.ensureSchema() : store.checkSchema());
if (config.storage.applySchemaOnStartup) {
await store.ensureSchema();
}
await store.checkSchema();
} catch (error) {
// pg's and Node's errors name the host, the database or the URL itself,
// and any of those can hold part of a secret that was bound or escaped

View file

@ -1,7 +1,14 @@
import { randomUUID } from "node:crypto";
import { Pool, type PoolClient, type QueryResultRow } from "pg";
import { type AppSlug, type HeimdallAuthAttemptStatus, type LinkedIdentityInput, type Provider } from "../contracts.js";
import { CREATE_SCHEMA_SQL, REQUIRED_COLUMNS, REQUIRED_RELATIONS } from "./schema.js";
import {
CREATE_SCHEMA_SQL,
REQUIRED_COLUMNS,
REQUIRED_KEYS,
REQUIRED_RELATIONS,
REQUIRED_TABLE_PRIVILEGES,
REQUIRED_TABLES,
} from "./schema.js";
import {
type CreateAccountInput,
type CreateAuthAttemptInput,
@ -292,33 +299,65 @@ export class PostgresStore implements HeimdallStore {
}
/**
* Proves, for a start that does not apply the schema, that the URL connects
* and that the database holds every relation and column the schema makes.
* A missing one fails with code SCHEMA_MISSING.
* Proves, before Heimdall serves, that the database can take its requests:
* every relation, column and key the schema makes is there, and the role
* may read, insert and update every table. A missing piece fails with code
* SCHEMA_MISSING and a missing privilege with SCHEMA_PRIVILEGES; neither
* error names the piece.
*/
async checkSchema(): Promise<void> {
const result = await this.pool.query<{ missing: string }>(
const result = await this.pool.query<{ kind: "missing" | "privilege" }>(
`
SELECT name AS missing FROM unnest($1::text[]) AS name WHERE to_regclass(name) IS NULL
SELECT 'missing' AS kind FROM unnest($1::text[]) AS name WHERE to_regclass(name) IS NULL
UNION ALL
SELECT required.table_name || '.' || required.column_name
FROM unnest($2::text[], $3::text[]) AS required(table_name, column_name)
SELECT 'missing' FROM unnest($2::text[], $3::text[]) AS required(table_name, column_name)
WHERE NOT EXISTS (
SELECT 1 FROM information_schema.columns c
WHERE c.table_schema = current_schema()
AND c.table_name = required.table_name
AND c.column_name = required.column_name
SELECT 1 FROM pg_attribute a
WHERE a.attrelid = to_regclass(required.table_name)
AND a.attname = required.column_name
AND a.attnum > 0
AND NOT a.attisdropped
)
UNION ALL
SELECT 'missing' FROM unnest($4::text[], $5::text[], $6::boolean[]) AS required(table_name, columns, is_primary)
WHERE NOT EXISTS (
SELECT 1 FROM pg_index i
WHERE i.indrelid = to_regclass(required.table_name)
AND i.indisunique
AND i.indpred IS NULL
AND (i.indisprimary OR NOT required.is_primary)
AND i.indnatts = cardinality(string_to_array(required.columns, ','))
AND (
SELECT string_agg(a.attname, ',' ORDER BY a.attname)
FROM unnest(i.indkey::int2[]) AS key(attnum)
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = key.attnum
) = required.columns
)
UNION ALL
SELECT 'privilege' FROM unnest($7::text[]) AS required(table_name)
CROSS JOIN unnest($8::text[]) AS wanted(privilege)
WHERE to_regclass(required.table_name) IS NOT NULL
AND NOT has_table_privilege(to_regclass(required.table_name), wanted.privilege)
`,
[
REQUIRED_RELATIONS,
REQUIRED_COLUMNS.map((required) => required.table),
REQUIRED_COLUMNS.map((required) => required.column),
REQUIRED_KEYS.map((required) => required.table),
REQUIRED_KEYS.map((required) => required.columns),
REQUIRED_KEYS.map((required) => required.primary),
REQUIRED_TABLES,
REQUIRED_TABLE_PRIVILEGES,
]
);
if (result.rows.length > 0) {
if (result.rows.some((row) => row.kind === "missing")) {
throw Object.assign(new Error("The Heimdall schema is not applied."), { code: "SCHEMA_MISSING" });
}
if (result.rows.length > 0) {
throw Object.assign(new Error("The Heimdall database role lacks a privilege the store needs."), {
code: "SCHEMA_PRIVILEGES",
});
}
}
async close(): Promise<void> {

View file

@ -163,15 +163,60 @@ CREATE INDEX IF NOT EXISTS audit_events_lookup_idx
ON audit_events(account_id, session_id, app_slug, created_at);
`;
/**
* What a start that does not apply the schema must find, read from the SQL
* above so the two cannot drift: every table and index it creates, and every
* column it adds to an existing table.
const schemaText = CREATE_SCHEMA_SQL.replace(/\r\n/g, "\n");
const createdTables = [...schemaText.matchAll(/^CREATE TABLE IF NOT EXISTS (\w+) \(\n([\s\S]*?)\n\);/gm)].map((match) => ({
table: match[1]!,
lines: match[2]!.split("\n").map((line) => line.trim().replace(/,$/, "")),
}));
/** The sorted, comma-separated columns of a `PRIMARY KEY (a, b)` or `UNIQUE(a, b)` line. */
function keyColumns(line: string, keyword: "PRIMARY KEY" | "UNIQUE"): string | undefined {
const match = new RegExp(`^${keyword}\\s*\\(([^)]*)\\)`).exec(line);
return match ? match[1]!.split(",").map((column) => column.trim()).sort().join(",") : undefined;
}
/*
* What every start must find before it serves, read from the SQL above so the
* two cannot drift. A start that applies the schema checks as well, because
* CREATE ... IF NOT EXISTS leaves a damaged table as it is.
*/
/** Every table and index the schema creates. */
export const REQUIRED_RELATIONS = [
...CREATE_SCHEMA_SQL.matchAll(/^CREATE (?:UNIQUE )?(?:TABLE|INDEX) IF NOT EXISTS (\w+)/gm),
...schemaText.matchAll(/^CREATE (?:UNIQUE )?(?:TABLE|INDEX) IF NOT EXISTS (\w+)/gm),
].map((match) => match[1]!);
/** Every table the schema creates. The store reads and writes each one. */
export const REQUIRED_TABLES = createdTables.map(({ table }) => table);
/** What the store does to its tables: it reads, inserts and updates, and never deletes. */
export const REQUIRED_TABLE_PRIVILEGES = ["SELECT", "INSERT", "UPDATE"];
/** Every column a created table declares, and every column the schema adds to an existing table. */
export const REQUIRED_COLUMNS = [
...CREATE_SCHEMA_SQL.matchAll(/^ALTER TABLE (\w+) ADD COLUMN IF NOT EXISTS (\w+)/gm),
].map((match) => ({ table: match[1]!, column: match[2]! }));
...createdTables.flatMap(({ table, lines }) =>
lines
.filter((line) => !/^(?:PRIMARY KEY|UNIQUE)\b/.test(line))
.map((line) => ({ table, column: /^(\w+)/.exec(line)![1]! }))
),
...[...schemaText.matchAll(/^ALTER TABLE (\w+) ADD COLUMN IF NOT EXISTS (\w+)/gm)].map((match) => ({
table: match[1]!,
column: match[2]!,
})),
];
/**
* Every primary key and unique constraint a created table declares, by its
* sorted columns. Foreign keys reference the primary keys, and each of the
* store's ON CONFLICT clauses needs a unique index on exactly its columns:
* without one, every such write fails.
*/
export const REQUIRED_KEYS = createdTables.flatMap(({ table, lines }) =>
lines.flatMap((line) => {
const inline = /^(\w+) .*\bPRIMARY KEY\b/.exec(line);
const columns = inline?.[1] ?? keyColumns(line, "PRIMARY KEY");
if (columns) return [{ table, columns, primary: true }];
const unique = keyColumns(line, "UNIQUE");
return unique ? [{ table, columns: unique, primary: false }] : [];
})
);