Repository navigation
fix(security): limit rh_wiki_redirects writes to the redirect owner - #238
Open
philbert440 wants to merge 11 commits into
Open
philbert440 wants to merge 11 commits into
philbert440 wants to merge 11 commits into
Conversation
philbert440
force-pushed
the
fix/issue-232
branch
2 times, most recently
from
November 25, 2026 18:05
685c858 to
17297ae
Compare
…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.
philbert440
force-pushed
the
fix/issue-232
branch
from
November 25, 2026 18:48
17297ae to
450473c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Close the
rh_wiki_redirectswrite 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 onerh_ownpolicy 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 forrh_topic_redirectsbut left this table out, although 0084 had addedowner_user_idto it.Fixes #232
What changes
0086_wiki_redirect_writes.sqlpolicies.rh_ownbecomesrh_read FOR SELECT, which keeps today's visibility, plus separaterh_insert,rh_updateandrh_deletepolicies. A write needs maintenance scope, or the principal inowner_user_idwhile the target page is visible to them. On an update, both the old and the new target must be visible. A policy withoutFORalso applies to SELECT, so an owner who lost access to the target page could otherwise still read the redirect.to_slugforeign key torh_wiki_topicswasON 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 addsrh_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 readrh_wiki_topicsbefore it locks redirect rows, so any order of blocking table locks can deadlock with it. The migration therefore takesrh_wiki_redirectsandrh_wiki_topicsin ACCESS EXCLUSIVE mode withNOWAIT, 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 withcould not lock rh_wiki_redirects and rh_wiki_topicsand the hintStop 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.RemovePagedeletes 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 withRedirectsHeldbefore any write when another person's redirect is involved.visibility = 'private', andowner_user_idis 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.from_slugorder. It then locks the page rows in slug order, locks the redirect set again withNOWAIT, 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 withRedirectsMovingand 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.TopicPageStore::check_retireis gone. The newTopicPageStore::fold_sectionmoves 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 withInvalidPatchbefore 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 inkernel). 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_VERSIONis 86, so serving waits for the new policies.Who writes
rh_wiki_redirectsrh memory transfer,merge-store, and import run in maintenance scope. They are unchanged.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.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_redirectruns as a pinned person login (bob) in the shared space. At the first test commit it fails withINSERT 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_seeruns asbob. 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_ownermigrates 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_itdeletesbob's own scoped page asbobwhilealice's alias and an ownerless alias point at it. At d3b0167 it fails withgot 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 maintenanceremove_pagethen deletes the aliases and the page.redirect_page_adopts_a_legacy_redirect_even_with_the_same_targetcallsredirect_pagefor an ownerless alias ofalice's personal page. Asalicethe owner stays empty. In maintenance scope it becomesalice. At d3b0167 the maintenance step fails withmaintenance adopts an ownerless alias of a personal page for its owner.topic_sync_leaves_an_alias_that_already_points_rightcallsredirect_pageasalicefor an ownerless alias and abobalias to a shared page, and for her own alias. All succeed, and every row keeps its owner andcreated_at.a_viewer_cannot_retire_a_page_another_person_redirects_tofolds and removes a shared page thatbob's alias points at, asalice. Both returnRedirectsHeld, 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_nothingruns the topic sync asalicefor a sub-topic whose pagebob's alias points at. The sync fails withRedirectsHeld, 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_togetheradds a test trigger that insertsbob'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 withRedirectsHeld { 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, andpantryredirects tokitchen.a_fold_and_a_redirect_move_between_its_children_both_finishholds a redirect to childpantryon a second connection, waits until the fold is blocked on it, then moves the redirect to childlarderand commits. At d3b0167 the fold fails with40P01 deadlock detectedafter it has written the repository. With the fix both finish and the redirect points at the parent.a_refused_scoped_topic_sync_fold_changes_nothinganda_scoped_topic_sync_fold_moves_the_text_and_retires_the_page_togetherrun the two sync tests above with a wiki owner ofphil, soalice's pages live underusers/alice. At 685c858 (rebased as 66148d60) the refused sync returnsOk(())and the successful one leaves the clearedpantryrow active (left: 1). With the fix they behave like the root-scope tests, and thepantryfile is removed.a_topic_sync_fold_rebuilds_a_parent_file_from_its_indexed_rowdeleteskitchen.mdbut keeps its row. At 685c858 the sync returns success and the text is not moved. With the fixkitchen.mdis rebuilt with both its indexed text and the sub-topic's text, andpantryredirects tokitchen. The test also sets aliases, tags, entities, and related pages on thekitchenrow. 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_deadlockholds a key-share lock onpantryuntil the fold waits on it. It then commits a new aliasshelftopantry, locksshelfin a mover transaction that waits for the fold'scupboardrow, and releasespantry. At 685c858 the mover fails with40P01 deadlock detected. With the fix both finish and both aliases point atkitchen.the_redirect_migration_waits_for_an_in_flight_foldmigrates 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 with40P01atLOCK TABLE rh_wiki_redirects. With the fix both finish.the_redirect_migration_and_a_fold_that_read_its_pages_both_finishmigrates through 85, readsrh_wiki_topicsin 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 with40P01 deadlock detectedatALTER TABLE rh_wiki_redirects DROP CONSTRAINT. With the fix both finish.a_fold_across_wiki_homes_is_refused_and_changes_nothingfoldsalice'skitchen(underusers/alice) withbob'spantry(underusers/bob) in maintenance scope. At 17297ae (rebased as c8fda83) the fold returnsOk(["pantry"]). With the fix it fails withInvalidPatch,pantrykeeps its row and text, no redirect is written,kitchen.mddoes not get the text, andpantry.mdstays.a_viewer_merge_keeps_another_persons_aliasruns an entity merge as the entity owner whilebob'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-listreportsGATE OK: 0 finding(s).cargo xtask commits origin/release/0.8.19-rc..HEADreports GATE OK.sqlite_page_writers_preserve_transferred_fields_on_return_to_postgres(cli) deleted fixture pages with raw SQL while the fixture'smovealias pointed attransfer, 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.cargo nextest run -j5 --workspace --no-fail-fastagainst 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,40P01on a redirect added mid-fold,40P01in migration 86 beside a fold that holds a redirect row or has readrh_wiki_topics, a fold across wiki homes returnsOk, 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.