Skip to content

Bound DCR redirect_uris and persist the client store atomically - #137

Merged
aterga merged 2 commits into
mainfrom
security/dcr-limits-atomic-store
Aug 18, 2026
Merged

aterga merged 2 commits into
mainfrom
security/dcr-limits-atomic-store

Conversation

@aterga

@aterga aterga commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

What

Two hardening fixes to the unauthenticated dynamic-client-registration path (POST /oauth/register).

1. Bound a single registration's redirect_uris

MAX_CLIENTS (10 000) bounds the number of registrations, but nothing bounded the size of any one: a single unauthenticated POST could store a huge redirect_uris array of long strings, and MAX_CLIENTS such entries would bloat both memory and the persisted file (CWE-770).

  • MAX_REDIRECT_URIS = 16 — max entries per registration
  • MAX_REDIRECT_URI_LEN = 2048 — max bytes per URI

Either overflow is rejected with invalid_redirect_uri before anything is validated or stored. The store is now bounded at roughly MAX_CLIENTS × MAX_REDIRECT_URIS × MAX_REDIRECT_URI_LEN. Real clients register a handful of short redirect URLs, so the ceilings are generous.

2. Atomic write-through of the client store

persist_clients used std::fs::write, which truncates the target in place. A crash or a concurrent load_clients mid-write could observe a half-written, unparseable file — and load_clients treats a parse failure as "start with no clients", silently dropping every registration on the next load.

Now it serializes to a sibling .tmp file and renames over the target (atomic on POSIX). Only one writer runs at a time (ClientStore's writing guard), so the fixed temp name cannot be raced. A reader always sees either the old complete file or the new one.

Path-taking inner helpers (persist_clients_to / load_clients_from) are split out so the round trip is unit-testable without mutating process-global OAUTH_CLIENTS_FILE.

Tests

  • registration_bounds_redirect_uris (router-level): an over-count array and an over-long URI are both refused with invalid_redirect_uri; a normal small registration still returns 201.
  • client_store_persists_atomically (unit): persist → load round-trips, no .tmp is left behind, and the persisted target is always a complete JSON document.
  • Full suite: 176 lib + 9 router tests pass; clippy clean on the changed files.

Notes

MVP with no stability commitment (see https://mcp.internetcomputer.org/terms). Defense-in-depth on the unauthenticated open-DCR surface; not a confidentiality/integrity issue.

🤖 Generated with Claude Code

…B-379)

`POST /oauth/register` is unauthenticated (open DCR, as MCP clients
require). MAX_CLIENTS bounds the NUMBER of registrations but not the size
of any one, so a single request could carry an unbounded `redirect_uris`
array of unbounded strings — pinning memory and bloating the persisted
file, with MAX_CLIENTS such entries amplifying it (CWE-770).

Cap a single registration at MAX_REDIRECT_URIS (16) entries, each at most
MAX_REDIRECT_URI_LEN (2048) bytes, rejected with `invalid_redirect_uri`
before anything is validated or stored. This bounds the store at roughly
MAX_CLIENTS × MAX_REDIRECT_URIS × MAX_REDIRECT_URI_LEN. Real clients
register a few short redirect URLs, so the ceilings are generous.

Separately, make the write-through atomic. `std::fs::write` truncates the
target in place, so a crash or a concurrent `load_clients` mid-write could
read a half-written, unparseable file and drop EVERY registration on the
next load. Serialize to a sibling `.tmp` and `rename` over the target
(atomic on POSIX); only one writer runs at a time, so the fixed temp name
cannot be raced. Split path-taking inner helpers out of load/persist so
the round trip is unit-testable without touching process-global env.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@aterga
aterga requested review from a team and a balanced review from Copilot August 18, 2026 11:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Hardens dynamic OAuth client registration and client-store persistence.

Changes:

  • Limits redirect URI count and byte length.
  • Persists registrations through temporary-file rename.
  • Adds router and persistence tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/auth.rs Adds registration bounds and atomic persistence helpers.
tests/routers.rs Tests redirect URI limits.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/auth.rs Resolved
Comment thread tests/routers.rs Resolved
Comment thread src/auth.rs Resolved
Three fixes from the PR review:

* Order the redirect_uris bounds AHEAD of grant-type validation. Before,
  a request that was both over-count AND carried unsupported grant types
  returned `invalid_client_metadata`, contradicting the stated contract
  that redirect overflow is rejected with `invalid_redirect_uri` before
  any other validation. The bounds now run first, so the redirect error
  is deterministic. A combined-invalid-input assertion locks this in.

* The router cap test no longer performs an accepting registration. That
  registration scheduled a background write to the store file shared by
  all router tests (via isolate_client_store), which could race the
  file assertions in dynamic_client_registration_round_trips_and_persists
  and make it miss its own client_id. Every assertion is now a rejection
  (returns before the store is touched), so the test schedules no write;
  the accepting path is already covered by the round-trip test.

* The atomic-persistence unit test pre-writes an old snapshot and then
  persists a different set over it, so it exercises REPLACE-over-existing
  (the production path) rather than only rename-to-absent. It asserts the
  swap is total: only the new entry loads, no old entry lingers.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings August 18, 2026 11:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/auth.rs:2314

  • This test does not exercise atomic visibility: it reads only after persist_clients_to returns. The previous truncate-in-place implementation would also replace the old map, produce valid JSON, and leave no .tmp, so every assertion here would pass after an atomic-write regression. Add a test that observes the target while a write is paused/in progress (or concurrently reads during repeated large replacements) and asserts every observation is exactly the complete old or new snapshot.
    fn client_store_persists_atomically() {

src/auth.rs:216

  • The new limits are enforced only for fresh registrations. load_clients_from still reads and deserializes legacy snapshots without checking either redirect bound, so an oversized registration persisted before this upgrade is allocated at startup and remains in every later snapshot. That means the claimed store bound does not hold after deployment and the CWE-770 payload can survive the fix. Apply the same limits while loading (and avoid reading a file larger than the maximum bounded snapshot into memory), dropping or rejecting invalid legacy entries.
    match std::fs::read(path) {

@aterga
aterga merged commit fa74d44 into main Aug 18, 2026
8 checks passed
@aterga aterga changed the title Bound DCR redirect_uris and persist the client store atomically (ICPBB-379) Bound DCR redirect_uris and persist the client store atomically Aug 25, 2026
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.

3 participants