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
9 changes: 8 additions & 1 deletion drafts/draft-lcurley-moq-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ 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}}).
A code from the REQUEST_ERROR registry ({{moqt}}): UNAUTHORIZED, MALFORMED_AUTH_TOKEN, EXPIRED_AUTH_TOKEN, NOT_SUPPORTED ({{prefixes}}), or GOING_AWAY.
GOING_AWAY answers an AUTH the acceptor no longer admits after its GOAWAY ({{moqt}}); it refuses the request, not the token, so the token stays valid for another session.
Comment thread
kixelated marked this conversation as resolved.
Resolved

**Reason Phrase**:
A human-readable reason, at most 8,192 bytes.
Expand Down Expand Up @@ -229,6 +230,12 @@ This document requests three registrations in the "MOQT Message Types" registry

--- back

# Changelog

## Since draft-lcurley-moq-auth-00 (in progress)

- AUTH_ERROR can carry GOING_AWAY, refusing an AUTH past the acceptor's GOAWAY without refusing the token.

# Acknowledgments
{:numbered="false"}

Expand Down
16 changes: 9 additions & 7 deletions js/net/src/auth_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface AuthWire {
read(stream: Stream): Promise<WireReply | undefined>;
/** The token the peer presents on a stream it opened. */
accept(stream: Stream): Promise<Uint8Array>;
/** Wait for the presenter to withdraw while validating follow-up messages. */
withdrawn(stream: Stream): Promise<void>;
/** Tell the presenter a grant. Throws {@link Unsupported} when this wire cannot represent it. */
grant(stream: Stream, grant: WireGrant): Promise<void>;
/** Refuse or revoke the presenter's token. */
Expand Down Expand Up @@ -172,11 +174,8 @@ class IssuedGrant implements Issued {
this.#stream = stream;
this.#wire = wire;
// The presenter withdraws by closing or cancelling its side.
this.closed = stream.reader
.done()
.then((done) => {
if (!done) throw new ProtocolViolation("additional message on an AUTH stream");
})
this.closed = wire
.withdrawn(stream)
.then(
() => null,
(err: unknown) => (err instanceof StreamError && err.code === StreamCode.Cancel ? null : error(err)),
Expand Down Expand Up @@ -540,8 +539,11 @@ export class AuthSession implements Handle {
}
console.warn("auth token refused", reply.refused);
// A refused setup token leaves an empty union, not an unknown (unrestricted)
// one. A grant the acceptor could not tell is unknown, not empty.
if (!(reply.refused instanceof Unsupported)) this.#replied = true;
// one. A grant the acceptor could not tell, or never judged, is unknown, not empty.
const unjudged =
reply.refused instanceof Unsupported ||
(reply.refused instanceof StreamError && reply.refused.code === StreamCode.GoingAway);
if (!unjudged) this.#replied = true;
result = reply.refused;
stream.close();
break;
Expand Down
12 changes: 10 additions & 2 deletions js/net/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,23 @@ export function closeError(quic: WebTransport): Promise<Error> {
);
}

/**
* Whether a stream failed because its whole session closed, rather than on its own.
*
* @internal
*/
export function sessionClosed(err: unknown): boolean {
return typeof err === "object" && err !== null && (err as { source?: unknown }).source === "session";
}

/**
* Report a failure the session's close caused as the session's own error, which carries the
* peer's close code; any other failure passes through.
*
* @internal
*/
export async function sessionCause(quic: WebTransport | undefined, err: unknown): Promise<Error> {
const source = typeof err === "object" && err !== null ? (err as { source?: unknown }).source : undefined;
if (quic && source === "session") return closeError(quic);
if (quic && sessionClosed(err)) return closeError(quic);
return error(err);
}

Expand Down
143 changes: 76 additions & 67 deletions js/net/src/ietf/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Once } from "@moq/signals";
import { Once, race } from "@moq/signals";
import { Mutex } from "async-mutex";
import type { Drain } from "../connection/goaway.ts";
import { error, ProtocolViolation, SessionCode, SessionError } from "../error.ts";
import { error, ProtocolViolation, SessionCode, SessionError, sessionClosed } from "../error.ts";
import { type OpenOptions, Reader, Stream, type Writer } from "../stream.ts";
import { untilAborted } from "../util/abort.ts";
import * as Varint from "../varint.ts";
Expand Down Expand Up @@ -471,81 +471,90 @@ export class ControlStreamAdapter implements Session {
void this.#acceptNativeBidis();
}

for (;;) {
const done = await this.#reader.done();
if (done) {
cause = new ProtocolViolation("control stream closed");
break;
}
const read = async () => {
for (;;) {
const done = await this.#reader.done();
if (done) {
throw new ProtocolViolation("control stream closed");
}

const typeId = await this.#reader.u53();
const size = await this.#reader.u16();
const body = await this.#reader.read(size);
const typeId = await this.#reader.u53();
const size = await this.#reader.u16();
const body = await this.#reader.read(size);

const classified = await this.#classify(typeId, body);
const classified = await this.#classify(typeId, body);

if (classified.route === Route.GoAway) {
// The session keeps serving: a GOAWAY asks us to migrate, not to stop reading.
const msg = await GoAway.decodeBody(body, this.version);
if (this.goaway.peek() !== undefined) throw new ProtocolViolation("duplicate GOAWAY");
// A client may leave, but only the server may name where to go.
if (!this.client && msg.newSessionUri !== "") {
throw new ProtocolViolation("client GOAWAY must not name a redirect");
if (classified.route === Route.GoAway) {
// The session keeps serving: a GOAWAY asks us to migrate, not to stop reading.
const msg = await GoAway.decodeBody(body, this.version);
if (this.goaway.peek() !== undefined) throw new ProtocolViolation("duplicate GOAWAY");
// A client may leave, but only the server may name where to go.
if (!this.client && msg.newSessionUri !== "") {
throw new ProtocolViolation("client GOAWAY must not name a redirect");
}
this.goaway.set(msg.drain());
continue;
}
this.goaway.set(msg.drain());
continue;
}

if (classified.route === Route.Update) {
this.spendRequest(classified.ownRequestId);
// An update whose request is gone, or rides a stream of its own like draft 16's
// SUBSCRIBE_NAMESPACE, is still owed its one answer. Draft 14 answers no update.
if (
!this.#pushMessage(classified.requestId, typeId, size, body) &&
this.version !== Version.DRAFT_14
) {
void this.#refuseUpdate(classified.ownRequestId);
if (classified.route === Route.Update) {
this.spendRequest(classified.ownRequestId);
// An update whose request is gone, or rides a stream of its own like draft 16's
// SUBSCRIBE_NAMESPACE, is still owed its one answer. Draft 14 answers no update.
if (
!this.#pushMessage(classified.requestId, typeId, size, body) &&
this.version !== Version.DRAFT_14
) {
void this.#refuseUpdate(classified.ownRequestId);
}
continue;
}
continue;
}

const { route, requestId } = classified;
const updated = this.#updates.get(requestId);
if ((route === Route.Response || route === Route.ErrorResponse) && updated !== undefined) {
// A refused update leaves its request open: the answer is all it ends.
this.#updates.delete(requestId);
if (this.version === Version.DRAFT_16) this.#pushMessage(updated, typeId, size, body);
continue;
}
const { route, requestId } = classified;
const updated = this.#updates.get(requestId);
if ((route === Route.Response || route === Route.ErrorResponse) && updated !== undefined) {
// A refused update leaves its request open: the answer is all it ends.
this.#updates.delete(requestId);
if (this.version === Version.DRAFT_16) this.#pushMessage(updated, typeId, size, body);
continue;
}

switch (route) {
case Route.NewRequest:
this.#admitHeld(requestId);
this.#newRequest(typeId, size, body, requestId);
break;
case Route.Response:
this.#pushMessage(requestId, typeId, size, body);
break;
case Route.ErrorResponse:
this.#pushMessage(requestId, typeId, size, body);
this.#closeStream(requestId);
break;
case Route.CloseStream:
this.#closeStream(requestId);
break;
case Route.FollowUp:
this.#pushMessage(requestId, typeId, size, body);
break;
case Route.MaxRequestId:
this.#maxRequestId = requestId;
for (const resolve of this.#maxRequestIdResolves) resolve();
this.#maxRequestIdResolves.clear();
break;
switch (route) {
case Route.NewRequest:
this.#admitHeld(requestId);
this.#newRequest(typeId, size, body, requestId);
break;
case Route.Response:
this.#pushMessage(requestId, typeId, size, body);
break;
case Route.ErrorResponse:
this.#pushMessage(requestId, typeId, size, body);
this.#closeStream(requestId);
break;
case Route.CloseStream:
this.#closeStream(requestId);
break;
case Route.FollowUp:
this.#pushMessage(requestId, typeId, size, body);
break;
case Route.MaxRequestId:
this.#maxRequestId = requestId;
for (const resolve of this.#maxRequestIdResolves) resolve();
this.#maxRequestIdResolves.clear();
break;
}
}
}
};
const stopped = this.#writer.closed.then(() => {
throw new ProtocolViolation("control stream stopped");
});
await race([read(), stopped]);
} catch (err: unknown) {
cause = error(err);
throw err;
// A closing session fails this stream too, which reports the session's error.
cause =
err instanceof ProtocolViolation || err instanceof RequestWindowError || sessionClosed(err)
? error(err)
: new ProtocolViolation(error(err).message, { cause: err });
throw cause;
} finally {
this.close(cause);
}
Expand Down
5 changes: 4 additions & 1 deletion js/net/src/ietf/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { Unsupported } from "../auth.ts";
import { SessionCode, type SessionError } from "../error.ts";
import { SessionCode, type SessionError, StreamCode, type StreamError } from "../error.ts";
import * as Path from "../path.ts";
import { Reader, Writer } from "../stream.ts";
import { AuthError, AuthMessage, AuthOk, fromSetup, intoSetup } from "./auth.ts";
Expand Down Expand Up @@ -131,6 +131,9 @@ test("AUTH_ERROR preserves actionable refusal kinds", async () => {
expect(expired.code).toBe(SessionCode.Expired);
const unauthorized = new AuthError(0x1, "bad").toError() as SessionError;
expect(unauthorized.code).toBe(SessionCode.Unauthorized);
// Past the acceptor's GOAWAY: the request is refused, not the token.
const goingAway = new AuthError(0x6, "going away").toError() as StreamError;
expect(goingAway.code).toBe(StreamCode.GoingAway);
});

test("drafts before 17 carry no AUTH", async () => {
Expand Down
15 changes: 12 additions & 3 deletions js/net/src/ietf/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Unsupported } from "../auth.ts";
import type { AuthWire, WireGrant, WireReply } from "../auth_session.ts";
import { SessionCode, SessionError } from "../error.ts";
import { ProtocolViolation, SessionCode, SessionError, StreamCode, StreamError } 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";
import { nextMessage } from "./request_stream.ts";
import { type IetfVersion, Version } from "./version.ts";

/**
Expand Down Expand Up @@ -46,6 +47,8 @@ export function intoSetup(params: SetupOptions, version: IetfVersion) {
const UNAUTHORIZED = 0x1;
const NOT_SUPPORTED = 0x3;
const EXPIRED_AUTH_TOKEN = 0x5;
/** Draft-17 assigned it, the first draft this extension negotiates on. */
const GOING_AWAY = 0x6;

/** Longest AUTH_ERROR reason, in bytes, matching the Rust decoder. */
const MAX_REASON = 8192;
Expand Down Expand Up @@ -210,6 +213,7 @@ export class AuthError {
toError(): Error {
if (this.code === NOT_SUPPORTED) return new Unsupported(this.reason);
if (this.code === EXPIRED_AUTH_TOKEN) return new SessionError(SessionCode.Expired, { reason: this.reason });
if (this.code === GOING_AWAY) return new StreamError(StreamCode.GoingAway, { reason: this.reason });
return new SessionError(SessionCode.Unauthorized, { reason: this.reason });
}
}
Expand All @@ -233,8 +237,8 @@ export class IetfAuthWire implements AuthWire {
}

async read(stream: Stream): Promise<WireReply | undefined> {
if (await stream.reader.done()) return undefined;
const id = await stream.reader.u53();
const id = await nextMessage(stream.reader, this.#version, this.#session.client);
if (id === undefined) return undefined;
switch (id) {
case AuthOk.id: {
const ok = await AuthOk.decode(stream.reader, this.#version);
Expand All @@ -256,6 +260,11 @@ export class IetfAuthWire implements AuthWire {
return msg.token;
}

async withdrawn(stream: Stream): Promise<void> {
if ((await nextMessage(stream.reader, this.#version, this.#session.client)) !== undefined)
throw new ProtocolViolation("additional message on an AUTH stream");
}

async grant(stream: Stream, grant: WireGrant): Promise<void> {
await new AuthOk(grant.publish, grant.subscribe, grant.expires).encode(stream.writer, this.#version);
}
Expand Down
Loading