Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions js/net/src/ietf/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ export class Connection implements Established {
}
case SubscribeUpdate.id: {
// REQUEST_UPDATE (0x02) is a follow-up, not a valid initial message
stream.abort(new Error("unexpected REQUEST_UPDATE as initial message"));
break;
throw new ProtocolViolation("unexpected REQUEST_UPDATE as initial message");
}
// Publisher handles incoming requests
case Subscribe.id: {
Expand Down Expand Up @@ -310,11 +309,9 @@ export class Connection implements Established {
// SUBSCRIBE_NAMESPACE there, and the message alone does not say which.
const legacy = this.#session.version === Version.DRAFT_14 || this.#session.version === Version.DRAFT_15;
if (this.#solicit !== undefined && !legacy) {
console.error(
throw new ProtocolViolation(
`unsolicited publish_namespace from a peer that implements MoQ Solicit: broadcast=${msg.trackNamespace}`,
);
this.#close();
break;
}

await this.#subscriber.runPublishNamespace(msg, stream);
Expand Down
99 changes: 99 additions & 0 deletions js/net/src/ietf/peer-violations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { expect, spyOn, test } from "bun:test";
import { SessionCode } from "../error.ts";
import { createMockTransportPair } from "../mock.ts";
import * as Path from "../path.ts";
import { Stream } from "../stream.ts";
import { NativeSession } from "./adapter.ts";
import { Connection } from "./connection.ts";
import { PublishNamespace } from "./publish_namespace.ts";
import { Subscribe, SubscribeOk } from "./subscribe.ts";
import { Subscriber } from "./subscriber.ts";
import { ALPN, Version } from "./version.ts";

const versions = [
[Version.DRAFT_18, ALPN.DRAFT_18],
[Version.DRAFT_22, ALPN.DRAFT_22],
] as const;

// Both connection drivers run, while the client can inject a malformed peer stream.
for (const [version, alpn] of versions) {
test.each(["update", "unsolicited"] as const)(
`${alpn}: %s closes the peer session with its violation`,
async (kind) => {
const pair = createMockTransportPair(alpn);
const clientControl = await Stream.open(pair.client, { version });
const serverControl = await Stream.accept(pair.server, version);
if (!serverControl) throw new Error("missing control stream");
const server = new Connection({
url: new URL("https://example.com"),
quic: pair.server,
control: serverControl,
maxRequestId: 100n,
version,
client: false,
solicit: true,
});
const client = new Connection({
url: new URL("https://example.com"),
quic: pair.client,
control: clientControl,
maxRequestId: 100n,
version,
client: true,
solicit: true,
});
const closing = spyOn(pair.server, "close");
const logged = spyOn(console, "error").mockImplementation(() => {});
try {
const stream = await Stream.open(pair.client, { version });
if (kind === "update") await stream.writer.u62(0x02n);
else {
await stream.writer.u53(PublishNamespace.id);
await new PublishNamespace({ requestId: 0n, trackNamespace: Path.from("unasked") }).encode(
stream.writer,
version,
);
}
// The handler either ends the stream or the session before this read settles.
await stream.reader.u53().catch(() => undefined);
expect(closing.mock.calls[0]?.[0]?.closeCode).toBe(SessionCode.ProtocolViolation);
expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation);
} finally {
client.close();
server.close();
closing.mockRestore();
logged.mockRestore();
}
},
);

test(`${alpn}: a shared alias for different tracks closes with a violation`, async () => {
const pair = createMockTransportPair(alpn);
const subscriber = new Subscriber({
session: new NativeSession(pair.server, version, true),
quic: pair.server,
});
const broadcast = subscriber.consume(Path.from("room"));
const tracks = [];
try {
for (const name of ["video", "audio"]) {
const track = broadcast.track(name).subscribe();
tracks.push(track);
const peer = await Stream.accept(pair.client, version);
if (!peer) throw new Error("missing subscription");
expect(await peer.reader.u53()).toBe(Subscribe.id);
const request = await Subscribe.decode(peer.reader, version);
await peer.writer.u53(SubscribeOk.id);
await new SubscribeOk({ requestId: request.requestId, trackAlias: 7n }).encode(peer.writer, version);
await track.info();
}
expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation);
} finally {
for (const track of tracks) track.close();
broadcast.close();
subscriber.close();
pair.client.close();
pair.server.close();
}
});
}
11 changes: 10 additions & 1 deletion js/net/src/ietf/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
error,
ProtocolViolation,
reason,
SessionCode,
StreamCode,
StreamError,
sessionCause,
Expand Down Expand Up @@ -804,7 +805,15 @@ export class Subscriber {
// Only one alias naming two different tracks is the session's problem. A publisher
// sharing an alias between subscriptions to one track is allowed to do that, and
// disconnecting over it would drop every other broadcast on the session.
if (err instanceof DuplicateTrackAlias) this.#session.close();
if (err instanceof DuplicateTrackAlias) {
// Send the violation before the session's own close sends a clean code.
try {
this.#quic?.close({ closeCode: SessionCode.ProtocolViolation, reason: err.message });
} catch {
// Already closed.
}
this.#session.close();
}
throw err;
}
state.registeredAlias = ok.trackAlias;
Expand Down
Loading