Skip to content

fix(db): grant service_role the eight tables it could not read, and stop search_path drifting - #725

Merged
thinkclay merged 2 commits into
mainfrom
claude/db-grants-searchpath
Sep 10, 2026
Merged

thinkclay merged 2 commits into
mainfrom
claude/db-grants-searchpath

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Two grant/definition gaps, both reproduced against a live database before and after.

#481 — eight core tables service_role cannot read

service_role is the bypass-everything role, and edge-function code reaches for the service client whenever it needs to read past RLS. These eight were readable by authenticated (three also by anon) and not by service_role at all, so that client got a GRANT error — which fires before RLS is even evaluated:

{ code: "42501", message: "permission denied for table soft_skills" }

All eight have RLS enabled with policies already written, so it is the same omission migrations 332 (api_keys) and 335 (scheduling) fixed: the policies were authored, the table-level GRANTs beneath them were not. A policy cannot widen access past an absent GRANT.

core.soft_skills is the clearest case — public reference data seeded by migration 001, with a public-read policy for anon and authenticated. It is also where this bit: GET /v1/profiles/:username had to resolve skill names with the caller's client because the service client could not read the catalog (see resolveSkillNames, #464).

Migration 352 grants service_role only.

before after
the issue's detection query 8 tables none
soft_skills / portfolio_items for anon+authenticated granted unchanged
archetypes for authenticated granted unchanged

#468 — ten functions with no search_path

Migration 308 set one on every owned function in core/data/community/onet/public. Ten had none, because migrations 314–325 created them after 308 ran.

Nine are trigger functions plus core.tasks_handle_completion, so today's risk is low — but a function without an explicit search_path resolves unqualified names against whatever the caller's search_path happens to be, and "it is only a trigger function" is not a property that stays true.

Migration 353 sets them, as explicit ALTERs against real signatures rather than another positional DO-sweep. A sweep is one-shot by construction — it covers what exists when it runs and nothing after — which is exactly how these drifted unnoticed for eleven migrations. The count the issue asks about is now 0.

The half that matters more

scripts/check-migrations.mjs, wired into pnpm lint and prepush, fails the build when a migration above 353 creates a function without a search_path. So there is no twelfth.

It is exercised rather than assumed — the first version was decorative. Its regex read (?:or\s+replace\s+)+?, requiring at least one occurrence, so a plain CREATE FUNCTION — the usual form for a new function, and the only form this guard exists to catch — passed straight through. It was reporting "✓" over zero files, which is the same shape as the bugs the last two sessions were spent removing, so I probed it with a deliberate offender instead of trusting the green.

Now verified against five cases:

case result
plain CREATE FUNCTION, no path fails ✅
CREATE OR REPLACE, no path fails ✅
path declared in the header passes ✅
search_path mentioned only in the body fails ✅
function in an unowned schema out of scope ✅

Also: two migrations sharing a number

The same script fails on duplicate migration numbers. Not hypothetical — #723 landed 351_drop_redundant_review_link_policies.sql while this branch was writing its own 351. Git merges both files without a conflict, and migration order becomes whatever the filesystem returns. Concurrent branches make this likely rather than exotic.

Mine are renumbered to 352 and 353, and the check now covers all 189 migrations.

Closes #481
Closes #468

…top search_path drifting

Two grant/definition gaps, both reproduced against a live database before and
after.

**#481 — eight core tables service_role cannot read.** `service_role` is the
bypass-everything role, and edge-function code reaches for the service client
whenever it needs to read past RLS. These eight were readable by
`authenticated` (three also by `anon`) and not by `service_role` at all, so that
client got a GRANT error — which fires before RLS is even evaluated:

    { code: "42501", message: "permission denied for table soft_skills" }

All eight have RLS enabled with policies already written, so it is the same
omission migrations 332 (api_keys) and 335 (scheduling) fixed: the policies were
authored, the table-level GRANTs beneath them were not. A policy cannot widen
access past an absent GRANT.

`core.soft_skills` is the clearest case — public reference data seeded by
migration 001, with a public-read policy for anon and authenticated. It is also
where this bit: GET /v1/profiles/:username had to resolve skill names with the
caller's client because the service client could not read the catalog (see
`resolveSkillNames`, #464).

Migration 352 grants service_role only. anon and authenticated are untouched,
verified after: soft_skills and portfolio_items still anon+authenticated,
archetypes still authenticated. The issue's own detection query now returns
nothing.

**#468 — ten functions with no search_path.** Migration 308 set one on every
owned function in core/data/community/onet/public. Ten had none, because
migrations 314-325 created them after 308 ran. Nine are trigger functions plus
core.tasks_handle_completion, so today's risk is low — but a function without an
explicit search_path resolves unqualified names against whatever the caller's
search_path happens to be, and "it is only a trigger function" is not a property
that stays true.

Migration 353 sets them, as explicit ALTERs against real signatures rather than
another positional DO-sweep. A sweep is one-shot by construction — it covers
what exists when it runs and nothing after — which is exactly how these drifted
unnoticed for eleven migrations. The count the issue asks about is now 0.

**The half that matters more:** scripts/check-migrations.mjs, wired into
`pnpm lint` and prepush, fails the build when a migration above 353 creates a
function without a search_path. So there is no twelfth.

It is exercised rather than assumed — the first version was decorative. Its
regex read `(?:or\s+replace\s+)+?`, requiring at least one occurrence, so a
plain `CREATE FUNCTION` (the usual form for a new function, and the only form
this guard exists to catch) passed straight through. Caught by probing it with a
deliberate offender instead of trusting a green run over zero files. Now
verified against five cases: plain CREATE without a path fails, CREATE OR
REPLACE without a path fails, a path in the header passes, a path mentioned only
in the body fails, and a function in an unowned schema is out of scope.

The same script also fails on two migrations sharing a number. That is not
hypothetical either: #723 landed 351_drop_redundant_review_link_policies.sql
while this branch was writing its own 351, and git merges both files without a
conflict — leaving migration order to whatever the filesystem returns. Mine are
renumbered to 352 and 353.

Closes #481
Closes #468

Co-Authored-By: Claude Opus 5 <[email protected]>
Scaffald/sdk#22 points sdk's tsx/tsup/typescript/vitest at the workspace
catalog. Only tsx actually moves: it declared `^4.7.0` and resolved to 4.22.3,
which does not satisfy the catalog's `~4.19.3`, so the lockfile carried both
that and the 4.19.4 that packages/ai-pipeline gets through `catalog:`.

`pnpm dedupe --check` — what Monorepo integrity runs — fails on that split. It
is invisible on main because the check is gated on the deps paths filter, so
only a PR touching a package.json ever runs it, and this PR's two-line script
addition was the first in a while.

tsx now resolves to 4.19.4 and `pnpm dedupe --check` exits 0.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit 5297050 into main Sep 10, 2026
12 checks passed
@thinkclay
thinkclay deleted the claude/db-grants-searchpath branch September 10, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(db): 8 core tables grant SELECT to authenticated but not service_role fix(db): 10 functions from migrations 314-325 have no search_path

1 participant