-
-
Notifications
You must be signed in to change notification settings - Fork 249
fix(auth): read and write legacy put/get token grants #4190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * The JSON encoding of the grants in claims and key scopes. | ||
| * | ||
| * Grants were once prefix lists named `put` and `get`, and every published `moq-token` | ||
| * reader still expects them. Must stay in lockstep with `wire.rs` in the Rust | ||
| * `moq-auth` crate. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import { Pattern, Patterns } from "@moq/pattern"; | ||
|
|
||
| /** One grant in whichever encoding it arrived: legacy `put`/`get` prefix lists, or patterns. */ | ||
| type WireGrants = { | ||
| put?: string | string[]; | ||
| get?: string | string[]; | ||
| publish?: string[]; | ||
| subscribe?: string[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Read legacy `moq-token` prefix grants as the subtree patterns they always meant. | ||
| * | ||
| * A prefix `p` is exactly `p/**` (and `""` is `**`). A document never mixes the two | ||
| * encodings, and a legacy prefix containing `*` is refused: it had no wildcards, so | ||
| * reading one now would silently widen the grant. | ||
| */ | ||
| export function decodeGrants<T extends WireGrants>( | ||
| wire: T, | ||
| ctx: { issues: unknown[] }, | ||
| ): Omit<T, "put" | "get"> & { publish?: string[]; subscribe?: string[] } { | ||
| const { put, get, ...rest } = wire; | ||
| if (put === undefined && get === undefined) return rest; | ||
|
|
||
| const fail = (message: string) => { | ||
| ctx.issues.push({ code: "custom", message, input: wire }); | ||
| return rest; | ||
| }; | ||
| if (rest.publish !== undefined || rest.subscribe !== undefined) { | ||
| return fail("mixes the legacy put/get fields with publish/subscribe"); | ||
| } | ||
|
|
||
| const subtrees = (prefixes: string | string[] | undefined) => | ||
| prefixes === undefined | ||
| ? undefined | ||
| : (typeof prefixes === "string" ? [prefixes] : prefixes).map((prefix) => Pattern.subtree(prefix).text); | ||
| try { | ||
| return { ...rest, publish: subtrees(put), subscribe: subtrees(get) }; | ||
| } catch (error) { | ||
| return fail(`legacy prefix: ${error instanceof Error ? error.message : String(error)}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write grants the legacy way when every pattern is a subtree, so every published | ||
| * `moq-token` reader agrees on what they grant. Anything a prefix can't say is written | ||
| * as `publish`/`subscribe`, which an older reader refuses rather than misreads. | ||
| */ | ||
| export function encodeGrants<T extends { publish?: string[]; subscribe?: string[] }>( | ||
| grants: T, | ||
| ): Omit<T, "publish" | "subscribe"> & WireGrants { | ||
| const { publish, subscribe, ...rest } = grants; | ||
| const prefixes = (texts: string[] | undefined) => { | ||
| const out: string[] = []; | ||
| for (const pattern of new Patterns((texts ?? []).map((text) => Pattern.parse(text)))) { | ||
| const prefix = pattern.asPrefix(); | ||
| if (prefix === undefined) return undefined; | ||
| out.push(prefix); | ||
| } | ||
| return out; | ||
| }; | ||
|
|
||
| const put = prefixes(publish); | ||
| const get = prefixes(subscribe); | ||
| if (put === undefined || get === undefined) { | ||
| return { | ||
| ...rest, | ||
| ...(publish?.length && { publish }), | ||
| ...(subscribe?.length && { subscribe }), | ||
| }; | ||
| } | ||
| return { ...rest, ...(put.length && { put }), ...(get.length && { get }) }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a legacy token uses an empty root and a valid 32-segment
putorgetprefix,Pattern.subtreeappends**, producing 33 segments and throwing because patterns are limited to 32. The transform consequently rejects a previously valid credential in both implementations, even though at maximum path depth the prefix is equivalent to the 32-segment literal because no deeper valid path exists. Decode this boundary case as a literal, and consider the equivalent reverse encoding, to preserve published-wire compatibility. (Written by GPT-5.6 Sol)AGENTS.md reference: AGENTS.md:L75-L77
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declining: a legacy prefix at the full 32-segment depth is refused loudly rather than misread, which matches the fail-closed rule. Decoding it as a literal would add a depth special case to both languages' encoders for credentials that are unlikely to exist. Happy to revisit if one shows up.
(Written by Claude Opus 5.5)