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
8 changes: 6 additions & 2 deletions js/auth/src/corpus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ describe("tokens", () => {
}
});

// A key or set given as a string is its raw text, for what a JSON value cannot spell,
// such as a repeated member.
const text = (document: unknown) => (typeof document === "string" ? document : JSON.stringify(document));

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))],
["jwk", corpus.jwk, (jwk) => Key.parse(text(jwk))],
["sets", corpus.sets, (jwks) => KeySet.parse(text(jwks))],
["grants", corpus.grants, (grant) => GrantSchema.parse(grant)],
];
for (const [section, cases, parse] of sections) {
Expand Down
144 changes: 144 additions & 0 deletions js/auth/src/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// A JSON parser that refuses what `moq-auth`'s serde_json refuses, so a key loads in both
// languages or in neither.
//
// `JSON.parse` keeps a repeated member's last value, while serde refuses a field it reads
// twice and ignores the members it doesn't read. So a repeated member parses here as
// `REPEATED`, which every schema refuses where it reads the member and strips where it
// doesn't. Beyond that it refuses the inputs serde_json does and `JSON.parse` doesn't:
// nesting past serde_json's recursion limit, a lone surrogate, and a number too large for
// a double.

// What a member parses to when its object repeats it.
const REPEATED = Symbol("repeated member");

// serde_json refuses a document nesting arrays and objects 128 deep.
const MAX_DEPTH = 127;

const WHITESPACE = /[ \t\n\r]*/y;
const NUMBER = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y;
// biome-ignore lint/suspicious/noControlCharactersInRegex: JSON refuses an unescaped control character in a string.
const PLAIN = /[^"\\\u0000-\u001f]*/y;
const LONE_SURROGATE = /\p{Surrogate}/u;
const ESCAPES: Record<string, string> = { '"': '"', "\\": "\\", "/": "/", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t" };

/** Parse a JSON document as `moq-auth` does. Throws on anything serde_json refuses. */
export function parse(text: string): unknown {
const parser = new Parser(text);
const value = parser.value(0);
parser.skip();
if (parser.pos !== text.length) parser.fail("trailing characters");
return value;
}

class Parser {
readonly text: string;
pos = 0;

constructor(text: string) {
this.text = text;
}

fail(message: string): never {
throw new SyntaxError(`invalid JSON: ${message} at offset ${this.pos}`);
}

skip(): void {
this.pos += this.match(WHITESPACE).length;
}

match(pattern: RegExp): string {
pattern.lastIndex = this.pos;
return pattern.exec(this.text)?.[0] ?? "";
}

eat(char: string): boolean {
this.skip();
if (this.text[this.pos] !== char) return false;
this.pos++;
return true;
}

value(depth: number): unknown {
this.skip();
const char = this.text[this.pos];
if (char === "{" || char === "[") {
if (depth === MAX_DEPTH) this.fail("recursion limit exceeded");
this.pos++;
return char === "{" ? this.object(depth + 1) : this.array(depth + 1);
}
if (char === '"') return this.string();
for (const [word, value] of [
["true", true],
["false", false],
["null", null],
] as const) {
if (this.text.startsWith(word, this.pos)) {
this.pos += word.length;
return value;
}
}
const number = this.match(NUMBER);
if (!number) this.fail("expected value");
this.pos += number.length;
const value = Number(number);
if (!Number.isFinite(value)) this.fail("number out of range");
return value;
}

object(depth: number): Record<string, unknown> {
const object: Record<string, unknown> = {};
if (this.eat("}")) return object;
do {
this.skip();
if (this.text[this.pos] !== '"') this.fail("expected member name");
const key = this.string();
if (!this.eat(":")) this.fail("expected `:`");
const value = this.value(depth);
// Defined rather than assigned, so a `__proto__` member stays a plain member.
Object.defineProperty(object, key, {
value: Object.hasOwn(object, key) ? REPEATED : value,
enumerable: true,
writable: true,
configurable: true,
});
} while (this.eat(","));
if (!this.eat("}")) this.fail("expected `,` or `}`");
return object;
}

array(depth: number): unknown[] {
const array: unknown[] = [];
if (this.eat("]")) return array;
do array.push(this.value(depth));
while (this.eat(","));
if (!this.eat("]")) this.fail("expected `,` or `]`");
return array;
}

string(): string {
this.pos++; // the opening quote
let out = "";
for (;;) {
const plain = this.match(PLAIN);
out += plain;
this.pos += plain.length;
const char = this.text[this.pos++];
if (char === '"') break;
if (char === undefined) this.fail("unterminated string");
if (char !== "\\") this.fail("control character in string");
const escaped = this.text[this.pos++];
if (escaped === "u") {
const hex = this.text.slice(this.pos, this.pos + 4);
if (!/^[0-9a-fA-F]{4}$/.test(hex)) this.fail("invalid unicode escape");
out += String.fromCharCode(Number.parseInt(hex, 16));
this.pos += 4;
} else {
const decoded = escaped === undefined ? undefined : ESCAPES[escaped];
if (decoded === undefined) this.fail("invalid escape");
out += decoded;
}
}
if (LONE_SURROGATE.test(out)) this.fail("lone surrogate");
return out;
}
}
8 changes: 4 additions & 4 deletions js/auth/src/key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ test("parse - a field with non-base64 characters is refused, whatever its length
expect(() => Key.parse(encodeJwk(invalidKey))).toThrow();
});

test("parse - accepts a JWK in standard padded base64", () => {
test("parse - refuses a JWK in standard padded base64, as moq-auth does", () => {
// The note survives as "+" and "/" in standard base64, and the kid length forces padding.
const json = JSON.stringify({ ...testKey, kid: "key-1", note: "~~~???" });
const encoded = Buffer.from(json).toString("base64");
expect(encoded).toMatch(/\+/);
expect(encoded).toMatch(/\//);
expect(encoded).toMatch(/=$/);

const key = Key.parse(encoded);
expect(key.kid).toBe("key-1");
expect((key as { k?: string }).k).toBe(testKey.k);
expect(() => Key.parse(encoded)).toThrow(/invalid base64url/);
expect(() => Key.parse(encoded.replace(/=+$/, ""))).toThrow(/invalid base64url/);
expect(Key.parse(Buffer.from(json).toString("base64url")).kid).toBe("key-1");
});

test("generate - an HMAC secret is unpadded base64url that round-trips", async () => {
Expand Down
65 changes: 35 additions & 30 deletions js/auth/src/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as jose from "jose";
import * as z from "zod/mini";
import { type Algorithm, AlgorithmSchema } from "./algorithm.ts";
import { type Claims, ClaimsSchema, ScopeSchema, scopeAllows } from "./claims.ts";
import * as Json from "./json.ts";
import * as Path from "./path.ts";
import { encodeGrants, nestRates } from "./wire.ts";

Expand Down Expand Up @@ -35,7 +36,7 @@ const EC_ALGORITHM_TO_CURVE: Record<"ES256" | "ES384", "P-256" | "P-384"> = {

const Base64FieldSchema = z.string().check(
z.minLength(1),
z.refine((value) => decodeBase64Flexible(value) !== null, {
z.refine((value) => decodeBase64url(value, true) !== null, {
message: "Field must be valid base64url data",
}),
);
Expand Down Expand Up @@ -130,30 +131,30 @@ function toPublic(key: Key): PublicKey {

/** Parse a key from a string, auto-detecting JSON or base64url encoding. */
function parse(jwk: string): Key {
const trimmed = jwk.trim();
// Trimmed as Rust's `str::trim` does, which differs from `String.trim` on U+0085 and U+FEFF.
const trimmed = jwk.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");

let data: unknown;
if (trimmed.startsWith("{")) {
// Plain JSON
try {
data = JSON.parse(trimmed);
} catch {
throw new Error("Failed to parse JWK: invalid JSON format");
}
} else {
// Base64url encoded JSON
const decoded = decodeBase64Flexible(trimmed);
let json = trimmed;
if (!trimmed.startsWith("{")) {
// Base64url encoded JSON, unpadded as both languages write it.
const decoded = decodeBase64url(trimmed, false);
if (!decoded) {
throw new Error("Failed to decode JWK: invalid base64url encoding");
}
try {
const jsonString = new TextDecoder().decode(decoded);
data = JSON.parse(jsonString);
json = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(decoded);
} catch {
throw new Error("Failed to parse JWK: invalid JSON format after base64url decode");
throw new Error("Failed to decode JWK: invalid UTF-8 after base64url decode");
}
}

let data: unknown;
try {
data = Json.parse(json);
} catch (error) {
throw new Error(`Failed to parse JWK: ${error instanceof Error ? error.message : "invalid JSON"}`);
}

try {
return KeySchema.parse(data);
} catch (error) {
Expand Down Expand Up @@ -413,7 +414,7 @@ function keyIssue(
switch (key.kty) {
case "oct": {
if (!HMAC_ALGORITHMS.has(key.alg)) return `Algorithm ${key.alg} is incompatible with oct keys`;
const secret = decodeBase64Flexible(key.k);
const secret = decodeBase64url(key.k, true);
if (!secret || secret.byteLength < MIN_HMAC_SECRET_BYTES) {
return `Secret must be at least ${MIN_HMAC_SECRET_BYTES} bytes when decoded`;
}
Expand All @@ -428,8 +429,8 @@ function keyIssue(
if (key.crv !== expectedCurve) return `Algorithm ${key.alg} requires curve ${expectedCurve}`;
const length = key.crv === "P-256" ? 32 : 48;
if (
decodeBase64Flexible(key.x)?.byteLength !== length ||
decodeBase64Flexible(key.y)?.byteLength !== length
decodeBase64url(key.x, true)?.byteLength !== length ||
decodeBase64url(key.y, true)?.byteLength !== length
) {
return `Coordinates must be ${length} bytes for ${key.crv}`;
}
Expand Down Expand Up @@ -483,16 +484,20 @@ async function importJoseKey(key: Key | PublicKey): Promise<CryptoKey | Uint8Arr
return jose.importJWK(jwk, key.alg);
}

function decodeBase64Flexible(value: string): Uint8Array | null {
const trimmed = value.trim();
if (!trimmed) {
return null;
}
const BASE64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

try {
// Accept standard base64 too by mapping it onto the base64url alphabet.
return jose.base64url.decode(trimmed.replaceAll("+", "-").replaceAll("/", "_"));
} catch {
return null;
}
/**
* Decode base64url exactly as `moq-auth` does, or return null: unpadded, or with canonical
* padding when `padded` allows it, never empty, and with no whitespace, standard-alphabet
* characters, or nonzero trailing bits.
*/
function decodeBase64url(value: string, padded: boolean): Uint8Array | null {
const body = padded && value.length % 4 === 0 ? value.replace(/==?$/, "") : value;
if (!/^[A-Za-z0-9_-]+$/.test(body) || body.length % 4 === 1) return null;

// The last character's low bits fall past the final byte, so a canonical encoding zeroes them.
const spare = [0, 0, 0b1111, 0b11][body.length % 4] ?? 0;
if (BASE64URL.indexOf(body[body.length - 1] ?? "") & spare) return null;

return jose.base64url.decode(body);
}
7 changes: 4 additions & 3 deletions js/auth/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import * as jose from "jose";
import * as z from "zod/mini";
import type { Claims } from "./claims.ts";
import * as Json from "./json.ts";
import { Key, KeySchema, type PublicKey } from "./key.ts";

/**
Expand Down Expand Up @@ -47,9 +48,9 @@ export interface PublicKeySet {
function parse(jwks: string): KeySet {
let data: unknown;
try {
data = JSON.parse(jwks.trim());
} catch {
throw new Error("Failed to parse JWKS: invalid JSON format");
data = Json.parse(jwks);
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
} catch (error) {
throw new Error(`Failed to parse JWKS: ${error instanceof Error ? error.message : "invalid JSON"}`);
}

try {
Expand Down
29 changes: 0 additions & 29 deletions quest/m1/auth-key-strictness.md

This file was deleted.

18 changes: 13 additions & 5 deletions rs/moq-auth/src/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ struct TokenCase {
claims: Option<Value>,
}

/// A document under `jwk`, `jwks`, or `grant`, and whether it is accepted.
/// A document under `jwk`, `jwks`, or `grant`, and whether it is accepted. A key or set
/// given as a string is its raw text, for what a JSON value cannot spell, such as a
/// repeated member.
#[derive(Deserialize)]
struct Case {
name: String,
Expand Down Expand Up @@ -97,18 +99,24 @@ fn tokens() {
}
}

/// A document's text: a string as written, anything else serialized.
fn text(document: Value) -> String {
match document {
Value::String(text) => text,
document => document.to_string(),
}
}

#[test]
fn documents() {
let corpus = corpus();
type Parse = fn(Value) -> Result<(), String>;
let parsers: [(&str, Vec<Case>, Parse); 3] = [
("jwk", corpus.jwk, |jwk| {
Key::from_str(&jwk.to_string()).map(drop).map_err(|err| err.to_string())
Key::from_str(&text(jwk)).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())
KeySet::from_str(&text(jwks)).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())?;
Expand Down
Loading