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
50 changes: 2 additions & 48 deletions js/auth/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,7 @@ export const Key: {
generate: (algorithm: Algorithm, kid?: string) => Promise<Key>;
parse: (jwk: string) => Key;
public: (key: Key) => PublicKey;
sign: (key: Key, claims: Omit<{
exp?: number;
get?: string | string[];
iat?: number;
iss?: unknown;
jti?: unknown;
nbf?: number;
publish?: string[] | {
path: string[];
rate: number;
};
put?: string | string[];
root?: string;
sub?: unknown;
subscribe?: string[] | {
path: string[];
rate: number;
};
}, "publish" | "subscribe"> & {
publish?: string[];
publish_rate?: number;
subscribe?: string[];
subscribe_rate?: number;
}) => Promise<string>;
sign: (key: Key, claims: Claims) => Promise<string>;
verify: (key: PublicKey | SymmetricKey, token: string) => Promise<Claims>;
};

Expand Down Expand Up @@ -405,30 +382,7 @@ export const KeySet: {
find: (set: KeySet, kid: string) => Key | undefined;
parse: (jwks: string) => KeySet;
public: (set: KeySet) => PublicKeySet;
sign: (set: KeySet, claims: Omit<{
exp?: number;
get?: string | string[];
iat?: number;
iss?: unknown;
jti?: unknown;
nbf?: number;
publish?: string[] | {
path: string[];
rate: number;
};
put?: string | string[];
root?: string;
sub?: unknown;
subscribe?: string[] | {
path: string[];
rate: number;
};
}, "publish" | "subscribe"> & {
publish?: string[];
publish_rate?: number;
subscribe?: string[];
subscribe_rate?: number;
}) => Promise<string>;
sign: (set: KeySet, claims: Claims) => Promise<string>;
verify: (set: KeySet, token: string) => Promise<Claims>;
};

Expand Down
8 changes: 5 additions & 3 deletions js/auth/src/key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,15 @@ test("sign - subtree grants are written as legacy put/get", async () => {
expect(payload).toEqual({ root: "test-path", put: ["alice"], get: [""] });
});

test("sign - legacy-shaped input cannot slip past a key scope", async () => {
test("sign - refuses the legacy put/get spelling, even within scope", async () => {
const scoped: Key = {
...Key.parse(encodeJwk(testKey)),
scope: { root: "demo", publish: ["inside/**"] },
};
const legacy = { root: "demo", put: ["outside"] } as unknown as Parameters<typeof Key.sign>[1];
await expect(Key.sign(scoped, legacy)).rejects.toThrow(/scope/);
const inside = { root: "demo", put: ["inside"] } as unknown as Claims;
await expect(Key.sign(scoped, inside)).rejects.toThrow("legacy put/get");
const outside = { root: "demo", put: ["outside"] } as unknown as Claims;
await expect(Key.sign(scoped, outside)).rejects.toThrow("legacy put/get");
});

test("key scope is enforced when signing and verifying", async () => {
Expand Down
18 changes: 5 additions & 13 deletions js/auth/src/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,18 @@ function parse(jwk: string): Key {
* wire rather than being stamped with the current time.
*
* Takes each role as its patterns, capped by `publish_rate` or `subscribe_rate` beside
* it, as the decoded {@link Claims} spell them; the `{path, rate}` wire form is refused.
* it, as the decoded {@link Claims} spell them; the `{path, rate}` wire form and the
* legacy `put`/`get` spelling are refused.
*
* Throws on a root spelled with an empty segment, a `\` escaping anything but `\` or
* `/`, or more than 4096 bytes. Verifying stays lenient on the empty segments, so tokens
* minted before stay valid.
*/
async function sign(
key: Key,
claims: Omit<z.input<typeof ClaimsSchema>, "publish" | "subscribe"> & {
publish?: string[];
publish_rate?: number;
subscribe?: string[];
subscribe_rate?: number;
},
): Promise<string> {
async function sign(key: Key, claims: Claims): Promise<string> {
ensureOperationSupported(key, "sign");

// Scope-check and sign what the schema parsed, never the raw input: an untyped
// caller could pass legacy `put`/`get` fields the scope check would not see. The
// caps nest back into their roles first, which the schema refuses a role already in.
// Scope-check and sign what the schema parsed, never the raw input. The caps nest
// back into their roles first, which the schema refuses a role already in.
let parsed: Claims;
try {
parsed = ClaimsSchema.parse(nestRates(claims));
Expand Down
2 changes: 1 addition & 1 deletion js/auth/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function find(set: KeySet, kid: string): Key | undefined {
}

/** Sign the claims with the first key that permits signing and contains signing material. */
async function sign(set: KeySet, claims: Parameters<typeof Key.sign>[1]): Promise<string> {
async function sign(set: KeySet, claims: Claims): Promise<string> {
const key = set.keys.find((key) => key.key_ops.includes("sign") && (key.kty === "oct" || key.d !== undefined));
if (!key) throw new Error("Cannot find signing key");
return await Key.sign(key, claims);
Expand Down
6 changes: 5 additions & 1 deletion js/auth/src/wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ export function decodeRoles<T extends { publish?: WireRole; subscribe?: WireRole

/**
* Nest each `_rate` field back into its role, so the decoded claims `Key.verify`
* returns parse again. Throws on a role already in the capped form, so a cap has one spelling.
* returns parse again. Throws on a role already in the capped form, so a cap has one
* spelling, and on the legacy `put`/`get` form, which signing no longer accepts.
*/
export function nestRates<
T extends { publish?: unknown; subscribe?: unknown; publish_rate?: number; subscribe_rate?: number },
>(claims: T) {
const { publish_rate, subscribe_rate, ...rest } = claims;
if ("put" in rest || "get" in rest) {
throw new Error("legacy put/get grants are refused; sign publish and subscribe instead");
}
for (const role of [rest.publish, rest.subscribe]) {
if (role !== undefined && !Array.isArray(role)) {
throw new Error("a role is a pattern list, capped by publish_rate or subscribe_rate");
Expand Down
1 change: 0 additions & 1 deletion quest/m2/api-shape/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ Rejected in planning:
## Required

- [moq-net's route stream goes public](/quest/m2/api-shape/net.md) - un-hide `origin::Consumer::routes` and reshape `RouteEvent`, now that #8063 merged
- [JS Key.sign takes exactly Claims](/quest/m2/api-shape/auth-sign.md) - `@moq/auth` signs only `Claims`, with no legacy `put`/`get` input, like Rust
25 changes: 0 additions & 25 deletions quest/m2/api-shape/auth-sign.md

This file was deleted.