Skip to content

fix(db): drop four review-link policies 347 added that already existed - #723

Merged
thinkclay merged 1 commit into
mainfrom
claude/fix-347-redundant-policies
Sep 10, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/fix-347-redundant-policies

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Follow-up to #667, correcting a mistake in it.

What was wrong

Migration 347 added review_links_{select,insert,update,delete}_own. Migration 326 had already created review_links_owner_{select,insert,update,delete} with the same predicate — subject_user_id = auth.uid() — when it enabled RLS. Different names, so nothing collided, and the table ended up with two identical sets:

 review_links_delete_own   | DELETE | (subject_user_id = auth.uid())
 review_links_owner_delete | DELETE | (subject_user_id = auth.uid())
 review_links_insert_own   | INSERT | (subject_user_id = auth.uid())
 review_links_owner_insert | INSERT | (subject_user_id = auth.uid())
 …

Nothing was widened — the predicates are identical, and permissive policies for the same command OR together. But two sets saying the same thing is a trap for whoever audits this table next.

The fix in #667 was still the right one

326 enabled RLS and wrote the policies but never issued a GRANT, so authenticated and service_role had no privileges and every endpoint answered permission denied for table review_links. That is the #481 / 332 / 335 pattern, and the GRANTs are what resolved it. Only the policy half of 347 was redundant.

How I got it wrong

select polname||' | '||polcmd from pg_policy where polrelid=…
ERROR:  operator is not unique: text || "char"

I ran that with 2>/dev/null. The error printed nothing, an empty result read as "no policies at all", and that claim went into the migration comment, the PR body and the issue. pg_policies shows them plainly — which is what I used for role_assignments in the same PR, and why that half was right.

Worth keeping: suppressing stderr on a query turns an error into a false negative that reads like a finding.

Verified after dropping them

check result
list ✅ 200
create ✅ 201
another user listing your links ✅ 0
cross-user revoke ✅ 404

326's four policies carry the table on their own.

🤖 Generated with Claude Code

Migration 347 wrote review_links_{select,insert,update,delete}_own onto
core.review_links. Migration 326 had already created
review_links_owner_{select,insert,update,delete} with the same predicate,
`subject_user_id = auth.uid()`, when it enabled RLS. The names differed so
nothing collided, and the table ended up with two identical sets.

The bug 347 fixed is real and stays fixed: 326 enabled RLS and wrote policies
but never issued a GRANT, so `authenticated` and `service_role` had no
privileges and every review-link endpoint answered `permission denied for table
review_links`. That is the #481 / 332 / 335 pattern, and the GRANTs are what
resolved it.

The policy half came from a bad reading. This query:

    select polname||' | '||polcmd from pg_policy where polrelid=...

fails with `operator is not unique: text || "char"`. It was run with stderr
discarded, so the error printed nothing, an empty result read as "no policies at
all", and that claim went into the migration comment, the PR and the issue.
pg_policies shows them plainly, which is what the role_assignments check in the
same PR used.

Nothing was widened — the duplicates are identical and permissive policies for
the same command OR together — but two sets saying the same thing is a trap for
whoever audits this table next.

Verified after dropping them: list 200, create 201, another user sees none of
your links, and a cross-user revoke is 404.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit 82020ba into main Sep 10, 2026
7 of 8 checks passed
@thinkclay
thinkclay deleted the claude/fix-347-redundant-policies branch September 10, 2026 19:53
thinkclay added a commit that referenced this pull request Sep 10, 2026
…top search_path drifting (#725)

* fix(db): grant service_role the eight tables it could not read, and stop 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]>

* chore(deps): bump the sdk pointer to collapse the tsx duplicate

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]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>
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.

1 participant