Repository navigation
Bound DCR redirect_uris and persist the client store atomically - #137
Merged
Merged
Conversation
…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]>
There was a problem hiding this comment.
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.
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]>
There was a problem hiding this comment.
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_toreturns. 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_fromstill 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) {
MRmarioruci
approved these changes
Aug 18, 2026
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.
What
Two hardening fixes to the unauthenticated dynamic-client-registration path (
POST /oauth/register).1. Bound a single registration's
redirect_urisMAX_CLIENTS(10 000) bounds the number of registrations, but nothing bounded the size of any one: a single unauthenticated POST could store a hugeredirect_urisarray of long strings, andMAX_CLIENTSsuch entries would bloat both memory and the persisted file (CWE-770).MAX_REDIRECT_URIS = 16— max entries per registrationMAX_REDIRECT_URI_LEN = 2048— max bytes per URIEither overflow is rejected with
invalid_redirect_uribefore anything is validated or stored. The store is now bounded at roughlyMAX_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_clientsusedstd::fs::write, which truncates the target in place. A crash or a concurrentload_clientsmid-write could observe a half-written, unparseable file — andload_clientstreats a parse failure as "start with no clients", silently dropping every registration on the next load.Now it serializes to a sibling
.tmpfile andrenames over the target (atomic on POSIX). Only one writer runs at a time (ClientStore'swritingguard), 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-globalOAUTH_CLIENTS_FILE.Tests
registration_bounds_redirect_uris(router-level): an over-count array and an over-long URI are both refused withinvalid_redirect_uri; a normal small registration still returns201.client_store_persists_atomically(unit): persist → load round-trips, no.tmpis left behind, and the persisted target is always a complete JSON document.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