Skip to content

fix(broker): bound every vendor call, and build the metadata URL RFC 8414 names (TASK-172) - #2006

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/task172-hardening
Sep 29, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/task172-hardening

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

The three items left over from slice 3a's review (recorded non-blocking in #1995), now that the rest of the stack has landed: a deadline on every vendor call, the RFC 8414 metadata URL, and the authorization request's separator. Hardening rather than capability — no new state, no new error code.

1. Every outbound call is bounded

upstreamFetch sets AbortSignal.timeout(UPSTREAM_TIMEOUT_MS) — 15s — on all three calls: discovery, the code exchange, and the refresh. All three previously used bare fetch, so a vendor that accepted the connection and then went quiet held its caller — a browser sitting on the callback, or a seat's tool call — until the OS's own socket timeout, which is minutes.

No new error surface was needed: each site already maps a transport failure onto a named state, so a deadline arrives as issuer_unreachable, exchange_unreachable, or refresh_unreachable. The third is the interesting one — a timed-out refresh is a retryable failure, and the lease stays held, so a vendor having a bad moment is damped by the backoff rather than retried by every caller queued behind it.

timeoutMs is a parameter with the shipped constant as its default. That is a test seam: an arm cannot wait fifteen seconds, and the mechanism (a stalled fetch refuses at the deadline) is asserted once, in upstreamFetch.test.js, with a 20ms deadline.

2. Discovery builds the URL RFC 8414 §3.1 names

The well-known segment goes between the host and the issuer's path — /tenant resolves at /.well-known/oauth-authorization-server/tenant. The old shape appended it, which is identical for a bare origin and wrong for every issuer mounted under a path. The bare-origin arm that already existed cannot witness the rule, so the new arm uses a path-bearing issuer, with the appended shape asserted not to be what was sent.

Two refusals came with it: an issuer that is not a URL, and one carrying a query or fragment (§2 forbids both), are refused as issuer_unreachable rather than interpolated — interpolating builds a URL that reaches a host or path the entry never named and then reports the result as that entry's metadata. The arm has a positive control: the same stub is called for a well-formed issuer, so "the network was not reached" is a refusal rather than a stub that never ran.

3. The authorization request joins the endpoint's own query

buildAuthorizeUrl used to append ? unconditionally. It now uses & when the endpoint already carries a query, and no separator at all when it ends in ? or & (a vendor inviting parameters) — appending ? blind turns that into an empty parameter and moves client_id into a second query the vendor does not parse.

Evidence

  • baseline (4, 76) — the four suites whose arms name these claims; the wider hostedMcp set is 11 suites / 126 tests green, so no existing arm regressed
  • tsc:check exit 0; eslint 0 on the four changed sources
  • ledger: 15 mutants — 14 killed, 1 recorded EQUIVALENT, 0 survivors, 0 named-arm misses, 0 not-applied, all four sources restored

The one equivalent is worth naming because it is a measurement rather than a judgement: M04 ignores timeoutMs and uses the shipped constant. The ledger applied it and the suite passed 76/76 — the arm asserts which failure arrives, and an ignored parameter still produces it, fifteen seconds later instead of in twenty milliseconds. The parameter is a seam for the arm's own duration; nothing outside the suite reads it. Recorded rather than papered over with an elapsed-time assertion, which would be a wall-clock budget in a suite — the exact thing the previous review round removed one of.

The arms that make the deadlines load-bearing

Each of the three call sites asserts the signal it was handed (discovery, the route's exchange, and the shipped refresh path with only the socket stubbed), so a deadline that reaches discovery and stops there is a red, not a half-fix. The refresh arm additionally asserts the timed-out vendor arrives as retryable with the lease still held.

…8414 names (TASK-172)

The three items left over from slice 3a's review, all hardening:

- Every outbound hosted-MCP call now carries a deadline (`upstreamFetch`,
  `UPSTREAM_TIMEOUT_MS = 15s`). Discovery, the code exchange and the refresh all
  went through bare `fetch`, so a vendor that accepted the connection and then
  went quiet held its caller — a browser sitting on the callback, or a seat's tool
  call — until the OS gave up. All three already map a transport failure onto a
  named state, so the deadline arrives as `issuer_unreachable` /
  `exchange_unreachable` / `refresh_unreachable` with no new error surface.
- Discovery inserts the well-known segment BETWEEN the host and the issuer's path
  (RFC 8414 §3.1). The old shape appended it, which agrees for a bare origin and is
  wrong for every issuer mounted under one; an issuer that is not a URL, or that
  carries a query or fragment (§2 forbids both), is refused rather than
  interpolated into a URL that reaches a host the entry never named.
- `buildAuthorizeUrl` joins the endpoint's own query: `&` when one is already
  there, and nothing at all when it ends in `?` or `&`. Appending `?` blind turns a
  vendor's own parameter into an empty one and moves `client_id` into a query it
  does not parse.

Evidence: baseline (4, 76); tsc:check clean; eslint clean on the four sources;
ledger 15 mutants — 14 killed, 1 recorded EQUIVALENT and measured rather than
argued (the ledger applied it and the suite passed 76/76, because the arm asserts
WHICH failure arrives and an ignored timeout parameter still produces it, fifteen
seconds later instead of in twenty milliseconds), no survivors, no named-arm
misses, no not-applied.

The call-site arms are the point: each of the three fetches asserts the signal it
was handed, so a deadline that reaches discovery and stops there is a red rather
than a half-fix, and the refresh arm asserts a timed-out vendor arrives as a
retryable failure with the lease still held.
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 29, 2026
Merged via the queue into main with commit 0fe0811 Sep 29, 2026
16 checks passed
lilyshen0722 added a commit to thantiklermcirony/commonly that referenced this pull request Oct 9, 2026
…cument claims (TASK-172) (Team-Commonly#2007)

* fix(broker): compose the caller's signal, and check the issuer the document claims (TASK-172)

Two guards the vendor path was missing, both recorded by the Team-Commonly#2006 review rather
than blocking on it:

- `upstreamFetch` composed nothing: `{ ...init, signal: AbortSignal.timeout(…) }`
  reads as though `init` were honoured and silently drops the one field in it that
  cancels. A caller's signal is now composed with the deadline instead of replaced,
  and the arms cover both directions — a caller who aborts stops the call, and a
  caller whose signal never aborts still hits the deadline. No caller passes one
  today (all three take the default), so this is the difference between a wrapper
  that is bounded and one that is bounded AND cancellable.
- Discovery read endpoints out of the metadata without ever comparing the
  document's `issuer` to the issuer the URL was built from. RFC 8414 §3.3 requires
  them identical, and what the check defends is the next line of the flow:
  `authorization_endpoint` is where the person's browser is sent, so a document
  written for another issuer hands that consent to whoever wrote it. A missing
  `issuer` is `issuer_metadata_incomplete` (§2 makes it REQUIRED); a different one
  is the new `issuer_mismatch`, which the start route maps to a 502 with the other
  vendor-side states.

Evidence: baseline (3, 50); tsc:check clean; eslint clean on the three sources;
ledger 10 mutants — 9 killed, 1 recorded equivalent, no survivors, no named-arm
misses, no not-applied. The equivalent is measured rather than argued: applying it
leaves the suite 50/50, because the arm asserts WHICH failure arrives and an
ignored timeout parameter still produces it, fifteen seconds later rather than in
twenty milliseconds.

* test(hosted-mcp): give every metadata fixture the issuer §3.3 requires

The credential suite's METADATA was the only one that failed, because it is the
only one whose discovery is real rather than mocked: with no `issuer`, the new
check refuses the document and the refresh arm dies on
`authorization server metadata names no issuer` — a red that says nothing about
the deadline it is testing. The two mocked stubs (connect's AS_METADATA,
callback's discovery return) are corrected in the same pass: they stand in for a
conformant vendor today and would have been a live trap the moment either was
wired to real discovery.

* test(broker): the mid-flight fence arm waits at the winner's commit seam, not on a budget

The arm failed in 4 of 8 isolated runs BEFORE this change, always the same way:
the loser's `WINNER_WAIT_MS` budget elapsed while the winner was still writing
the row, so the loser refused `credential_refreshing` and the arm read as the
fence having broken.

The clock is the reason, and it is the same defect Team-Commonly#2004 removed elsewhere. This
suite injects `sleep`, which advances a VIRTUAL clock, while the winner's
progress — its vendor call, its `commit` — is real. A poll loop therefore spends
1500 virtual milliseconds in microseconds of real time: the budget expires faster
than the work it is waiting for, and which side wins is decided by mongod's
scheduling. A budget that expires faster than the work it waits for is not a
budget.

So the loser's first poll still wakes the winner and then WAITS for the winner's
commit at its seam, which the suite now wraps alongside the bump. The arm turns on
the store's state — a fresh row means the winner wrote — instead of on which task
the event loop ran first, and the deadline keeps its deterministic witness in
`a live lease is not takeable`, where nothing else can move the row.

Witnessed by instrument rather than by a quiet re-run:
- 10/10 isolated runs green, against 4/8 red before
- mutant S1 (the fence takes no lease at all — the defect this arm exists for)
  still turns it RED, along with two sibling arms, so the seam did not hollow it
  out
- mutant S2 (`preImageWasStale &&` dropped from the loser's freshness check)
  SURVIVED: that clause is not a tautology, so this is an UNWITNESSED BRANCH, not
  an equivalence — the corner it covers (no `credentialRef` while `expiresAt` is
  fresh) has no arm, and whether refusing `credential_refreshing` there is right
  is a behaviour question for the board, not something to pin inside a test-only
  diff

* test(broker): the incomplete-document arm names the issuer, so the endpoint guard is witnessed again

Vera's catch, and it is the good kind: my §3.3 check landed ABOVE the endpoint
guard, and this arm sent a document with no issuer. It went on refusing, with the
SAME error code as a missing endpoint, so deleting the endpoint guard outright
changed no test in the neighbourhood (she measured 102/102 green; I reproduce it).
The refusal was never in doubt — its ATTRIBUTION was. Two defects, one code, and
the arm was pinning whichever ran first.

So the document carries an issuer on purpose, and the arm pins its MESSAGE as well
as its code — the message is the only thing that tells the two apart, which is why
the no-issuer arm pins its own message too.

Witnessed by mutant: delete `if (!body.authorization_endpoint || !body.token_endpoint)`
and this arm is the one that goes red, by name, alone. 13 suites / 155 tests green
otherwise.
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