Files
accounted/tests/schema/schema-guard.ts
T
Mattsson f24b26a139 fix: similar-sweep currency remediation, security hardening and v1 API fixes (#1215)
* fix(security): gate replace_sie_import behind owner/admin membership

The RPC was SECURITY DEFINER with EXECUTE granted to PUBLIC and anon, no
company_members lookup, no auth.uid() reference and no unauthorized raise,
while setting gnubok.allow_delete to disarm the BFL immutability and
retention triggers. Any caller holding a company_id and an import id could
hard delete another tenant's verifikationer. Confirmed live in production.

Applies the same fail closed owner/admin guard that undo_sie_import already
carries (migration 20260624120000), resolving the actor from
COALESCE(p_user_id, auth.uid()) so it denies when the role is NULL, then
revokes EXECUTE from PUBLIC and anon. search_path and the raised
statement_timeout are restated, since CREATE OR REPLACE drops settings that
are not repeated.

userId is a required parameter on replaceSIEImport: the service client has a
NULL auth.uid(), so a caller without an explicit actor now fails to compile
rather than hitting the closed gate at runtime.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(security): validate arcim OAuth callback state server side

The callback route is skipAuth and decoded the state parameter as plain
base64url JSON, trusting consentId and provider from it. A one time code was
minted at flow start and never read. An unauthenticated attacker who learned
a consent id could run an OAuth flow on their own provider account and post
the callback with a forged state, landing their tokens on another tenant's
consent, so the victim's next migration imported the attacker's ledger.

State is now an opaque randomBytes(32) pointer to a provider_otc row,
consumed by a single atomic UPDATE guarded on used_at IS NULL and
expires_at, so a replay loses the row lock race and updates nothing.
provider is read from provider_consents rather than trusted from the client.
provider_otc already existed for exactly this purpose and was never wired up.

Also scopes getConsent to an owning company, closing a cross tenant status
oracle where the preview and migrate paths echoed a consent's status before
the scoped check ran.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(security): scope documents storage to company_id (phase A)

The documents bucket policies matched on auth.uid(), and upload keys were
documents/{userId}/..., so company membership was never consulted. Removing a
member revoked nothing: their session still authenticated and they kept
direct Storage read access to every receipt, supplier invoice and bank
statement they had uploaded. The same bug was fixed for sie-files in
20260416120000; this bucket was left behind.

Phase A is additive. Company scoped policies are added alongside the
uploader scoped ones, uploads move to documents/{companyId}/{userId}/..., and
reads accept either layout so nothing breaks mid migration. Phase C, which
drops the old policies, is gated on the backfill reporting zero remaining
legacy prefix objects.

The policy compares the company segment as text rather than casting to uuid
the way sie-files does: this bucket holds keys whose second segment is not a
uuid (MCP audit packages), and Postgres does not guarantee the bucket prefix
qual runs before the cast, so a planner reordering would raise 22P02 and fail
the whole query instead of filtering the row out.

deleteDocument now removes both candidate keys. Removing only the stored
pointer would leave a readable orphan copy of a document the user asked to
erase.

The backfill script is included but has never been run. It defaults to dry
run, refuses .env.local by name, and verifies each copy is readable and
SHA-256 identical before repointing the row.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(security): enforce events:read scope and membership on /api/events

This was the only one of the three validateApiKey call sites with no
downstream guard: v1 and the MCP server both check scope and re-verify
company membership, this route did neither. An events:read scope existed and
was documented as gating the endpoint but was never called, so a legacy key
falling back to DEFAULT_SCOPES read the full log. The bound company id went
straight from the api_keys row into a service role query, so a key whose user
had been removed from the company kept reading.

Adds the scope check before any database access, re-verifies company_members
with archived_at IS NULL, honours test mode by stamping X-Gnubok-Mode instead
of ignoring it, applies minimisePayload so the pull surface can never return
a wider payload than the push surface, and replaces the three flat error
strings with the canonical envelope.

Test key reads are served rather than blocked: TEST_KEY_WRITE_BLOCKED is
gated on mutations in with-api-v1, so a read gets the same treatment as every
other v1 read endpoint.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* perf(bookkeeping): sweep remaining journal_entries!inner embeds

A previous refactor removed this pattern from lib/reports and introduced
fetchEntryLines, but the class was never swept. Seventeen sites remained and
had become the top application consumer of production database time:
measured across the resulting query shapes, 32,694 calls and 25,848 seconds
of execution, mean 790ms, with shapes averaging 2.6s and 3.0s and maxing at
7,962ms against the 8s statement_timeout, which surfaced to users as 500s on
the booking path.

PostgREST compiles an embed with filters on the embedded side into a
correlated INNER JOIN LATERAL with a parameterized LIMIT, which stops
Postgres reordering the join, so each query walked the whole
journal_entry_lines table across all tenants. Driving from the entries side
instead turns that into two indexed round trips.

Converted sites keep their existing shape: the helper reattaches the parent
entry under the same key the embed produced. Several conversions also remove
a latent silent truncation where an unpaginated query was capped at
PostgREST's 1000 row ceiling.

Two deliberate exceptions. The free text ilike legs of the MCP display query
stay on the embed, because each is capped at legLimit and that cap drives the
truncation contract the tool reports, while the helper is unbounded. The
accounts route moves to the existing get_account_usage_counts RPC instead,
since its embed was a head count and the helper returns rows.

commitEntry's write path is untouched: the change there is confined to the
read query of the pre-commit dimension rule check.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(api): anchor v1 list cursors on created_at

Page two returned page one, forever, while still advertising a fresh
next_cursor. The three routes sorted by and encoded a Postgres date column,
which serializes as YYYY-MM-DD, but decodeDefaultCursor validates the cursor
timestamp as full ISO-8601 and returned null, so the keyset filter was never
applied and has_more never went false. An integrator syncing verifikat looped
on the newest rows indefinitely.

The transactions route already solved this and its comment names the trap;
the fix was never ported. All three now order and encode on created_at with
an id tie break, matching the transactions keyset predicate exactly.
ISO_TIMESTAMP is deliberately left alone: relaxing it would silently change
sort semantics on the route that currently works.

Default ordering therefore moves from business date to insert order. Every
business date is still on the row, and the invoices list gains date_from and
date_to filters so a date range is still reachable; the other two already had
them.

The tests use an in-memory PostgREST that actually evaluates the filters,
because the repo's pass-through mock cannot catch this class of bug: the bug
is that the filter is never sent. They walk to exhaustion with a hard
iteration cap, so an unterminated walk fails instead of hanging.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(api): separate dry run from commit in the idempotency hash

The request hash was built from url.pathname, which excludes the query
string, so a dry run and its commit hashed identically. Following the flow
documented in dry-run.ts, re-issuing the request with the same
Idempotency-Key returned the cached preview with Idempotent-Replayed set and
wrote nothing, while reporting 200. An agent or integrator saw success for a
write that never happened.

dry_run is folded into the hash only when true, not as an unconditional
boolean. Including it as false would change the hash of every ordinary write,
and with a 24h idempotency TTL any key in flight across the deploy would fail
the request_hash comparison and 409 on a legitimate retry. Both hash call
sites now go through one shared helper so they cannot drift into a permanent
cache miss, and dry run responses are no longer stored at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* ci: install the Bedrock SDK out of tree in the compliance review

The Swedish accounting compliance gate had failed ten consecutive runs and so
was posting nothing. With --no-package-lock npm discarded the lockfile and
re-resolved the whole tree from package.json, floating @hookform/resolvers to
5.4.3, whose valibot ^1 peer conflicts with the pinned valibot 0.39.0.

Installing into the parent of the checkout resolves only that one package, so
an unrelated peer conflict can never take the gate down again. Node still
finds it because ESM bare specifiers walk up parent node_modules; NODE_PATH
would not have worked, as it is CommonJS only. --legacy-peer-deps was
rejected because it masks future genuine peer conflicts and still reifies the
full tree.

The same step's SDK version is aligned from 0.31.0 back to the 0.29.1 that
package.json and check:guards enforce after the streaming outage. That drift
went unnoticed because the pin guard only inspects package.json and the
lockfile, never workflow files.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* build(docker): generate crontabs from vercel.json

vercel.json defines 16 cron jobs; both Docker crontabs carried 9, and were
byte identical to each other. Self hosted deployments therefore never sent
recurring invoices, never dispatched webhooks and never cleaned up
idempotency keys. tax-deadlines also ran once a year on 2 January instead of
daily, and documents/verify weekly instead of daily.

Extension crons are included rather than excluded. The Dockerfile copies the
whole tree before building, so every extension cron route is compiled into
the image regardless of the enabled preset, and each returns 200 when its
extension is unconfigured, so curl -sf logs no failure. Two such entries were
already present in the crontab for extensions absent from the preset, which
settles the intent.

documents/verify is treated as drift rather than a self hosted concession:
the weekly cadence was present in the hosted crontab too, and the run is
capped at 200 documents walking a nulls-first queue, so weekly drains the
integrity queue seven times slower on a check that exists for BFL retention.

webhooks/dispatch keeps its per minute cadence, adding 1,440 requests a day
on self hosted. A gentler tick would silently stretch the first retry, since
the retry ladder opens at 60 seconds. SCHEDULE_OVERRIDES is the one line
place to change that.

A parity test asserts the path sets match minus a documented exclusion list,
and ratchets three cron routes that are currently scheduled nowhere so they
are named rather than silently rotting.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(observability): add a provider agnostic error sink

There is no error tracking in this codebase: logs go to console and Vercel
retention and nowhere else, nothing alerts on the 16 cron jobs, and seven
code comments across lib, app, components and extensions asserted that Sentry
captures errors when Sentry is not a dependency. The two most recent bug
fixes on this repo were both discovered by customer email.

This adds the sink, not a vendor. No dependency is taken: the interface has a
no-op default and a registration point, so behaviour is unchanged until an
adapter is registered. Releases are tagged from the build id already inlined
by next.config.ts.

Redaction moved out of lib/logger.ts into a leaf module that both the logger
and the sink import, so there is one denylist and no path from application
data to a third party can skip the personnummer regex, including direct sink
calls that bypass the logger. That matters here because these logs carry
personnummer and financial data.

verifyCronSecret now reports its own 401s, which covers all 16 jobs without
touching a route file and catches the case where CRON_SECRET is rotated
without updating the scheduler and every job silently 401s forever. The
threshold is one failure rather than the backup alert's three: suppressing
the first occurrence is precisely how an outage stays invisible.

The seven misleading comments are corrected to describe what the code
actually does, including the two cases that still are not covered: the client
side one, since the sink is server side, and a warn level call that is not
forwarded.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: remediate the 2026-07-26 similar-sweep findings across all surfaces

Resolves the ~150-finding sweep (dev_docs/similar-sweep-2026-07-26.md) with
one agent per finding; every behavioural fix carries a regression test proven
to fail at HEAD. Full status, corrections to the sweep, refusals and open
decisions in dev_docs/similar-sweep-2026-07-26-remediation-status.md.

Structural roots closed:
- resolveSekAmountOrNull(): honest SEK resolution refuses instead of booking
  1:1; four duplicated toSek closures now refuse via INVOICE_FX_RATE_MISSING
- ledger-line-amount.ts: journal_entry_lines.currency labels the document,
  not the amount; SQL pre-filter decoy proven and fixed
- sparse-patch.ts: .partial() does not strip .default() in Zod 4.4.3; the
  exploitable salary payslip-line PATCH and KPI preferences sinks fixed
- tests/schema: migration-replay phantom-column guard (13k+ refs, closed
  CHECK sets, onConflict targets); found 28 real defects, all fixed, all
  four baselines now empty
- three new ratchet guards: sek-labelled-amount, cross-extension-import,
  ungated-extension-route

Highlights: lawful VAT-rate set on all seven invoice surfaces (ML 6 kap),
RC input VAT mismatch wired on web + both MCP callers, missing-underlag
resource delegates to the shared RPC predicate, push-notifications consent
polarity fail-closed, deadlines undo honours requested state, silent-failure
and read-side-fabrication classes fixed across settings/KPI/inbox/Stripe/
Arcim/kassaflodesanalys, error-envelope stringification fixed at 10+ sites
with isSwedishUserMessage extended.

Also includes the parallel session's MCP invoice tools (update_invoice,
recurring schedules, invoice deliveries) which share files with the sweep
work and are verified green together.

13 new migrations are NOT applied anywhere; they apply via branch merge.
20260726120000 backfills 1247 supplier-invoice rows. pg tests for new
DDL are written but unrun (no local Postgres).

Verified: 11088 tests / 881 files green, tsc 0 non-test errors, lint 0
errors, check:guards passing, MCP payload 57475/57500.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(migrations): rename replace_sie_import migration off main's 20260726090000 version

origin/main shipped 20260726090000_agent_quota_rpc_caller_guard.sql; keeping
our replace_sie_import migration on the same version would abort the Supabase
apply with a schema_migrations_pkey duplicate at merge time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(review): remediate pre-publish deep-review findings across all slices

A 13-agent review of the full branch diff surfaced 1 critical, 5 high and
~45 further findings; this commit resolves them in one pass:

- replace_sie_import / undo_sie_import: p_user_id honored only for
  service_role callers; any other caller is pinned to auth.uid()
  (impersonation gate bypass), authz raise errcode 42501 mapped to a
  Swedish 403 in the route, new caller-guard migration for undo
- bulk_book_transactions refuses homogeneous non-SEK batches instead of
  writing foreign magnitudes into SEK ledger columns
- credit-note cap trigger: company-match on credited_invoice_id, no
  cross-tenant figures in exception text
- link_voucher RPCs resolve NULL invoice currency as SEK end to end
- personal-number ciphertext CHECK split into NOT VALID + VALIDATE
- same-currency foreign settlements clear 1510 at booking rate and book
  realized diff to 3960/7960; rate-less foreign write paths refuse
- receivables revaluation covers partially_paid and outstanding amounts
- period lock guard paginates candidates past the PostgREST 1000 cap
- documents: service-client storage removals after authz, dual-layout
  reads in integrity cron and archive export, backfill delete-source
  sweep actually deletes with hash verification and shared-key grouping
- invoice matching normalizes NULL/lowercase currencies (regression),
  duplicate candidates stop claiming amount matches they never ran
- match-invoice aborts on any booking failure (no paid-without-verifikat)
- refresh-exchange-rate reverts on concurrent booking (TOCTOU window)
- KPI preferences upsert arbiter aligned to the company-scoped constraint
- personnummer_last4 stripped from all salary responses incl. MCP tools
- worked-hours batch restores destroyed rows on conflict and error paths
- MCP: shared duplicate-claim builder (no more 'null kr'), short-circuit
  on tag_journal_lines overflow, auto_send schedules stage as high risk
- observability sink redacts emails/IBANs/API keys and keeps redacted
  stacks in prod; assorted small guards (safe-return-to /@, dry_run=True,
  cursor helper off-by-one, OAuth state TTL 10 min, arcim saveMappings
  call removed)

Full dispositions, deferred items and hand-verified accounting numbers
are documented in the PR body and DECISIONS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(personnummer): implement masking and encryption for personal numbers with tests

* fix(review): address CI and compliance-bot findings for PR #1215

pg-real: the CI image's auth shim reads the legacy request.jwt.claim.role
GUC, so both service-role simulations (runAsServiceRole and the
invoice-delivery test's local helper) never satisfied auth.role() =
'service_role' and every legitimate p_user_id path failed closed; the
shared helper now sets both GUC shapes plus SET LOCAL ROLE with a
fail-loud sanity check, and the delivery test reuses it. The link-voucher
migration had recreated both RPCs from pre-rewrite file text,
reintroducing the NULL-unsafe membership pattern the
null-safe-tenant-guards ratchet bans; both guards now use
public.caller_is_company_member() with all currency changes preserved.

Compliance bots: the customers export now emits the standard masked form
instead of raw AES-256-GCM ciphertext in the Org-/personnummer column,
and maskCustomerRow returns a non-round-trippable placeholder on decrypt
failure instead of 500ing the list. MCP parity: gnubok_lock_period's
staging pre-check now runs the exact countUnbookedInPeriod the commit
path enforces (exported from period-service; local mirror deleted), and
gnubok_agi_status resolves AGI state run-scoped so a correction run no
longer renders as already filed.

Declined with evidence: PR-Agent's opening-balances null-zeroing concern
(all mergeable columns are NOT NULL with defaults per 20260713101000).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(review): address codex review findings on PR #1215

- restore 20260726140000 to its preview-recorded content and restate the
  NULL-safe tenant guard under 20260727130000: a recorded migration version
  never re-runs, so the in-place edit could not reach the preview branch
- replace toFixed() with sv-SE two-decimal formatting in the ROT/RUT cap
  warning texts and update the pinned test expectations
- drop the em dash in the fiscal-periods route comment
- strip trailing whitespace in import-existing.test.ts

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(reports): raise timeout on real PDF render tests

renderToBuffer does real @react-pdf layout work and exceeds the 5s
default when the full suite saturates the CPU; tests pass in isolation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 03:34:56 +02:00

1614 lines
50 KiB
TypeScript

/**
* Static phantom-column detector for Supabase query builders.
*
* WHY THIS EXISTS
* ---------------
* `createQueuedMockSupabase()` returns a chainable stub: every `.eq()`,
* `.select()` and `.order()` resolves regardless of whether the column exists.
* A green unit test therefore proves nothing about the column names in a query.
* That blind spot shipped a broken article delete for ten days, plus sixteen
* further phantom-column sites, two phantom CHECK-constraint values
* (`source_type = 'transaction'` when the real value is `bank_transaction`) and
* one `onConflict` naming a dropped unique constraint (42P10 on every call).
*
* This module reads the two things that cannot lie to each other:
* 1. the schema, replayed from `supabase/migrations/*.sql` in version order;
* 2. every Supabase query builder chain in the source, via the TypeScript AST.
* It then asserts that each column named in a query exists on the table it is
* named against. See `tests/schema/no-phantom-columns.test.ts`.
*
* GROUND TRUTH: the migration files, not a live database and not a checked-in
* snapshot. The migrations are already the repo's contract with prod (CLAUDE.md:
* "never leave a remote DB ahead of the repo"), replaying them costs ~200ms, and
* there is no snapshot artifact that can go stale. The cost is parser fidelity:
* anything this file fails to parse must degrade to "unresolved", never to a
* false accusation. Every classification below is chosen in that direction.
*/
import fs from 'node:fs'
import path from 'node:path'
import ts from 'typescript'
// ---------------------------------------------------------------------------
// Schema model
// ---------------------------------------------------------------------------
export interface TableModel {
name: string
columns: Set<string>
/** column -> referenced table, used to resolve `alias:fk_column(...)` embeds. */
fkTargets: Map<string, string>
/** constraint name -> closed value set, from `CHECK (col IN (...))` or an enum type. */
checks: Map<string, { column: string; values: Set<string> }>
/** unique/PK column sets, sorted and joined by ',', for `onConflict` validation. */
uniqueSets: Set<string>
/**
* Named UNIQUE/PK constraint -> its unique-set key, so `DROP CONSTRAINT` can
* retract the set. Only NAMED constraints are tracked: a column-level bare
* `UNIQUE` has no name in the DDL, so its set cannot be retracted by name
* and stays in uniqueSets (a missed retraction there loses strictness, it
* never invents an accusation).
*/
uniqueConstraintKeys: Map<string, string>
}
export interface SchemaModel {
tables: Map<string, TableModel>
/** Views and materialised views: valid `.from()` targets whose columns we do not model. */
views: Set<string>
/** index name -> { table, key } so DROP INDEX can retract a unique set. */
uniqueIndexes: Map<string, { table: string; key: string }>
}
const CONSTRAINT_LEAD = new Set([
'CONSTRAINT',
'PRIMARY',
'UNIQUE',
'FOREIGN',
'CHECK',
'EXCLUDE',
'LIKE',
'DEFERRABLE',
])
function emptyTable(name: string): TableModel {
return {
name,
columns: new Set(),
fkTargets: new Map(),
checks: new Map(),
uniqueSets: new Set(),
uniqueConstraintKeys: new Map(),
}
}
/**
* Split a SQL file into statements. Quote-, comment- and dollar-quote-aware, so
* a `CREATE TABLE` inside a `$$ ... $$` function body never leaks into the model
* and a `;` inside a string literal never splits a statement. Comments are
* dropped; string and identifier literals are preserved (CHECK lists need them).
*/
export function splitStatements(sql: string): string[] {
const out: string[] = []
let buf = ''
let i = 0
const n = sql.length
while (i < n) {
const ch = sql[i]
if (ch === '-' && sql[i + 1] === '-') {
while (i < n && sql[i] !== '\n') i++
buf += ' '
continue
}
if (ch === '/' && sql[i + 1] === '*') {
let depth = 1
i += 2
while (i < n && depth > 0) {
if (sql[i] === '/' && sql[i + 1] === '*') {
depth++
i += 2
} else if (sql[i] === '*' && sql[i + 1] === '/') {
depth--
i += 2
} else i++
}
buf += ' '
continue
}
if (ch === "'" || ch === '"') {
let j = i + 1
while (j < n) {
if (sql[j] === ch && sql[j + 1] === ch) {
j += 2
continue
}
if (sql[j] === ch) {
j++
break
}
j++
}
buf += sql.slice(i, j)
i = j
continue
}
if (ch === '$') {
const tag = /^\$\$|^\$[A-Za-z_][A-Za-z0-9_]*\$/.exec(sql.slice(i))?.[0]
if (tag) {
const end = sql.indexOf(tag, i + tag.length)
const stop = end === -1 ? n : end + tag.length
buf += sql.slice(i, stop)
i = stop
continue
}
}
if (ch === ';') {
out.push(buf)
buf = ''
i++
continue
}
buf += ch
i++
}
out.push(buf)
return out.map((s) => s.trim()).filter(Boolean)
}
/** Split on commas that sit at paren/bracket depth 0, outside quotes. */
function splitTopLevel(text: string): string[] {
const parts: string[] = []
let depth = 0
let buf = ''
let i = 0
while (i < text.length) {
const ch = text[i]
if (ch === "'" || ch === '"') {
let j = i + 1
while (j < text.length) {
if (text[j] === ch && text[j + 1] === ch) {
j += 2
continue
}
if (text[j] === ch) {
j++
break
}
j++
}
buf += text.slice(i, j)
i = j
continue
}
if (ch === '(' || ch === '[') depth++
else if (ch === ')' || ch === ']') depth--
if (ch === ',' && depth === 0) {
parts.push(buf)
buf = ''
i++
continue
}
buf += ch
i++
}
parts.push(buf)
return parts.map((p) => p.trim()).filter(Boolean)
}
/** Content of the parenthesised group that starts at or after `from`. */
function balancedParens(text: string, from: number): { body: string; end: number } | null {
const open = text.indexOf('(', from)
if (open === -1) return null
let depth = 0
let i = open
while (i < text.length) {
const ch = text[i]
if (ch === "'" || ch === '"') {
let j = i + 1
while (j < text.length) {
if (text[j] === ch && text[j + 1] === ch) {
j += 2
continue
}
if (text[j] === ch) {
j++
break
}
j++
}
i = j
continue
}
if (ch === '(') depth++
else if (ch === ')') {
depth--
if (depth === 0) return { body: text.slice(open + 1, i), end: i + 1 }
}
i++
}
return null
}
const ident = (raw: string): string => raw.replace(/^"|"$/g, '')
/**
* Extract a CLOSED value set from a CHECK expression, or null when the
* expression is anything more complex than `col IN (...)` / `col = ANY(ARRAY[])`
* (optionally guarded by `col IS NULL OR`). Anything else stays unmodelled, so a
* compound CHECK never produces a false "phantom value" accusation.
*/
function parseClosedCheck(
expr: string
): { column: string; values: Set<string> } | null {
let s = expr.replace(/\s+/g, ' ').trim()
while (s.startsWith('(') && balancedParens(s, 0)?.end === s.length) {
s = s.slice(1, -1).trim()
}
// Tolerate a nullability guard on either side of the OR.
s = s.replace(/^"?\w+"?\s+IS\s+NULL\s+OR\s+/i, '').trim()
s = s.replace(/\s+OR\s+"?\w+"?\s+IS\s+NULL$/i, '').trim()
while (s.startsWith('(') && balancedParens(s, 0)?.end === s.length) {
s = s.slice(1, -1).trim()
}
const inMatch = /^"?([A-Za-z_][A-Za-z0-9_]*)"?(?:\s*::\s*\w+)?\s+IN\s*\(/i.exec(s)
const anyMatch = /^"?([A-Za-z_][A-Za-z0-9_]*)"?(?:\s*::\s*\w+)?\s*=\s*ANY\s*\(/i.exec(s)
const m = inMatch ?? anyMatch
if (!m) return null
const group = balancedParens(s, m[0].length - 1)
if (!group || group.end !== s.length) return null
let list = group.body.trim()
if (anyMatch) {
const arr = /^ARRAY\s*\[([\s\S]*)\]$/i.exec(list)
if (!arr) return null
list = arr[1]
}
// The list must be nothing but string literals (casts allowed): a subquery or
// a column reference means the set is not closed.
const values = new Set<string>()
for (const item of splitTopLevel(list)) {
const lit = /^'((?:[^']|'')*)'(?:\s*::\s*[\w .]+)?$/.exec(item.trim())
if (!lit) return null
values.add(lit[1].replace(/''/g, "'"))
}
if (values.size === 0) return null
return { column: m[1], values }
}
function recordUnique(table: TableModel, columns: string[], constraintName?: string | null): void {
if (columns.length === 0) return
const key = [...columns].sort().join(',')
table.uniqueSets.add(key)
// Remember the name so `ALTER TABLE ... DROP CONSTRAINT <name>` can retract
// the set again. Unnamed (column-level) uniques have nothing to key on.
if (constraintName) table.uniqueConstraintKeys.set(constraintName, key)
}
function parseColumnList(body: string): string[] {
return splitTopLevel(body)
.map((c) => ident(c.trim().replace(/\s+(ASC|DESC)$/i, '')))
.filter((c) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(c))
}
/** One `CREATE TABLE` body item: either a column definition or a constraint. */
function applyTableItem(
table: TableModel,
item: string,
enums: Map<string, Set<string>>
): void {
const lead = /^"?([A-Za-z_][A-Za-z0-9_$]*)"?/.exec(item.trim())
if (!lead) return
const upper = lead[1].toUpperCase()
if (CONSTRAINT_LEAD.has(upper)) {
let rest = item.trim()
let name: string | null = null
const named = /^CONSTRAINT\s+"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+/i.exec(rest)
if (named) {
name = named[1]
rest = rest.slice(named[0].length).trim()
}
if (/^CHECK\s*\(/i.test(rest)) {
const group = balancedParens(rest, 0)
if (group) {
const parsed = parseClosedCheck(group.body)
if (parsed) {
table.checks.set(name ?? `${table.name}_${parsed.column}_check`, parsed)
}
}
return
}
if (/^UNIQUE\s*\(/i.test(rest) || /^PRIMARY\s+KEY\s*\(/i.test(rest)) {
const group = balancedParens(rest, 0)
if (group) recordUnique(table, parseColumnList(group.body), name)
return
}
if (/^FOREIGN\s+KEY\s*\(/i.test(rest)) {
const group = balancedParens(rest, 0)
const target = /REFERENCES\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(rest)
if (group && target) {
for (const col of parseColumnList(group.body)) {
table.fkTargets.set(col, target[1])
}
}
return
}
return
}
const column = ident(lead[1])
table.columns.add(column)
const rest = item.trim().slice(lead[0].length).trim()
const typeName = /^(?:public\.)?"?([A-Za-z_][A-Za-z0-9_]*)"?/.exec(rest)?.[1]
if (typeName) {
const enumValues = enums.get(typeName.toLowerCase())
if (enumValues) {
table.checks.set(`${table.name}_${column}_enumtype`, {
column,
values: new Set(enumValues),
})
}
}
const ref = /REFERENCES\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(rest)
if (ref) table.fkTargets.set(column, ref[1])
const checkAt = rest.search(/\bCHECK\s*\(/i)
if (checkAt !== -1) {
const group = balancedParens(rest, checkAt)
if (group) {
// A column-level CHECK may omit the column name: `status text CHECK (... IN ...)`
// always constrains this column, so parseClosedCheck's name wins only when
// it matches; otherwise we assume the owning column.
const parsed = parseClosedCheck(group.body)
if (parsed && parsed.column === column) {
table.checks.set(`${table.name}_${column}_check`, parsed)
}
}
}
// Column-level UNIQUE / PRIMARY KEY, but not the word inside a CHECK body.
const withoutChecks = checkAt === -1 ? rest : rest.slice(0, checkAt)
if (/\bUNIQUE\b/i.test(withoutChecks) || /\bPRIMARY\s+KEY\b/i.test(withoutChecks)) {
recordUnique(table, [column])
}
}
/** Replay every migration in version order into a column/constraint model. */
export function buildSchemaFromMigrations(migrationsDir: string): SchemaModel {
const files = fs
.readdirSync(migrationsDir)
.filter((f) => f.endsWith('.sql'))
.sort()
const model: SchemaModel = {
tables: new Map(),
views: new Set(),
uniqueIndexes: new Map(),
}
const enums = new Map<string, Set<string>>()
for (const file of files) {
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8')
for (const stmt of splitStatements(sql)) {
applyStatement(model, enums, stmt)
}
}
return model
}
function applyStatement(
model: SchemaModel,
enums: Map<string, Set<string>>,
stmt: string
): void {
const flat = stmt.replace(/\s+/g, ' ').trim()
const createType = /^CREATE\s+TYPE\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+AS\s+ENUM\s*\(/i.exec(
flat
)
if (createType) {
const group = balancedParens(flat, createType[0].length - 1)
if (group) {
const values = new Set<string>()
for (const item of splitTopLevel(group.body)) {
const lit = /^'((?:[^']|'')*)'$/.exec(item.trim())
if (lit) values.add(lit[1].replace(/''/g, "'"))
}
if (values.size) enums.set(createType[1].toLowerCase(), values)
}
return
}
const createView = /^CREATE\s+(?:OR\s+REPLACE\s+)?(?:MATERIALIZED\s+)?VIEW\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(
flat
)
if (createView) {
model.views.add(createView[1])
return
}
const dropView = /^DROP\s+(?:MATERIALIZED\s+)?VIEW\s+(?:IF\s+EXISTS\s+)?(.+)$/i.exec(flat)
if (dropView) {
for (const raw of splitTopLevel(dropView[1].replace(/\s+(CASCADE|RESTRICT)\s*$/i, ''))) {
model.views.delete(ident(raw.trim().replace(/^public\./i, '')))
}
return
}
const createTable = /^CREATE\s+(?:UNLOGGED\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(
flat
)
if (createTable) {
const name = createTable[1]
// `CREATE TABLE x AS SELECT ...` has no column list we can read: treat the
// table as opaque (a view, for our purposes) rather than guessing.
const group = balancedParens(flat, createTable[0].length)
if (!group || /\bAS\s+SELECT\b/i.test(flat.slice(createTable[0].length, group.end))) {
model.views.add(name)
return
}
const table = model.tables.get(name) ?? emptyTable(name)
model.tables.set(name, table)
for (const item of splitTopLevel(group.body)) applyTableItem(table, item, enums)
return
}
const dropTable = /^DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?(.+)$/i.exec(flat)
if (dropTable) {
for (const raw of splitTopLevel(dropTable[1].replace(/\s+(CASCADE|RESTRICT)\s*$/i, ''))) {
const name = ident(raw.trim().replace(/^public\./i, ''))
model.tables.delete(name)
model.views.delete(name)
}
return
}
const uniqueIndex = /^CREATE\s+UNIQUE\s+INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+ON\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(
flat
)
if (uniqueIndex) {
const table = model.tables.get(uniqueIndex[2])
const group = balancedParens(flat, uniqueIndex[0].length)
if (table && group) {
const cols = parseColumnList(group.body)
// An expression index (lower(x), coalesce(...)) yields no plain column
// list; skip rather than invent one.
if (cols.length === splitTopLevel(group.body).length) {
const key = [...cols].sort().join(',')
table.uniqueSets.add(key)
model.uniqueIndexes.set(uniqueIndex[1], { table: table.name, key })
}
}
return
}
const dropIndex = /^DROP\s+INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+EXISTS\s+)?(.+)$/i.exec(flat)
if (dropIndex) {
for (const raw of splitTopLevel(dropIndex[1].replace(/\s+(CASCADE|RESTRICT)\s*$/i, ''))) {
const name = ident(raw.trim().replace(/^public\./i, ''))
const entry = model.uniqueIndexes.get(name)
if (entry) {
model.tables.get(entry.table)?.uniqueSets.delete(entry.key)
model.uniqueIndexes.delete(name)
}
}
return
}
const alterTable = /^ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+(.+)$/i.exec(
flat
)
if (alterTable) {
const table = model.tables.get(alterTable[1])
if (!table) return
for (const action of splitTopLevel(alterTable[2])) {
applyAlterAction(model, table, action, enums)
}
return
}
if (/^DO\b/i.test(stmt)) {
applyDoBlockDdl(model, enums, stmt)
applyDynamicDdl(model, enums, flat)
}
}
/** Index of the first `ALTER TABLE` outside any quoted region, or -1. */
function findUnquotedAlterTable(text: string): number {
let i = 0
while (i < text.length) {
const ch = text[i]
if (ch === "'" || ch === '"') {
let j = i + 1
while (j < text.length) {
if (text[j] === ch && text[j + 1] === ch) {
j += 2
continue
}
if (text[j] === ch) {
j++
break
}
j++
}
i = j
continue
}
if ((ch === 'A' || ch === 'a') && /^ALTER\s+TABLE\b/i.test(text.slice(i))) return i
i++
}
return -1
}
/**
* Plain (non-dynamic) `ALTER TABLE` inside a `DO $$ ... $$` block. Every such
* block in this repo is an idempotency guard whose intent is "make it so"
* (`IF NOT EXISTS (...) THEN ALTER TABLE ... ADD COLUMN ...`), so applying the
* DDL unconditionally reproduces the end state. Without this, the model denies
* `supplier_invoice_payments.user_id` and the whole `webhooks` table (renamed
* from `automation_webhooks` inside such a block).
*/
function applyDoBlockDdl(
model: SchemaModel,
enums: Map<string, Set<string>>,
stmt: string
): void {
const bodyStart = /\$\$|\$[A-Za-z_][A-Za-z0-9_]*\$/.exec(stmt)
if (!bodyStart) return
const tag = bodyStart[0]
const start = (bodyStart.index ?? 0) + tag.length
const end = stmt.indexOf(tag, start)
const body = stmt.slice(start, end === -1 ? stmt.length : end)
for (const sub of splitStatements(body)) {
const at = findUnquotedAlterTable(sub)
if (at === -1) continue
const flat = sub.slice(at).replace(/\s+/g, ' ').trim()
const alter = /^ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+(.+)$/i.exec(
flat
)
if (!alter) continue
const table = model.tables.get(alter[1])
if (!table) continue
for (const action of splitTopLevel(alter[2])) {
applyAlterAction(model, table, action, enums)
}
}
}
/**
* `DO $$ ... EXECUTE format('ALTER TABLE public.%I ADD COLUMN ...', tbl) ... $$`
* over an `ARRAY[...]` of table names. The multi-tenant refactor added
* `company_id` to forty tables this way; without this the model would deny that
* `chart_of_accounts.company_id` exists and the guard would accuse hundreds of
* correct call sites.
*
* Deliberately only handles ADD COLUMN, i.e. it can only ever ADD columns to the
* model. A misread here loses coverage, it never invents an accusation.
*/
function applyDynamicDdl(
model: SchemaModel,
enums: Map<string, Set<string>>,
stmt: string
): void {
const candidates: string[] = []
const arrayRe = /ARRAY\s*\[/gi
for (let m = arrayRe.exec(stmt); m; m = arrayRe.exec(stmt)) {
const open = stmt.indexOf('[', m.index)
let depth = 0
let i = open
for (; i < stmt.length; i++) {
if (stmt[i] === '[') depth++
else if (stmt[i] === ']') {
depth--
if (depth === 0) break
}
}
for (const item of splitTopLevel(stmt.slice(open + 1, i))) {
const lit = /^'((?:[^']|'')*)'$/.exec(item.trim())
if (lit) candidates.push(lit[1].replace(/''/g, "'"))
}
}
if (candidates.length === 0) return
const templateRe = /'((?:[^']|'')*)'/g
for (let m = templateRe.exec(stmt); m; m = templateRe.exec(stmt)) {
const template = m[1].replace(/''/g, "'").replace(/\s+/g, ' ').trim()
const alter = /^ALTER\s+TABLE\s+(?:public\.)?%[Is]\s+(ADD\s+(?:COLUMN\s+)?(?:IF\s+NOT\s+EXISTS\s+)?.+)$/i.exec(
template
)
if (!alter) continue
for (const name of candidates) {
const table = model.tables.get(name)
if (table) applyAlterAction(model, table, alter[1], enums)
}
}
}
function applyAlterAction(
model: SchemaModel,
table: TableModel,
action: string,
enums: Map<string, Set<string>>
): void {
const a = action.trim()
const renameTable = /^RENAME\s+TO\s+"?([A-Za-z_][A-Za-z0-9_$]*)"?$/i.exec(a)
if (renameTable) {
model.tables.delete(table.name)
table.name = renameTable[1]
model.tables.set(table.name, table)
return
}
const renameColumn = /^RENAME\s+(?:COLUMN\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+TO\s+"?([A-Za-z_][A-Za-z0-9_$]*)"?$/i.exec(
a
)
if (renameColumn) {
table.columns.delete(renameColumn[1])
table.columns.add(renameColumn[2])
return
}
const dropColumn = /^DROP\s+COLUMN\s+(?:IF\s+EXISTS\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(a)
if (dropColumn) {
table.columns.delete(dropColumn[1])
return
}
const dropConstraint = /^DROP\s+CONSTRAINT\s+(?:IF\s+EXISTS\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(
a
)
if (dropConstraint) {
const name = dropConstraint[1]
table.checks.delete(name)
// Retract the unique set a NAMED UNIQUE/PK constraint carried, so an
// `onConflict` naming the dropped constraint's columns fails here instead
// of 42P10-ing at runtime. The set survives when another constraint or a
// unique index still provides the same column key. A column-level bare
// `UNIQUE` (parsed by applyTableItem) has no name and is untouched.
const key = table.uniqueConstraintKeys.get(name)
if (key !== undefined) {
table.uniqueConstraintKeys.delete(name)
const stillProvided =
[...table.uniqueConstraintKeys.values()].includes(key) ||
[...model.uniqueIndexes.values()].some((e) => e.table === table.name && e.key === key)
if (!stillProvided) table.uniqueSets.delete(key)
}
return
}
const addConstraint = /^ADD\s+CONSTRAINT\s+"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+(.+)$/i.exec(a)
if (addConstraint) {
const name = addConstraint[1]
const body = addConstraint[2]
if (/^CHECK\s*\(/i.test(body)) {
const group = balancedParens(body, 0)
const parsed = group ? parseClosedCheck(group.body) : null
if (parsed) table.checks.set(name, parsed)
return
}
if (/^UNIQUE\s*\(/i.test(body) || /^PRIMARY\s+KEY\s*\(/i.test(body)) {
const group = balancedParens(body, 0)
if (group) recordUnique(table, parseColumnList(group.body), name)
return
}
if (/^FOREIGN\s+KEY\s*\(/i.test(body)) {
const group = balancedParens(body, 0)
const target = /REFERENCES\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_$]*)"?/i.exec(body)
if (group && target) {
for (const col of parseColumnList(group.body)) table.fkTargets.set(col, target[1])
}
return
}
return
}
// `ADD COLUMN x type`, and bare `ADD x type` (COLUMN is optional in Postgres).
const addColumn = /^ADD\s+(?:COLUMN\s+)?(?:IF\s+NOT\s+EXISTS\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+(.*)$/i.exec(
a
)
if (addColumn && !/^(CONSTRAINT|PRIMARY|UNIQUE|FOREIGN|CHECK|EXCLUDE|GENERATED)$/i.test(addColumn[1])) {
applyTableItem(table, `${addColumn[1]} ${addColumn[2]}`, enums)
return
}
const alterColumnType = /^ALTER\s+(?:COLUMN\s+)?"?([A-Za-z_][A-Za-z0-9_$]*)"?\s+(?:SET\s+DATA\s+)?TYPE\s+(?:public\.)?"?([A-Za-z_][A-Za-z0-9_]*)"?/i.exec(
a
)
if (alterColumnType) {
const enumValues = enums.get(alterColumnType[2].toLowerCase())
if (enumValues) {
table.checks.set(`${table.name}_${alterColumnType[1]}_enumtype`, {
column: alterColumnType[1],
values: new Set(enumValues),
})
}
return
}
}
/** Columns of a table whose value set is closed and unambiguous. */
export function closedValueSets(table: TableModel): Map<string, Set<string>> {
const byColumn = new Map<string, Set<string>[]>()
for (const check of table.checks.values()) {
const list = byColumn.get(check.column) ?? []
list.push(check.values)
byColumn.set(check.column, list)
}
const out = new Map<string, Set<string>>()
for (const [column, sets] of byColumn) {
// Two live constraints on one column: we cannot tell which is authoritative,
// so the column is not treated as closed.
if (sets.length === 1 && table.columns.has(column)) out.set(column, sets[0])
}
return out
}
// ---------------------------------------------------------------------------
// Query builder reference extraction
// ---------------------------------------------------------------------------
export interface ColumnRef {
table: string
column: string
kind: 'select' | 'filter' | 'order' | 'write' | 'onConflict' | 'logical' | 'match'
file: string
line: number
raw: string
}
export interface ValueRef {
table: string
column: string
value: string
file: string
line: number
raw: string
}
export interface ConflictRef {
table: string
columns: string[]
file: string
line: number
}
export interface TableRef {
table: string
file: string
line: number
}
export interface Unresolved {
reason: string
detail: string
file: string
line: number
}
export interface ScanResult {
columnRefs: ColumnRef[]
valueRefs: ValueRef[]
conflictRefs: ConflictRef[]
tableRefs: TableRef[]
unresolved: Unresolved[]
}
/** `.from()` receivers that are definitely not a PostgREST client. */
const NON_CLIENT_RECEIVERS = new Set([
'Array',
'Buffer',
'Object',
'String',
'Number',
'Date',
'Map',
'Set',
'WeakMap',
'WeakSet',
'Promise',
'Blob',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'BigInt64Array',
'BigUint64Array',
])
/** Methods whose first string argument is a single column name. */
const FILTER_METHODS = new Set([
'eq',
'neq',
'gt',
'gte',
'lt',
'lte',
'like',
'likeAllOf',
'likeAnyOf',
'ilike',
'ilikeAllOf',
'ilikeAnyOf',
'is',
'in',
'contains',
'containedBy',
'rangeGt',
'rangeGte',
'rangeLt',
'rangeLte',
'rangeAdjacent',
'overlaps',
'textSearch',
])
/** Any of these appearing in the chain proves it is a PostgREST builder. */
const POSTGREST_METHODS = new Set([
...FILTER_METHODS,
'select',
'insert',
'update',
'upsert',
'delete',
'order',
'limit',
'range',
'single',
'maybeSingle',
'match',
'not',
'filter',
'or',
'csv',
'throwOnError',
'returns',
'overrideTypes',
])
/** PostgREST filter operators, longest first so `gte` wins over `gt`. */
const PG_OPS = [
'plfts',
'phfts',
'wfts',
'imatch',
'match',
'isdistinct',
'not',
'neq',
'gte',
'lte',
'eq',
'gt',
'lt',
'like',
'ilike',
'is',
'in',
'cs',
'cd',
'sl',
'sr',
'nxl',
'nxr',
'adj',
'ov',
'fts',
]
interface ChainCall {
method: string
args: ts.NodeArray<ts.Expression>
node: ts.CallExpression
}
function literalText(node: ts.Expression | undefined): string | null {
if (!node) return null
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text
return null
}
function receiverText(expr: ts.Expression): string {
if (ts.isIdentifier(expr)) return expr.text
if (ts.isPropertyAccessExpression(expr)) {
return `${receiverText(expr.expression)}.${expr.name.text}`
}
if (ts.isCallExpression(expr)) return `${receiverText(expr.expression)}()`
return ''
}
/** Walk up a fluent chain from `start`, collecting `.method(args)` calls. */
function collectChain(start: ts.Expression): { calls: ChainCall[]; top: ts.Expression } {
const calls: ChainCall[] = []
let node: ts.Expression = start
for (;;) {
const parent = node.parent
if (
parent &&
ts.isPropertyAccessExpression(parent) &&
parent.expression === node &&
parent.parent &&
ts.isCallExpression(parent.parent) &&
parent.parent.expression === parent
) {
calls.push({ method: parent.name.text, args: parent.parent.arguments, node: parent.parent })
node = parent.parent
continue
}
// `await q.select()` / `(q.select())` sit above the chain, not inside it.
if (parent && ts.isNonNullExpression(parent) && parent.expression === node) {
node = parent
continue
}
return { calls, top: node }
}
}
/** Identifier that `top` is being bound to, if any. */
function bindingTarget(top: ts.Expression): string | null {
const parent = top.parent
if (parent && ts.isVariableDeclaration(parent) && parent.initializer === top) {
return ts.isIdentifier(parent.name) ? parent.name.text : null
}
if (
parent &&
ts.isBinaryExpression(parent) &&
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
parent.right === top &&
ts.isIdentifier(parent.left)
) {
return parent.left.text
}
return null
}
/** Nearest enclosing function-like node position, used to scope builder vars. */
function scopeId(node: ts.Node): number {
let current: ts.Node | undefined = node
while (current) {
if (ts.isFunctionLike(current)) return current.pos
current = current.parent
}
return -1
}
interface ParsedSelect {
columns: string[]
embeds: { name: string; alias: string | null; inner: string; hint: string | null }[]
unparsed: string[]
}
/**
* Parse a PostgREST select spec into plain columns and embedded resources.
*
* Embedded resources are the single biggest false-positive source: `items:invoice_items(*)`
* names a TABLE, not a column, and `customer:customers!fk(id)` adds a relationship
* hint. Anything ending in `(...)` is therefore classified as an embed and never
* asserted as a column of the outer table.
*/
export function parseSelectSpec(spec: string): ParsedSelect {
const out: ParsedSelect = { columns: [], embeds: [], unparsed: [] }
for (const rawItem of splitTopLevel(spec.replace(/\s+/g, ' '))) {
let item = rawItem.trim()
if (!item || item === '*') continue
if (item.startsWith('...')) item = item.slice(3).trim() // spread embed
// Alias: `alias:target`, where `::` is a cast and never an alias separator.
let alias: string | null = null
const aliasMatch = /^([A-Za-z_][A-Za-z0-9_]*)\s*:(?!:)/.exec(item)
if (aliasMatch) {
alias = aliasMatch[1]
item = item.slice(aliasMatch[0].length).trim()
}
if (item.endsWith(')') && item.includes('(')) {
const open = item.indexOf('(')
let name = item.slice(0, open).trim()
const inner = item.slice(open + 1, -1)
let hint: string | null = null
const bang = name.indexOf('!')
if (bang !== -1) {
hint = name.slice(bang + 1)
name = name.slice(0, bang)
}
// `col.sum()` and friends are PostgREST aggregates on a real column.
const aggregate = /^([A-Za-z_][A-Za-z0-9_]*)\.(sum|avg|count|max|min)$/i.exec(name)
if (aggregate && inner.trim() === '') {
out.columns.push(aggregate[1])
continue
}
if (name === 'count' && inner.trim() === '') continue
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
out.unparsed.push(rawItem)
continue
}
out.embeds.push({ name, alias, inner, hint })
continue
}
if (item === 'count') continue
// Strip casts and JSON paths: `metadata->>'key'`, `amount::text`.
let column = item.split('::')[0].split('->')[0].trim()
column = ident(column)
if (!column) continue
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(column)) {
out.unparsed.push(rawItem)
continue
}
out.columns.push(column)
}
return out
}
/** Split a PostgREST `or()`/`and()` string into `{ columnPath }` terms. */
export function parseLogicalSpec(spec: string): { paths: string[]; unparsed: string[] } {
const paths: string[] = []
const unparsed: string[] = []
const visit = (text: string): void => {
for (const rawTerm of splitTopLevel(text)) {
let term = rawTerm.trim()
if (!term) continue
const nested = /^(?:not\.)?(and|or)\(([\s\S]*)\)$/i.exec(term)
if (nested) {
visit(nested[2])
continue
}
if (term.startsWith('not.')) term = term.slice(4)
let matched = false
for (const op of PG_OPS) {
const at = term.indexOf(`.${op}.`)
if (at > 0) {
paths.push(term.slice(0, at))
matched = true
break
}
}
if (!matched) unparsed.push(rawTerm)
}
}
visit(spec.replace(/\s+/g, ' '))
return { paths, unparsed }
}
const SOURCE_EXTENSIONS = ['.ts', '.tsx']
const SKIP_DIRS = new Set(['node_modules', '.next', '.git', 'dist', 'build', 'coverage', '__tests__'])
export function listSourceFiles(roots: string[]): string[] {
const out: string[] = []
const walk = (dir: string): void => {
let entries: fs.Dirent[]
try {
entries = fs.readdirSync(dir, { withFileTypes: true })
} catch {
return
}
for (const entry of entries) {
if (entry.name.startsWith('.')) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) {
if (!SKIP_DIRS.has(entry.name)) walk(full)
} else if (
SOURCE_EXTENSIONS.some((ext) => entry.name.endsWith(ext)) &&
!entry.name.endsWith('.test.ts') &&
!entry.name.endsWith('.test.tsx') &&
!entry.name.endsWith('.d.ts')
) {
out.push(full)
}
}
}
for (const root of roots) walk(root)
return out.sort()
}
/**
* Extract every column reference from the Supabase builder chains in `files`.
* `schema` is consulted only to resolve embedded-resource and alias targets, so
* a reference whose table cannot be resolved is reported as unresolved rather
* than attributed to the wrong table.
*/
export function scanColumnRefs(files: string[], schema: SchemaModel, root: string): ScanResult {
const result: ScanResult = {
columnRefs: [],
valueRefs: [],
conflictRefs: [],
tableRefs: [],
unresolved: [],
}
for (const file of files) {
const text = fs.readFileSync(file, 'utf8')
if (!text.includes('.from(')) continue
const rel = path.relative(root, file).split(path.sep).join('/')
scanSourceText(text, rel, schema, result)
}
return result
}
/** Scan one source text. Exposed so the guard's own behaviour is testable. */
export function scanSourceText(
text: string,
rel: string,
schema: SchemaModel,
result: ScanResult = {
columnRefs: [],
valueRefs: [],
conflictRefs: [],
tableRefs: [],
unresolved: [],
}
): ScanResult {
const source = ts.createSourceFile(
rel,
text,
ts.ScriptTarget.Latest,
true,
rel.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS
)
scanFile(source, rel, schema, result)
return result
}
function scanFile(
source: ts.SourceFile,
rel: string,
schema: SchemaModel,
result: ScanResult
): void {
const lineOf = (node: ts.Node): number =>
source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1
// Pass 1: chains anchored on `<client>.from('table')`.
const anchors: { table: string; anchor: ts.CallExpression }[] = []
const visit = (node: ts.Node): void => {
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
node.expression.name.text === 'from' &&
node.arguments.length === 1
) {
const table = literalText(node.arguments[0])
const receiver = receiverText(node.expression.expression)
if (
table &&
/^[a-z_][a-z0-9_]*$/.test(table) &&
!NON_CLIENT_RECEIVERS.has(receiver.split('.')[0]) &&
!receiver.endsWith('storage')
) {
anchors.push({ table, anchor: node })
}
}
ts.forEachChild(node, visit)
}
visit(source)
// Builder variables: `let q = supabase.from('t')...` then `q = q.eq(...)`.
// Keyed by enclosing function so the same name in two functions of one file
// does not cross-contaminate. A name bound to two tables in one scope is
// marked ambiguous and every reference through it becomes unresolved.
const bindings = new Map<string, string | null>()
const bind = (key: string, table: string): void => {
if (!bindings.has(key)) bindings.set(key, table)
else if (bindings.get(key) !== table) bindings.set(key, null)
}
const chains: { table: string | null; key: string | null; calls: ChainCall[] }[] = []
for (const { table, anchor } of anchors) {
const { calls, top } = collectChain(anchor)
if (!calls.some((c) => POSTGREST_METHODS.has(c.method))) continue
const name = bindingTarget(top)
const key = name ? `${scopeId(top)}:${name}` : null
if (key) bind(key, table)
chains.push({ table, key, calls })
}
if (chains.length === 0) return
// Pass 2: chains anchored on a bound builder variable. Iterate to a fixpoint
// so `const q2 = q.eq(...)` propagates the table to `q2`.
const identifierAnchors: { id: ts.Identifier; key: string }[] = []
const collectIdentifiers = (node: ts.Node): void => {
if (
ts.isIdentifier(node) &&
node.parent &&
ts.isPropertyAccessExpression(node.parent) &&
node.parent.expression === node &&
node.parent.parent &&
ts.isCallExpression(node.parent.parent)
) {
identifierAnchors.push({ id: node, key: `${scopeId(node)}:${node.text}` })
}
ts.forEachChild(node, collectIdentifiers)
}
collectIdentifiers(source)
const seen = new Set<ts.CallExpression>()
const extraChains: { table: string | null; calls: ChainCall[]; node: ts.Node }[] = []
for (let round = 0; round < 4; round++) {
let changed = false
for (const { id, key } of identifierAnchors) {
if (!bindings.has(key)) continue
const { calls, top } = collectChain(id)
if (calls.length === 0) continue
if (!calls.some((c) => POSTGREST_METHODS.has(c.method))) continue
const head = calls[0].node
if (!seen.has(head)) {
seen.add(head)
extraChains.push({ table: bindings.get(key) ?? null, calls, node: id })
}
const name = bindingTarget(top)
if (name) {
const targetKey = `${scopeId(top)}:${name}`
const table = bindings.get(key)
const before = bindings.has(targetKey) ? bindings.get(targetKey) : undefined
if (table) bind(targetKey, table)
else bindings.set(targetKey, null)
if (before !== bindings.get(targetKey)) changed = true
}
}
if (!changed) break
}
const all: { table: string | null; calls: ChainCall[]; node: ts.Node }[] = [
...chains.map((c) => ({
table: c.key ? (bindings.get(c.key) ?? c.table) : c.table,
calls: c.calls,
node: c.calls[0].node,
})),
...extraChains,
]
for (const chain of all) {
processChain(chain.table, chain.calls, rel, lineOf, schema, result)
}
}
/** Resolve an embed name to a table: direct table, FK column, or unknown. */
function resolveEmbedTarget(
outer: string | null,
name: string,
schema: SchemaModel
): string | null {
if (schema.tables.has(name)) return name
if (schema.views.has(name)) return null
if (outer) {
const fk = schema.tables.get(outer)?.fkTargets.get(name)
if (fk) return fk
}
return null
}
function processChain(
table: string | null,
calls: ChainCall[],
rel: string,
lineOf: (node: ts.Node) => number,
schema: SchemaModel,
result: ScanResult
): void {
const first = calls[0].node
if (table) result.tableRefs.push({ table, file: rel, line: lineOf(first) })
// Alias -> table map, built from every select in the chain, so a dotted
// embedded filter like `.eq('salary_run.status', ...)` resolves.
const aliases = new Map<string, string>()
const registerAliases = (outer: string | null, spec: string): void => {
const parsed = parseSelectSpec(spec)
for (const embed of parsed.embeds) {
const target = resolveEmbedTarget(outer, embed.name, schema)
if (target) {
aliases.set(embed.alias ?? embed.name, target)
registerAliases(target, embed.inner)
}
}
}
for (const call of calls) {
if (call.method !== 'select') continue
const spec = literalText(call.args[0])
if (spec) registerAliases(table, spec)
}
const resolvePath = (
rawPath: string,
node: ts.Node,
kind: ColumnRef['kind'],
override?: string | null
): { table: string; column: string } | null => {
const clean = rawPath.split('::')[0].split('->')[0].trim()
const parts = clean.split('.')
let owner = override ?? table
let column = clean
if (parts.length > 1) {
const prefix = parts[0].split('!')[0]
column = parts[parts.length - 1]
owner = aliases.get(prefix) ?? (schema.tables.has(prefix) ? prefix : null)
if (!owner) {
result.unresolved.push({
reason: 'embedded-filter-target',
detail: rawPath,
file: rel,
line: lineOf(node),
})
return null
}
}
if (!owner) {
result.unresolved.push({
reason: 'unknown-builder-table',
detail: `${kind} ${rawPath}`,
file: rel,
line: lineOf(node),
})
return null
}
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(column)) {
result.unresolved.push({
reason: 'unparsed-column-path',
detail: rawPath,
file: rel,
line: lineOf(node),
})
return null
}
return { table: owner, column }
}
const push = (
owner: string,
column: string,
kind: ColumnRef['kind'],
node: ts.Node,
raw: string
): void => {
result.columnRefs.push({ table: owner, column, kind, file: rel, line: lineOf(node), raw })
}
for (const call of calls) {
const { method, args, node } = call
const line = lineOf(node)
if (method === 'select') {
if (args.length === 0) continue
const spec = literalText(args[0])
if (spec === null) {
result.unresolved.push({
reason: 'dynamic-select',
detail: args[0].getText().slice(0, 60).replace(/\s+/g, ' '),
file: rel,
line,
})
continue
}
const walkSelect = (owner: string | null, text: string): void => {
const parsed = parseSelectSpec(text)
for (const item of parsed.unparsed) {
result.unresolved.push({ reason: 'unparsed-select-item', detail: item, file: rel, line })
}
if (owner === null) {
if (parsed.columns.length) {
result.unresolved.push({
reason: 'unknown-builder-table',
detail: `select ${parsed.columns.join(',')}`,
file: rel,
line,
})
}
} else {
for (const column of parsed.columns) push(owner, column, 'select', node, column)
}
for (const embed of parsed.embeds) {
const target = resolveEmbedTarget(owner, embed.name, schema)
if (!target) {
result.unresolved.push({
reason: 'embedded-resource-target',
detail: `${owner ?? '?'} -> ${embed.name}`,
file: rel,
line,
})
continue
}
walkSelect(target, embed.inner)
}
}
walkSelect(table, spec)
continue
}
if (FILTER_METHODS.has(method) || method === 'not' || method === 'filter') {
const raw = literalText(args[0])
if (raw === null) {
if (args.length > 0) {
result.unresolved.push({
reason: 'dynamic-column',
detail: `${method}(${args[0].getText().slice(0, 40).replace(/\s+/g, ' ')})`,
file: rel,
line,
})
}
continue
}
const resolved = resolvePath(raw, node, 'filter')
if (!resolved) continue
push(resolved.table, resolved.column, 'filter', node, raw)
if (method === 'eq' || method === 'neq') {
const value = literalText(args[1])
if (value !== null) {
result.valueRefs.push({ ...resolved, value, file: rel, line, raw: `${method}('${raw}')` })
}
}
if (method === 'in' && args[1] && ts.isArrayLiteralExpression(args[1])) {
for (const element of args[1].elements) {
const value = literalText(element)
if (value !== null) {
result.valueRefs.push({ ...resolved, value, file: rel, line, raw: `in('${raw}')` })
}
}
}
continue
}
if (method === 'order') {
const raw = literalText(args[0])
if (raw === null) {
if (args.length > 0) {
result.unresolved.push({
reason: 'dynamic-column',
detail: `order(${args[0].getText().slice(0, 40).replace(/\s+/g, ' ')})`,
file: rel,
line,
})
}
continue
}
let override: string | null | undefined
if (args[1] && ts.isObjectLiteralExpression(args[1])) {
for (const prop of args[1].properties) {
if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name)) continue
if (prop.name.text !== 'referencedTable' && prop.name.text !== 'foreignTable') continue
const target = literalText(prop.initializer)
override = target && schema.tables.has(target) ? target : null
}
}
const resolved = resolvePath(raw, node, 'order', override)
if (resolved) push(resolved.table, resolved.column, 'order', node, raw)
continue
}
if (method === 'or' || method === 'and') {
const spec = literalText(args[0])
if (spec === null) {
if (args.length > 0) {
result.unresolved.push({ reason: 'dynamic-logical', detail: method, file: rel, line })
}
continue
}
let override: string | null | undefined
if (args[1] && ts.isObjectLiteralExpression(args[1])) {
for (const prop of args[1].properties) {
if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name)) continue
if (prop.name.text !== 'referencedTable' && prop.name.text !== 'foreignTable') continue
const target = literalText(prop.initializer)
override = target && schema.tables.has(target) ? target : null
}
}
const parsed = parseLogicalSpec(spec)
for (const item of parsed.unparsed) {
result.unresolved.push({ reason: 'unparsed-logical-term', detail: item, file: rel, line })
}
for (const p of parsed.paths) {
const resolved = resolvePath(p, node, 'logical', override)
if (resolved) push(resolved.table, resolved.column, 'logical', node, p)
}
continue
}
if (method === 'match' || method === 'insert' || method === 'update' || method === 'upsert') {
const kind: ColumnRef['kind'] = method === 'match' ? 'match' : 'write'
const payloads: ts.Expression[] = []
if (args[0]) {
if (ts.isArrayLiteralExpression(args[0])) payloads.push(...args[0].elements)
else payloads.push(args[0])
}
for (const payload of payloads) {
if (!ts.isObjectLiteralExpression(payload)) {
result.unresolved.push({
reason: 'dynamic-payload',
detail: `${method}(${payload.getText().slice(0, 40).replace(/\s+/g, ' ')})`,
file: rel,
line,
})
continue
}
for (const prop of payload.properties) {
if (ts.isSpreadAssignment(prop)) {
result.unresolved.push({
reason: 'spread-payload',
detail: `${method} ${prop.getText().slice(0, 40)}`,
file: rel,
line,
})
continue
}
let name: string | null = null
if (ts.isPropertyAssignment(prop) || ts.isShorthandPropertyAssignment(prop)) {
if (ts.isIdentifier(prop.name)) name = prop.name.text
else if (ts.isStringLiteral(prop.name)) name = prop.name.text
}
if (name === null) {
result.unresolved.push({
reason: 'computed-payload-key',
detail: `${method} ${prop.getText().slice(0, 40)}`,
file: rel,
line,
})
continue
}
if (!table) {
result.unresolved.push({
reason: 'unknown-builder-table',
detail: `${method} ${name}`,
file: rel,
line,
})
continue
}
push(table, name, kind, node, name)
if (ts.isPropertyAssignment(prop)) {
const value = literalText(prop.initializer)
if (value !== null) {
result.valueRefs.push({
table,
column: name,
value,
file: rel,
line,
raw: `${method} ${name}`,
})
}
}
}
}
if (method === 'upsert' && args[1] && ts.isObjectLiteralExpression(args[1])) {
for (const prop of args[1].properties) {
if (!ts.isPropertyAssignment(prop) || !ts.isIdentifier(prop.name)) continue
if (prop.name.text !== 'onConflict') continue
const spec = literalText(prop.initializer)
if (spec === null) {
result.unresolved.push({ reason: 'dynamic-on-conflict', detail: '', file: rel, line })
continue
}
const columns = spec.split(',').map((c) => c.trim()).filter(Boolean)
if (!table) {
result.unresolved.push({
reason: 'unknown-builder-table',
detail: `onConflict ${spec}`,
file: rel,
line,
})
continue
}
if (!columns.every((c) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(c))) {
result.unresolved.push({
reason: 'unparsed-on-conflict',
detail: spec,
file: rel,
line,
})
continue
}
for (const column of columns) push(table, column, 'onConflict', node, spec)
result.conflictRefs.push({ table, columns, file: rel, line })
}
}
continue
}
}
}