Skip to content

fix: verify a discovered server's App, keep only real links in notes, tui --once exit codes - #346

Open
sweepies wants to merge 2 commits into
mainfrom
fix/cli-discovery-notes
Open

sweepies wants to merge 2 commits into
mainfrom
fix/cli-discovery-notes

Conversation

@sweepies

@sweepies sweepies commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Three fixes in the cli and notes crates.

Server discovery trusted any bot's marker (security)

discover accepted a patchula-server: marker from any Bot account, as long as the advertised server's /api/v1/auth named that same bot. But the advertised server answers whatever it likes. Say another bot copies attacker text into an issue (a workflow running as github-actions[bot], or a bot that pastes release notes). The attacker's server could then claim that bot and hand out the real Count App's client_id. The user approves the familiar App on GitHub, and the CLI sends the resulting user token to the attacker.

Invariant: the device flow only ever signs in to the GitHub App that wrote the marker.

  • Discovery takes the app slug from the comment author's login ({slug}[bot]; anything that is not [a-zA-Z0-9-]+ is skipped without a request). It fetches GET https://api.github.com/apps/{slug} and requires that App's client_id to equal the one the server advertises. Any error or mismatch skips the marker (fail closed).
  • The verified client ID is pinned. It is stored with the user's answer in repositories.json (new required app field) and carried through --remote discovery as remote::Server. Sign-in and token refresh refuse a discovered server that later names a different App, which closes the check-then-use gap between discovery and login(). Servers the user names (PATCHULA_SERVER, --server) are not pinned; the user vouches for those.
  • guide/server.mdx no longer claims a marker "written by another App, is ignored". It now describes what is checked.

Hard cutover: a repositories.json written before this has no app, so it fails to load with the existing "is not valid; delete it to start over" message. Those choices were made under the broken check, so they should not carry over silently.

Tests (fake transport, as the rest of remote/tests.rs): the attack itself (other[bot] marker; the server claims other[bot] and the Count's client ID; GitHub says other has another ID), an App GitHub cannot describe, a server whose client ID does not match its author's App, slugs that are never looked up, and a remembered server that switches App being refused before any github.com/login request.

Notes sanitise kept non-links verbatim (security)

sanitise kept a [...] label untouched whenever any [ followed it, or any parentheses. GitHub shows [@alice][1] (no definition), [@org/team](see notes) and [#12][x] literally, so they became live mentions and backlinks from every Count PR.

Invariant: a label stays verbatim only when GitHub renders it as a link.

  • An inline destination must parse as CommonMark: <...>, or no spaces or control characters with balanced parentheses; an optional closed title; at most one line ending.
  • A reference (full, collapsed, shortcut) must name a definition GitHub honours. A definition sits outside code, starts a block (after a blank line, heading or another definition, since it cannot interrupt a paragraph), has no backslash in its label, and fits on one line with only a destination and an optional closed title. Definitions are now found once, and the inline definition check uses the same set.
  • Links do not nest, so a label holding a link (images excepted) is not one.
  • Everything else goes through the normal mention and issue handling. The checks are strict on purpose: a real link missed only gets its label neutralised.

The existing test that had [ref]: ... directly under a paragraph line was relying on a definition GitHub ignores. It now has blank lines around the definition. New tests cover the three reported cases plus undefined collapsed refs, invalid destinations (spaces, unclosed titles, blank lines, newlines in <>), definitions in code, interrupting a paragraph, with an escaped bracket or trailing text, and nested links. They also check that real links survive: full, collapsed and shortcut refs, <a b>, balanced parens, multi-line, escaped quotes in titles, badges, and paren titles.

The state and derive guards required version bumps for every row that hashes the notes sources (changelog, notes-adapter, and the notes derives in tests/derived-versions.tsv).

tui --once always exited 0

batch()'s exit-code logic (error, stale, lookup failure) moved into exit_code(), which tui --once now uses, so a CI snapshot fails like lookup (1 or 4). The help text, reference/cli.md (mise run docs:cli), the help snapshot and guide/tui.md say so. Tests: a unit test of the ranking, and tui --once on the existing lookup-failure fixture exits 4. That test now runs multi-threaded like main. The TUI checks the tree with a blocking git call (futures_executor::block_on), and that deadlocks on a current-thread test runtime.

Checks run

cargo test -p patchula-notes -p patchula-cli and cargo clippy -p patchula-notes -p patchula-cli --all-targets -D warnings pass on the VM.

Follow-ups

  • If another App that has device flow enabled writes a marker (for example, it copies attacker text), its server can still get a token for that App, never for the Count's. Showing the App's slug in the "Run there?" prompt would let the user notice.
  • copier::changed blocks on an async process with futures_executor::block_on. It works under #[tokio::main]'s multi-thread runtime but deadlocks on a current-thread one. A spawn_blocking or a sync process call would remove the trap.

View guided diff Turn on auto-fix

…it as a link

sanitise kept [label] text untouched whenever any [ or ( followed it, so
[@alice][1] with no definition, [@org/team](see notes) or [#12][x] reached
GitHub as literal text with a live mention or backlink. A label now stays
verbatim only for a CommonMark inline destination, or a full, collapsed or
shortcut reference to a definition GitHub honours (outside code, starting
a block, one line), and never when it holds a nested link; anything else
is neutralised as prose.
…--once exits like lookup

Discovery trusted a patchula-server marker from any bot whose login the
advertised server named, but that server answers whatever it likes and
could hand out the real Count App's client ID, sending the user's token
to an attacker. The author's App, as GET /apps/{slug} describes it, must
now have the client ID the server gives the device flow, and that ID is
remembered with the choice, so a server that later names another App is
refused before sign-in.

tui --once always exited 0; it now shares lookup's exit code (1 on a
repository error, 4 on a failed lookup).
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
count-patchula 504599c Commit Preview URL

Branch Preview URL
Nov 16 2026, 12:41 AM

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