Skip to content

fix(security): limit rh_wiki_redirects writes to the redirect owner - #238

Open
philbert440 wants to merge 11 commits into
release/0.8.19-rcfrom
fix/issue-232
Open

philbert440 wants to merge 11 commits into
release/0.8.19-rcfrom
fix/issue-232

Conversation

@philbert440

@philbert440 philbert440 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Close the rh_wiki_redirects write hole in the 0.8.19 stable release without breaking a legitimate writer, losing a redirect, or adding a deadlock. Migration 0054 gave the table one rh_own policy for every command. That policy allows a write when the login can read the target page. As a result, a person login in the space where a page is visible can insert a redirect to that page attributed to someone else. It can also rewrite or delete other people's redirects to that page. Migration 0085 (#221) split the read and write policies for rh_topic_redirects but left this table out, although 0084 had added owner_user_id to it.

Fixes #232

What changes

  • 0086_wiki_redirect_writes.sql policies. rh_own becomes rh_read FOR SELECT, which keeps today's visibility, plus separate rh_insert, rh_update and rh_delete policies. A write needs maintenance scope, or the principal in owner_user_id while the target page is visible to them. On an update, both the old and the new target must be visible. A policy without FOR also applies to SELECT, so an owner who lost access to the target page could otherwise still read the redirect.
  • No cascade from pages to redirects. The to_slug foreign key to rh_wiki_topics was ON DELETE CASCADE, and a cascade ignores row-level security. A pinned person login that deleted its own page also deleted other owners' redirects to it. 0086 drops that key (it finds the key by catalog lookup) and adds rh_wiki_redirects_to_slug_fkey ... ON DELETE RESTRICT. Deleting a page that still has redirects now fails with 23503. Dropping the old key takes ACCESS EXCLUSIVE on both tables. A fold can read rh_wiki_topics before it locks redirect rows, so any order of blocking table locks can deadlock with it. The migration therefore takes rh_wiki_redirects and rh_wiki_topics in ACCESS EXCLUSIVE mode with NOWAIT, both in one subtransaction per attempt. A failed attempt rolls back, which releases what it took, sleeps 0.5 seconds, and tries again. After 60 attempts (about 30 seconds) it fails with could not lock rh_wiki_redirects and rh_wiki_topics and the hint Stop the RivetHub nodes serving this database, then run the migration again. During a rolling upgrade the migration backs off until in-flight folds commit and then applies.
  • Every page delete clears its redirects first. Tidy RemovePage deletes the redirects to the page and then the page row, and only then removes the file. An entity merge repoints redirects to the survivor and deletes the survivor's own alias of the absorbed page. A fold repoints to the parent as before. Each write runs under the caller's policy, so maintenance runs work as before. A viewer is refused with RedirectsHeld before any write when another person's redirect is involved.
  • Owner backfill only where the owner is known. A redirect with no owner is maintenance-only. The migration gives such a row an owner only when it points at a personal page (visibility = 'private', and owner_user_id is not a space id). A shared page is owned by its space, so its ownerless aliases stay maintenance-only.
  • redirect_page. When the alias already points at the page, the viewer path writes nothing. Before this change, the topic page sync's upsert over an alias owned by someone else (or by nobody) failed with 42501 even when nothing would change. Maintenance still adopts an ownerless alias of a personal page for the page's owner, with the same rule as the backfill. In viewer scope that update matches no row under RLS.
  • Folds lock in one order and write the repository last. A fold now locks the redirects that point at the retired pages or name them or the parent, in from_slug order. It then locks the page rows in slug order, locks the redirect set again with NOWAIT, and checks ownership. A redirect committed onto a child between the two passes is not in the first set. If another transaction holds it, the fold rolls back to a savepoint, which releases every lock it took in this step, and starts the lock pass again. After three attempts it fails with RedirectsMoving and changes nothing. It repoints the redirects and retires the pages before it writes the wiki repository. A concurrent redirect move that holds a redirect and waits on a child page's key lock can no longer deadlock with the fold. Any refusal or error now comes before the git write.
  • Topic sync folds a sub-topic in one guarded operation. TopicPageStore::check_retire is gone. The new TopicPageStore::fold_section moves the sub-topic's text into its section of the root page and retires the sub-topic page. The Postgres store does this as one fold, with the check under the page-row lock, so a refusal leaves both pages unchanged. Before this change the sync checked on another connection, saved the parent and cleared the child, and then folded. A redirect committed in between left the alias on a cleared page. The fold resolves each page's home (scope directory and local file name), so pages in a user or space scope fold the same way as root pages. A fold whose existing pages live in more than one wiki repository is refused with InvalidPatch before any git or SQL write, the same rule fix(security): read a wiki page file only through a row the viewer sees #239 applies to tidy folds. A fold across repositories would commit git in one repository and remove files in another before the SQL commit, so a later failure would leave git and the index disagreeing. The fold locks the one repository and checks the homes again under the page locks. Only SQLite, which has no row-level security, still uses the save-then-fold steps (fold_section_steps, now crate-private in kernel). When the parent page has an index row but no Markdown file, the fold starts from the row's title, text, aliases, tags, entities, and related pages, and writes the file only after the redirect checks pass.
  • SCHEMA_VERSION is 86, so serving waits for the new policies.

Who writes rh_wiki_redirects

  • The wiki consolidate job, entity merges from the entity worker, rh memory transfer, merge-store, and import run in maintenance scope. They are unchanged.
  • Topic page sync and tidy run in the topic owner's viewer scope. They create redirects owned by that person and rewrite their own. An alias that already points at the right page is left alone, whoever owns it. A fold or removal of a page that another person's alias points at is refused with RedirectsHeld. The topic sweep records that as a failure for that topic and continues with the other topics. A maintenance run can still fold the page.
  • A direct SQL delete of a page that has redirects now fails with 23503 for every role, including maintenance. App paths clear the redirects first.

Release note

Schema 86: write access to wiki redirects is limited to the redirect's owner, while the owner can see the target page, and to maintenance jobs. Space members can still read them. Deleting a wiki page no longer deletes the redirects to it. The page must be folded, merged, or removed through RivetHub, which repoints or deletes them first. A fold, merge, or removal of a wiki page that another person's redirect points at is refused outside maintenance scope instead of deleting that redirect.

Upgrade every node that shares a database together. Run rh db migrate, then restart all of them on 0.8.19. A 0.8.18 node running the topic page sync against schema 86 can fail with 42501 on aliases its topic owner does not own, and its page removals fail with 23503 when the page has redirects.

Verification

  • a_space_member_cannot_rewrite_another_owners_wiki_redirect runs as a pinned person login (bob) in the shared space. At the first test commit it fails with INSERT as another owner by a space member should fail with 42501, got Ok(PgQueryResult { rows_affected: 1 }). With the fix, inserts and upserts over other owners' rows are refused with 42501, UPDATE and DELETE affect 0 rows, and SELECT still returns both rows.
  • an_owner_writes_only_redirects_to_pages_they_can_see runs as bob. He does not see his own redirect to a page he can no longer read, his UPDATE and DELETE of it affect 0 rows, and an INSERT to that page or an UPDATE onto it fails with 42501.
  • an_upgrade_keeps_legacy_wiki_redirects_writable_by_the_page_owner migrates through 85, adds ownerless aliases to a personal page, a space-owned page, and a shared page owned by a person, then runs 86. Only the personal page's alias gets an owner.
  • deleting_a_page_keeps_the_redirects_that_point_at_it deletes bob's own scoped page as bob while alice's alias and an ownerless alias point at it. At d3b0167 it fails with got Ok(PgQueryResult { rows_affected: 1 }), and the cascade removes both aliases. With the fix the delete fails with 23503 and both aliases are unchanged. A maintenance remove_page then deletes the aliases and the page.
  • redirect_page_adopts_a_legacy_redirect_even_with_the_same_target calls redirect_page for an ownerless alias of alice's personal page. As alice the owner stays empty. In maintenance scope it becomes alice. At d3b0167 the maintenance step fails with maintenance adopts an ownerless alias of a personal page for its owner.
  • topic_sync_leaves_an_alias_that_already_points_right calls redirect_page as alice for an ownerless alias and a bob alias to a shared page, and for her own alias. All succeed, and every row keeps its owner and created_at.
  • a_viewer_cannot_retire_a_page_another_person_redirects_to folds and removes a shared page that bob's alias points at, as alice. Both return RedirectsHeld, and the alias, the page row, and the parent page file are unchanged. In maintenance scope the fold succeeds and the alias points at the parent.
  • a_refused_topic_sync_fold_changes_nothing runs the topic sync as alice for a sub-topic whose page bob's alias points at. The sync fails with RedirectsHeld, the sub-topic keeps its text, the root page file does not get it, and the alias is unchanged.
  • a_topic_sync_fold_moves_the_text_and_retires_the_page_together adds a test trigger that inserts bob's alias when the sub-topic row is cleared, which stands in for a redirect committed between the check and the fold. At d3b0167 the sync clears the sub-topic and then fails with RedirectsHeld { page: "pantry", aliases: ["larder"] }. With the fix the sub-topic is never cleared on its own. Its text lands in the root page, its row is gone, and pantry redirects to kitchen.
  • a_fold_and_a_redirect_move_between_its_children_both_finish holds a redirect to child pantry on a second connection, waits until the fold is blocked on it, then moves the redirect to child larder and commits. At d3b0167 the fold fails with 40P01 deadlock detected after it has written the repository. With the fix both finish and the redirect points at the parent.
  • a_refused_scoped_topic_sync_fold_changes_nothing and a_scoped_topic_sync_fold_moves_the_text_and_retires_the_page_together run the two sync tests above with a wiki owner of phil, so alice's pages live under users/alice. At 685c858 (rebased as 66148d60) the refused sync returns Ok(()) and the successful one leaves the cleared pantry row active (left: 1). With the fix they behave like the root-scope tests, and the pantry file is removed.
  • a_topic_sync_fold_rebuilds_a_parent_file_from_its_indexed_row deletes kitchen.md but keeps its row. At 685c858 the sync returns success and the text is not moved. With the fix kitchen.md is rebuilt with both its indexed text and the sub-topic's text, and pantry redirects to kitchen. The test also sets aliases, tags, entities, and related pages on the kitchen row. At 17297ae (rebased as c8fda83) the fold clears them (the rebuilt parent keeps its aliases, got ["pantry"]). With the fix they survive.
  • a_redirect_added_while_a_fold_waits_for_its_page_does_not_deadlock holds a key-share lock on pantry until the fold waits on it. It then commits a new alias shelf to pantry, locks shelf in a mover transaction that waits for the fold's cupboard row, and releases pantry. At 685c858 the mover fails with 40P01 deadlock detected. With the fix both finish and both aliases point at kitchen.
  • the_redirect_migration_waits_for_an_in_flight_fold migrates through 85, holds a redirect row as a fold does, starts migration 86, and once the migration waits or backs off, deletes the redirect and the page row and commits. At 685c858 the migration fails with 40P01 at LOCK TABLE rh_wiki_redirects. With the fix both finish.
  • the_redirect_migration_and_a_fold_that_read_its_pages_both_finish migrates through 85, reads rh_wiki_topics in a fold transaction, starts migration 86, and once the migration waits or backs off, locks the redirect rows, deletes the redirect and the page, and commits. At 17297ae (rebased as c8fda83) the migration fails with 40P01 deadlock detected at ALTER TABLE rh_wiki_redirects DROP CONSTRAINT. With the fix both finish.
  • a_fold_across_wiki_homes_is_refused_and_changes_nothing folds alice's kitchen (under users/alice) with bob's pantry (under users/bob) in maintenance scope. At 17297ae (rebased as c8fda83) the fold returns Ok(["pantry"]). With the fix it fails with InvalidPatch, pantry keeps its row and text, no redirect is written, kitchen.md does not get the text, and pantry.md stays.
  • a_viewer_merge_keeps_another_persons_alias runs an entity merge as the entity owner while bob's alias points at the absorbed page. The merge is refused, and both entities and the alias are unchanged. In maintenance scope the merge repoints the alias to the survivor.
  • SQLX_OFFLINE=true CARGO_BUILD_JOBS=5 cargo xtask check --locked --no-nextest-list reports GATE OK: 0 finding(s).
  • cargo xtask commits origin/release/0.8.19-rc..HEAD reports GATE OK.
  • sqlite_page_writers_preserve_transferred_fields_on_return_to_postgres (cli) deleted fixture pages with raw SQL while the fixture's move alias pointed at transfer, so it failed with 23503 under the RESTRICT key. It now deletes those redirects first, as the app paths do. It passes on origin/release/0.8.19-rc only because the cascade is still there.
  • Full workspace cargo nextest run -j5 --workspace --no-fail-fast against Postgres at 450473c (rebased onto e7089b9, fix(memory): stop the topic-page sweep from blanking a fresh entity page #236): 3373 of 3373 pass, 1 skipped.

Proved with: each new test failing at 685c858 (rebased as 66148d60) or 17297ae (rebased as c8fda83): scoped sync refusal returns Ok(()), scoped fold leaves the sub-topic row, missing parent file skips the move, 40P01 on a redirect added mid-fold, 40P01 in migration 86 beside a fold that holds a redirect row or has read rh_wiki_topics, a fold across wiki homes returns Ok, a rebuilt parent loses its aliases; all pass at the fix. cargo xtask check (GATE OK); cargo xtask commits (GATE OK); full workspace nextest 3373/3373 at 450473c.

@philbert440
philbert440 force-pushed the fix/issue-232 branch 2 times, most recently from 685c858 to 17297ae Compare November 25, 2026 18:05
…redirect

A person login that is a member of the space where a wiki page is
visible can insert a redirect to that page attributed to another
person, and can rewrite or delete other people's redirects to it. The
rh_own policy from 0054 allows any write whose target page the login
can read.

The second test upgrades a database that holds a legacy redirect with
no owner, then runs redirect_page as the page owner, as the topic page
sync does.
Migration 0086 replaces the rh_own policy on rh_wiki_redirects. rh_read
FOR SELECT keeps today's visibility. Separate INSERT, UPDATE, and DELETE
policies allow maintenance scope, or the principal in owner_user_id
while the target page is visible to them. A policy without FOR applies
to SELECT too, so an owner could otherwise still read a redirect to a
page they lost access to.

A redirect with no owner is writable only in maintenance scope. The
migration gives such a row an owner only when it points at a personal
page, whose owner_user_id names a person. A shared page is owned by its
space, so its ownerless aliases stay maintenance-only.

The migration takes ACCESS EXCLUSIVE on rh_wiki_redirects, which the
policy DDL needs anyway, before the backfill, with a 10s lock_timeout.
No ownerless row can land between the backfill and the new policies,
and the lock is never upgraded mid-transaction.

redirect_page returns without writing when the alias already points at
the page. The topic page sync runs as the topic owner, and an upsert
over an alias that person does not own fails with 42501 even when it
would change nothing. It also no longer adopts an ownerless alias that
already points at the page. Migration 0086 does that once, and only for
personal pages.

SCHEMA_VERSION is 86, so serving waits for the new policies.

Fixes #232
Deleting a wiki page cascades to every redirect that points at it, and
the cascade ignores row-level security. Under the 0086 policies a
viewer-scoped fold or merge skips repointing redirects owned by someone
else, so the cascade then deleted them.

Consolidation folds, tidy merges and removals, entity merges, and the
topic page sync now check the absorbed page before they change any wiki
file or row. When the page has incoming redirects the caller may not
write, they fail with WikiStoreError::RedirectsHeld and leave everything
as it was. Maintenance scope may write every redirect, and a role that
bypasses row-level security is not checked, so those runs fold as
before.

The store locks the page row before the check, so a concurrent redirect
insert, whose foreign key check takes a key-share lock on that row,
waits until the fold commits or rolls back. The topic page sync checks
through TopicPageStore::check_retire before it moves a sub-topic's text
into the root page.
…lds and fold deadlocks

A pinned login deleting its own page must not cascade away other
people's redirects. A maintenance redirect_page adopts an ownerless
alias of a personal page. A topic sync fold moves the sub-topic text
and retires the page together. A fold and a redirect moving between its
children both finish.
Migration 0086 replaces the rh_wiki_redirects to_slug cascade with
ON DELETE RESTRICT, so a page delete no longer removes redirects that
row-level security would refuse to the caller. Page removal and entity
merges now delete or repoint the redirects first under the caller's
policy.

Folds lock the redirects that point at or name the retired pages in
key order, then the page rows, check ownership, and repoint and retire
before the wiki file is written. A topic sync moves the sub-topic text
and retires its page in that one guarded fold, so a refusal changes
nothing. redirect_page again lets maintenance adopt an ownerless alias
of a personal page that already points right.
…ile and fold lock races

A topic sync folds a sub-topic in a non-root wiki scope, or refuses it,
in one step. A section fold whose parent page has an index row but no
file rebuilds the parent and moves the text. A redirect committed while
a fold waits for its page, and the 0086 migration running beside an
in-flight fold, both finish without a deadlock.
…edirect set

A fold now resolves each page's home (scope directory and local name),
locks the repositories it touches in directory order, and checks the
homes again under the page locks. A section fold of a scoped page runs
the same guarded fold as a root page, so the save-then-fold steps are
left only to SQLite. A parent page with an index row but no file is
rebuilt from that row and written after the redirect checks pass.

After locking the page rows, the fold locks the redirect set again with
NOWAIT. If a redirect it did not hold is locked by another transaction,
it rolls back to a savepoint, releasing its locks, and retries up to
three times before failing with RedirectsMoving.

Migration 0086 locks rh_wiki_redirects before rh_wiki_topics, the order
a fold takes, so it waits for an in-flight fold instead of deadlocking.
… round trip

Migration 0086 makes the wiki redirect foreign key ON DELETE RESTRICT,
so the round-trip test now deletes the fixture's redirect to the
transfer page before it deletes the page rows.
…igration

A fold reads rh_wiki_topics and then locks rh_wiki_redirects. Migration
0086 locked rh_wiki_redirects first and then needed ACCESS EXCLUSIVE on
rh_wiki_topics to drop the foreign key, so the two could deadlock. The
migration now takes both tables with NOWAIT in one subtransaction per
attempt, backs off half a second after a failed attempt, and after 60
attempts fails with a hint to stop serving nodes and rerun.
When a fold's parent page file is missing, the fold rebuilt the page from
its rh_wiki_topics row but left aliases, tags, entities and related pages
empty, and the upsert then cleared them in the index. The rebuilt page now
carries every indexed field of the row.
A fold across wiki homes committed the parent in one repository and
removed children in another before the SQL commit, so a later failure
left git and the index disagreeing. Such folds are now refused before any
git or SQL write, and the fold locks and writes the one repository that
holds every page.
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