fix: reject a non-string protocolVersion, and drop the test double casts

Copilot review follow-ups:

- `applyProtocolVersion` took the value straight from an unvalidated JSON
  body and went to `RegExp.test`, which coerces — `123` and `true` both
  matched the token pattern and reached `Transport.setProtocolVersion` with
  the wrong type. It now takes `unknown` and checks `typeof === "string"`
  first, with cases for the coercible values.
- Replace `as unknown as Transport` in the new session fixtures with a
  `makeTransport()` helper returning a structurally valid `Transport`, so an
  interface change stays visible to the tests.
- Drop `as unknown as typeof fetch` in the new transport test —
  `vi.fn<typeof fetch>()` is already the right type.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0119LaPQ3v4NyBK3ZTdM9j84
This commit is contained in:
cliffhall 2026-08-10 20:55:51 -04:00
commit ddfce0f7ac

View file

@ -69,11 +69,14 @@ export class RemoteSession {
*
* Ignores anything that isn't a plausible version token so a client can't
* push arbitrary bytes into an upstream header, and re-applies only on
* change (the value rides every `/api/mcp/send`).
* change (the value rides every `/api/mcp/send`). The value arrives from an
* unvalidated JSON body, so the type is checked rather than assumed —
* `RegExp.test` would otherwise coerce a `123` or `true` into a "matching"
* string and hand the wrong type to the transport.
*/
applyProtocolVersion(version: string | undefined): void {
applyProtocolVersion(version: unknown): void {
if (
version === undefined ||
typeof version !== "string" ||
version === this.appliedProtocolVersion ||
!RemoteSession.PROTOCOL_VERSION_PATTERN.test(version)
) {