Skip to content

fix(security): read a wiki page file only through a row the viewer sees - #239

Open
philbert440 wants to merge 4 commits into
release/0.8.19-rcfrom
fix/issue-231
Open

philbert440 wants to merge 4 commits into
release/0.8.19-rcfrom
fix/issue-231

Conversation

@philbert440

@philbert440 philbert440 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Why

In 0.8.19 stable, a person on a shared Postgres hub must never read, write or delete another owner's wiki file. Owners, space members and maintenance must keep working. On Postgres, a viewer who could not see a page's row still read that page's file from the primary owner's wiki. #231 showed this for topic_page. The same fallback also reached the archive read and remove_page, and remove_page deleted the file. Review rounds 1 and 2 found more paths that built a file path from a global key alone, or trusted an owner they had not verified.

Fixes #231

Rule

Before any file access, the store resolves the page's home (scope directory and local name) from a row the caller can see. It also checks that the row's owner owns that home. If it cannot establish both, it serves the row text and opens no file, or it refuses.

Root cause

page_home (crates/memory-postgres/src/wiki_scope.rs) made up a home in the primary wiki (directory "", local name = slug) when the current scope could not see a row. It also mapped every legacy row (scope IS NULL) to the primary wiki without checking its owner. Other paths never called it. fold_wiki opened the primary repository and used global keys as file names. The recompile CLI queued file names from the primary wiki, but the worker read them as global keys.

Fix

  • page_home returns a home only for a visible row whose owner owns the home. A scoped row must belong to its scope's owner, or to the space for a space scope. A legacy row must belong to the primary wiki's owner.
  • The primary wiki's owner is a LegacyOwner, resolved by PostgresMemory::legacy_owner. It reads the owner recorded on the root scope and compares it with the configured wiki owner. If both exist and disagree, the owner is contested, and no legacy row gets a file (row text only). RLS hides a private root scope row from everyone but its owner, so this one read runs under Scope::Maintenance. It reads one fixed column and takes no caller input. Each caller resolves it before it takes its own connection. If no owner is recorded or configured, only maintenance or an unscoped single-user store falls back to the primary wiki.
  • page_home and the consolidation stats share one query (with_homes), so planning and execution apply the same home rule.
  • fold_wiki (tidy fold, consolidation) resolves the home of the parent and of each child before it touches git. It folds by local name in the parent's own wiki and stores the parent row in its scope. It refuses a fold across wikis, and it refuses a child whose home it cannot verify.
  • wiki_topic_stats plans folds only from primary-wiki pages with a verified home. It logs the pages it skips, so one rejected page no longer aborts the consolidation batch.
  • Topic sync under a person scope: a new placement with no visible row never reads the file at its target. It writes through LockedRepository::create (crates/wiki-files), which syncs the repository under the writer lock and then checks whether the destination path exists with symlink_metadata, without opening the file. If the path exists, the sync returns SlugConflict. This matches the create signature in fix(memory): stop the topic-page sweep from blanking a fresh entity page #236. When fix(memory): stop the topic-page sweep from blanking a fresh entity page #236 lands, keep one create with this existence check. Maintenance keeps the old behavior of adopting an orphan file.
  • page_place returns None for a row the caller cannot see. within rejects that target, and overlaid returns no overlay for it.
  • Recompile: the CLI resolves each primary-wiki file name to the global key of the primary page that owns it (PostgresMemory::primary_page_key). It skips a loose file whose name is another page's key. The worker recovers a page with no row only when no row holds the key. When the primary owner is known, it first establishes that owner's primary scope (primary_scope) and stores the recovered row in that scope with its owner. When the owner is contested, it skips the page and logs why. A single-user store with no owner recorded or configured keeps the legacy row, which the same home rule accepts under maintenance (the wiki_bounded recompile tests cover this).
  • read_wiki_page, read_wiki_archive, topic_page, edit_page and remove_page keep their round-1 behavior: with no home they serve the row only or touch nothing.

SQLite is unchanged. A SQLite node serves one person.

Tests

crates/memory-postgres/tests/it/page_access.rs runs as the RLS probe role under Scope::Viewer, with different prose in each row and its file:

  • a_visible_row_in_a_hidden_scope_never_reads_the_primary_wiki: the space member gets exactly the row text, and the gateway-backed markdown must contain it. The owner gets the prose from her file.
  • a_legacy_row_reads_the_primary_wiki_only_for_its_owner: coco's legacy row never serves phil's diary.md. Phil's shared legacy row still reads his file.
  • a_legacy_row_never_reads_a_root_wiki_recorded_for_another_owner (round 2, S2): phil is the configured owner and sees his own legacy row. He gets the row text, not coco's root file.
  • folding_space_pages_touches_only_the_space_wiki and a_fold_across_wikis_is_refused.
  • consolidation_skips_a_page_without_a_verified_home (round 2, S7): the plan leaves out coco's legacy row in the primary wiki, the valid fold runs, and the skipped row and its file stay.
  • a_topic_sync_never_writes_over_a_file_it_has_no_row_for: the crew file and the crew repository HEAD stay unchanged.
  • a_new_page_never_claims_an_unreadable_file and a_new_page_never_claims_a_file_the_sync_brings_in (round 2, S3): a file that does not parse, and a file that only the repository sync pulls in from the upstream, both give SlugConflict and stay unchanged.
  • a_summary_cites_no_page_its_owner_cannot_see.
  • a_recompile_targets_the_primary_wiki_page_by_its_file_name.
  • a_restored_file_recovers_into_the_primary_owners_scope and a_restored_file_is_skipped_while_the_primary_owner_is_contested (round 2, S5): on a database with no scopes and phil configured, recovery stores the row with scope and owner phil, and phil reads the file. With the root recorded for coco and phil configured, recovery returns nothing and inserts no row.

The six round-2 tests fail on the 58aa1d9 sources:

a_new_page_never_claims_an_unreadable_file: got Err(Page(Page(Invalid(MissingFrontmatter))))
a_new_page_never_claims_a_file_the_sync_brings_in: got Ok(())
a_legacy_row_never_reads_...: left: Some("Coco's root diary.")  right: Some("Phil's diary row.")
a_restored_file_recovers_...: left: (None, None, None)  right: (Some("phil"), Some("phil"), Some("orphan"))
a_restored_file_is_skipped_while_the_primary_owner_is_contested: left: Some(RecompileSource { .. })  right: None
consolidation_skips_...: Error: InvalidPatch("wiki page mesh-host-diary is outside the wiki of mesh-host")

Proved with: SQLX_OFFLINE=true CARGO_BUILD_JOBS=5 cargo xtask check --locked --no-nextest-list (GATE OK), cargo nextest run -j5 -p memory-postgres -p memory-sqlite -p mcp -p gateway -p rivethub -p cli -p workers --no-fail-fast against Postgres on a2446d3 (2124 passed, 1 skipped), cargo xtask commits origin/release/0.8.19-rc..HEAD (GATE OK), and the six round-2 page_access tests failing on the 58aa1d9 sources.

A viewer who cannot see a page row still reads the page file from the
primary wiki through topic_page and the archive read, and remove_page
deletes it. A visible row whose wiki scope the viewer cannot see reads
the primary wiki file of the same local name. Refs #231.
page_home invented a primary-wiki home for any slug whose row the current
scope could not see, and for a row whose wiki scope it could not see. The
callers then read, archived, or removed the primary owner's file of that
name. page_home now returns no home in both cases. topic_page falls back to
the row, the archive read and recompile source return nothing, edit_page
and remove_page touch nothing, and page placement refuses to write. remove_page
also removes the file from the page's own wiki instead of the primary one.

Recompile keeps a primary-wiki fallback for a row-less page under the
maintenance scope only, where no row is hidden and the rebuilt page stays
in the primary wiki. Page routing keeps its raw row lookup through
page_place so its placement does not change.

Fixes #231.
page_home now returns a home only for a row whose owner owns that home.
A scoped row must belong to its scope's owner, or to the space for a
space scope. A legacy row with no scope must belong to the primary
wiki's owner: the configured owner, or else the owner of the primary
scope row. A person whose primary owner is unknown gets no legacy file.
Rows without a verified home serve their row text only.

fold_wiki resolved nothing and used global keys as file names in the
primary repository. A tidy fold of space pages could read, rewrite and
delete primary files of the same name. It now resolves the parent's and
each child's home first, folds by local names in the parent's own wiki,
stores the parent row in its scope, and refuses a fold across wikis or
of a child whose home it cannot verify.

Topic sync under a person scope treated a slug as free when the row
holding it was hidden, then read and committed whatever file had that
local name in the target wiki. A fresh placement now refuses with a
slug conflict when the file already exists, before reading or writing.

page_place returns None for an invisible row, so wiki routing no longer
cites a page the summary owner cannot see as a primary page.

The recompile CLI queued primary wiki file names, which the worker read
as global keys. It now resolves each file name to the key of the primary
page that owns it, and skips a loose file whose name is another page's
key. The worker recovers a rowless page only when no row holds the key,
and writes it into the primary scope.

Refs #231.
…didates

The configured wiki owner took precedence over the owner recorded on the
root scope. When the two disagreed, a legacy row of the configured owner
read the root files of the recorded owner. RLS hides that root scope row
from everyone but its owner, so the SQL check could not see the
disagreement. legacy_owner now reads the recorded root owner under the
maintenance scope (one fixed column) and compares it with the configured
owner. If they disagree, no legacy row gets a file, and those rows serve
their row text only.

A new page under a person scope checked that its file was vacant by
reading and parsing the file before the write synced the repository. An
unparseable file returned a parse error instead of a slug conflict, and a
file that the sync pulled in was overwritten. The write now goes through
LockedRepository::create, which syncs and then checks that the
destination path exists with symlink_metadata, without opening the file.
The new page path no longer reads the existing file at all.

Rowless recompile recovery accepted a missing root scope and stored an
ownerless legacy row, which the worker rejects once an owner is
configured. When the primary owner is known, recovery now establishes
the primary scope for that owner first and stores the row in that scope
with its owner. When the owner is contested, it skips the page and logs
why. A single-user store with no owner recorded or configured keeps the
legacy row, which the same home rule accepts under maintenance.

Consolidation planned folds from every primary-wiki row, so one row with
no verified home aborted the whole batch. wiki_topic_stats now applies
the same home rule as fold execution (the shared homes query), logs the
rows it skips, and plans folds only from verified pages.

Refs #231.
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