From ae3c85d48f77d749f1f87c9471c9b2a021b92d2a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:05:48 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m1/token-parse-strict.md Quest-Claim: 479afac7c324b966 From 89cd394a9bb1ec713d01757ada4ed34e37693dd7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 17:21:46 -0800 Subject: [PATCH 2/4] fix(auth)!: Rust and JS run one token and key-set corpus `rs/moq-auth/tests/corpus.json` holds tokens, JWKs, key sets, and grants, each accepted with its claims or refused, and both `moq-auth` and `@moq/auth` replay it. It replaces the hand-copied interop fixtures on both sides. The cases it pins: - `exp`, `iat`, `nbf`, and the grant `expires` refuse `null` and anything but whole seconds from the epoch to the clock's last second. Rust read `"exp": null` as never expiring and saturated out-of-range times. - A key set with a duplicate `kid` is refused in both languages. - A JWK's scope root passes `path::validate` on import in both languages. Beside them, in the relay and `moq auth serve`: - The public rules detect a `jwt` query parameter form-decoded, as the server reads it, so `?%6Awt=` is refused rather than falling through. - `Lease::new` runs `Grant::validate`; an expired grant refuses as expired. - Key files are cached and re-read only when their mtime or size changes. Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/auth.md | 2 +- js/auth/MIGRATE.md | 7 + js/auth/src/claims.ts | 17 +- js/auth/src/cli.ts | 3 - js/auth/src/contract.ts | 4 +- js/auth/src/corpus.test.ts | 72 +++++++ js/auth/src/interop.test.ts | 252 +------------------------ js/auth/src/set.ts | 20 +- js/auth/src/wire.ts | 6 + quest/m1/README.md | 1 - quest/m1/token-parse-strict.md | 20 -- rs/moq-auth/MIGRATE.md | 9 + rs/moq-auth/src/claims.rs | 4 +- rs/moq-auth/src/corpus.rs | 127 +++++++++++++ rs/moq-auth/src/grant.rs | 19 +- rs/moq-auth/src/key.rs | 107 ----------- rs/moq-auth/src/lib.rs | 2 + rs/moq-auth/src/serve.rs | 124 ++++++++++-- rs/moq-auth/src/set.rs | 8 + rs/moq-auth/src/wire.rs | 34 +++- rs/moq-auth/tests/corpus.json | 336 +++++++++++++++++++++++++++++++++ rs/moq-relay/MIGRATE.md | 7 + rs/moq-relay/src/auth.rs | 48 ++++- rs/moq-relay/src/cluster.rs | 2 +- 24 files changed, 793 insertions(+), 438 deletions(-) create mode 100644 js/auth/src/corpus.test.ts delete mode 100644 quest/m1/token-parse-strict.md create mode 100644 rs/moq-auth/src/corpus.rs create mode 100644 rs/moq-auth/tests/corpus.json diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index f49142e01ef..d83407b0917 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -433,7 +433,7 @@ evaluated or refused: nothing is ignored, and no two are combined. | a JWT and a certificate | never requested | refused | A JWT is verified against `--key FILE`, `--key-dir DIR`, or `--key-set FILE` -(by `kid`, read per request so rotation needs no restart) and authorized at the +(by `kid`, re-read when the file changes so rotation needs no restart) and authorized at the dialed path, as in [Path matching](#path-matching). A bad token is refused; it never falls through to the anonymous rules. A JWT presented with a certificate is refused too, because neither can safely win, so a peer presents diff --git a/js/auth/MIGRATE.md b/js/auth/MIGRATE.md index cde9eb4cbd9..6516605e9ea 100644 --- a/js/auth/MIGRATE.md +++ b/js/auth/MIGRATE.md @@ -4,6 +4,13 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Times and key sets parse strictly, as `moq-auth` does + +`ClaimsSchema` refuses an `exp`, `iat`, or `nbf`, and `GrantSchema` an `expires`, below 0 +or above 18446744073. `KeySet.parse` refuses two keys sharing a `kid`, and `ScopeSchema` +a root `Key.sign` would refuse, such as `/demo`; give each key its own `kid` and spell +scope roots without boundary slashes. + ### Multi-prime RSA JWKs are refused (#6898) `Key.parse` rejects an RSA JWK containing `oth` instead of discarding it. diff --git a/js/auth/src/claims.ts b/js/auth/src/claims.ts index 4b3d18bd9f2..15e805e384a 100644 --- a/js/auth/src/claims.ts +++ b/js/auth/src/claims.ts @@ -8,7 +8,7 @@ import { Pattern, Patterns } from "@moq/pattern"; import * as z from "zod/mini"; import * as Path from "./path.ts"; -import { decodeGrants, decodeRoles, PatternListSchema, WireRoleSchema } from "./wire.ts"; +import { decodeGrants, decodeRoles, PatternListSchema, UnixSecondsSchema, WireRoleSchema } from "./wire.ts"; export { PatternListSchema } from "./wire.ts"; @@ -32,7 +32,7 @@ const ScopeFields = { * `root` is optional on the wire to match the Rust `moq-auth` crate, which omits it * when the scope sits at the top level. A legacy `put`/`get` prefix scope reads as the * subtree patterns it meant. A scope bounds paths, never bitrate, so a capped role is - * refused. Any other field is refused. + * refused, as is a root `Key.sign` would refuse. Any other field is refused. */ export const ScopeSchema = z .pipe( @@ -47,6 +47,13 @@ export const ScopeSchema = z z.refine((data) => (data.publish?.length ?? 0) > 0 || (data.subscribe?.length ?? 0) > 0, { message: "Either publish or subscribe must contain at least one pattern", }), + z.superRefine((data, ctx) => { + try { + Path.validate(data.root); + } catch (error) { + ctx.addIssue({ code: "custom", message: error instanceof Error ? error.message : String(error) }); + } + }), ); /** A value parsed by {@link ScopeSchema}. */ @@ -59,11 +66,11 @@ const ClaimsFields = { /** Patterns the holder may subscribe to, relative to `root`, optionally with a bitrate cap. */ subscribe: z.optional(WireRoleSchema), /** Expiration time, as a whole unix timestamp in seconds. */ - exp: z.optional(z.int()), + exp: z.optional(UnixSecondsSchema), /** Issued-at time, as a whole unix timestamp in seconds. */ - iat: z.optional(z.int()), + iat: z.optional(UnixSecondsSchema), /** Not-before time, as a whole unix timestamp in seconds. Enforced by verification. */ - nbf: z.optional(z.int()), + nbf: z.optional(UnixSecondsSchema), }; // Registered claims that narrow nothing: an issuer's bookkeeping, read and dropped. diff --git a/js/auth/src/cli.ts b/js/auth/src/cli.ts index 55b8f984ccb..0fbcdfc48ed 100755 --- a/js/auth/src/cli.ts +++ b/js/auth/src/cli.ts @@ -6,7 +6,6 @@ import { base64url } from "jose"; import type { Algorithm } from "./algorithm.ts"; import { authorize, ClaimsSchema, type Scope, ScopeSchema } from "./claims.ts"; import { Key } from "./key.ts"; -import * as Path from "./path.ts"; import { encodeGrants } from "./wire.ts"; const program = new Command(); @@ -36,8 +35,6 @@ program ...(options.publish && { publish: options.publish }), ...(options.subscribe && { subscribe: options.subscribe }), }); - // A scope root `Key.sign` would refuse could never sign anything beneath it. - Path.validate(scope.root); key = { ...key, scope }; } diff --git a/js/auth/src/contract.ts b/js/auth/src/contract.ts index 1e8b09fa2d0..e4f15f18ebf 100644 --- a/js/auth/src/contract.ts +++ b/js/auth/src/contract.ts @@ -9,7 +9,7 @@ import { Pattern } from "@moq/pattern"; import * as z from "zod/mini"; -import { decodeRoles, encodeRoles, WireRoleSchema } from "./wire.ts"; +import { decodeRoles, encodeRoles, UnixSecondsSchema, WireRoleSchema } from "./wire.ts"; const PathSchema = z.string().check( z.refine( @@ -195,7 +195,7 @@ export const GrantSchema = z /** Subtrees read from elsewhere: each path, relative to the root, resolves at the absolute path it maps to. Read-only. */ mounts: z.optional(z.record(z.string(), z.string())), /** When the session closes, as whole unix seconds. */ - expires: z.optional(z.int()), + expires: z.optional(UnixSecondsSchema), /** How long until the relay asks again, in whole seconds. Zero would be a tight loop. */ revalidate: z.optional(z.int().check(z.positive())), /** The stats tier this session's traffic is bucketed under: `/`-joined segments of ASCII letters, digits, `.`, `_`, and `-`. */ diff --git a/js/auth/src/corpus.test.ts b/js/auth/src/corpus.test.ts new file mode 100644 index 00000000000..fd6e89a7809 --- /dev/null +++ b/js/auth/src/corpus.test.ts @@ -0,0 +1,72 @@ +// Replays rs/moq-auth/tests/corpus.json, which the Rust crate replays too, so the two +// parsers cannot disagree on a token, key, key set, or grant without both failing. +import { describe, expect, test } from "bun:test"; +import * as jose from "jose"; +import { type Claims, patterns } from "./claims.ts"; +import { GrantSchema } from "./contract.ts"; +import { Key } from "./key.ts"; +import { KeySet } from "./set.ts"; + +type Case = { name: string; jwk?: unknown; jwks?: unknown; grant?: unknown; valid: boolean }; + +const corpus = (await Bun.file(new URL("../../../rs/moq-auth/tests/corpus.json", import.meta.url)).json()) as { + keys: Record; + tokens: { name: string; key: string; token?: string; payload?: jose.JWTPayload; claims: unknown }[]; + jwk: Case[]; + sets: Case[]; + grants: Case[]; +}; + +// The claims as the corpus writes them: patterns reduced, absent roles empty, and rates +// and times only when present. +function canonical(claims: Claims): unknown { + return { + root: claims.root, + publish: patterns(claims.publish).toJSON(), + subscribe: patterns(claims.subscribe).toJSON(), + ...(claims.publish_rate !== undefined && { publish_rate: claims.publish_rate }), + ...(claims.subscribe_rate !== undefined && { subscribe_rate: claims.subscribe_rate }), + ...(claims.exp !== undefined && { exp: claims.exp }), + ...(claims.iat !== undefined && { iat: claims.iat }), + ...(claims.nbf !== undefined && { nbf: claims.nbf }), + }; +} + +describe("tokens", () => { + for (const c of corpus.tokens) { + test(c.name, async () => { + const key = Key.parse(JSON.stringify(corpus.keys[c.key])); + if ((c.token === undefined) === (c.payload === undefined)) + throw new Error("give exactly one of token or payload"); + // Sign the raw payload, bypassing `Key.sign`'s own checks, so verification sees it as written. + const token = + c.token ?? + (await new jose.SignJWT(c.payload) + .setProtectedHeader({ alg: key.alg, ...(key.kid && { kid: key.kid }) }) + .sign(await jose.importJWK(key as jose.JWK, key.alg))); + const verified = Key.verify(key as Parameters[0], token); + if (c.claims === null) { + await expect(verified).rejects.toThrow(); + } else { + expect(canonical(await verified)).toEqual(c.claims); + } + }); + } +}); + +const sections: [string, Case[], (input: unknown) => unknown][] = [ + ["jwk", corpus.jwk, (jwk) => Key.parse(JSON.stringify(jwk))], + ["sets", corpus.sets, (jwks) => KeySet.parse(JSON.stringify(jwks))], + ["grants", corpus.grants, (grant) => GrantSchema.parse(grant)], +]; +for (const [section, cases, parse] of sections) { + describe(section, () => { + for (const c of cases) { + test(c.name, () => { + const run = () => parse(c.jwk ?? c.jwks ?? c.grant); + if (c.valid) run(); + else expect(run).toThrow(); + }); + } + }); +} diff --git a/js/auth/src/interop.test.ts b/js/auth/src/interop.test.ts index d9d71299694..e6326529a15 100644 --- a/js/auth/src/interop.test.ts +++ b/js/auth/src/interop.test.ts @@ -1,256 +1,8 @@ import { describe, expect, test } from "bun:test"; -import { authorize } from "./claims.ts"; import { encodeGrant, GrantSchema, RequestSchema } from "./contract.ts"; -import { Key } from "./key.ts"; -import { KeySet } from "./set.ts"; -// Tests that the JS @moq/auth package can load keys generated by the Rust -// moq-auth crate and verify tokens signed by Rust, and vice versa. - -describe("Rust-generated fixtures", () => { - // moq auth generate --out /tmp/rust-hs256.jwk - const RUST_HS256_KEY = JSON.stringify({ - alg: "HS256", - key_ops: ["verify", "sign"], - kty: "oct", - k: "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", - kid: "d55b1f13e6bda281", - }); - - // moq auth sign --key /tmp/rust-hs256.jwk --root demo --publish 'alice/**' --subscribe 'bob/**' - const RUST_HS256_TOKEN = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdY"; - - // A token scoped to the root path. Rust skips serializing an empty `root`, so the - // claims arrive with no `root` field at all. - // moq auth sign --key /tmp/rust-hs256.jwk --publish 'alice/**' --subscribe 'bob/**' - const RUST_HS256_TOKEN_EMPTY_ROOT = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJwdWJsaXNoIjpbImFsaWNlLyoqIl0sInN1YnNjcmliZSI6WyJib2IvKioiXX0.f1uYbrxNNyGg_0TkRWXHRhU0K_HKlOjeiddwHqppK-0"; - - // moq auth generate --algorithm EdDSA --out /tmp/rust-eddsa-private.jwk --public /tmp/rust-eddsa-public.jwk - const RUST_EDDSA_PRIVATE_KEY = JSON.stringify({ - alg: "EdDSA", - key_ops: ["sign", "verify"], - kty: "OKP", - crv: "Ed25519", - x: "S9n3tEG677fSNpa0Ftf46OPBte5y1pTwvm4fPkEw_Z8", - d: "EhFsuE3CFkJoQpZCT2Wwr6YF7ZwjsaEIgbwO4Uc4b5U", - kid: "dfab9113ea85dc27", - }); - - const RUST_EDDSA_PUBLIC_KEY = JSON.stringify({ - alg: "EdDSA", - key_ops: ["verify"], - kty: "OKP", - crv: "Ed25519", - x: "S9n3tEG677fSNpa0Ftf46OPBte5y1pTwvm4fPkEw_Z8", - kid: "dfab9113ea85dc27", - }); - - // moq auth sign --key /tmp/rust-eddsa-private.jwk --root room --publish 'stream1/**' --publish 'stream2/**' --subscribe 'feed1/**' - const RUST_EDDSA_TOKEN = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMS8qKiIsInN0cmVhbTIvKioiXSwic3Vic2NyaWJlIjpbImZlZWQxLyoqIl19.dpZ_p-eyKxaw6bRZp7sfKjmao04MwPuPqbu744bucPjiQVNfGZodfeyEk-fsY79fa9YSbjM1m6DPw3Wg7pzRBA"; - - test("parse Rust HS256 key", () => { - const key = Key.parse(RUST_HS256_KEY); - expect(key.alg).toBe("HS256"); - expect(key.kid).toBe("d55b1f13e6bda281"); - expect(key.kty).toBe("oct"); - }); - - test("verify Rust HS256 token", async () => { - const key = Key.parse(RUST_HS256_KEY); - const claims = await Key.verify(key, RUST_HS256_TOKEN); - expect(claims.root).toBe("demo"); - expect(claims.publish).toEqual(["alice/**"]); - expect(claims.subscribe).toEqual(["bob/**"]); - }); - - test("verify Rust HS256 token scoped to the root path", async () => { - const key = Key.parse(RUST_HS256_KEY); - const claims = await Key.verify(key, RUST_HS256_TOKEN_EMPTY_ROOT); - expect(claims.root).toBe(""); - expect(claims.publish).toEqual(["alice/**"]); - expect(claims.subscribe).toEqual(["bob/**"]); - - // An empty root grants its patterns wherever the connection lands. - const permissions = authorize(claims, "alice"); - expect(permissions.subscribe.toJSON()).toEqual([]); - expect(permissions.publish.toJSON()).toEqual(["**"]); - }); - - test("sign with Rust HS256 key and verify roundtrip", async () => { - const key = Key.parse(RUST_HS256_KEY); - const token = await Key.sign(key, { root: "js-test", publish: ["pub1/**"], subscribe: ["sub1/**"] }); - const claims = await Key.verify(key, token); - expect(claims.root).toBe("js-test"); - expect(claims.publish).toEqual(["pub1/**"]); - expect(claims.subscribe).toEqual(["sub1/**"]); - }); - - test("parse Rust EdDSA keys", () => { - const privateKey = Key.parse(RUST_EDDSA_PRIVATE_KEY); - expect(privateKey.alg).toBe("EdDSA"); - expect(privateKey.kty).toBe("OKP"); - - const publicKey = Key.parse(RUST_EDDSA_PUBLIC_KEY); - expect(publicKey.alg).toBe("EdDSA"); - expect(publicKey.kty).toBe("OKP"); - }); - - test("sign with Rust EdDSA private key and verify with public key", async () => { - const privateKey = Key.parse(RUST_EDDSA_PRIVATE_KEY); - const publicKey = Key.parse(RUST_EDDSA_PUBLIC_KEY); - const token = await Key.sign(privateKey, { root: "js-eddsa", publish: ["test/**"] }); - const claims = await Key.verify(publicKey, token); - expect(claims.root).toBe("js-eddsa"); - expect(claims.publish).toEqual(["test/**"]); - }); - - test("verify Rust EdDSA token with public key", async () => { - const key = Key.parse(RUST_EDDSA_PUBLIC_KEY); - const claims = await Key.verify(key, RUST_EDDSA_TOKEN); - expect(claims.root).toBe("room"); - expect(claims.publish).toEqual(["stream1/**", "stream2/**"]); - expect(claims.subscribe).toEqual(["feed1/**"]); - }); - - // moq auth generate --root project --publish 'live/**' --subscribe 'watch/**' --out /tmp/scoped.jwk - const RUST_SCOPED_KEY = JSON.stringify({ - alg: "HS256", - key_ops: ["verify", "sign"], - kty: "oct", - k: "pLDMYsYrp6vZLQbefVzHXN6P4wc1ADTLn4L7xzi1Qm4", - kid: "e396f70014db9fe9", - scope: { root: "project", publish: ["live/**"], subscribe: ["watch/**"] }, - }); - - // moq auth sign --key /tmp/scoped.jwk --root project --publish 'live/room/**' --subscribe 'watch/**' - const RUST_SCOPED_TOKEN = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImUzOTZmNzAwMTRkYjlmZTkifQ.eyJyb290IjoicHJvamVjdCIsInB1Ymxpc2giOlsibGl2ZS9yb29tLyoqIl0sInN1YnNjcmliZSI6WyJ3YXRjaC8qKiJdfQ.JHx-OFoj5chiC33R6zVeaeHYUPCTEYKBfCETPLwrwtc"; - - test("parse a Rust key's scope", () => { - const key = Key.parse(RUST_SCOPED_KEY); - expect(key.scope).toEqual({ root: "project", publish: ["live/**"], subscribe: ["watch/**"] }); - }); - - test("verify a Rust token signed within its key scope", async () => { - const key = Key.parse(RUST_SCOPED_KEY); - const claims = await Key.verify(key, RUST_SCOPED_TOKEN); - expect(claims.publish).toEqual(["live/room/**"]); - expect(claims.subscribe).toEqual(["watch/**"]); - }); - - test("JS enforces the same scope Rust does", async () => { - const key = Key.parse(RUST_SCOPED_KEY); - - // Rust rejects this exact pair with "token capabilities exceed the key scope": - // moq auth sign --key /tmp/scoped.jwk --root project --publish 'other/**' - await expect(Key.sign(key, { root: "project", publish: ["other/**"] })).rejects.toThrow("exceed the key scope"); - - // ...and accepts the grants Rust signed into RUST_SCOPED_TOKEN. The encoding - // is not compared: JS orders the header differently and stamps `iat`. - const token = await Key.sign(key, { root: "project", publish: ["live/room/**"], subscribe: ["watch/**"] }); - const claims = await Key.verify(key, token); - expect(claims.publish).toEqual(["live/room/**"]); - expect(claims.subscribe).toEqual(["watch/**"]); - }); -}); - -describe("Rust-generated JWK Set", () => { - // A JWK Set written by the Rust `KeySet::to_str()`, holding two HS256 keys. - // cargo run --example _fixture (see the moq-auth crate) - const RUST_JWKS = JSON.stringify({ - keys: [ - { - alg: "HS256", - key_ops: ["sign", "verify"], - kty: "oct", - k: "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", - kid: "d55b1f13e6bda281", - }, - { - alg: "HS256", - key_ops: ["verify", "sign"], - kty: "oct", - k: "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - kid: "second-key", - }, - ], - }); - - // Signed by the Rust `KeySet::sign()`, so the kid header selects the first key. - const RUST_JWKS_TOKEN = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdY"; - - test("parse a Rust JWKS", () => { - const set = KeySet.parse(RUST_JWKS); - expect(set.keys.length).toBe(2); - expect(KeySet.find(set, "d55b1f13e6bda281")?.alg).toBe("HS256"); - expect(KeySet.find(set, "second-key")?.alg).toBe("HS256"); - }); - - test("verify a Rust KeySet-signed token, selecting the key by kid", async () => { - const set = KeySet.parse(RUST_JWKS); - const claims = await KeySet.verify(set, RUST_JWKS_TOKEN); - expect(claims.root).toBe("demo"); - expect(claims.publish).toEqual(["alice/**"]); - expect(claims.subscribe).toEqual(["bob/**"]); - }); - - test("a token whose kid is absent from the Rust JWKS is rejected", async () => { - const set = KeySet.parse(RUST_JWKS); - const stranger = await Key.generate("HS256", "not-in-the-set"); - const token = await Key.sign(stranger, { root: "demo", publish: ["alice/**"] }); - await expect(KeySet.verify(set, token)).rejects.toThrow(/Cannot find key with kid not-in-the-set/); - }); -}); - -describe("wrong key rejects token", () => { - // moq auth generate --out /tmp/rust-hs256.jwk - const RUST_HS256_KEY = JSON.stringify({ - alg: "HS256", - key_ops: ["verify", "sign"], - kty: "oct", - k: "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", - kid: "d55b1f13e6bda281", - }); - - // moq auth sign --key /tmp/rust-hs256.jwk --root demo --publish 'alice/**' --subscribe 'bob/**' - const RUST_HS256_TOKEN = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdY"; - - test("different HS256 key rejects Rust token", async () => { - const wrongKey = await Key.generate("HS256"); - await expect(Key.verify(wrongKey, RUST_HS256_TOKEN)).rejects.toThrow(); - }); - - test("unrelated path is not authorized by the token", async () => { - const key = Key.parse(RUST_HS256_KEY); - const claims = await Key.verify(key, RUST_HS256_TOKEN); - expect(() => authorize(claims, "wrong-root")).toThrow(); - }); - - test("tampered token is rejected", async () => { - const key = Key.parse(RUST_HS256_KEY); - // Flip a character in the signature - const tampered = RUST_HS256_TOKEN.slice(0, -1) + (RUST_HS256_TOKEN.at(-1) === "A" ? "B" : "A"); - await expect(Key.verify(key, tampered)).rejects.toThrow(); - }); - - test("EdDSA token rejected by HS256 key", async () => { - const key = Key.parse(RUST_HS256_KEY); - // moq auth sign --key /tmp/rust-eddsa-private.jwk --root room --publish 'stream1/**' --publish 'stream2/**' --subscribe 'feed1/**' - const eddsaToken = - "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMS8qKiIsInN0cmVhbTIvKioiXSwic3Vic2NyaWJlIjpbImZlZWQxLyoqIl19.dpZ_p-eyKxaw6bRZp7sfKjmao04MwPuPqbu744bucPjiQVNfGZodfeyEk-fsY79fa9YSbjM1m6DPw3Wg7pzRBA"; - await expect(Key.verify(key, eddsaToken)).rejects.toThrow(); - }); - - test("garbage token is rejected", async () => { - const key = Key.parse(RUST_HS256_KEY); - await expect(Key.verify(key, "not-a-jwt")).rejects.toThrow(); - }); -}); +// The relay contract as the Rust moq-auth crate serializes it. Tokens, keys, and key +// sets run through the shared corpus instead; see corpus.test.ts. describe("Rust-serialized contract", () => { // The exact bytes `moq_auth::Request` and `Grant` serialize to; see diff --git a/js/auth/src/set.ts b/js/auth/src/set.ts index 13801b97baa..c3502fc37ca 100644 --- a/js/auth/src/set.ts +++ b/js/auth/src/set.ts @@ -17,10 +17,22 @@ import { Key, KeySchema, type PublicKey } from "./key.ts"; * * See {@link https://datatracker.ietf.org/doc/html/rfc7517#section-5 | RFC 7517, section 5}. */ -export const KeySetSchema = z.object({ - /** The keys in the set, in preference order. */ - keys: z.array(KeySchema), -}); +export const KeySetSchema = z + .object({ + /** The keys in the set, in preference order. */ + keys: z.array(KeySchema), + }) + .check( + // A token selects its key by `kid`, so two keys sharing one would make the + // verifying key depend on the order the set happens to list them in. + z.refine( + ({ keys }) => { + const kids = keys.flatMap((key) => (key.kid === undefined ? [] : [key.kid])); + return new Set(kids).size === kids.length; + }, + { message: "duplicate kid" }, + ), + ); /** A set of keys, each optionally identified by a `kid`. */ export type KeySet = z.infer; diff --git a/js/auth/src/wire.ts b/js/auth/src/wire.ts index b5f3285422c..6fbe6c79134 100644 --- a/js/auth/src/wire.ts +++ b/js/auth/src/wire.ts @@ -30,6 +30,12 @@ export const PatternListSchema = z.array( ), ); +/** + * A time in whole unix seconds, from the epoch to the last second the Rust crate's clock + * holds. Anything else is refused, `null` included, so both languages read the same times. + */ +export const UnixSecondsSchema = z.int().check(z.gte(0), z.lte(18_446_744_073)); + // At most 2^53 - 1, so every JSON reader holds the cap exactly. const RateSchema = z.number().check( z.refine((bps) => Number.isSafeInteger(bps) && bps > 0, { diff --git a/quest/m1/README.md b/quest/m1/README.md index 897602cde36..517141d815f 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -35,7 +35,6 @@ moves what remains of them once their claims merge. - [Drop set](/quest/m1/drop-set.md) - the drop set's track and cursor runs are billed to the cache pool, so fragmenting drops costs what the pool holds; eviction folds final runs or ends the cursor with `TooFarBehind` - [Only the relay's own stats publisher uses the stats prefix](/quest/m1/stats-prefix.md) - a broad grant can no longer publish unmetered media or spoof node stats under `.stats` - [js/net and moq-net decode the same inputs the same way](/quest/m1/js-net-input-checks.md) - a shared input corpus pins both decoders, and a typed endpoint stops loopback spellings from slipping past GOAWAY checks -- [Rust and JS run one token and key-set corpus](/quest/m1/token-parse-strict.md) - a shared corpus pins both parsers, so a null `exp` no longer means never-expiring in Rust - [Resetting a kio sleep to its deadline does not wake it](/quest/m1/kio-reset-same-deadline.md) - a caller that resets on every poll no longer spins the simulator - [A refused lite SUBSCRIBE_UPDATE ends the subscription](/quest/m1/lite-update-refused.md) - the publisher no longer serves a removed range or reports the wrong start after the model refuses an update - [A cluster peer's identity is a type, not a string](/quest/m1/cluster-peer-identity.md) - a canonical `PeerKey` and a refused duplicate cluster id stop self-dials, double links, and silent mesh partitions diff --git a/quest/m1/token-parse-strict.md b/quest/m1/token-parse-strict.md deleted file mode 100644 index 8aeea378630..00000000000 --- a/quest/m1/token-parse-strict.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] Rust and JS run one token and key-set corpus - -## Goal - -`rs/moq-auth` and `js/auth` load one shared corpus of tokens, key sets and JWKs, each marked accepted (with the resulting grant) or refused, so a parser difference fails a test on both sides. The cases this audit found pass: - -- `exp`, `iat` and `nbf` refuse `null` and values outside the range both languages represent. Rust reads `"exp": null` as no expiry, a token that never expires, while JS refuses it; the Grant wire `expires` has the same gap. -- A key set with a duplicate `kid` is refused in both languages instead of the first match winning. -- A JWK's scope root passes `path::validate` on import in both languages, as the `with_scope` doc promises. - -Two relay-side gaps found beside them are fixed in the same PR: - -- The relay detects a token the way the server parses one (`presents_token` vs the form-decoded `jwt`), so `?%6Awt=` cannot fall through to the public grant, and key sources are cached rather than re-read per token. -- `Lease::new` (`rs/moq-relay/src/auth.rs`) runs `Grant::validate`. - -## Plan - -Found by the 2026-11-07 invariant audit (auth-2, auth-4, auth-9, auth-11, auth-12). - -Decided 2026-11-07 (maintainer, invariant-audit root causes): the cause is two hand-written parsers checked only by JS loading a few Rust-signed fixtures (`js/auth/src/interop.test.ts`). The corpus replaces those fixtures and runs in both directions. diff --git a/rs/moq-auth/MIGRATE.md b/rs/moq-auth/MIGRATE.md index 7dbb2b0b915..19c5c9ee57f 100644 --- a/rs/moq-auth/MIGRATE.md +++ b/rs/moq-auth/MIGRATE.md @@ -4,6 +4,15 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Times and key sets parse strictly, as `@moq/auth` does + +A token's `exp`, `iat`, and `nbf`, and a grant's `expires`, refuse `null` and anything +but whole seconds from 0 to 18446744073: `"exp": null` no longer reads as never +expiring, and a negative or later time is refused rather than clamped. Omit a field to +leave it unset. A `KeySet` refuses two keys sharing a `kid`, and a JWK refuses a scope +whose root `path::validate` refuses, such as `/demo`; give each key its own `kid` and +spell scope roots without boundary slashes. + ### Shared unsupported CPU error `Error::UnsupportedCpu` now carries `moq_crypto::cpu::UnsupportedCpu` instead of a `String`. Use its `Display` implementation for the missing feature message. Core builds still need no rustls. diff --git a/rs/moq-auth/src/claims.rs b/rs/moq-auth/src/claims.rs index f16f32017fb..79ffa9bd9b4 100644 --- a/rs/moq-auth/src/claims.rs +++ b/rs/moq-auth/src/claims.rs @@ -29,8 +29,10 @@ pub struct Scope { } impl Scope { - /// Returns an error when the scope permits nothing, making the key unusable. + /// Returns an error when the scope permits nothing, making the key unusable, or names + /// a root [`Key::sign`](crate::Key::sign) would refuse. pub fn validate(&self) -> crate::Result<()> { + path::validate(&self.root)?; if self.publish.is_empty() && self.subscribe.is_empty() { return Err(crate::Error::UselessScope); } diff --git a/rs/moq-auth/src/corpus.rs b/rs/moq-auth/src/corpus.rs new file mode 100644 index 00000000000..e8c842e1c4e --- /dev/null +++ b/rs/moq-auth/src/corpus.rs @@ -0,0 +1,127 @@ +//! Replays `tests/corpus.json`, which `js/auth/src/corpus.test.ts` replays too, so the +//! two parsers cannot disagree on a token, key, key set, or grant without both failing. + +use crate::{Claims, Grant, Key, KeySet}; +use kio::clock::Wall; +use serde::Deserialize; +use serde_json::{Value, json}; +use std::collections::BTreeMap; +use std::str::FromStr; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Corpus { + keys: BTreeMap, + tokens: Vec, + jwk: Vec, + sets: Vec, + grants: Vec, +} + +/// A token, given signed or as a payload the case's key signs here, and the claims it +/// verifies to, or `null` when it is refused. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct TokenCase { + name: String, + key: String, + token: Option, + payload: Option, + claims: Option, +} + +/// A document under `jwk`, `jwks`, or `grant`, and whether it is accepted. +#[derive(Deserialize)] +struct Case { + name: String, + #[serde(alias = "jwk", alias = "jwks", alias = "grant")] + input: Value, + valid: bool, +} + +fn corpus() -> Corpus { + serde_json::from_str(include_str!("../tests/corpus.json")).unwrap() +} + +/// The claims as the corpus writes them: patterns reduced, absent roles empty, and +/// rates and times only when present. +fn canonical(claims: &Claims) -> Value { + let seconds = |wall: Wall| wall.checked_duration_since(Wall::UNIX_EPOCH).unwrap().as_secs(); + let mut out = json!({ + "root": claims.root, + "publish": claims.publish, + "subscribe": claims.subscribe, + }); + let fields = [ + ("publish_rate", claims.publish_rate.map(u64::from)), + ("subscribe_rate", claims.subscribe_rate.map(u64::from)), + ("exp", claims.expires.map(seconds)), + ("iat", claims.issued.map(seconds)), + ("nbf", claims.not_before.map(seconds)), + ]; + for (field, value) in fields { + if let Some(value) = value { + out[field] = value.into(); + } + } + out +} + +#[test] +fn tokens() { + let corpus = corpus(); + let keys: BTreeMap<_, _> = corpus + .keys + .into_iter() + .map(|(name, jwk)| (name, serde_json::from_value::(jwk).unwrap())) + .collect(); + + for case in corpus.tokens { + let key = &keys[&case.key]; + let token = match (case.token, case.payload) { + (Some(token), None) => token, + (None, Some(payload)) => { + let mut header = jsonwebtoken::Header::new(key.algorithm.into()); + header.kid = key.kid.as_ref().map(ToString::to_string); + jsonwebtoken::encode(&header, &payload, key.to_encoding_key().unwrap()).unwrap() + } + _ => panic!("{}: give exactly one of token or payload", case.name), + }; + let verified = key.verify(&token); + match (&case.claims, verified) { + (Some(expected), Ok(claims)) => assert_eq!(&canonical(&claims), expected, "{}", case.name), + (None, Err(_)) => {} + (Some(_), Err(err)) => panic!("{}: refused: {err}", case.name), + (None, Ok(claims)) => panic!("{}: accepted as {}", case.name, canonical(&claims)), + } + } +} + +#[test] +fn documents() { + let corpus = corpus(); + type Parse = fn(Value) -> Result<(), String>; + let parsers: [(&str, Vec, Parse); 3] = [ + ("jwk", corpus.jwk, |jwk| { + Key::from_str(&jwk.to_string()).map(drop).map_err(|err| err.to_string()) + }), + ("sets", corpus.sets, |jwks| { + KeySet::from_str(&jwks.to_string()) + .map(drop) + .map_err(|err| err.to_string()) + }), + ("grants", corpus.grants, |grant| { + let grant: Grant = serde_json::from_value(grant).map_err(|err| err.to_string())?; + grant.validate().map_err(|err| err.to_string()) + }), + ]; + for (section, cases, parse) in parsers { + for case in cases { + match (case.valid, parse(case.input)) { + (true, Err(err)) => panic!("{section}/{}: refused: {err}", case.name), + (false, Ok(())) => panic!("{section}/{}: accepted", case.name), + _ => {} + } + } + } +} diff --git a/rs/moq-auth/src/grant.rs b/rs/moq-auth/src/grant.rs index 7c461bcd754..4871aefe104 100644 --- a/rs/moq-auth/src/grant.rs +++ b/rs/moq-auth/src/grant.rs @@ -269,18 +269,15 @@ mod tests { ); } + /// The clock's last time round-trips, and the furthest expiry the wire carries does + /// not overflow the instant. #[kio::test] - async fn expiry_saturates_to_the_wall_range() { - let parse = |expires: i64| { - serde_json::from_value::(serde_json::json!({"publish":["**"],"expires":expires})).unwrap() - }; - let past = parse(-1); - assert_eq!(past.expires, Some(Wall::UNIX_EPOCH)); - assert!(matches!(past.validate(), Err(crate::Error::GrantExpired))); - - // The furthest expiry the wire carries must not overflow the instant. - let far = parse(i64::MAX); - assert_eq!(far.expires, Some(Wall::UNIX_EPOCH.saturating_add(Duration::MAX))); + async fn the_last_expiry_fits_the_clock() { + let mut last = Grant::new(patterns(&["**"]), Patterns::default()); + last.expires = Some(Wall::UNIX_EPOCH.saturating_add(Duration::MAX)); + let json = serde_json::to_value(&last).unwrap(); + assert_eq!(json["expires"], 18_446_744_073u64); + let far: Grant = serde_json::from_value(json).unwrap(); far.validate().unwrap(); far.deadline(); } diff --git a/rs/moq-auth/src/key.rs b/rs/moq-auth/src/key.rs index cc6521c8d7f..8d02e1a11ab 100644 --- a/rs/moq-auth/src/key.rs +++ b/rs/moq-auth/src/key.rs @@ -670,7 +670,6 @@ impl Key { /// The scope is validated here, and it is the only way to set one, so a key can never carry a /// scope that permits nothing, nor a root [`sign`](Self::sign) would refuse. pub fn with_scope(mut self, scope: crate::Scope) -> crate::Result { - crate::path::validate(&scope.root)?; scope.validate()?; self.jwk.scope = Some(scope); Ok(self) @@ -1819,55 +1818,6 @@ mod tests { } } - // Tests that Rust can load keys generated by the JS @moq/auth package - // and verify tokens signed by JS. - // - // Generated with: bun -e 'import { generate } from "./js/auth/src/generate.ts"; ...' - // See js/auth/src/interop.test.ts for the JS-side counterpart. - - /// JS-generated HS256 key (from @moq/auth generate("HS256", "js-test-key")) - const JS_HS256_KEY: &str = r#"{"kty":"oct","alg":"HS256","k":"xm6xsSkfFqzPU3KfcbAcF2_h0OkStxQ_nNqVPYl0ync","kid":"js-test-key","key_ops":["sign","verify"],"guest":[],"guest_sub":[],"guest_pub":[]}"#; - - /// JS-generated HS256 token (from @moq/auth sign(key, {root:"live", publish:["camera1"], subscribe:["camera1","camera2"]})) - const JS_HS256_TOKEN: &str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1Ymxpc2giOlsiY2FtZXJhMSJdLCJzdWJzY3JpYmUiOlsiY2FtZXJhMSIsImNhbWVyYTIiXSwiaWF0IjoxNzc1MTc2NzU0fQ.DxrRkpYDd7Cc215qstY4RfnB7hRvc8RG61YWO4UwyWg"; - - /// A token @moq/token minted before patterns, with `put` and `get` prefix lists. - const JS_HS256_LEGACY_TOKEN: &str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1dCI6WyJjYW1lcmExIl0sImdldCI6WyJjYW1lcmExIiwiY2FtZXJhMiJdLCJpYXQiOjE3NzUxNzY3NTR9.tHNQtHh_HCIKxXOexDCM7AkjqWzbULLZzjEckfOGRfY"; - - /// JS-generated EdDSA private key (from @moq/auth generate("EdDSA", "js-eddsa-key")) - const JS_EDDSA_PRIVATE_KEY: &str = r#"{"kty":"OKP","alg":"EdDSA","crv":"Ed25519","x":"UiU9fT_SdBBpkFtJPRCY0gX1jK_Dr9syYLFuEz4QUM4","d":"lm-L_PV3ksuQ-KrFBgFMDJqAZC3_Z6Z5UC4ZQY5OoDQ","kid":"js-eddsa-key","key_ops":["sign","verify"],"guest":[],"guest_sub":[],"guest_pub":[]}"#; - - /// JS-generated EdDSA public key (from @moq/auth toPublicKey(key)) - const JS_EDDSA_PUBLIC_KEY: &str = r#"{"kty":"OKP","alg":"EdDSA","crv":"Ed25519","x":"UiU9fT_SdBBpkFtJPRCY0gX1jK_Dr9syYLFuEz4QUM4","kid":"js-eddsa-key","guest":[],"guest_sub":[],"guest_pub":[],"key_ops":["verify"]}"#; - - /// JS-generated EdDSA token (from @moq/auth sign(key, {root:"stream", publish:["video/**"]})) - const JS_EDDSA_TOKEN: &str = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsImtpZCI6ImpzLWVkZHNhLWtleSJ9.eyJyb290Ijoic3RyZWFtIiwicHVibGlzaCI6WyJ2aWRlby8qKiJdLCJpYXQiOjE3NzUxNzY3NTZ9.Vq00rznemlwxsGLdAR8EJs1J8cgXhaiuEiCysmCxnWbDemIFxS6kTNv3kp6LKnokp-mpHiRpO24Nv7b47jcIAQ"; - - #[test] - fn test_js_hs256_key_load() { - let key = Key::from_str(JS_HS256_KEY).unwrap(); - assert_eq!(key.algorithm, Algorithm::HS256); - assert_eq!(key.kid, Some(crate::KeyId::decode("js-test-key").unwrap())); - } - - #[test] - fn test_js_hs256_token_verify() { - let key = Key::from_str(JS_HS256_KEY).unwrap(); - let claims = key.verify(JS_HS256_TOKEN).unwrap(); - assert_eq!(claims.root, "live"); - assert_eq!(claims.publish, patterns(&["camera1"])); - assert_eq!(claims.subscribe, patterns(&["camera1", "camera2"])); - } - - #[test] - fn test_js_legacy_prefix_token_verifies_as_subtrees() { - let key = Key::from_str(JS_HS256_KEY).unwrap(); - let claims = key.verify(JS_HS256_LEGACY_TOKEN).unwrap(); - assert_eq!(claims.root, "live"); - assert_eq!(claims.publish, patterns(&["camera1/**"])); - assert_eq!(claims.subscribe, patterns(&["camera1/**", "camera2/**"])); - } - #[test] fn test_legacy_scoped_key_signs_within_its_prefixes() { // A key minted by moq-token-cli with `--root demo --put room`. @@ -1896,63 +1846,6 @@ mod tests { ); } - #[test] - fn test_js_hs256_sign_and_roundtrip() { - let key = Key::from_str(JS_HS256_KEY).unwrap(); - let claims = Claims { - root: "rust-test".to_string(), - publish: patterns(&["pub1/**"]), - subscribe: patterns(&["sub1/**"]), - ..Default::default() - }; - let token = key.sign(&claims).unwrap(); - let verified = key.verify(&token).unwrap(); - assert_eq!(verified.root, "rust-test"); - assert_eq!(verified.publish, patterns(&["pub1/**"])); - } - - #[test] - fn test_js_eddsa_key_load() { - let private_key = Key::from_str(JS_EDDSA_PRIVATE_KEY).unwrap(); - assert_eq!(private_key.algorithm, Algorithm::EdDSA); - assert!(matches!(private_key.material, KeyMaterial::OKP { .. })); - - let public_key = Key::from_str(JS_EDDSA_PUBLIC_KEY).unwrap(); - assert_eq!(public_key.algorithm, Algorithm::EdDSA); - } - - #[test] - fn test_js_eddsa_token_verify_with_private_key() { - let key = Key::from_str(JS_EDDSA_PRIVATE_KEY).unwrap(); - let claims = key.verify(JS_EDDSA_TOKEN).unwrap(); - assert_eq!(claims.root, "stream"); - assert_eq!(claims.publish, patterns(&["video/**"])); - } - - #[test] - fn test_js_eddsa_token_verify_with_public_key() { - let key = Key::from_str(JS_EDDSA_PUBLIC_KEY).unwrap(); - let claims = key.verify(JS_EDDSA_TOKEN).unwrap(); - assert_eq!(claims.root, "stream"); - assert_eq!(claims.publish, patterns(&["video/**"])); - } - - #[test] - fn test_js_token_wrong_key_fails() { - // Generate a different HS256 key - let wrong_key = Key::generate(Algorithm::HS256, None).unwrap(); - let result = wrong_key.verify(JS_HS256_TOKEN); - assert!(result.is_err()); - } - - #[test] - fn test_js_eddsa_token_wrong_key_fails() { - // Try verifying EdDSA token with the HS256 key - let wrong_key = Key::from_str(JS_HS256_KEY).unwrap(); - let result = wrong_key.verify(JS_EDDSA_TOKEN); - assert!(result.is_err()); - } - #[test] fn test_file_io_base64url() { let key = create_test_key(); diff --git a/rs/moq-auth/src/lib.rs b/rs/moq-auth/src/lib.rs index 8a8d4ba3676..ed0557cd0ef 100644 --- a/rs/moq-auth/src/lib.rs +++ b/rs/moq-auth/src/lib.rs @@ -27,6 +27,8 @@ mod algorithm; mod claims; +#[cfg(test)] +mod corpus; mod crypto; mod error; mod fs; diff --git a/rs/moq-auth/src/serve.rs b/rs/moq-auth/src/serve.rs index 985151dc9b1..dfd4d58176f 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -10,7 +10,7 @@ use std::borrow::Cow; use std::collections::{BTreeMap, HashMap}; use std::net::IpAddr; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -23,8 +23,9 @@ use kio::clock::Instant; use crate::{Event, Grant, Key, KeyId, KeySet, Patterns, Permissions, Rate, Request, Token}; -/// Where the signing keys a `jwt` is verified against come from. Read per request, -/// so a rotated file takes effect without a restart. +/// Where the signing keys a `jwt` is verified against come from. Each file is parsed +/// once and re-read when its modification time or size changes, so a rotated file +/// takes effect without a restart. #[derive(Clone, Debug)] #[non_exhaustive] pub enum Keys { @@ -229,6 +230,65 @@ pub struct Policy { pub expires: Option, /// Live session caps. pub limits: Limits, + /// The key files as last parsed. + cache: KeyCache, +} + +/// Parsed key files, keyed by path, so a token costs a `stat` rather than a read and parse. +#[derive(Clone, Default)] +struct KeyCache(Arc>>); + +struct Cached { + stamp: (kio::clock::Wall, u64), + keys: Arc, +} + +impl std::fmt::Debug for KeyCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("KeyCache") + } +} + +impl KeyCache { + /// The keys in `path`, parsed again only when its modification time or size changed + /// since they were cached. A file that is gone forgets its keys. + async fn load(&self, path: &Path, parse: fn(&str) -> Result) -> Result, Denial> { + let lock = || self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Ok(meta) = tokio::fs::metadata(path).await else { + lock().remove(path); + return Err(Denial::UnknownKey); + }; + // Stamped before reading, so a write racing the read is caught at the next token. + let modified = meta.modified().ok().and_then(|modified| modified.try_into().ok()); + let stamp = modified.map(|modified| (modified, meta.len())); + if let Some(cached) = lock().get(path) + && Some(cached.stamp) == stamp + { + return Ok(cached.keys.clone()); + } + let json = tokio::fs::read_to_string(path).await.map_err(|_| Denial::UnknownKey)?; + let keys = Arc::new(parse(&json)?); + // A file without a modification time this clock holds parses every time. + if let Some(stamp) = stamp { + lock().insert( + path.to_owned(), + Cached { + stamp, + keys: keys.clone(), + }, + ); + } + Ok(keys) + } +} + +/// One key file as a set holding just that key, which selects it by `kid` exactly as +/// verifying with the key alone would. +fn parse_key(json: &str) -> Result { + let key = json.parse::().map_err(|_| Denial::UnknownKey)?; + Ok(KeySet { + keys: vec![Arc::new(key)], + }) } /// Why the policy denied a session, mapped to the typed [`Refusal`](crate::Refusal) in the 403 body. @@ -415,27 +475,23 @@ impl Policy { // Every key source reads the header first, so a token that is not a JWT fails here // whichever one is configured. let header = jsonwebtoken::decode_header(jwt).map_err(|err| Denial::MalformedToken(err.to_string()))?; - let claims = match keys { - Keys::File(path) => Key::from_file_async(path) - .await - .map_err(|_| Denial::UnknownKey)? - .verify_claims(jwt), + let set = match keys { + Keys::File(path) => self.cache.load(path, parse_key).await?, Keys::Dir(dir) => { let kid = header.kid.ok_or(Denial::MissingKeyId)?; let kid = KeyId::decode(&kid).map_err(|_| Denial::UnknownKey)?; - Key::from_file_async(dir.join(format!("{kid}.jwk"))) - .await - .map_err(|_| Denial::UnknownKey)? - .verify_claims(jwt) + self.cache.load(&dir.join(format!("{kid}.jwk")), parse_key).await? } Keys::Set(path) => { - let json = tokio::fs::read_to_string(path).await.map_err(|_| Denial::UnknownKey)?; - json.parse::() - .map_err(|err| Denial::InvalidToken(err.to_string()))? - .key(jwt) - .and_then(|key| key.verify_claims(jwt)) + self.cache + .load(path, |json| { + json.parse::() + .map_err(|err| Denial::InvalidToken(err.to_string())) + }) + .await? } }; + let claims = set.key(jwt).and_then(|key| key.verify_claims(jwt)); let claims = claims.map_err(|err| match err { crate::Error::Key(crate::KeyError::MissingKid) => Denial::MissingKeyId, crate::Error::Key(crate::KeyError::KeyNotFound(_)) => Denial::UnknownKey, @@ -1036,6 +1092,40 @@ mod tests { assert!(policy.decide(&request("/demo")).await.is_ok()); } + /// A key file is parsed once and re-read only when it changes, so rotating it still + /// takes effect without a restart. + #[tokio::test] + async fn a_key_file_is_cached_until_it_changes() { + let (dir, key) = key_dir(); + let path = dir.path().join("kid1.jwk"); + let policy = Policy { + keys: Some(Keys::Dir(dir.path().into())), + ..Default::default() + }; + let old = with_token(request("/demo"), &sign(&key, "demo", &["**"], &[], None)); + policy.decide(&old).await.unwrap(); + + // Rewritten at the same size and modification time, the cached key still answers. + let before = std::fs::metadata(&path).unwrap(); + let rotated = Key::generate(Algorithm::HS256, Some(KeyId::decode("kid1").unwrap())).unwrap(); + rotated.to_file(&path).unwrap(); + let file = std::fs::File::options().write(true).open(&path).unwrap(); + assert_eq!(file.metadata().unwrap().len(), before.len()); + file.set_modified(before.modified().unwrap()).unwrap(); + policy.decide(&old).await.unwrap(); + + // A new modification time reads the rotated key. + file.set_modified(before.modified().unwrap() + Duration::from_secs(1)) + .unwrap(); + assert!(matches!(policy.decide(&old).await, Err(Denial::InvalidToken(_)))); + let new = with_token(request("/demo"), &sign(&rotated, "demo", &["**"], &[], None)); + policy.decide(&new).await.unwrap(); + + // A removed file forgets its key. + std::fs::remove_file(&path).unwrap(); + assert_eq!(policy.decide(&new).await.unwrap_err(), Denial::UnknownKey); + } + #[tokio::test] async fn a_single_key_file_checks_the_kid() { let dir = tempfile::tempdir().unwrap(); diff --git a/rs/moq-auth/src/set.rs b/rs/moq-auth/src/set.rs index 86f83d75943..1b4b81fd25d 100644 --- a/rs/moq-auth/src/set.rs +++ b/rs/moq-auth/src/set.rs @@ -38,6 +38,14 @@ impl<'de> Deserialize<'de> for KeySet { } let raw = RawKeySet::deserialize(deserializer)?; + // A token selects its key by `kid`, so two keys sharing one would make the + // verifying key depend on the order the set happens to list them in. + let mut kids = std::collections::HashSet::new(); + for kid in raw.keys.iter().filter_map(|key| key.kid.as_ref()) { + if !kids.insert(kid.encode()) { + return Err(serde::de::Error::custom(format!("duplicate kid {kid}"))); + } + } Ok(KeySet { keys: raw.keys.into_iter().map(Arc::new).collect(), }) diff --git a/rs/moq-auth/src/wire.rs b/rs/moq-auth/src/wire.rs index 1a0e72de723..6beae3ceba5 100644 --- a/rs/moq-auth/src/wire.rs +++ b/rs/moq-auth/src/wire.rs @@ -20,7 +20,6 @@ use std::time::Duration; /// The wire form of [`Claims`](crate::Claims). #[serde_with::skip_serializing_none] -#[serde_as] #[derive(Serialize, Deserialize, Default)] #[serde(default, deny_unknown_fields)] pub(crate) struct Claims { @@ -34,11 +33,11 @@ pub(crate) struct Claims { publish: Option, #[serde(deserialize_with = "present")] subscribe: Option, - #[serde_as(as = "Option")] + #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] exp: Option, - #[serde_as(as = "Option")] + #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] iat: Option, - #[serde_as(as = "Option")] + #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] nbf: Option, // Registered claims that narrow nothing: an issuer's bookkeeping, read and dropped. // Every other claim is refused, since an unknown one may narrow the grant, and a @@ -171,6 +170,31 @@ fn present<'de, D: Deserializer<'de>, T: Deserialize<'de>>(deserializer: D) -> R T::deserialize(deserializer).map(Some) } +/// A present time in whole unix seconds, from the epoch to the clock's last second, which +/// `@moq/auth` holds exactly too. Anything else is refused rather than saturated, and an +/// explicit `null` rather than read as absent, which for `exp` would mean never. +mod seconds { + use kio::clock::Wall; + use serde::{Deserialize, Deserializer, Serializer, de::Error}; + + /// Truncated, so the clock's last time writes as a second this reads back, and an + /// expiry never moves later. + pub(super) fn serialize(wall: &Option, serializer: S) -> Result { + match wall { + Some(wall) => serializer.serialize_u64(wall.saturating_duration_since(Wall::UNIX_EPOCH).as_secs()), + None => serializer.serialize_none(), + } + } + + pub(super) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + let seconds = u64::deserialize(deserializer)?; + Wall::UNIX_EPOCH + .checked_add(std::time::Duration::from_secs(seconds)) + .map(Some) + .ok_or_else(|| D::Error::custom(format!("{seconds} is past the last representable time"))) + } +} + /// The grant fields shared by both documents, in whichever encoding they arrived. struct Grants { put: Option>, @@ -307,7 +331,7 @@ pub(crate) struct Grant { root: Option, #[serde(skip_serializing_if = "BTreeMap::is_empty")] mounts: BTreeMap, - #[serde_as(as = "Option")] + #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] expires: Option, #[serde_as(as = "Option>")] revalidate: Option, diff --git a/rs/moq-auth/tests/corpus.json b/rs/moq-auth/tests/corpus.json new file mode 100644 index 00000000000..c5e274a10b1 --- /dev/null +++ b/rs/moq-auth/tests/corpus.json @@ -0,0 +1,336 @@ +{ + "keys": { + "hs256": { + "alg": "HS256", + "key_ops": ["verify", "sign"], + "kty": "oct", + "k": "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", + "kid": "d55b1f13e6bda281" + }, + "eddsa": { + "alg": "EdDSA", + "key_ops": ["verify"], + "kty": "OKP", + "crv": "Ed25519", + "x": "S9n3tEG677fSNpa0Ftf46OPBte5y1pTwvm4fPkEw_Z8", + "kid": "dfab9113ea85dc27" + }, + "js_hs256": { + "kty": "oct", + "alg": "HS256", + "k": "xm6xsSkfFqzPU3KfcbAcF2_h0OkStxQ_nNqVPYl0ync", + "kid": "js-test-key", + "key_ops": ["sign", "verify"], + "guest": [], + "guest_sub": [], + "guest_pub": [] + }, + "js_eddsa": { + "kty": "OKP", + "alg": "EdDSA", + "crv": "Ed25519", + "x": "UiU9fT_SdBBpkFtJPRCY0gX1jK_Dr9syYLFuEz4QUM4", + "kid": "js-eddsa-key", + "key_ops": ["verify"] + }, + "scoped": { + "alg": "HS256", + "key_ops": ["verify", "sign"], + "kty": "oct", + "k": "pLDMYsYrp6vZLQbefVzHXN6P4wc1ADTLn4L7xzi1Qm4", + "kid": "e396f70014db9fe9", + "scope": { "root": "project", "publish": ["live/**"], "subscribe": ["watch/**"] } + } + }, + "tokens": [ + { + "name": "rust_hs256", + "key": "hs256", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdY", + "claims": { "root": "demo", "publish": ["alice/**"], "subscribe": ["bob/**"] } + }, + { + "name": "rust_hs256_empty_root", + "key": "hs256", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJwdWJsaXNoIjpbImFsaWNlLyoqIl0sInN1YnNjcmliZSI6WyJib2IvKioiXX0.f1uYbrxNNyGg_0TkRWXHRhU0K_HKlOjeiddwHqppK-0", + "claims": { "root": "", "publish": ["alice/**"], "subscribe": ["bob/**"] } + }, + { + "name": "rust_eddsa", + "key": "eddsa", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMS8qKiIsInN0cmVhbTIvKioiXSwic3Vic2NyaWJlIjpbImZlZWQxLyoqIl19.dpZ_p-eyKxaw6bRZp7sfKjmao04MwPuPqbu744bucPjiQVNfGZodfeyEk-fsY79fa9YSbjM1m6DPw3Wg7pzRBA", + "claims": { "root": "room", "publish": ["stream1/**", "stream2/**"], "subscribe": ["feed1/**"] } + }, + { + "name": "rust_scoped", + "key": "scoped", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImUzOTZmNzAwMTRkYjlmZTkifQ.eyJyb290IjoicHJvamVjdCIsInB1Ymxpc2giOlsibGl2ZS9yb29tLyoqIl0sInN1YnNjcmliZSI6WyJ3YXRjaC8qKiJdfQ.JHx-OFoj5chiC33R6zVeaeHYUPCTEYKBfCETPLwrwtc", + "claims": { "root": "project", "publish": ["live/room/**"], "subscribe": ["watch/**"] } + }, + { + "name": "js_hs256", + "key": "js_hs256", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1Ymxpc2giOlsiY2FtZXJhMSJdLCJzdWJzY3JpYmUiOlsiY2FtZXJhMSIsImNhbWVyYTIiXSwiaWF0IjoxNzc1MTc2NzU0fQ.DxrRkpYDd7Cc215qstY4RfnB7hRvc8RG61YWO4UwyWg", + "claims": { "root": "live", "publish": ["camera1"], "subscribe": ["camera1", "camera2"], "iat": 1775176754 } + }, + { + "name": "js_legacy_prefixes", + "key": "js_hs256", + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1dCI6WyJjYW1lcmExIl0sImdldCI6WyJjYW1lcmExIiwiY2FtZXJhMiJdLCJpYXQiOjE3NzUxNzY3NTR9.tHNQtHh_HCIKxXOexDCM7AkjqWzbULLZzjEckfOGRfY", + "claims": { + "root": "live", + "publish": ["camera1/**"], + "subscribe": ["camera1/**", "camera2/**"], + "iat": 1775176754 + } + }, + { + "name": "js_eddsa", + "key": "js_eddsa", + "token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsImtpZCI6ImpzLWVkZHNhLWtleSJ9.eyJyb290Ijoic3RyZWFtIiwicHVibGlzaCI6WyJ2aWRlby8qKiJdLCJpYXQiOjE3NzUxNzY3NTZ9.Vq00rznemlwxsGLdAR8EJs1J8cgXhaiuEiCysmCxnWbDemIFxS6kTNv3kp6LKnokp-mpHiRpO24Nv7b47jcIAQ", + "claims": { "root": "stream", "publish": ["video/**"], "subscribe": [], "iat": 1775176756 } + }, + { + "name": "eddsa_token_under_an_hs256_key", + "key": "hs256", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMS8qKiIsInN0cmVhbTIvKioiXSwic3Vic2NyaWJlIjpbImZlZWQxLyoqIl19.dpZ_p-eyKxaw6bRZp7sfKjmao04MwPuPqbu744bucPjiQVNfGZodfeyEk-fsY79fa9YSbjM1m6DPw3Wg7pzRBA", + "claims": null + }, + { + "name": "tampered_signature", + "key": "hs256", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdA", + "claims": null + }, + { "name": "not_a_jwt", "key": "hs256", "token": "not-a-jwt", "claims": null }, + { + "name": "times_at_the_range_bounds", + "key": "hs256", + "payload": { "root": "demo", "publish": ["**"], "exp": 18446744073, "iat": 0, "nbf": 0 }, + "claims": { "root": "demo", "publish": ["**"], "subscribe": [], "exp": 18446744073, "iat": 0, "nbf": 0 } + }, + { "name": "null_exp", "key": "hs256", "payload": { "publish": ["**"], "exp": null }, "claims": null }, + { "name": "null_iat", "key": "hs256", "payload": { "publish": ["**"], "iat": null }, "claims": null }, + { "name": "null_nbf", "key": "hs256", "payload": { "publish": ["**"], "nbf": null }, "claims": null }, + { "name": "negative_iat", "key": "hs256", "payload": { "publish": ["**"], "iat": -1 }, "claims": null }, + { + "name": "fractional_exp", + "key": "hs256", + "payload": { "publish": ["**"], "exp": 4102444800.5 }, + "claims": null + }, + { "name": "string_exp", "key": "hs256", "payload": { "publish": ["**"], "exp": "4102444800" }, "claims": null }, + { + "name": "exp_past_the_clock", + "key": "hs256", + "payload": { "publish": ["**"], "exp": 18446744074 }, + "claims": null + }, + { "name": "expired", "key": "hs256", "payload": { "publish": ["**"], "exp": 1 }, "claims": null }, + { + "name": "not_yet_valid", + "key": "hs256", + "payload": { "publish": ["**"], "nbf": 4102444800 }, + "claims": null + }, + { + "name": "legacy_prefixes", + "key": "hs256", + "payload": { "root": "demo", "put": "alice", "get": ["", "bob"] }, + "claims": { "root": "demo", "publish": ["alice/**"], "subscribe": ["**"] } + }, + { + "name": "legacy_prefix_with_a_wildcard", + "key": "hs256", + "payload": { "put": ["al*"] }, + "claims": null + }, + { + "name": "mixed_encodings", + "key": "hs256", + "payload": { "put": ["alice"], "subscribe": ["**"] }, + "claims": null + }, + { + "name": "registered_claims_are_ignored", + "key": "hs256", + "payload": { "publish": ["**"], "iss": "issuer", "sub": 7, "jti": ["x"] }, + "claims": { "root": "", "publish": ["**"], "subscribe": [] } + }, + { "name": "unknown_claim", "key": "hs256", "payload": { "publish": ["**"], "aud": "relay" }, "claims": null }, + { "name": "grants_nothing", "key": "hs256", "payload": { "publish": [], "subscribe": [] }, "claims": null }, + { "name": "null_role", "key": "hs256", "payload": { "publish": null, "subscribe": ["**"] }, "claims": null }, + { + "name": "capped_role", + "key": "hs256", + "payload": { "publish": { "path": ["alice/**"], "rate": 1000 }, "subscribe": ["**"] }, + "claims": { "root": "", "publish": ["alice/**"], "publish_rate": 1000, "subscribe": ["**"] } + }, + { + "name": "misspelled_cap", + "key": "hs256", + "payload": { "publish": { "path": ["alice/**"], "rat": 1000 } }, + "claims": null + }, + { + "name": "zero_rate", + "key": "hs256", + "payload": { "publish": { "path": ["**"], "rate": 0 } }, + "claims": null + }, + { "name": "invalid_pattern", "key": "hs256", "payload": { "publish": ["**/a/**"] }, "claims": null }, + { + "name": "minted_root_with_boundary_slashes", + "key": "hs256", + "payload": { "root": "/demo/", "publish": ["**"] }, + "claims": { "root": "/demo/", "publish": ["**"], "subscribe": [] } + }, + { + "name": "outside_the_key_scope", + "key": "scoped", + "payload": { "root": "project", "publish": ["other/**"] }, + "claims": null + } + ], + "jwk": [ + { + "name": "legacy_prefix_scope", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "demo", "put": ["room"] } + }, + "valid": true + }, + { + "name": "scope_root_with_a_leading_slash", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "/demo", "publish": ["**"] } + }, + "valid": false + }, + { + "name": "scope_root_with_an_empty_segment", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "a//b", "publish": ["**"] } + }, + "valid": false + }, + { + "name": "scope_root_with_a_stray_escape", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "a\\b", "publish": ["**"] } + }, + "valid": false + }, + { + "name": "scope_granting_nothing", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": {} + }, + "valid": false + }, + { + "name": "scope_with_a_cap", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "publish": { "path": ["**"], "rate": 1000 } } + }, + "valid": false + }, + { + "name": "short_secret", + "jwk": { "kty": "oct", "alg": "HS256", "k": "c2hvcnQ" }, + "valid": false + }, + { + "name": "missing_kty", + "jwk": { "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68" }, + "valid": false + } + ], + "sets": [ + { + "name": "distinct_kids", + "jwks": { + "keys": [ + { + "kty": "oct", + "alg": "HS256", + "k": "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", + "kid": "first" + }, + { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "kid": "second" + } + ] + }, + "valid": true + }, + { + "name": "keys_without_kids", + "jwks": { + "keys": [ + { "kty": "oct", "alg": "HS256", "k": "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA" }, + { "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68" } + ] + }, + "valid": true + }, + { + "name": "duplicate_kid", + "jwks": { + "keys": [ + { "kty": "oct", "alg": "HS256", "k": "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", "kid": "same" }, + { "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", "kid": "same" } + ] + }, + "valid": false + }, + { + "name": "one_invalid_key", + "jwks": { + "keys": [ + { + "kty": "oct", + "alg": "HS256", + "k": "jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA", + "kid": "first" + }, + { "kty": "oct", "alg": "HS256", "k": "c2hvcnQ", "kid": "second" } + ] + }, + "valid": false + } + ], + "grants": [ + { "name": "bounded", "grant": { "publish": ["**"], "expires": 4102444800, "revalidate": 60 }, "valid": true }, + { "name": "unbounded", "grant": { "subscribe": ["**"] }, "valid": true }, + { "name": "null_expires", "grant": { "publish": ["**"], "expires": null }, "valid": false }, + { "name": "negative_expires", "grant": { "publish": ["**"], "expires": -1 }, "valid": false }, + { "name": "fractional_expires", "grant": { "publish": ["**"], "expires": 4102444800.5 }, "valid": false }, + { "name": "expires_past_the_clock", "grant": { "publish": ["**"], "expires": 18446744074 }, "valid": false }, + { "name": "revalidate_without_expires", "grant": { "publish": ["**"], "revalidate": 60 }, "valid": false }, + { "name": "grants_nothing", "grant": { "expires": 4102444800 }, "valid": false }, + { "name": "upstream_without_peer", "grant": { "publish": ["**"], "upstream": true }, "valid": false }, + { "name": "invalid_tier", "grant": { "publish": ["**"], "tier": "a b" }, "valid": false } + ] +} diff --git a/rs/moq-relay/MIGRATE.md b/rs/moq-relay/MIGRATE.md index 6f06755853d..6cff81e44f8 100644 --- a/rs/moq-relay/MIGRATE.md +++ b/rs/moq-relay/MIGRATE.md @@ -5,6 +5,13 @@ Rolling a cluster across a release also follows the [upgrade guide](https://moq. ## Unreleased +### `Lease::new` validates its grant + +`Lease::new` refuses a grant `Grant::validate` refuses, as an auth server's answer +already was: one that grants nothing, marks a non-peer upstream, revalidates without a +bound, names an invalid tier or label, or has expired. Fix the grant an embedder builds; +an expired one is refused as `Refusal` reason `expired`. + ### Shutdown reports deadline cuts separately from end reports `shutdown::Observer::drained()` now returns `shutdown::Drained` instead of `()`. diff --git a/rs/moq-relay/src/auth.rs b/rs/moq-relay/src/auth.rs index 5e0bb770b67..8427e1b8950 100644 --- a/rs/moq-relay/src/auth.rs +++ b/rs/moq-relay/src/auth.rs @@ -376,6 +376,11 @@ impl From for Error { match err { moq_auth::Error::Refused(refusal) => Self::Refused(refusal), moq_auth::Error::UselessGrant => Self::Refused(Default::default()), + moq_auth::Error::GrantExpired => { + let mut refusal = moq_auth::Refusal::default(); + refusal.reason = moq_auth::refusal::Reason::Expired; + Self::Refused(refusal) + } other => Self::Unavailable(other.to_string()), } } @@ -549,10 +554,11 @@ impl Lease { /// Hold `consumer` for a session that dialed `path`, reducing its grant to /// what the origin scopes by. /// - /// Fails if the grant's paths are too long; see [`Token::new`]. Fails too if the - /// grant caps a bitrate, which nothing here enforces. + /// Fails on a grant [`Grant::validate`] refuses, or whose paths are too long; see + /// [`Token::new`]. Fails too if the grant caps a bitrate, which nothing here enforces. pub fn new(path: &str, consumer: lease::Consumer) -> Result { let grant = consumer.grant(); + grant.validate()?; Self::granted(path, &grant, consumer, None) } @@ -879,13 +885,12 @@ impl Decider { } /// Whether `request` carries a token: a SETUP token of any type, or a non-empty -/// `jwt` query parameter, the convention `moq auth serve` reads. +/// `jwt` query parameter, form-decoded as `moq auth serve` reads it, so an escaped +/// key such as `%6Awt` counts too. fn presents_token(request: &Request) -> bool { request.token.is_some() || request.query.as_deref().is_some_and(|query| { - query - .split('&') - .any(|pair| pair.strip_prefix("jwt=").is_some_and(|jwt| !jwt.is_empty())) + url::form_urlencoded::parse(query.as_bytes()).any(|(key, jwt)| key == "jwt" && !jwt.is_empty()) }) } @@ -1491,6 +1496,18 @@ mod tests { } } + /// An embedder's grant meets the contract an auth server's does. + #[test] + fn a_lease_refuses_a_grant_the_contract_refuses() { + let mut tier = Grant::new(moq_net::Pattern::all(), moq_net::Pattern::all()); + tier.tier = Some("a b".into()); + let mut upstream = Grant::new(moq_net::Pattern::all(), moq_net::Pattern::all()); + upstream.upstream = true; + for grant in [tier, upstream] { + assert!(Lease::new("/room", lease::Consumer::fixed(grant)).is_err()); + } + } + #[tokio::test(start_paused = true)] async fn an_invalid_label_at_revalidation_ends_the_lease() { let grant = Grant::new(moq_net::Pattern::all(), moq_net::Pattern::all()); @@ -1718,7 +1735,10 @@ mod tests { }); let mut http = auth.request(moq_auth::Transport::Http, "/"); http.query = Some("jwt=eyJ".into()); - for request in [query, setup, http] { + // `moq auth serve` form-decodes the key, so an escaped one is a token too. + let mut escaped = auth.request(moq_auth::Transport::WebSocket, "/"); + escaped.query = Some("%6Awt=eyJ".into()); + for request in [query, setup, http, escaped] { let Err(err) = auth.admit(request).await else { panic!("a token was admitted on the public grant"); }; @@ -1903,13 +1923,21 @@ mod tests { use std::time::Duration; let mut grant = Grant::new(patterns(&["**"]), patterns(&["**"])); - grant.expires = Some(kio::clock::wall() - Duration::from_secs(1)); - assert!(matches!(grant.validate(), Err(moq_auth::Error::GrantExpired))); + grant.expires = Some(kio::clock::wall() + Duration::from_secs(1)); + let (producer, consumer) = lease::Producer::new(grant.clone()); + let mut lease = Lease::new("/room", consumer).unwrap(); + grant.expires = Some(kio::clock::wall() - Duration::from_secs(1)); let start = kio::clock::now(); - let mut lease = Lease::new("/room", lease::Consumer::fixed(grant)).unwrap(); + producer.update(grant.clone()); assert_eq!(lease.ended().await, lease::Reason::Expired); assert_eq!(kio::clock::now(), start, "no grace after expiry"); + + // Nor is a lease taken on one already past. + let Err(err) = Lease::new("/room", lease::Consumer::fixed(grant)) else { + panic!("an expired grant was leased"); + }; + assert_eq!(moq_net::SessionError::from(&err), moq_net::SessionError::Expired); } #[test] diff --git a/rs/moq-relay/src/cluster.rs b/rs/moq-relay/src/cluster.rs index d9dc6239237..db5919a86cc 100644 --- a/rs/moq-relay/src/cluster.rs +++ b/rs/moq-relay/src/cluster.rs @@ -2544,7 +2544,7 @@ mod tests { options.peer.announces = 20; let cluster = Cluster::spawn(options).unwrap(); for (peer, upstream, expected) in [(false, false, 2), (true, false, 20), (true, true, 20)] { - let mut grant = moq_auth::Grant::new(moq_auth::Patterns::new(), moq_auth::Patterns::new()); + let mut grant = moq_auth::Grant::new(moq_net::Pattern::all(), moq_net::Pattern::all()); grant.peer = peer; grant.upstream = upstream; let (_producer, consumer) = moq_auth::lease::Producer::new(grant); From af9ce725c320c5bbe74a82a28cdc2888186a646b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 17:32:39 -0800 Subject: [PATCH 3/4] fix(auth): round not-before up and key the cache by format Co-Authored-By: Claude Opus 5.5 --- rs/moq-auth/src/claims.rs | 15 ++++++++ rs/moq-auth/src/serve.rs | 72 +++++++++++++++++++++++++-------------- rs/moq-auth/src/wire.rs | 19 ++++++++++- 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/rs/moq-auth/src/claims.rs b/rs/moq-auth/src/claims.rs index 79ffa9bd9b4..faf5f4fe51d 100644 --- a/rs/moq-auth/src/claims.rs +++ b/rs/moq-auth/src/claims.rs @@ -269,6 +269,21 @@ mod tests { texts.iter().map(|text| text.parse().unwrap()).collect() } + /// A fractional expiry writes earlier and a fractional not-before later, so neither + /// widens the window the claims asked for. + #[test] + fn fractional_times_narrow_the_window() { + let at = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(1500); + let claims = Claims { + publish: patterns(&["**"]), + expires: Some(at), + not_before: Some(at), + ..Default::default() + }; + let json = serde_json::to_value(&claims).unwrap(); + assert_eq!((json["exp"].as_u64(), json["nbf"].as_u64()), (Some(1), Some(2))); + } + fn create_test_claims() -> Claims { Claims { publish_rate: None, diff --git a/rs/moq-auth/src/serve.rs b/rs/moq-auth/src/serve.rs index dfd4d58176f..8345aae06e6 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -234,9 +234,36 @@ pub struct Policy { cache: KeyCache, } -/// Parsed key files, keyed by path, so a token costs a `stat` rather than a read and parse. +/// Parsed key files, keyed by path and format, so a token costs a `stat` rather than a +/// read and parse. #[derive(Clone, Default)] -struct KeyCache(Arc>>); +struct KeyCache(Arc>>); + +/// How a key file is read. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum Format { + /// One key, as a set holding just it, which selects it by `kid` exactly as + /// verifying with the key alone would. + Key, + /// A JWK Set. + Set, +} + +impl Format { + fn parse(self, json: &str) -> Result { + match self { + Self::Key => { + let key = json.parse::().map_err(|_| Denial::UnknownKey)?; + Ok(KeySet { + keys: vec![Arc::new(key)], + }) + } + Self::Set => json + .parse() + .map_err(|err: crate::Error| Denial::InvalidToken(err.to_string())), + } + } +} struct Cached { stamp: (kio::clock::Wall, u64), @@ -252,26 +279,27 @@ impl std::fmt::Debug for KeyCache { impl KeyCache { /// The keys in `path`, parsed again only when its modification time or size changed /// since they were cached. A file that is gone forgets its keys. - async fn load(&self, path: &Path, parse: fn(&str) -> Result) -> Result, Denial> { + async fn load(&self, path: &Path, format: Format) -> Result, Denial> { let lock = || self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let entry = (path.to_owned(), format); let Ok(meta) = tokio::fs::metadata(path).await else { - lock().remove(path); + lock().remove(&entry); return Err(Denial::UnknownKey); }; // Stamped before reading, so a write racing the read is caught at the next token. let modified = meta.modified().ok().and_then(|modified| modified.try_into().ok()); let stamp = modified.map(|modified| (modified, meta.len())); - if let Some(cached) = lock().get(path) + if let Some(cached) = lock().get(&entry) && Some(cached.stamp) == stamp { return Ok(cached.keys.clone()); } let json = tokio::fs::read_to_string(path).await.map_err(|_| Denial::UnknownKey)?; - let keys = Arc::new(parse(&json)?); + let keys = Arc::new(format.parse(&json)?); // A file without a modification time this clock holds parses every time. if let Some(stamp) = stamp { lock().insert( - path.to_owned(), + entry, Cached { stamp, keys: keys.clone(), @@ -282,15 +310,6 @@ impl KeyCache { } } -/// One key file as a set holding just that key, which selects it by `kid` exactly as -/// verifying with the key alone would. -fn parse_key(json: &str) -> Result { - let key = json.parse::().map_err(|_| Denial::UnknownKey)?; - Ok(KeySet { - keys: vec![Arc::new(key)], - }) -} - /// Why the policy denied a session, mapped to the typed [`Refusal`](crate::Refusal) in the 403 body. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[non_exhaustive] @@ -476,20 +495,13 @@ impl Policy { // whichever one is configured. let header = jsonwebtoken::decode_header(jwt).map_err(|err| Denial::MalformedToken(err.to_string()))?; let set = match keys { - Keys::File(path) => self.cache.load(path, parse_key).await?, + Keys::File(path) => self.cache.load(path, Format::Key).await?, Keys::Dir(dir) => { let kid = header.kid.ok_or(Denial::MissingKeyId)?; let kid = KeyId::decode(&kid).map_err(|_| Denial::UnknownKey)?; - self.cache.load(&dir.join(format!("{kid}.jwk")), parse_key).await? - } - Keys::Set(path) => { - self.cache - .load(path, |json| { - json.parse::() - .map_err(|err| Denial::InvalidToken(err.to_string())) - }) - .await? + self.cache.load(&dir.join(format!("{kid}.jwk")), Format::Key).await? } + Keys::Set(path) => self.cache.load(path, Format::Set).await?, }; let claims = set.key(jwt).and_then(|key| key.verify_claims(jwt)); let claims = claims.map_err(|err| match err { @@ -1169,6 +1181,14 @@ mod tests { let jwt = sign(&key, "demo", &["**"], &[], None); assert!(policy.decide(&with_token(request("/demo"), &jwt)).await.is_ok()); + // The same file read as a single key is no key, whatever the set cached. + let mut single = policy.clone(); + single.keys = Some(Keys::File(dir.path().join("keys.jwks"))); + assert_eq!( + single.decide(&with_token(request("/demo"), &jwt)).await.unwrap_err(), + Denial::UnknownKey + ); + let mut wrong = key.export(); wrong.kid = Some(KeyId::decode("kid2").unwrap()); let wrong = wrong.import().unwrap(); diff --git a/rs/moq-auth/src/wire.rs b/rs/moq-auth/src/wire.rs index 6beae3ceba5..5b4167f1e4e 100644 --- a/rs/moq-auth/src/wire.rs +++ b/rs/moq-auth/src/wire.rs @@ -37,7 +37,11 @@ pub(crate) struct Claims { exp: Option, #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] iat: Option, - #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] + #[serde( + serialize_with = "seconds::serialize_ceil", + deserialize_with = "seconds::deserialize", + skip_serializing_if = "Option::is_none" + )] nbf: Option, // Registered claims that narrow nothing: an issuer's bookkeeping, read and dropped. // Every other claim is refused, since an unknown one may narrow the grant, and a @@ -186,6 +190,19 @@ mod seconds { } } + /// Rounded up, so a not-before never moves earlier; past the clock's last second it + /// writes a time this refuses to read. + pub(super) fn serialize_ceil(wall: &Option, serializer: S) -> Result { + match wall { + Some(wall) => { + let since = wall.saturating_duration_since(Wall::UNIX_EPOCH); + let seconds = since.as_secs() + u64::from(since.subsec_nanos() > 0); + serializer.serialize_u64(seconds) + } + None => serializer.serialize_none(), + } + } + pub(super) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { let seconds = u64::deserialize(deserializer)?; Wall::UNIX_EPOCH From 7f735cac08edc3fe7464db9d5f5b05cbfb7f9f18 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 21:03:44 -0800 Subject: [PATCH 4/4] fix(auth): keep to_encoding_key crate-visible for the corpus test #8088 narrowed it to private; the corpus test signs raw payloads with it. Co-Authored-By: Claude Opus 5.5 --- rs/moq-auth/src/key.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-auth/src/key.rs b/rs/moq-auth/src/key.rs index 02972f7fe94..bbff5b28b4c 100644 --- a/rs/moq-auth/src/key.rs +++ b/rs/moq-auth/src/key.rs @@ -544,7 +544,7 @@ impl Key { Ok(self.decode.get_or_init(|| decoding_key)) } - fn to_encoding_key(&self) -> crate::Result<&EncodingKey> { + pub(crate) fn to_encoding_key(&self) -> crate::Result<&EncodingKey> { if let Some(key) = self.encode.get() { return Ok(key); }