diff --git a/drafts/draft-lcurley-moq-auth.md b/drafts/draft-lcurley-moq-auth.md index af2674cfdbf..c202c7cccf7 100644 --- a/drafts/draft-lcurley-moq-auth.md +++ b/drafts/draft-lcurley-moq-auth.md @@ -89,6 +89,9 @@ After an AUTH_ERROR the acceptor closes its side of the stream. The presenter withdraws a token by closing or resetting its side of the stream, and the acceptor then closes its own. An acceptor that closes its side without an AUTH_ERROR ends the grant without a reason. +A receiver MUST close the session with PROTOCOL_VIOLATION if an AUTH message fails to decode, has trailing body bytes, or has an unexpected type for its direction. +A presenter sends exactly one AUTH on its request stream; any additional message is a PROTOCOL_VIOLATION. + ## Scope {#scope} An endpoint's scope is the union of the grants of its open Auth requests. @@ -132,7 +135,8 @@ AUTH Message { ~~~ **Request ID**: -The request's identifier, allocated like that of any other request ({{moqt}}). +The request's identifier, allocated from the same space as every other request ({{moqt}}). +A duplicate or wrong-parity Request ID closes the session with INVALID_REQUEST_ID, including an ID from an AUTH that has ended. **Token**: The credential to verify, opaque to this extension. @@ -162,6 +166,7 @@ A namespace prefix the presenter may subscribe to and discover. **Expires**: The number of milliseconds until the grant lapses, or 0 for never. +Expires MUST NOT exceed 2^53 - 1 milliseconds; a larger value is a PROTOCOL_VIOLATION. The acceptor revokes a lapsed grant with AUTH_ERROR, and the presenter uses Expires to present a replacement token in time. ## AUTH_ERROR {#auth-error} @@ -179,6 +184,7 @@ AUTH_ERROR Message { ~~~ **Error Code**: +A code MUST fit in 32 bits; a larger value is a PROTOCOL_VIOLATION. A code from the REQUEST_ERROR registry ({{moqt}}): UNAUTHORIZED, MALFORMED_AUTH_TOKEN, EXPIRED_AUTH_TOKEN, or NOT_SUPPORTED ({{prefixes}}). **Reason Phrase**: diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 2069448e4c9..9caf4125ea8 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1457,6 +1457,9 @@ A pattern that breaks any of these rules is a PROTOCOL_VIOLATION. A grant covers a request when one of its patterns matches the path. +An endpoint MUST close the session with PROTOCOL_VIOLATION when an AUTH stream message fails to decode, has trailing body bytes, or has an unexpected type for its direction. +The opener sends exactly one AUTH; any additional message on its side is a PROTOCOL_VIOLATION. + ## AUTH_ERROR {#auth-error} AUTH_ERROR refuses a token, or revokes it after an AUTH_OK. @@ -1566,6 +1569,7 @@ The `Message Length` describes the payload size on the wire. - Added the AUTH_TOKEN_CACHE_OVERFLOW (0x13) and UNKNOWN_AUTH_TOKEN_ALIAS (0x17) session codes, with moq-transport's meaning. - Limited AUTH_OK Expires to 2^53 - 1 milliseconds, with an out-of-range value closing the session with PROTOCOL_VIOLATION. - Invalid UTF-8 or an oversized AUTH_ERROR reason closes the session with PROTOCOL_VIOLATION. +- An Auth Stream message that fails to decode, has trailing body bytes, or has an unexpected type, or a second AUTH from the opener, closes the session with PROTOCOL_VIOLATION. - Added NOT_SUPPORTED (0x30) to session errors; unsupported Auth Stream tokens and grants use AUTH_ERROR before or after a grant. - Added the Auth Stream (0x7) with AUTH, AUTH_OK, and AUTH_ERROR: either endpoint presents a token on its own stream and learns the [path patterns](#path-pattern) it may publish and subscribe to. The union of a session's open grants is its scope. A shrink withdraws what it no longer covers, and an announcement outside the scope closes the session with UNAUTHORIZED. A peer without the stream resets it. diff --git a/js/net/bench/request-ids.ts b/js/net/bench/request-ids.ts index 27291fe4220..b59e7c18c35 100644 --- a/js/net/bench/request-ids.ts +++ b/js/net/bench/request-ids.ts @@ -1,5 +1,5 @@ // Request-ID admission cost across independent sessions and reordered windows. -import { ControlStreamAdapter } from "../src/ietf/adapter.ts"; +import { ControlStreamAdapter, SpentIds } from "../src/ietf/adapter.ts"; import { Version } from "../src/ietf/version.ts"; import { Stream } from "../src/stream.ts"; @@ -78,3 +78,30 @@ try { } finally { console.debug = debug; } + +// Draft 17+ has no MAX_REQUEST_ID window, yet still remembers skipped runs to refuse reuse. +console.log("modern_mode,sessions,requests,admit_us,accepted"); +for (const mode of ["ordered", "reordered", "scattered"]) { + for (const sessions of [1, 8, 32]) { + for (const requests of [8, 128, 220_000]) { + const ids = order(mode, requests); + const rounds = Math.max(1, Math.floor(8192 / requests)); + let elapsed = 0; + let accepted = 0; + for (let round = 0; round < rounds; round++) { + const receivers = Array.from({ length: sessions }, () => new SpentIds(0n)); + const start = performance.now(); + for (const receiver of receivers) { + for (const id of ids) { + receiver.spend(id); + accepted++; + } + } + elapsed += performance.now() - start; + } + console.log( + `modern-${mode},${sessions},${requests},${((elapsed * 1000) / rounds).toFixed(2)},${accepted / rounds}`, + ); + } + } +} diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 97e2818bb5a..4eae496d328 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -3,13 +3,18 @@ import type { Getter } from "@moq/signals"; import { type Grant, type Issued, Unsupported } from "./auth.ts"; import { accept as acceptSession, connect as connectSession, type Established } from "./connection/index.ts"; import { SessionCode, SessionError, StreamCode, toStreamCode } from "./error.ts"; +import { AuthMessage as IetfAuthMessage, IetfAuthWire } from "./ietf/auth.ts"; import * as Ietf from "./ietf/index.ts"; -import { LiteAuthWire } from "./lite/auth.ts"; +import * as IetfMessage from "./ietf/message.ts"; +import { TrackStatusRequest } from "./ietf/track.ts"; +import { Version as IetfVersion } from "./ietf/version.ts"; +import { AuthMessage as LiteAuthMessage, LiteAuthWire } from "./lite/auth.ts"; import * as Lite from "./lite/index.ts"; +import * as LiteMessage from "./lite/message.ts"; import { createMockTransportPair, type MockTransport } from "./mock.ts"; import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; -import { Writer } from "./stream.ts"; +import { type Stream, Writer } from "./stream.ts"; import { withTimeout } from "./util/timeout.ts"; import { wireOf } from "./wire.ts"; @@ -443,3 +448,205 @@ test("a revoked grant resets its subscriptions with UNAUTHORIZED", async () => { server.abort(); } }); + +// Capture the acceptor's actual AUTH writer after a valid setup grant. The following +// bytes travel through the same connection dispatcher and presenter as a real peer. +describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("AUTH violations %s", (protocol) => { + test("a second presenter message closes the session", async () => { + const streams: Stream[] = []; + const prototype = protocol === Lite.ALPN_07_WIP ? LiteAuthWire.prototype : IetfAuthWire.prototype; + const original = prototype.present; + const spy = spyOn(prototype, "present").mockImplementation(async function ( + this: LiteAuthWire & IetfAuthWire, + token, + ) { + const stream = await original.call(this, token); + streams.push(stream); + return stream; + }); + let pair: Pair | undefined; + try { + pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + await streams[0]?.writer.u53(0); + expect((await withTimeout(pair.transport.closed, 100, "second AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair?.client.abort(); + pair?.server.abort(); + } + }); + + test("a malformed presenter AUTH closes the session", async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const lite = protocol === Lite.ALPN_07_WIP; + const prototype = lite ? LiteAuthMessage.prototype : IetfAuthMessage.prototype; + const spy = spyOn(prototype, "encode").mockImplementation(async (writer: Writer) => { + const body = async (w: Writer) => { + if (!lite) await w.u62(2n); + await w.u53(2); + }; + if (lite) await LiteMessage.encode(writer, body); + else await IetfMessage.encode(writer, body, IetfAuthMessage.id); + }); + const added = pair.client.auth.add("bad").catch(() => undefined); + try { + expect((await withTimeout(pair.transport.closed, 100, "malformed AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + await added; + } + }); + + for (const fault of [ + "unknown reply", + "oversized code", + "truncated error", + "extra body", + "oversized expiry", + ] as const) { + test(`${fault} closes the session`, async () => { + const streams: Stream[] = []; + const lite = protocol === Lite.ALPN_07_WIP; + const prototype = lite ? LiteAuthWire.prototype : IetfAuthWire.prototype; + const original = prototype.grant; + const spy = spyOn(prototype, "grant").mockImplementation(async function ( + this: LiteAuthWire & IetfAuthWire, + stream, + value, + ) { + streams.push(stream); + await original.call(this, stream, value); + }); + let pair: Pair | undefined; + try { + pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const stream = streams[0]; + if (!stream) throw new Error("setup AUTH writer missing"); + if (fault === "unknown reply") { + await stream.writer.u53(99); + } else { + const body = async (writer: Writer) => { + if (fault === "oversized expiry") { + await writer.u53(0); + await writer.u53(0); + await writer.u62(1n << 53n); + } else { + await writer.u62(fault === "oversized code" ? 1n << 32n : 1n); + if (fault !== "truncated error") await writer.string(""); + if (fault === "extra body") await writer.u53(0); + } + }; + const id = fault === "oversized expiry" ? (lite ? 0 : 0x40b62) : lite ? 1 : 0x40b63; + if (lite) await LiteMessage.encode(stream.writer, body, { id }); + else await IetfMessage.encode(stream.writer, body, id); + } + expect((await withTimeout(pair.transport.closed, 100, "malformed AUTH did not close")).closeCode).toBe( + SessionCode.ProtocolViolation, + ); + } finally { + spy.mockRestore(); + pair?.client.abort(); + pair?.server.abort(); + } + }); + } +}); + +// AUTH shares its ID space with every other request from the same peer. +describe.each([Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("AUTH request IDs %s", (protocol) => { + for (const client of [true, false]) { + for (const fault of ["reused", "wrong parity"] as const) { + test(`${client ? "client" : "server"} ${fault} closes the session`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const original = IetfAuthMessage.prototype.encode; + const spy = spyOn(IetfAuthMessage.prototype, "encode").mockImplementation(async function ( + this: IetfAuthMessage, + writer, + version, + ) { + this.requestId = fault === "reused" ? (client ? 0n : 1n) : client ? 1n : 0n; + await original.call(this, writer, version); + }); + const added = (client ? pair.client : pair.server).auth.add("bad ID").catch(() => undefined); + try { + expect( + (await withTimeout(pair.transport.closed, 100, "invalid AUTH ID left session open")).closeCode, + ).toBe(SessionCode.InvalidRequestId); + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + await added; + } + }); + } + } +}); + +for (const [protocol, version] of [ + [Ietf.ALPN.DRAFT_17, IetfVersion.DRAFT_17], + [Ietf.ALPN.DRAFT_22, IetfVersion.DRAFT_22], +] as const) { + test(`TRACK_STATUS cannot reuse setup AUTH ID on ${protocol}`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + try { + const raw = await pair.transport.createBidirectionalStream(); + const writer = new Writer(raw.writable, version); + await writer.u53(TrackStatusRequest.id); + await new TrackStatusRequest({ + requestId: 0n, + trackNamespace: Path.from("test"), + trackName: "video", + }).encode(writer, version); + expect( + (await withTimeout(pair.transport.closed, 100, "cross-request reuse left session open")).closeCode, + ).toBe(SessionCode.InvalidRequestId); + } finally { + pair.client.abort(); + pair.server.abort(); + } + }); +} + +for (const protocol of [Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22]) { + test(`AUTH accepts reordered IDs on ${protocol}`, async () => { + const pair = await connect({ protocol }); + await waitFor(pair.client.auth.grant, (g) => g !== undefined); + await waitFor(pair.server.auth.grant, (g) => g !== undefined); + const original = IetfAuthMessage.prototype.encode; + const spy = spyOn(IetfAuthMessage.prototype, "encode").mockImplementation(async function ( + this: IetfAuthMessage, + writer, + version, + ) { + // Swap the next two independently opened request IDs, preserving endpoint parity. + this.requestId = this.requestId < 4n ? this.requestId + 2n : this.requestId - 2n; + await original.call(this, writer, version); + }); + try { + for (const endpoint of [pair.client, pair.server]) { + await expect(endpoint.auth.add("first")).rejects.toBeInstanceOf(Unsupported); + await expect(endpoint.auth.add("second")).rejects.toBeInstanceOf(Unsupported); + } + } finally { + spy.mockRestore(); + pair.client.abort(); + pair.server.abort(); + } + }); +} diff --git a/js/net/src/auth_session.ts b/js/net/src/auth_session.ts index ac9c3824c65..c790fbb90ef 100644 --- a/js/net/src/auth_session.ts +++ b/js/net/src/auth_session.ts @@ -1,4 +1,4 @@ -import { type Dispose, type Getter, Signal } from "@moq/signals"; +import { type Dispose, type Getter, Once, Signal } from "@moq/signals"; import { type Auth as AuthApi, type Grant, @@ -9,7 +9,7 @@ import { type Token, Unsupported, } from "./auth.ts"; -import { closeReason, error, SessionCode, StreamCode, StreamError } from "./error.ts"; +import { closeReason, error, ProtocolViolation, SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import * as Path from "./path.ts"; import type { Stream } from "./stream.ts"; @@ -154,10 +154,15 @@ class IssuedGrant implements Issued { this.#stream = stream; this.#wire = wire; // The presenter withdraws by closing or cancelling its side. - this.closed = stream.reader.closed.then( - () => null, - (err: unknown) => (err instanceof StreamError && err.code === StreamCode.Cancel ? null : error(err)), - ); + this.closed = stream.reader + .done() + .then((done) => { + if (!done) throw new ProtocolViolation("additional message on an AUTH stream"); + }) + .then( + () => null, + (err: unknown) => (err instanceof StreamError && err.code === StreamCode.Cancel ? null : error(err)), + ); } #write(write: () => Promise) { @@ -237,6 +242,8 @@ export interface AuthSessionProps { * @internal */ export class AuthSession implements AuthApi { + /** The peer sent a malformed AUTH message; the owning connection closes the session. */ + readonly violated = new Once(); #wire?: AuthWire; #peerGrant: Grant; @@ -297,7 +304,19 @@ export class AuthSession implements AuthApi { const wire = this.#wire; if (!wire) throw new Error("auth not negotiated"); - const token = await wire.accept(stream); + let token: Uint8Array; + try { + token = await wire.accept(stream); + } catch (err) { + if ( + err instanceof StreamError || + err instanceof SessionError || + err instanceof Unsupported || + (typeof err === "object" && err !== null && "source" in err && err.source === "session") + ) + throw err; + throw new ProtocolViolation("malformed AUTH", { cause: err }); + } await this.#decided; if (this.#acceptor === "undecided") this.#acceptor = "default"; @@ -314,8 +333,9 @@ export class AuthSession implements AuthApi { issued.update(this.#peerGrant); } - await issued.closed; + const ended = await issued.closed; issued.close(); + if (ended instanceof ProtocolViolation) throw ended; } /** End the session: fail every pending token and close the requests. */ @@ -344,7 +364,19 @@ export class AuthSession implements AuthApi { if (token.withdrawn) throw cancel(); for (;;) { - const reply = await wire.read(stream); + let reply: WireReply | undefined; + try { + reply = await wire.read(stream); + } catch (err) { + if ( + err instanceof StreamError || + err instanceof SessionError || + err instanceof Unsupported || + (typeof err === "object" && err !== null && "source" in err && err.source === "session") + ) + throw err; + throw new ProtocolViolation("malformed AUTH reply", { cause: err }); + } if (!reply) { // The peer ended the grant without revoking it, or closed without ever // answering. Close our side too, which is what settles its Issued.closed. @@ -376,6 +408,9 @@ export class AuthSession implements AuthApi { result = new Unsupported(); } else { result = error(err); + if (result instanceof ProtocolViolation && !this.#closed && this.violated.peek() === undefined) { + this.violated.set(result); + } if (!this.#closed) console.warn("auth token ended", result); } } diff --git a/js/net/src/goaway-requests.test.ts b/js/net/src/goaway-requests.test.ts index 29b56adf5a4..d217691d213 100644 --- a/js/net/src/goaway-requests.test.ts +++ b/js/net/src/goaway-requests.test.ts @@ -258,6 +258,9 @@ test("announcing and subscribing after GOAWAY still open streams, on the adapter nextRequestId() { return Promise.resolve(0n); }, + spendRequest() { + throw new Error("unexpected incoming request"); + }, close() {}, }; diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 5259dd4113a..8298f1fb0e4 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -1,7 +1,7 @@ import { Once } from "@moq/signals"; import { Mutex } from "async-mutex"; import type { Drain } from "../connection/goaway.ts"; -import { error, ProtocolViolation, SessionCode } from "../error.ts"; +import { error, ProtocolViolation, SessionCode, SessionError } from "../error.ts"; import { Reader, Stream, type Writer } from "../stream.ts"; import { untilAborted } from "../util/abort.ts"; import * as Varint from "../varint.ts"; @@ -30,6 +30,8 @@ export interface Session { openNativeBi?(): Promise; acceptBi(): Promise; nextRequestId(signal?: AbortSignal): Promise; + /** Spend a request ID the peer used without holding a request open, closing the session if it cannot. */ + spendRequest(id: bigint): void; close(): void; readonly version: IetfVersion; } @@ -43,12 +45,24 @@ export class NativeSession implements Session { // moq-transport reserves even request IDs for the client and odd for the server, // so the two peers' ID spaces never overlap. #requestId: bigint; + // The peer's IDs, of the other parity. Draft 17 has no window, yet an ID stays spent. + #spent: SpentIds; readonly version: IetfVersion; constructor(quic: WebTransport, version: IetfVersion, client: boolean) { this.#quic = quic; this.version = version; this.#requestId = client ? 0n : 1n; + this.#spent = new SpentIds(client ? 1n : 0n); + } + + spendRequest(id: bigint): void { + try { + this.#spent.spend(id); + } catch (err) { + if (err instanceof SessionError) this.#quic.close({ closeCode: err.code, reason: err.message }); + throw err; + } } async openBi(): Promise { @@ -104,12 +118,26 @@ const RUN_CHUNK = 256; */ class MissingRuns { #chunks: [bigint, bigint][][] = []; + #size = 0; + + /** How many runs are remembered. */ + get size(): number { + return this.#size; + } /** Whether `id` was skipped and not yet received. */ has(id: bigint): boolean { return this.#find(id) !== undefined; } + /** Whether taking `id` leaves one more run, landing strictly inside its run. */ + splits(id: bigint): boolean { + const found = this.#find(id); + if (!found) return false; + const [start, end] = this.#chunks[found[0]][found[1]]; + return start < id && id + 2n < end; + } + /** Mark a skipped `id` received, splitting its run. */ take(id: bigint): void { const found = this.#find(id); @@ -120,6 +148,7 @@ class MissingRuns { const rest: [bigint, bigint][] = []; if (start < id) rest.push([start, id]); if (id + 2n < end) rest.push([id + 2n, end]); + this.#size += rest.length - 1; chunk.splice(run, 1, ...rest); if (chunk.length === 0) this.#chunks.splice(index, 1); else if (chunk.length > 2 * RUN_CHUNK) this.#chunks.splice(index + 1, 0, chunk.splice(RUN_CHUNK)); @@ -127,6 +156,7 @@ class MissingRuns { /** Record `[start, end)` as skipped, above every existing run. */ push(start: bigint, end: bigint): void { + this.#size++; const last = this.#chunks.at(-1); if (last && last.length < 2 * RUN_CHUNK) last.push([start, end]); else this.#chunks.push([[start, end]]); @@ -142,6 +172,39 @@ class MissingRuns { } } +/** + * The peer's spent request IDs on draft 17 and later: every ID of its parity below `#next`, + * except the skipped runs. Mirrors Rust's `Spent`. + */ +export class SpentIds { + #next: bigint; + #missing = new MissingRuns(); + // No window bounds the runs here, and forgetting one would make its IDs usable again. + #limit: number; + + constructor(first: bigint, limit = Number(REQUEST_WINDOW)) { + this.#next = first; + this.#limit = limit; + } + + /** Spend `id`, throwing the session error a reused, wrong-parity, or unrecordable ID closes with. */ + spend(id: bigint): void { + // No peer reaches 2^62 requests; refusing past it matches Rust, whose `next` would overflow. + const unused = id < 1n << 62n && (id >= this.#next || this.#missing.has(id)); + if (id % 2n !== this.#next % 2n || !unused) throw new SessionError(SessionCode.InvalidRequestId); + const splits = id < this.#next ? this.#missing.splits(id) : id > this.#next; + if (splits && this.#missing.size >= this.#limit) { + throw new SessionError(SessionCode.Internal, { reason: "request ID history exhausted" }); + } + if (id < this.#next) { + this.#missing.take(id); + return; + } + if (id > this.#next) this.#missing.push(this.#next, id); + this.#next = id + 2n; + } +} + /** The last index whose key is at or below `id`, or -1, in an array sorted by key. */ function lastAtOrBelow(items: T[], id: bigint, key: (item: T) => bigint): number { let lo = 0; @@ -418,7 +481,7 @@ export class ControlStreamAdapter implements Session { } if (classified.route === Route.Update) { - this.#admitUpdate(classified.ownRequestId); + this.spendRequest(classified.ownRequestId); this.#pushMessage(classified.requestId, typeId, size, body); continue; } @@ -590,7 +653,7 @@ export class ControlStreamAdapter implements Session { * An update spends its own request ID like any request but keeps no stream, so the * slot is retired at once. The ID is spent even when the target is already gone. */ - #admitUpdate(requestId: bigint): void { + spendRequest(requestId: bigint): void { this.#admitHeld(requestId); this.#open.delete(requestId); this.#retire(); diff --git a/js/net/src/ietf/auth.ts b/js/net/src/ietf/auth.ts index cad3f9c85c4..905df84ddd3 100644 --- a/js/net/src/ietf/auth.ts +++ b/js/net/src/ietf/auth.ts @@ -3,6 +3,7 @@ import type { AuthWire, WireGrant, WireReply } from "../auth_session.ts"; import { SessionCode, SessionError } from "../error.ts"; import * as Path from "../path.ts"; import type { Reader, Stream, Writer } from "../stream.ts"; +import type { Session } from "./adapter.ts"; import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; import { SetupOption, type SetupOptions } from "./parameters.ts"; @@ -195,6 +196,7 @@ export class AuthError { guard(version); return Message.decode(r, async (rd) => { const code = await rd.u53(); + if (code > 0xffffffff) throw new Error("AUTH_ERROR code exceeds 32 bits"); const reason = await rd.string(); if (new TextEncoder().encode(reason).byteLength > MAX_REASON) { throw new Error("AUTH_ERROR reason exceeds 8,192 bytes"); @@ -212,24 +214,18 @@ export class AuthError { /** The moq-transport binding of the token lifecycle. @internal */ export class IetfAuthWire implements AuthWire { - #openBi: () => Promise; - #nextRequestId: () => Promise; + #session: Session; #version: IetfVersion; - constructor(props: { - openBi: () => Promise; - nextRequestId: () => Promise; - version: IetfVersion; - }) { - this.#openBi = props.openBi; - this.#nextRequestId = props.nextRequestId; - this.#version = props.version; + constructor(session: Session) { + this.#session = session; + this.#version = session.version; } async present(token: Uint8Array): Promise { - const requestId = await this.#nextRequestId(); + const requestId = await this.#session.nextRequestId(); if (requestId === undefined) throw new Error("no request id available"); - const stream = await this.#openBi(); + const stream = await this.#session.openBi(); await new AuthMessage(requestId, token).encode(stream.writer, this.#version); return stream; } @@ -254,6 +250,7 @@ export class IetfAuthWire implements AuthWire { /** The type id is already consumed by the dispatcher. */ async accept(stream: Stream): Promise { const msg = await AuthMessage.decode(stream.reader, this.#version); + this.#session.spendRequest(msg.requestId); return msg.token; } diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index 4bdbbfc9b50..7d5b2098921 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -152,6 +152,11 @@ test("SUBSCRIBE_TRACKS refuses only its request", async () => { for (let i = 0; i < 2; i++) { const stream = await Stream.open(pair.client, { version }); await stream.writer.u62(BigInt(SUBSCRIBE_TRACKS_ID)); + await Message.encode(stream.writer, async (writer) => { + await writer.u62(BigInt(i * 2)); + await Namespace.encode(writer, Path.from("")); + await writer.u53(0); + }); expect(await stream.reader.u53()).toBe(RequestError.id); const refused = await RequestError.decode(stream.reader, version); expect(refused.errorCode).toBe(0x3); @@ -529,14 +534,14 @@ function subscribeWithToken(version: IetfVersion, token: readonly number[]): Uin /** An update to that SUBSCRIBE whose only parameters are AUTHORIZATION TOKENs holding `tokens`. */ function updateWithTokens(version: IetfVersion, ...tokens: (readonly number[])[]): Uint8Array { // Draft-14 names its own Request ID and the subscription's, then the range, priority, - // and forward inline. Drafts 15 and 16 name both IDs, and later drafts only the request - // it updates. + // and forward inline. Drafts 15 and 16 name both IDs, and later drafts only its own, + // which must be fresh: the subscription already spent 0. const head = version === Version.DRAFT_14 ? [2, 0, 0, 0, 0, 0x80, 0x01] : version === Version.DRAFT_15 || version === Version.DRAFT_16 ? [2, 0] - : [0]; + : [2]; const body = [...head, ...tokenParams(version, ...tokens)]; return new Uint8Array([0x02, 0, body.length, ...body]); } diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 74a6f83eaee..e7f1f81599a 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -18,6 +18,7 @@ import * as Cluster from "./cluster.ts"; import { type RequestKind, toRequestCode } from "./error.ts"; import { Fetch, FetchError, FetchHeader } from "./fetch.ts"; import { GoAway } from "./goaway.ts"; +import * as Message from "./message.ts"; import { Group } from "./object.ts"; import { Publish, PublishError } from "./publish.ts"; import { PublishNamespace, PublishNamespaceError } from "./publish_namespace.ts"; @@ -193,14 +194,7 @@ export class Connection implements Established { // since we consume on demand, and subscribing to whatever we publish. const session = this.#session; this.#auth = new AuthSession({ - wire: - auth && authSupported(version) - ? new IetfAuthWire({ - openBi: async () => session.openBi(), - nextRequestId: () => session.nextRequestId(), - version, - }) - : undefined, + wire: auth && authSupported(version) ? new IetfAuthWire(session) : undefined, peerGrant: { publish: new Path.Patterns([Path.Pattern.all()]), subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), @@ -208,6 +202,10 @@ export class Connection implements Established { }); this.#client = client; + this.#auth.violated.subscribe((err) => { + if (err) this.#violated(err); + }); + this.#publisher = new Publisher({ quic: this.#quic, session: this.#session, @@ -366,6 +364,7 @@ export class Connection implements Established { } catch (err) { if (!(err instanceof MalformedAuthToken) || err.requestId === undefined) throw err; adapter?.admitRequest(err.requestId); + this.#spend(err.requestId); try { await this.#refuseToken(stream, "subscribe_namespace", err.requestId); } finally { @@ -374,6 +373,7 @@ export class Connection implements Established { break; } if (adapter) adapter.admitRequest(legacy.requestId); + this.#spend(legacy.requestId); try { if (legacy.subscribeOptions === SubscribeOptions.PUBLISH) { await this.#publisher.refuseSubscribeNamespace(legacy.requestId, stream); @@ -407,6 +407,12 @@ export class Connection implements Established { if (this.#session.version < Version.DRAFT_18) { throw new ProtocolViolation("SUBSCRIBE_TRACKS before draft-18"); } + const requestId = await Message.decode(stream.reader, async (reader) => { + const id = await reader.u62(); + await reader.readAll(); + return id; + }); + this.#session.spendRequest(requestId); await this.#publisher.runSubscribeTracks(stream); break; } @@ -480,14 +486,27 @@ export class Connection implements Established { * * @returns undefined once refused. */ - async #request(stream: Stream, kind: RequestKind, decoded: Promise): Promise { + async #request( + stream: Stream, + kind: RequestKind, + decoded: Promise, + ): Promise { + let msg: T; try { - return await decoded; + msg = await decoded; } catch (err) { if (!(err instanceof MalformedAuthToken) || err.requestId === undefined) throw err; + this.#spend(err.requestId); await this.#refuseToken(stream, kind, err.requestId); return undefined; } + this.#spend(msg.requestId); + return msg; + } + + /** Spend a request's ID on draft 17+; the control stream adapter already spent it on earlier drafts. */ + #spend(requestId: bigint) { + if (this.#session instanceof NativeSession) this.#session.spendRequest(requestId); } /** Refuse a request MALFORMED_AUTH_TOKEN, ending its stream. Mirrors Rust's `token::reject`. */ diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 9b299057c20..dc5ce445e14 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -602,6 +602,7 @@ test("a failed stream open does not kill the announce loop", async () => { version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + spendRequest: (id) => inner.spendRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -647,6 +648,7 @@ test("a namespace refused once is retried without anything else changing", async version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + spendRequest: (id) => inner.spendRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -683,6 +685,7 @@ test("a solicited legacy advertisement refused once is retried", async () => { version: inner.version, acceptBi: () => inner.acceptBi(), nextRequestId: () => inner.nextRequestId(), + spendRequest: (id) => inner.spendRequest(id), close: () => inner.close(), openBi: () => { if (failures-- > 0) throw new Error("no stream credit"); @@ -1667,7 +1670,7 @@ function fixture(version: IetfVersion = V20): { close: () => void; } { const pair = createMockTransportPair(ALPNS[version]); - const session = new NativeSession(pair.server, version, true); + const session = new NativeSession(pair.server, version, false); const { pub, origin } = publisher(pair.server, { session }); const broadcast = publish(origin, Path.from("test")); const uni = pair.client.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader< diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 07563572b86..54db985ca12 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -530,7 +530,7 @@ export class Publisher { let params: Parameters; try { params = await Message.decode(stream.reader, async (r) => { - await r.u62(); + this.#session.spendRequest(await r.u62()); if (version === Version.DRAFT_17) await r.u62(); return Parameters.decode(r, version, "request-update"); }); diff --git a/js/net/src/ietf/request_ids.test.ts b/js/net/src/ietf/request_ids.test.ts new file mode 100644 index 00000000000..4c9f59554ca --- /dev/null +++ b/js/net/src/ietf/request_ids.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, test } from "bun:test"; +import { SessionCode, SessionError } from "../error.ts"; +import { SpentIds } from "./adapter.ts"; + +function code(ids: SpentIds, id: bigint): SessionCode | undefined { + try { + ids.spend(id); + } catch (err) { + if (err instanceof SessionError) return err.code; + throw err; + } + return undefined; +} + +describe("draft-17+ request ID history", () => { + test.each([0n, 1n])("allows reordering but permanently spends IDs, first=%s", (first) => { + const ids = new SpentIds(first); + for (const offset of [4n, 0n, 2n, 8n, 6n]) ids.spend(first + offset); + for (const offset of [0n, 2n, 4n, 6n, 8n]) expect(code(ids, first + offset)).toBe(SessionCode.InvalidRequestId); + expect(code(ids, first ^ 1n)).toBe(SessionCode.InvalidRequestId); + }); + + test("contiguous traffic in either order leaves no gap", () => { + const forward = new SpentIds(0n, 0); + const reverse = new SpentIds(0n, 1); + for (let i = 0n; i < 4096n; i++) { + forward.spend(i * 2n); + reverse.spend((4095n - i) * 2n); + } + expect(code(forward, 0n)).toBe(SessionCode.InvalidRequestId); + expect(code(reverse, 8190n)).toBe(SessionCode.InvalidRequestId); + }); + + test("a full history refuses a new gap without forgetting IDs", () => { + const ids = new SpentIds(0n, 1); + ids.spend(0n); + ids.spend(4n); + expect(code(ids, 8n)).toBe(SessionCode.Internal); + ids.spend(2n); + ids.spend(8n); + for (const id of [0n, 2n, 4n, 8n]) expect(code(ids, id)).toBe(SessionCode.InvalidRequestId); + }); + + test("a huge sparse ID does not allocate its intervening gap", () => { + const ids = new SpentIds(0n, 1); + ids.spend((1n << 62n) - 2n); + ids.spend(0n); + for (const id of [(1n << 62n) - 2n, 1n << 62n, (1n << 64n) - 2n]) { + expect(code(ids, id)).toBe(SessionCode.InvalidRequestId); + } + }); +}); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 9e6b94852d4..88a585a3b73 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1067,6 +1067,8 @@ export class Subscriber { update = await PublishNamespaceUpdate.decode(stream.reader, version); } catch (err) { if (!(err instanceof MalformedAuthToken)) throw err; + // A refused update still spends its own request ID. + if (err.requestId !== undefined) this.#session.spendRequest(err.requestId); // Section 8.9 rejects the update, which withdraws the advertisement as any // refused update does. Only draft-17+ updates arrive here, so no Request ID. await stream.writer.u53(RequestError.id); @@ -1077,6 +1079,7 @@ export class Subscriber { stream.close(); break; } + this.#session.spendRequest(update.requestId); // The parameters exist only on a session that negotiated the extension; // anywhere else they are the peer's violation. diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index a8f0a7987a7..e6b05fe413b 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -1224,7 +1224,8 @@ test("integration: ietf fetch group is unsupported", async () => { // subscription already on the session keeps going. test("integration: ietf SUBSCRIBE_TRACKS is refused per request", async () => { // SUBSCRIBE_TRACKS, Length, Request ID, Track Namespace Prefix ("room"), no parameters. - const subscribeTracks = new Uint8Array([0x51, 0x00, 0x08, 0x01, 0x01, 0x04, 0x72, 0x6f, 0x6f, 0x6d, 0x00]); + // A fresh even client ID, distinct from the connection's setup/discovery/subscription requests. + const subscribeTracks = new Uint8Array([0x51, 0x00, 0x08, 0x3e, 0x01, 0x04, 0x72, 0x6f, 0x6f, 0x6d, 0x00]); for (const protocol of [ Ietf.ALPN.DRAFT_18, diff --git a/js/net/src/lite/auth.ts b/js/net/src/lite/auth.ts index 1107c3483ea..f87ef2ac52a 100644 --- a/js/net/src/lite/auth.ts +++ b/js/net/src/lite/auth.ts @@ -134,6 +134,7 @@ export class AuthError { static async #decode(r: Reader): Promise { const code = await r.u53(); + if (code > 0xffffffff) throw new ProtocolViolation("AUTH_ERROR code exceeds 32 bits"); const size = await r.u62(); if (size > BigInt(MAX_REASON)) { throw new ProtocolViolation("AUTH_ERROR reason exceeds 8,192 bytes"); diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index 78891d1bf38..353b18a65cf 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -159,6 +159,12 @@ export class Connection implements Established { subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), }, }); + this.#auth.violated.subscribe((err) => { + if (!err) return; + this.#quic.close({ closeCode: err.code, reason: err.message }); + this.abort(); + }); + this.#publisher = new Publisher(this.#quic, this.#version, this.hop, publish, { grant: this.#auth.grant, ready: this.#auth.setupAnswered(), @@ -299,7 +305,10 @@ export class Connection implements Established { // A protocol violation on one stream is the peer breaking the session. // Resetting that stream leaves it free to repeat the violation; a duplicate // GOAWAY is the one this dispatcher raises. - if (err instanceof ProtocolViolation) this.abort(); + if (err instanceof ProtocolViolation) { + this.#quic.close({ closeCode: err.code, reason: err.message }); + this.abort(); + } }) .finally(() => { stream.writer.close(); diff --git a/quest/m1/auth/README.md b/quest/m1/auth/README.md index e5f120c0d75..eb44f36f93d 100644 --- a/quest/m1/auth/README.md +++ b/quest/m1/auth/README.md @@ -99,7 +99,6 @@ because `main` assigned 0x3A to NOT_FETCHABLE first. ## Required - [WebSocket refusal](/quest/m1/auth/ws-unauthorized.md) - the relay serves WebSocket through moq-tokio, so a refused token closes the session as Unauthorized, as QUIC does -- [AUTH violations](/quest/m1/auth/violations.md) - every AUTH protocol violation closes the session in Rust and JS, lite and IETF - [Relay tokens](/quest/m1/auth/relay-refresh.md) - the relay verifies tokens sent in band, unions their grants, and cancels only work that loses access - [Request-token decode](/quest/m1/auth/request-token-decode.md) - an diff --git a/quest/m1/auth/violations.md b/quest/m1/auth/violations.md deleted file mode 100644 index 999eae214da..00000000000 --- a/quest/m1/auth/violations.md +++ /dev/null @@ -1,42 +0,0 @@ -# [M] AUTH protocol violations close the session everywhere - -## Goal - -Every AUTH stream message that the lite or IETF drafts call a -PROTOCOL_VIOLATION closes the session in Rust and `@moq/net` alike, instead -of ending only its token. Examples: an AUTH_ERROR code past `u32`, an -out-of-range IETF AUTH_OK expiry, or an AUTH or AUTH_ERROR that fails to -decode. The lite AUTH_OK pattern and `Expires` decode gap, plus invalid UTF-8 -and oversized AUTH_ERROR reasons, belong to -[Malformed grant](https://github.com/moq-dev/moq/pull/5256). - -## Plan - -AUTH endings (#4550) closes the session only -on an explicit `ProtocolViolation` in Rust lite. These gaps remain: - -- JS reports the exact oversized code but doesn't close the session. Let the - JS connection close on an `AuthSession` protocol violation. -- An out-of-range IETF AUTH_OK expiry still ends only its token. -- Other AUTH_ERROR framing or decode failures end only the token; invalid - UTF-8 and oversized reasons already close the session. -- On the acceptor side, a presenter's AUTH that fails to decode only aborts - its stream (Rust lite `AuthServe`, JS `#runBidis`), so a peer can repeat - malformed AUTH streams without closing the session. -- Both IETF AUTH acceptors (Rust `Serve::run`, JS `IetfAuthWire.accept`) - discard the decoded Request ID, so a reused ID is accepted. Validate it like - every other request, where a duplicate is session-fatal. -- An AUTH reply of an unknown type (neither AUTH_OK nor AUTH_ERROR) fails - the type dispatch before either decoder runs, and ends only the token. -- A second AUTH on an open IETF AUTH stream is never read: after the first, - Rust `Serve::run` and JS `AuthSession.serve` wait only for the stream to - close. Treat it like a repeated PUBLISH_NAMESPACE, a duplicate request. - -This list comes from review, not a full audit. Walk every AUTH read path on -both sides, both protocols, and both languages, and fold in anything missing. - -Close the session on each, in both languages and on both protocols, with one -regression test per case, and check the lite and IETF draft text agrees. Run -`just test interop --all`. - -Public API: none. Wire: none beyond draft wording. diff --git a/quest/m1/lite-malformed-close.md b/quest/m1/lite-malformed-close.md index baadda12811..d21893f0fe4 100644 --- a/quest/m1/lite-malformed-close.md +++ b/quest/m1/lite-malformed-close.md @@ -51,8 +51,7 @@ Rust-to-Rust and Rust-to-JS sessions never hit these; third-party peers do. it. Prefer one shared close path over per-handler fixes. - One test per rule per language, driving raw bytes like the runner does, including a malformed SUBSCRIBE_UPDATE and a malformed PROBE report. - Share the session-close helper from - [AUTH protocol violations](/quest/m1/auth/violations.md) if it lands first. + Share the session-close path that malformed AUTH messages already use. Public API: none. Wire: none; the behavior now matches the draft. @@ -65,4 +64,3 @@ Public API: none. Wire: none; the behavior now matches the draft. ## Related - [Malformed moq-transport input](/quest/m2/ietf-malformed-close.md) - the same rule for draft-18 and draft-21 -- [AUTH protocol violations](/quest/m1/auth/violations.md) - the AUTH-only slice, in progress diff --git a/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs index aee593308c5..72e7991b5e0 100644 --- a/rs/moq-net/benches/request-ids.rs +++ b/rs/moq-net/benches/request-ids.rs @@ -29,6 +29,35 @@ fn request_ids(c: &mut Criterion) { } group.finish(); } + +/// Draft-17+ admission, which spends IDs without a window but remembers skipped runs. +fn request_history(c: &mut Criterion) { + let mut group = c.benchmark_group("modern-request-ids"); + group.sample_size(20); + group.warm_up_time(Duration::from_millis(200)); + group.measurement_time(Duration::from_secs(1)); + for mode in ["ordered", "reordered", "scattered"] { + for sessions in [1, 8, 32] { + for requests in [8, 128, 220_000] { + let ids = ids(mode, requests); + println!( + "admitted-modern,{mode},{sessions},{requests},{}", + moq_net::fuzz::request_history(&ids) * sessions + ); + group.bench_function(BenchmarkId::new(mode, format!("{sessions}x{requests}")), |b| { + b.iter(|| { + let accepted: usize = (0..sessions) + .map(|_| moq_net::fuzz::request_history(black_box(&ids))) + .sum(); + black_box(accepted) + }); + }); + } + } + } + group.finish(); +} + /// Every ID in the window: in order, reversed (one skip to the top, then each fill /// shortens it), or scattered (the top first, then every other ID descending, so each /// splits the lowest skipped run and the remembered runs grow to half the window). @@ -48,5 +77,5 @@ fn ids(mode: &str, window: u64) -> Vec { } } -criterion_group!(benches, request_ids); +criterion_group!(benches, request_ids, request_history); criterion_main!(benches); diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index e0dd718fb8b..2ae193a0c4e 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -30,6 +30,19 @@ use bytes::Bytes; use crate::{Error, Patterns, Result, SessionError, time::Instant}; +/// Classify malformed AUTH messages separately from transport and local failures. +pub(crate) fn protocol_error(err: Error) -> Error { + match err { + Error::Decode(_) + | Error::BoundsExceeded(_) + | Error::WrongSize + | Error::TooManyParameters + | Error::UnexpectedMessage + | Error::UnexpectedStream => Error::ProtocolViolation, + err => err, + } +} + /// `ready!` for a `kio::Shared` poll, which yields a guard rather than a result. macro_rules! ready_or { ($e:expr) => { diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 06154eb85e4..7ca5a21be26 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1176,6 +1176,11 @@ impl crate::transport::Error for NoError { } } +/// Admit modern request IDs for the history benchmark, returning the accepted count. +pub fn request_history(ids: &[u64]) -> usize { + ietf::request_history(ids) +} + /// Admit a draft-16 window for the request-ID benchmark, returning the accepted count. pub fn request_ids(window: u64, ids: &[u64]) -> usize { ietf::request_ids(window, ids) diff --git a/rs/moq-net/src/ietf/auth.rs b/rs/moq-net/src/ietf/auth.rs index c257962294c..f7f41c2b8fc 100644 --- a/rs/moq-net/src/ietf/auth.rs +++ b/rs/moq-net/src/ietf/auth.rs @@ -140,7 +140,8 @@ impl Message for AuthOk { let subscribe = decode_prefixes(r)?; let expires = match r.varint()? { 0 => None, - ms => Some(Duration::from_millis(ms)), + ms if ms <= MAX_EXPIRES_MS => Some(Duration::from_millis(ms)), + _ => return Err(DecodeError::InvalidValue), }; Ok(Self { publish, @@ -177,6 +178,9 @@ impl Message for AuthError { fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { check_version(version)?; let code = r.varint()?; + if code > u32::MAX as u64 { + return Err(DecodeError::InvalidValue); + } let len = r.varint()?; if len > MAX_REASON as u64 { return Err(DecodeError::InvalidValue); @@ -232,7 +236,8 @@ pub(super) async fn run_present( peer_setup: peer::PeerSetup, version: Version, going_away: crate::goaway::GoingAway, -) where +) -> Result<(), Error> +where S: crate::transport::poll::Boxable, { if !peer_setup.get().await.auth { @@ -241,7 +246,19 @@ pub(super) async fn run_present( } let mut tasks = crate::util::TaskSet::owned(); - while let Some((id, token)) = tasks.drive(|waiter| handle.poll_opening(waiter)).await { + let violation: kio::Shared> = Default::default(); + while let Some((id, token)) = tasks + .drive(|waiter| { + if let Poll::Ready(mut err) = violation.poll(waiter, |err| match err.is_some() { + true => Poll::Ready(()), + false => Poll::Pending, + }) { + return Poll::Ready(Err(err.take().unwrap())); + } + handle.poll_opening(waiter).map(Ok) + }) + .await? + { let present = Present { runtime: runtime.clone(), session: session.clone(), @@ -251,12 +268,18 @@ pub(super) async fn run_present( id, }; let going_away = going_away.clone(); + let violation = violation.clone(); tasks.push(async move { // After a GOAWAY the peer must not see new requests. let err = match going_away.is_set() { true => Error::GoingAway, false => present.run(token).await, }; + let err = crate::auth::protocol_error(err); + if matches!(err, Error::ProtocolViolation) { + violation.lock().get_or_insert(err.clone()); + } + match &err { Error::Cancel | Error::Unsupported | Error::Transport(_) | Error::Session(_) => { tracing::debug!(%err, "auth token ended") @@ -404,7 +427,7 @@ impl Serve { mut stream: Stream, msg: Auth, version: Version, - ) { + ) -> Result<(), Error> { let serving = Serving::new(self.handle.clone()); let issue = &serving.issue; let mut default = None; @@ -423,7 +446,7 @@ impl Serve { if stream.writer.encode_message(&reply).await.is_ok() { let _ = stream.writer.close().await; } - return; + return Ok(()); } // Re-sent whenever a new limit changes it. None => default = Some(crate::auth::DefaultGrant::new(self.handle.clone(), self.peer_grant)), @@ -441,7 +464,12 @@ impl Serve { err } }; - serving.end(err); + let err = crate::auth::protocol_error(err); + serving.end(err.clone()); + if matches!(err, Error::ProtocolViolation) { + return Err(err); + } + Ok(()) } } @@ -462,7 +490,12 @@ async fn serve_issue( loop { let next = kio::wait(|waiter| { let mut cx = std::task::Context::from_waker(waiter.waker()); - if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { + if let Poll::Ready(res) = stream.reader.poll_read_chunk(&mut cx, 1).map(|res| { + res.and_then(|chunk| match chunk { + None => Ok(()), + Some(_) => Err(Error::ProtocolViolation), + }) + }) { return Poll::Ready(Next::Withdrawn(res)); } if let Some(default) = default.as_deref_mut() diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index b5a85859d91..c4a9bf0fed3 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -5,10 +5,62 @@ use crate::{ ietf::{RequestId, Version}, }; -/// The skipped `start..end` run holding `id`, if any. -fn missing_run(missing: &BTreeMap, id: u64) -> Option<(u64, u64)> { - let (&start, &end) = missing.range(..=id).next_back()?; - (id < end).then_some((start, end)) +/// The peer's spent request IDs: every ID of its parity below `next`, except the skipped +/// `start..end` runs in `missing`. +/// +/// Independent streams can reorder, so a peer's IDs need not arrive in sequence. A jump +/// costs one run however far it skips, and only a request landing inside a run splits it, +/// so the runs never outnumber the requests received. +struct Spent { + next: u64, + missing: BTreeMap, +} + +impl Spent { + fn new(first: u64) -> Self { + Self { + next: first, + missing: BTreeMap::new(), + } + } + + /// The skipped run holding `id`, if any. + fn run(&self, id: u64) -> Option<(u64, u64)> { + let (&start, &end) = self.missing.range(..=id).next_back()?; + (id < end).then_some((start, end)) + } + + /// Whether `id` has the peer's parity and was never spent. + /// + /// No peer reaches 2^62 requests, and refusing the IDs past it keeps `next` from + /// overflowing on a draft-17+ varint, which can carry a full 64 bits. + fn unused(&self, id: u64) -> bool { + id < 1 << 62 && id % 2 == self.next % 2 && (id >= self.next || self.run(id).is_some()) + } + + /// Whether spending `id` would record one more run. + fn splits(&self, id: u64) -> bool { + match self.run(id) { + Some((start, end)) => start < id && id + 2 < end, + None => id > self.next, + } + } + + /// Spend an [unused](Self::unused) `id`. + fn spend(&mut self, id: u64) { + if let Some((start, end)) = self.run(id) { + self.missing.remove(&start); + if start < id { + self.missing.insert(start, id); + } + if id + 2 < end { + self.missing.insert(id + 2, end); + } + } else if id > self.next { + self.missing.insert(self.next, id); + } + self.next = self.next.max(id + 2); + } } /// The MAX_REQUEST_ID that first admits `window` requests from a peer. @@ -25,6 +77,10 @@ struct ControlState { request_id_max: Option, /// The peer's requests, flow controlled on drafts 14 to 16 only. incoming: Option, + /// The peer's spent IDs on draft 17 and later, which has no window to bound the runs. + modern: Spent, + /// The most runs `modern` may hold. Forgetting one would make its IDs usable again. + history: u64, } /// The MAX_REQUEST_ID window we grant the peer. @@ -39,12 +95,10 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, - /// One past the highest accepted ID, preserving IDs whose requests retired. - next: RequestId, - /// Draft 16 can reorder across streams, so it remembers the IDs skipped below `next` - /// as `start..end` runs. A jump costs one run however far it skips, and only a request - /// landing inside a run splits it, so the runs never outnumber the requests received. - missing: Option>, + /// Every accepted ID, preserving IDs whose requests retired. + spent: Spent, + /// Draft 16 can reorder across streams; drafts 14 and 15 require the next ID. + reorder: bool, } #[derive(Clone)] @@ -59,6 +113,9 @@ impl Control { request_id_next: if client { RequestId(0) } else { RequestId(1) }, request_id_max, incoming: None, + // The peer takes the other parity: a client's IDs are even, a server's odd. + modern: Spent::new(u64::from(client)), + history: crate::session::Limits::default().requests(), }), } } @@ -75,12 +132,19 @@ impl Control { sent: max, live: 0, retired: 0, - next: RequestId(u64::from(client)), - missing: (version == Version::Draft16).then(BTreeMap::new), + spent: Spent::new(u64::from(client)), + reorder: version == Version::Draft16, }); self } + /// Bound the runs of skipped IDs a draft-17+ peer may leave, closing the session with + /// INTERNAL_ERROR past `limit` rather than forgetting any. + pub fn with_history(self, limit: u64) -> Self { + self.state.lock().history = limit; + self + } + pub fn max_request_id(&self, max: RequestId) { // Mutating through the guard wakes any `next_request_id` waiting on the limit. self.state.lock().request_id_max = Some(max); @@ -91,46 +155,38 @@ impl Control { /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. /// Drafts 14/15 require the next ID; draft 16 permits cross-stream reordering. - /// Used IDs close with INVALID_REQUEST_ID even after retirement. - /// `None` when the peer's requests are not flow controlled. + /// Used IDs close with INVALID_REQUEST_ID even after retirement, on every draft. + /// `None` when the peer's requests are not flow controlled (draft 17 and later). pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); + let state = &mut *state; let Some(incoming) = &mut state.incoming else { + let modern = &mut state.modern; + if !modern.unused(id.0) { + return Err(SessionError::InvalidRequestId.into()); + } + if modern.splits(id.0) && modern.missing.len() as u64 >= state.history { + tracing::warn!(limit = state.history, "request ID history exhausted"); + return Err(SessionError::Internal.into()); + } + modern.spend(id.0); return Ok(None); }; if id.0 >= incoming.max { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } - let run = match &incoming.missing { - Some(missing) if id < incoming.next => missing_run(missing, id.0), - _ => None, + let unused = match incoming.reorder { + true => incoming.spent.unused(id.0), + false => id.0 == incoming.spent.next, }; - let unused = if id < incoming.next { - run.is_some() - } else { - id == incoming.next || incoming.missing.is_some() - }; - if id.0 % 2 != incoming.next.0 % 2 || !unused { + if !unused { return Err(SessionError::InvalidRequestId.into()); } if incoming.live >= incoming.window { return Err(SessionError::TooManyRequests.into()); } - if let Some(missing) = &mut incoming.missing { - if let Some((start, end)) = run { - missing.remove(&start); - if start < id.0 { - missing.insert(start, id.0); - } - if id.0 + 2 < end { - missing.insert(id.0 + 2, end); - } - } else if id > incoming.next { - missing.insert(incoming.next.0, id.0); - } - } - incoming.next.0 = incoming.next.0.max(id.0 + 2); + incoming.spent.spend(id.0); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -318,22 +374,11 @@ mod tests { for id in 1..1024 { drop(control.accept(RequestId(id * 2)).unwrap()); let state = control.state.lock(); - let missing = state.incoming.as_ref().unwrap().missing.as_ref().unwrap(); + let missing = &state.incoming.as_ref().unwrap().spent.missing; assert_eq!(missing.iter().collect::>(), [(&0, &2)]); } drop(control.accept(RequestId(0)).unwrap()); - assert!( - control - .state - .lock() - .incoming - .as_ref() - .unwrap() - .missing - .as_ref() - .unwrap() - .is_empty() - ); + assert!(control.state.lock().incoming.as_ref().unwrap().spent.missing.is_empty()); for id in [0, 2, 2046] { assert!(matches!( control.accept(RequestId(id)), @@ -350,7 +395,7 @@ mod tests { let top = 2 * (window - 1); let runs = |control: &Control| { let state = control.state.lock(); - let missing = state.incoming.as_ref().unwrap().missing.as_ref().unwrap(); + let missing = &state.incoming.as_ref().unwrap().spent.missing; missing.iter().map(|(&start, &end)| (start, end)).collect::>() }; @@ -370,11 +415,80 @@ mod tests { } } - /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. + #[test] + fn modern_requests_reject_reuse_and_wrong_parity() { + for client in [false, true] { + let control = Control::new(None, client); + let first = u64::from(client); + for id in [first + 4, first, first + 2] { + assert!(control.accept(RequestId(id)).unwrap().is_none()); + } + for id in [first, first + 2, first + 4, first ^ 1] { + assert!( + matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + ), + "accepted spent or wrong-parity ID {id}" + ); + } + } + } + + /// Contiguous arrival in either order leaves no runs, and a full history refuses a new + /// gap rather than forgetting an old one. + #[test] + fn modern_history_coalesces_and_never_forgets() { + for reversed in [false, true] { + let control = Control::new(None, false).with_history(1); + for n in 0..4096 { + let id = if reversed { 4095 - n } else { n } * 2; + control.accept(RequestId(id)).unwrap(); + } + assert!(control.state.lock().modern.missing.is_empty()); + assert!(matches!( + control.accept(RequestId(0)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + let control = Control::new(None, false).with_history(1); + control.accept(RequestId(0)).unwrap(); + control.accept(RequestId(4)).unwrap(); + assert!(matches!( + control.accept(RequestId(8)), + Err(Error::Session(SessionError::Internal)) + )); + control.accept(RequestId(2)).unwrap(); + control.accept(RequestId(8)).unwrap(); + for id in [0, 2, 4, 8] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + #[test] + fn modern_sparse_ids_do_not_allocate_the_gap() { + let control = Control::new(None, false).with_history(1); + let top = (1 << 62) - 2; + control.accept(RequestId(top)).unwrap(); + control.accept(RequestId(0)).unwrap(); + let runs: Vec<_> = control.state.lock().modern.missing.clone().into_iter().collect(); + assert_eq!(runs, [(2, top)]); + for id in [top, 1 << 62, u64::MAX - 1] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + /// Draft-17+ has no MAX_REQUEST_ID: nothing is granted. #[test] fn unlimited_without_a_window() { let control = Control::new(None, true); - assert!(control.accept(RequestId(u64::MAX)).unwrap().is_none()); + assert!(control.accept(RequestId(1)).unwrap().is_none()); assert_eq!(granted(&control), None); } } diff --git a/rs/moq-net/src/ietf/mod.rs b/rs/moq-net/src/ietf/mod.rs index 9446fd6ad38..50f2fcdce83 100644 --- a/rs/moq-net/src/ietf/mod.rs +++ b/rs/moq-net/src/ietf/mod.rs @@ -69,3 +69,9 @@ pub(crate) fn request_ids(window: u64, ids: &[u64]) -> usize { .collect(); held.len() } + +#[cfg(any(test, feature = "fuzz"))] +pub(crate) fn request_history(ids: &[u64]) -> usize { + let control = Control::new(None, false); + ids.iter().filter(|id| control.accept(RequestId(**id)).is_ok()).count() +} diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 85af7a7f1b3..0500a90c775 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -990,7 +990,15 @@ where .reader .poll_decode_maybe::(&mut cx) { - Poll::Ready(Ok(Some(update))) => return Poll::Ready(Ok(Some(update))), + Poll::Ready(Ok(Some(update))) => { + if let Err(err) = self.control.accept(update.request_id) { + session + .clone() + .close(crate::SessionError::from(&err).to_code(), "invalid request ID"); + return Poll::Ready(Err(err)); + } + return Poll::Ready(Ok(Some(update))); + } Poll::Ready(Ok(None)) if !super::request_stream::fin_cancels(self.version) => { *finished = true } diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 50bfc7927b9..96e952f0ee2 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -405,7 +405,7 @@ where } }; - let control = Control::new(None, client); + let control = Control::new(None, client).with_history(limits.requests()); // Only the dialing side fails loud on a publication outside its grant: a // server's publish origin is everything the peer may read, not what it // intends to push. @@ -511,7 +511,7 @@ where ))); let mut datagrams = std::pin::pin!(err_only(run_datagrams(session.clone(), subscriber.clone()))); let mut goaway_recv = std::pin::pin!(err_only(goaway_recv)); - let mut present = std::pin::pin!(present); + let mut present = std::pin::pin!(err_only(present)); let mut enforce = std::pin::pin!(err_only(enforce)); let mut setup = std::pin::pin!(setup); // Unsolicited PUBLISH_NAMESPACE unless the peer requires solicitation; @@ -556,8 +556,10 @@ where if let Poll::Ready(err) = waiter.poll_future(goaway_recv.as_mut()) { return Poll::Ready(Err(err)); } - // Presenting tokens never ends the session. - let _ = waiter.poll_future(present.as_mut()); + // Malformed replies are session-fatal, unlike token refusals. + if let Poll::Ready(err) = waiter.poll_future(present.as_mut()) { + return Poll::Ready(Err(err)); + } if let Poll::Ready(err) = waiter.poll_future(enforce.as_mut()) { return Poll::Ready(Err(err)); } @@ -1267,6 +1269,22 @@ impl Bidi { } Err(err) => return Err(err), }; + if !matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16) + && matches!( + id, + ietf::Subscribe::ID + | ietf::Fetch::ID + | ietf::SubscribeNamespace::ID + | ietf::SubscribeNamespaceLegacy::ID + | ietf::SUBSCRIBE_TRACKS_ID + | ietf::TrackStatus::ID + | ietf::Publish::ID + | ietf::PublishNamespace::ID + | auth::Auth::ID + ) { + let request_id = RequestId::decode(&mut data.decoder(version), version)?; + publisher.control.accept(request_id)?; + } match id { // Draft-16 moved SUBSCRIBE_NAMESPACE to its own stream, past the control stream // that admits every other request, but it still takes a request ID from the @@ -1298,7 +1316,7 @@ impl Bidi { if !data.is_empty() { return Err(Error::WrongSize); } - serve.run(stream, msg, version).await; + serve.run(stream, msg, version).await?; } _ => { tracing::warn!(id, "unexpected bidi stream type"); @@ -1407,6 +1425,194 @@ mod tests { use super::*; use crate::model::ProduceTest; + #[moq_net_sim::test] + async fn auth_ids_share_the_modern_request_history() { + for version in [ + Version::Draft17, + Version::Draft18, + Version::Draft19, + Version::Draft20, + Version::Draft21, + Version::Draft22, + ] { + for client in [false, true] { + let first = u64::from(client); + for requests in [ + vec![(true, first), (true, first)], + vec![(true, first), (false, first)], + vec![(false, first), (true, first)], + vec![(true, first ^ 1)], + ] { + let incoming = requests + .into_iter() + .map(|(is_auth, id)| { + let mut body = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut body, version.into()); + let kind = if is_auth { + auth::Auth { + request_id: RequestId(id), + token: Default::default(), + } + .encode_msg(&mut encoder, version) + .unwrap(); + auth::Auth::ID + } else { + ietf::TrackStatus { + request_id: RequestId(id), + track_namespace: crate::Path::from("test"), + track_name: "video".into(), + properties_wanted: true, + } + .encode_msg(&mut encoder, version) + .unwrap(); + ietf::TrackStatus::ID + }; + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(kind).unwrap(); + encoder.u16(body.len() as u16); + script.extend(body); + script + }) + .collect(); + let session = + crate::lite::test_transport::ScriptedSession::new(Vec::new()).with_incoming_bidis(incoming); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + extensions: Default::default(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("invalid AUTH ID left the session open") + .expect_err("invalid request ID must be fatal"); + assert!( + matches!(err, Error::Session(SessionError::InvalidRequestId)), + "{version:?} client={client}: {err:?}" + ); + assert_eq!(log.closes()[0].0, SessionError::InvalidRequestId.to_code()); + } + } + } + } + + #[moq_net_sim::test] + async fn malformed_auth_replies_close_the_session() { + for version in [Version::Draft17, Version::Draft22] { + for (id, body) in [ + (99, vec![]), + (auth::AuthError::ID, vec![0xc0, 0, 0, 1, 0, 0, 0, 0, 0]), + (auth::AuthError::ID, vec![1]), + (auth::AuthError::ID, vec![1, 0, 0]), + (auth::AuthOk::ID, vec![0, 0, 0xc0, 0x20, 0, 0, 0, 0, 0, 0]), + ] { + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(id).unwrap(); + encoder.u16(body.len() as u16); + script.extend(body); + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![script]); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client: true, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + extensions: Default::default(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("the session outlived a malformed AUTH reply") + .expect_err("malformed AUTH is fatal"); + assert!(is_protocol_violation(&err), "{err:?}"); + assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); + } + } + } + + #[moq_net_sim::test] + async fn malformed_presenter_auth_closes_the_session() { + for version in [Version::Draft17, Version::Draft22] { + for body in [vec![0, 2], vec![0, 0, 0]] { + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + encoder.varint(auth::Auth::ID).unwrap(); + encoder.u16(2); + script.extend(body); + let session = + crate::lite::test_transport::ScriptedSession::new(Vec::new()).with_incoming_bidis(vec![script]); + let log = session.log.clone(); + let (driver, _, _) = start(Config { + runtime: crate::time::Clock::sim(), + session, + setup: None, + request_id_max: None, + limits: Default::default(), + client: false, + publish: None, + subscribe: None, + peer_hop: None, + cost: None, + version, + path: None, + authority: None, + peer_setup_stream: None, + peer_declared: Some(peer::Peer { + auth: true, + ..Default::default() + }), + auth: crate::auth::Handle::new(true), + early_unis: Vec::new(), + extensions: Default::default(), + }) + .unwrap(); + let err = moq_net_sim::timeout(std::time::Duration::from_secs(10), driver) + .await + .expect("malformed presenter AUTH was ignored") + .expect_err("malformed AUTH is fatal"); + assert!(is_protocol_violation(&err), "{err:?}"); + assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); + } + } + } + #[test] fn object_overflow_is_session_fatal_but_a_stream_error_is_not() { let mut reader = Reader::new(crate::lite::test_transport::PendingRecv, Version::Draft22); diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index b4d30fd3ff7..ede394365b8 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -1392,11 +1392,18 @@ where return Ok(()); } + // An update spends its own request ID, even one refused below. + let request_id = ietf::RequestId::decode(&mut body.decoder(self.version), self.version)?; + if let Err(err) = self.control.accept(request_id) { + self.session + .clone() + .close(crate::SessionError::from(&err).to_code(), "invalid request ID"); + return Err(err); + } let msg = match ietf::PublishNamespaceUpdate::decode_msg(&mut data, self.version) { // Section 8.9 rejects the update, which withdraws the advertisement as any // refused update does. Err(DecodeError::MalformedAuthToken) => { - let request_id = ietf::RequestId::decode(&mut body.decoder(self.version), self.version)?; let err = Error::Decode(DecodeError::MalformedAuthToken); self.write_error(stream, request_id, &err, "repeated AUTHORIZATION TOKEN") .await?; @@ -7065,7 +7072,7 @@ mod tests { crate::time::Clock::sim(), session.clone(), origin, - Control::new(None, false), + Control::new(None, true), None, peer::PeerSetup::default(), self_origin, @@ -7143,6 +7150,32 @@ mod tests { ) } + /// Namespace updates spend IDs from the same lifetime history as initial requests. + #[moq_net_sim::test] + async fn namespace_update_cannot_reuse_a_spent_request_id() { + let self_origin = crate::Hop::new(5).unwrap(); + let peer = peer_9(); + let (clean, _) = clean_and_looped(); + let script = publish_namespace_updates(std::slice::from_ref(&clean)).await; + let (mut subscriber, _consumer, mut stream, _driver) = update_harness(self_origin, &peer, &clean, script).await; + let log = subscriber.session.log.clone(); + // The initial request is already retired; its ID remains permanently spent. + assert!(subscriber.control.accept(RequestId(3)).unwrap().is_none()); + let mut attached = true; + let err = subscriber + .run_publish_namespace_updates( + &mut stream, + &crate::Path::new("room/host"), + Some(clean), + peer, + &mut attached, + ) + .await + .expect_err("a repeated update ID must close the session"); + assert!(matches!(err, Error::Session(crate::SessionError::InvalidRequestId))); + assert_eq!(log.closes()[0].0, crate::SessionError::InvalidRequestId.to_code()); + } + /// A reflected update detaches the route but MUST NOT end the stream. Updates ride /// the stream that already carries the advertisement, so closing it strands the /// namespace even when the peer's path goes clean again. It is also not ours to diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index f8348ce069f..c242acd714c 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -419,7 +419,7 @@ impl AuthServe { } fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - let res = ready!(self.poll_serve(waiter)); + let res = ready!(self.poll_serve(waiter)).map_err(crate::auth::protocol_error); let err = match res { Ok(()) if self.unsupported => Error::Unsupported, Ok(()) => Error::Cancel, @@ -437,7 +437,14 @@ impl AuthServe { } }; if let Some(serving) = &self.serving { - serving.end(err); + serving.end(err.clone()); + } + if matches!(err, Error::ProtocolViolation) { + self.shared.auth.close(err.clone()); + self.shared + .session + .clone() + .close(SessionError::ProtocolViolation.to_code(), &err.to_string()); } Poll::Ready(Ok(())) } @@ -489,7 +496,12 @@ impl AuthServe { // The presenter withdrew the token, by FIN or by a cancelling reset, or its // stream died. - if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { + if let Poll::Ready(res) = stream.reader.poll_read_chunk(&mut cx, 1).map(|res| { + res.and_then(|chunk| match chunk { + None => Ok(()), + Some(_) => Err(Error::ProtocolViolation), + }) + }) { match res { Ok(()) | Err(Error::Stream(crate::StreamError::Cancel)) => {} Err(err) => return Poll::Ready(Err(err)), diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index bb48334b59c..b4c713a09d6 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -548,12 +548,10 @@ impl kio::Task for PresentToken { // A peer that predates AUTH resets a stream type it does not know, and one // that takes no tokens in band refuses the same way: either way, before any // reply, this token earns nothing here. - Error::Stream(_) | Error::Decode(crate::DecodeError::Short) if !answered => Error::Unsupported, - // A malformed grant is the peer breaking the session protocol, not refusing a token. - Error::Decode(crate::DecodeError::InvalidValue | crate::DecodeError::InvalidString(_)) => { - Error::ProtocolViolation - } - err => err, + Error::Stream(_) if !answered => Error::Unsupported, + // A malformed reply, truncated ones included, is the peer breaking the session + // protocol, not refusing a token. + err => crate::auth::protocol_error(err), }; match &err { Error::Cancel | Error::Unsupported | Error::Transport(_) | Error::Session(_) => { @@ -610,7 +608,7 @@ impl PresentToken { // The peer ended the grant without revoking it, or closed without // ever answering (mapped to unsupported by the caller). Ok(None) if *answered => return Poll::Ready(Error::Cancel), - Ok(None) => return Poll::Ready(Error::Decode(crate::DecodeError::Short)), + Ok(None) => return Poll::Ready(Error::Unsupported), Err(err) => return Poll::Ready(err), }; match reply { @@ -896,21 +894,20 @@ mod tests { use crate::coding::Encode; use crate::lite::test_transport::ScriptedSession; - /// An AUTH_ERROR code the session registry cannot hold is the peer breaking the - /// protocol: it closes the session rather than surfacing a code the peer never sent. #[moq_net_sim::test] - async fn an_auth_error_code_past_u32_closes_the_session() { - let refused = crate::lite::AuthReply::Error(crate::lite::AuthError { - code: u64::from(u32::MAX) + 1, - reason: String::new(), - }); - let mut script = Vec::new(); - refused - .encode( - &mut crate::coding::Encoder::new(&mut script, Version::Lite07.into()), - Version::Lite07, - ) - .unwrap(); + async fn malformed_auth_replies_close_the_session() { + // Unknown reply, invalid code, truncated error, and trailing body bytes. + for script in [ + vec![99], + vec![1, 9, 0xc0, 0, 0, 1, 0, 0, 0, 0, 0], + vec![1, 1, 1], + vec![1, 3, 1, 0, 0], + ] { + malformed_auth_reply(script).await; + } + } + + async fn malformed_auth_reply(script: Vec) { // The setup token's AUTH stream is the first one the session opens. let transport = ScriptedSession::per_stream(vec![script]); let log = transport.log.clone(); @@ -942,6 +939,35 @@ mod tests { assert_eq!(log.closes()[0].0, SessionError::ProtocolViolation.to_code()); } + #[moq_net_sim::test] + async fn malformed_presenter_auth_closes_the_session() { + for script in [vec![7, 1, 2], vec![7, 1, 0, 0]] { + let transport = ScriptedSession::new(Vec::new()).with_incoming_bidis(vec![script]); + let log = transport.log.clone(); + let start = start(Config { + runtime: crate::time::Clock::sim(), + client: false, + limits: Default::default(), + session: transport, + setup_stream: None, + publish: None, + subscribe: None, + peer_hop: None, + version: Version::Lite07, + our_setup: Setup::default(), + peer_setup: None, + auth: crate::auth::Handle::new(true), + }) + .unwrap(); + let mut driver = start.driver; + let _ = driver.poll(&kio::Waiter::noop()); + assert_eq!( + log.closes().first().map(|close| close.0), + Some(SessionError::ProtocolViolation.to_code()) + ); + } + } + #[moq_net_sim::test] async fn malformed_auth_ok_closes_the_session() { for pattern in [ @@ -972,6 +998,18 @@ mod tests { assert_malformed_reply_closes(script).await; } + /// A reply cut off inside its declared size fails to decode, unlike a peer that + /// resets before answering. + #[moq_net_sim::test] + async fn truncated_auth_reply_closes_the_session() { + let mut script = Vec::new(); + let mut w = crate::coding::Encoder::new(&mut script, Version::Lite07.into()); + w.varint(0).unwrap(); + w.varint(4).unwrap(); + w.varint(0).unwrap(); + assert_session_closes(ScriptedSession::per_stream_eof(vec![script])).await; + } + #[moq_net_sim::test] async fn malformed_auth_error_reasons_close_the_session() { for reason in [vec![0xff], vec![b'a'; 8193]] { @@ -987,7 +1025,10 @@ mod tests { } async fn assert_malformed_reply_closes(script: Vec) { - let transport = ScriptedSession::per_stream(vec![script]); + assert_session_closes(ScriptedSession::per_stream(vec![script])).await; + } + + async fn assert_session_closes(transport: ScriptedSession) { let log = transport.log.clone(); let start = start(Config { runtime: crate::time::Clock::sim(), diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index c10968de5ac..c3ca782f07a 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -673,7 +673,9 @@ impl Drop for Withdrawing { /// /// On moq-transport drafts 14 to 16 the caps also size the request window, which counts /// every request (FETCH, TRACK_STATUS and SUBSCRIBE_UPDATE included), so very low caps -/// can starve it. +/// can starve it. Later drafts have no window but still refuse a reused request ID, so a +/// peer may leave at most twice the sum of these caps runs of skipped IDs; one more closes +/// the session with `INTERNAL_ERROR` rather than forgetting any. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub struct Limits { diff --git a/rs/moq-net/tests/subscribe_tracks.rs b/rs/moq-net/tests/subscribe_tracks.rs index 960b3cf9163..4bac13268c3 100644 --- a/rs/moq-net/tests/subscribe_tracks.rs +++ b/rs/moq-net/tests/subscribe_tracks.rs @@ -25,7 +25,7 @@ const TIMEOUT: Duration = Duration::from_secs(10); const SUBSCRIBE_TRACKS: &[u8] = &[ 0x51, // SUBSCRIBE_TRACKS 0x00, 0x08, // Length - 0x01, // Request ID + 0x3f, // Fresh server-parity Request ID, after the existing subscription. 0x01, 0x04, b'r', b'o', b'o', b'm', // Track Namespace Prefix 0x00, // Number of Parameters ];