diff --git a/bench/browser/relay.toml b/bench/browser/relay.toml index 41336bcf436..323faaecd7d 100644 --- a/bench/browser/relay.toml +++ b/bench/browser/relay.toml @@ -18,4 +18,4 @@ listen = "127.0.0.1:4460" [auth] # Allow anonymous access to everything. -public = "**" +public = "" diff --git a/bench/relay.sh b/bench/relay.sh index 7972a7ceb03..8229fb1cbc6 100755 --- a/bench/relay.sh +++ b/bench/relay.sh @@ -96,7 +96,7 @@ relay_config() { "${stream_config[@]}" \ '' \ '[auth]' \ - 'public = "**"' \ + 'public = ""' \ "${runtime_config[@]}" >"$path" if [[ $PROFILE == heap ]]; then # Its /sessions list shows when the relay has dropped every session after the load. diff --git a/dart/moq/test/moq_test.dart b/dart/moq/test/moq_test.dart index 0351feef5f9..1419a067f5c 100644 --- a/dart/moq/test/moq_test.dart +++ b/dart/moq/test/moq_test.dart @@ -363,8 +363,8 @@ void main() { // Generated records compare by identity, so compare the fields. void expectEverything(Grant? grant) { - expect(grant?.publish, ['**']); - expect(grant?.subscribe, ['**']); + expect(grant?.publish, ['']); + expect(grant?.subscribe, ['']); expect(grant?.expires, isNull); } diff --git a/demo/carla/README.md b/demo/carla/README.md index c9d4b60b295..a243fbaea4d 100644 --- a/demo/carla/README.md +++ b/demo/carla/README.md @@ -210,8 +210,8 @@ checkout. just carla venv just carla service-install -# The units read this. MOQ_URL carries a token allowed to publish `carla/**` -# and subscribe `viewer/carla/**`, where drivers publish; keeping it here keeps +# The units read this. MOQ_URL carries a token allowed to publish `carla` +# and subscribe `viewer/carla`, where drivers publish; keeping it here keeps # it off the command line. CARLA_IMAGE is the tag the moq-dev/carla build printed, since the worker # refuses a stock simulator. cat >~/.config/moq/carla.env <<'EOF' diff --git a/demo/carla/sim/systemd/install-user-service.sh b/demo/carla/sim/systemd/install-user-service.sh index 129ea0f4f3c..55b029b680c 100755 --- a/demo/carla/sim/systemd/install-user-service.sh +++ b/demo/carla/sim/systemd/install-user-service.sh @@ -43,8 +43,8 @@ Before starting, write the relay settings to: MOQ_URL=https://cdn.moq.dev/demo?jwt= CARLA_IMAGE=localhost/moq-carla: -The token is a demo-project JWT scoped to publish carla/** and subscribe -viewer/carla/**. For local development, MOQ_URL=http://localhost:4443 needs +The token is a demo-project JWT scoped to publish carla and subscribe +viewer/carla. For local development, MOQ_URL=http://localhost:4443 needs no token. Then run: diff --git a/demo/carla/sim/systemd/install-user-service.test.sh b/demo/carla/sim/systemd/install-user-service.test.sh index fcd3de472dd..b57d043f82f 100755 --- a/demo/carla/sim/systemd/install-user-service.test.sh +++ b/demo/carla/sim/systemd/install-user-service.test.sh @@ -44,8 +44,8 @@ CARLA_SYSTEMD_USER_DIR="$tmp/units" \ # hosted relay, the locally built image, and the tokenless local relay. grep -Fxq ' MOQ_URL=https://cdn.moq.dev/demo?jwt=' "$tmp/install.out" grep -Fxq ' CARLA_IMAGE=localhost/moq-carla:' "$tmp/install.out" -grep -Fq 'scoped to publish carla/** and subscribe' "$tmp/install.out" -grep -Fq 'viewer/carla/**. For local development, MOQ_URL=http://localhost:4443 needs' "$tmp/install.out" +grep -Fq 'scoped to publish carla and subscribe' "$tmp/install.out" +grep -Fq 'viewer/carla. For local development, MOQ_URL=http://localhost:4443 needs' "$tmp/install.out" test "$(stat -c %a "$tmp/units")" = 700 test "$(stat -c %a "$tmp/config")" = 700 test "$(stat -c %a "$tmp/units/carla-sim.service")" = 600 diff --git a/demo/pub/justfile b/demo/pub/justfile index 251a5716792..a459346faa0 100644 --- a/demo/pub/justfile +++ b/demo/pub/justfile @@ -101,7 +101,7 @@ h264 name url="http://localhost:4443" *args: serve name *args: just download "{{ name }}" cargo build --bin moq - just ffmpeg-ts "media/{{ name }}.mp4" | cargo run --bin moq -- "${@:2}" --listen "[::]:4443" --listen-tls-generate "localhost" --auth-public "**" --broadcast "{{ name }}.hang" import ts + just ffmpeg-ts "media/{{ name }}.mp4" | cargo run --bin moq -- "${@:2}" --listen "[::]:4443" --listen-tls-generate "localhost" --auth-public "" --broadcast "{{ name }}.hang" import ts # Generate and serve an HLS stream from a video for testing. [positional-arguments] diff --git a/demo/relay/leaf0.toml b/demo/relay/leaf0.toml index e18d51e2741..d707c67837a 100644 --- a/demo/relay/leaf0.toml +++ b/demo/relay/leaf0.toml @@ -42,10 +42,10 @@ connect = ["https://localhost:4443/"] [auth] # Local development: anonymous access to everything, so no JWT is needed. -public = "**" +public = "" -# To exercise JWT + public-pattern auth instead, drop `public` above, run -# `moq auth serve --key root.jwk --public-subscribe 'anon/**' --public-subscribe 'demo/**' --public-publish 'anon/**' --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer`, +# To exercise JWT + public-prefix auth instead, drop `public` above, run +# `moq auth serve --key root.jwk --public-subscribe 'anon' --public-subscribe 'demo' --public-publish 'anon' --mtls-publish '' --mtls-subscribe '' --mtls-link peer`, # point the relay at it, and uncomment `listen.tls.root` above: # url = "http://127.0.0.1:4440/" diff --git a/demo/relay/leaf1.toml b/demo/relay/leaf1.toml index b888ff38256..d3e531cf5ce 100644 --- a/demo/relay/leaf1.toml +++ b/demo/relay/leaf1.toml @@ -42,10 +42,10 @@ connect = ["https://localhost:4443/", "https://localhost:4444/"] [auth] # Local development: anonymous access to everything, so no JWT is needed. -public = "**" +public = "" -# To exercise JWT + public-pattern auth instead, drop `public` above, run -# `moq auth serve --key root.jwk --public-subscribe 'anon/**' --public-subscribe 'demo/**' --public-publish 'anon/**' --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer`, +# To exercise JWT + public-prefix auth instead, drop `public` above, run +# `moq auth serve --key root.jwk --public-subscribe 'anon' --public-subscribe 'demo' --public-publish 'anon' --mtls-publish '' --mtls-subscribe '' --mtls-link peer`, # point the relay at it, and uncomment `listen.tls.root` above: # url = "http://127.0.0.1:4440/" diff --git a/demo/relay/localhost.toml b/demo/relay/localhost.toml index 9de1a2373e2..d488490e626 100644 --- a/demo/relay/localhost.toml +++ b/demo/relay/localhost.toml @@ -38,7 +38,7 @@ listen = "[::]:4443" # See root.toml and leaf.toml for auth and clustering examples. [auth] # Allow anonymous access to everything. -public = "**" +public = "" # Publish a single per-node stats broadcast on the cluster origin: # diff --git a/demo/relay/prod.toml b/demo/relay/prod.toml index 6d9802faf95..1ae1c624efa 100644 --- a/demo/relay/prod.toml +++ b/demo/relay/prod.toml @@ -44,16 +44,16 @@ key = "key.pem" # The auth server asked once per session event. The policy lives there: # # moq auth serve --listen 127.0.0.1:4440 --key root.jwk \ -# --public-subscribe 'anon/**' --public-subscribe 'demo/**' \ -# --public-publish 'anon/**' --public-publish 'demo/viewer/**' \ -# --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer +# --public-subscribe 'anon' --public-subscribe 'demo' \ +# --public-publish 'anon' --public-publish 'demo/viewer' \ +# --mtls-publish '' --mtls-subscribe '' --mtls-link peer # # Anonymous MoQ Boy viewers publish their commands under `demo/viewer/`, while only the # emulator, which signs in with a token, publishes games under `demo/boy/`. Anyone may # publish under another viewer's id; the emulator trusts nothing in a command beyond a # button state. # -# Anonymous CARLA drivers share the same `demo/viewer/**`, under which each one publishes +# Anonymous CARLA drivers share the same `demo/viewer`, under which each one publishes # its controls: a truck's broadcasts stay under `demo/carla/`, its worker's, which signs in # with a token. # diff --git a/demo/relay/root.toml b/demo/relay/root.toml index 9fb262e472d..befcbc3ea37 100644 --- a/demo/relay/root.toml +++ b/demo/relay/root.toml @@ -29,9 +29,9 @@ listen = "[::]:4443" [auth] # Local development: anonymous access to everything, so no JWT is needed. -public = "**" +public = "" -# To exercise JWT + public-pattern auth instead, drop `public` above, run -# `moq auth serve --key root.jwk --public-subscribe 'anon/**' --public-subscribe 'demo/**' --public-publish 'anon/**' --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer`, +# To exercise JWT + public-prefix auth instead, drop `public` above, run +# `moq auth serve --key root.jwk --public-subscribe 'anon' --public-subscribe 'demo' --public-publish 'anon' --mtls-publish '' --mtls-subscribe '' --mtls-link peer`, # point the relay at it, and uncomment `listen.tls.root` above: # url = "http://127.0.0.1:4440/" diff --git a/doc/bin/cli.md b/doc/bin/cli.md index bb7db8d865f..e9d5ec015dd 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -478,13 +478,14 @@ both `--connect` and a WAN cluster peer is refused at startup: use port 0, or dr ```bash moq auth generate --algorithm ES256 --out private.jwk --public public.jwk -moq auth sign --key private.jwk --root rooms/123 --publish 'alice/**' --subscribe '**' > alice.jwt +moq auth sign --key private.jwk --root rooms/123 --publish alice --subscribe '' > alice.jwt moq auth verify --key public.jwk --in alice.jwt -moq auth serve --listen 127.0.0.1:4440 --key-dir keys/ --public-subscribe 'anon/**' +moq auth serve --listen 127.0.0.1:4440 --key-dir keys/ --public-subscribe anon ``` -`--publish` and `--subscribe` take patterns: `alice` is one broadcast, -`alice/**` is a subtree, `**` is everything under `--root`. `moq auth serve` +`--publish` and `--subscribe` take prefixes: `alice` covers `alice` and +everything beneath it, and `''` is everything under `--root`. A wildcard such as +`alice/**` is refused. `moq auth serve` answers a relay's auth requests with the same keys (see [Auth server](/bin/relay/auth#auth-server)). It also reads every setting from an optional TOML file (`moq auth serve auth.toml`), where flags win and diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index 63ae6e6ecdb..ac249f640f9 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -10,13 +10,13 @@ A relay admits a session in exactly one of two ways: | Flag | What admits | | --- | --- | | `--auth-url` | An auth server, asked once per session event with everything the relay knows. `moq auth serve` is the reference server; a Worker or a service of your own answers the same contract. | -| `--auth-public` | A static grant for anonymous sessions: patterns rooted at `/`, like a token with an empty root. No server. | +| `--auth-public` | A static grant for anonymous sessions: prefixes rooted at `/`, like a token with an empty root. No server. | Setting both, or neither, fails at startup; an application that embeds the relay may leave both unset and decide [in process](#in-process) instead. Nothing else admits anyone: a verified client certificate is a fact the server weighs, never a grant on its own. A grant names publish and subscribe rights as -path patterns under a root, and the session can only see that part of the tree. +path prefixes under a root, and the session can only see that part of the tree. ## The contract @@ -30,7 +30,7 @@ certificate. Nothing is parsed on the server's behalf: the `jwt` query parameter `moq auth serve`, not of the relay. A path with a `.` or `..` segment is refused before the server is asked: a URL transport resolves those away, but a SETUP path arrives verbatim. The grant answers with `publish` and -`subscribe` pattern unions, each optionally [capped](#bitrate-caps), and optionally a `root` alias, read-only `mounts`, +`subscribe` prefix lists, each optionally [capped](#bitrate-caps), and optionally a `root` alias, read-only `mounts`, `expires`, a `revalidate` cadence, a stats `tier` and `label`, and a `link` (`peer` or `upstream`) to mark another relay and an [upstream link](/bin/relay/cluster#upstream-links). A `tier` is `/`-joined segments of ASCII letters, digits, `.`, `_`, and `-`, such as `region/sjc`, @@ -42,7 +42,9 @@ The relay reads a grant as strictly as a token's [claims](#claims): a field it does not know refuses the session, since a field added later may narrow the grant. A relay before 0.22 sends no `relay` and ignores a field it does not know, enforcing the grant without it, so a server branches on `relay` before -sending a field newer than 0.22. Such a relay reads `peer: true` in place of +sending a field newer than 0.22. Such a relay reads a bare `foo` as one path, so +`moq_auth` and `@moq/auth`'s `GrantSchema` spell each prefix as `foo/**` or `**`, +which every relay reads as the subtree. It also reads `peer: true` in place of `link` and knows no upstream: `moq_auth` writes `peer: true` beside every `link`, so that relay admits an upstream as a plain peer, which can carry traffic between two upstreams. Where that matters, refuse the session when the request has no `relay` (or one older than 0.22), @@ -156,7 +158,7 @@ moq auth generate --algorithm ES256 --out private.jwk --public public.jwk # Let the bearer publish under rooms/123/alice and subscribe to anything in rooms/123. # The token expires in an hour unless --ttl, --expires, or --unbounded says otherwise. -moq auth sign --key private.jwk --root rooms/123 --publish 'alice/**' --subscribe '**' > alice.jwt +moq auth sign --key private.jwk --root rooms/123 --publish alice --subscribe '' > alice.jwt moq auth verify --key public.jwk --in alice.jwt ``` @@ -199,7 +201,7 @@ it is retried with backoff like any other failure, within the retry window, presenting whatever tokens are current. Older versions refuse in-band tokens. -Each accepted token adds its publish and subscribe patterns to the session's +Each accepted token adds its publish and subscribe prefixes to the session's union, within its admitted root and SETUP role. A publish-only connection cannot become a subscriber. A replacement must retain the root, mounts, and routing classification. Dropping a token handle, expiry, or refusal at revalidation @@ -222,8 +224,8 @@ cannot verify a presented token; both refuse it as unsupported. On moq-transport each distinct `AUTHORIZATION TOKEN` is verified once while any open request on that session names it. Its grant admits only requests naming that token, independently of the session's scope. Discovery reveals only paths it grants; -a `PUBLISH_NAMESPACE` grant must cover the whole namespace: `room/**`, not just -`room`. The last naming request ending releases the lease at the auth server; +a `PUBLISH_NAMESPACE` grant must cover the whole namespace: a grant of `room/a` +does not admit announcing `room`. The last naming request ending releases the lease at the auth server; a later request verifies the token again. Expiry or revocation ends every request naming it. Expiry says so, so the client knows to renew the token: a request not yet answered is refused with `EXPIRED_AUTH_TOKEN`, and an answered @@ -251,8 +253,8 @@ outage still closes. | Claim | Meaning | | --- | --- | | `root` | Base path. Optional. | -| `publish` | Patterns the bearer may publish under `root`. `**` means everything; omitted means no publishing. | -| `subscribe` | Patterns the bearer may subscribe to under `root`. Same rules. | +| `publish` | Prefixes the bearer may publish under `root`, each covering itself and everything beneath it. `""` means everything; omitted means no publishing. | +| `subscribe` | Prefixes the bearer may subscribe to under `root`. Same rules. | | `exp`, `iat`, `nbf` | Expiry, issue time, and not-before. `exp` ends that credential's grant, including after connect; another accepted token can keep the session alive. A token is refused from its `exp` on and before its `nbf`. | | `jti` | The token's ID, which its issuer may record to revoke it: 1 to 128 printable ASCII characters, without spaces. Optional; `moq auth sign --id` writes one. | | `iss`, `sub` | Read and ignored. | @@ -262,14 +264,14 @@ claim may narrow the grant, and a misspelled `root` would otherwise widen it to everything. Put application data somewhere other than the token. Tokens and key scopes from the older `moq-token` format still verify: each -`put` and `get` prefix `p` reads as the subtree `p/**`. Nothing writes that +`put` and `get` entry reads as the same prefix. Nothing writes that form any more, so a verifier that reads only `put` and `get`, such as moq-relay 0.14 and earlier, refuses a new token. ### Bitrate caps -A role may cap its bitrate, in bits per second, by naming its patterns as -`{"path":["alice/**"],"rate":8000000}` in a token or an auth-server grant. A +A role may cap its bitrate, in bits per second, by naming its prefixes as +`{"path":["alice"],"rate":8000000}` in a token or an auth-server grant. A `publish` cap bounds what the session sends the relay, and a `subscribe` cap what the relay sends it, QUIC overhead and retransmissions included. Each averages over a second, with one second of the rate as a burst. Older relays @@ -289,23 +291,29 @@ the relay opens the window once it admits the session. ### Path matching -Grants are [patterns](https://docs.rs/moq-pattern) relative to `root`: `foo` -is exactly `foo`, `foo/**` is `foo` and everything beneath it, `live/*` is -every broadcast one segment under `live`, and `**` is everything. Matching is -on path boundaries (`foo/**` covers `foo/bar` but not `foobar`), and the relay +Grants are [prefixes](https://docs.rs/moq-pattern) relative to `root`: `foo` +covers `foo` and everything beneath it, and the empty prefix `""` covers +everything. A grant takes no wildcard: `foo/**`, `**`, `live/*`, or `cam-*.hang` +is refused in `moq auth serve` rules, relay config, and CLI flags, with a +message naming the prefix to write instead. Tokens, key scopes, and auth +server grants spell each prefix as the subtree pattern releases before 0.22 +read, `foo/**` and `**`, and read that spelling back as the prefix, so they work +with relays and verifiers on either side of the upgrade; any other wildcard +there is refused. +Matching is on path boundaries (`foo` covers `foo/bar` but not `foobar`), and the relay announces, publishes, and resolves only paths inside the grant. `moq auth serve` authorizes the token at the dialed path: the path may equal the root, extend it (which narrows the grant), or be a parent of it (the grant still applies at the root). An unrelated path is rejected. The session's root is always the path it dialed, and what it sees is named -relative to that. For a grant of `demo/**` from the root: +relative to that. For a grant of `demo` from the root: | Dialed | Session root | Granted | | --- | --- | --- | -| `/demo/bar` | `demo/bar` | `**` | -| `/demo` | `demo` | `**` | -| `/` | \`\` | `demo/**` | +| `/demo/bar` | `demo/bar` | everything (`""`) | +| `/demo` | `demo` | everything (`""`) | +| `/` | \`\` | `demo` | | `/other` | refused | | Anonymous and mTLS rules, on the relay and in `moq auth serve`, are authorized @@ -313,9 +321,9 @@ the same way, as a token with an empty root. | root | publish | subscribe | Publish | Subscribe | | --- | --- | --- | --- | --- | -| `demo` | `my-stream/**` | `**` | `demo/my-stream/**` | `demo/**` | -| `demo` | (none) | `**` | nothing | `demo/**` | -| `""` | `**` | `**` | everything | everything | +| `demo` | `my-stream` | `""` | `demo/my-stream` | `demo` | +| `demo` | (none) | `""` | nothing | `demo` | +| `""` | `""` | `""` | everything | everything | Libraries: [`moq-auth`](/lib/rs/moq-auth) (Rust) and [`@moq/auth`](/lib/js/auth) (TypeScript) sign and verify the same tokens. @@ -323,20 +331,21 @@ Libraries: [`moq-auth`](/lib/rs/moq-auth) (Rust) and [`@moq/auth`](/lib/js/auth) ```toml [auth] -public = "anon/**" # anyone may publish and subscribe under anon/ +public = "anon" # anyone may publish and subscribe under anon/ # or asymmetric rules: -public_subscribe = ["anon/**", "demo/**"] -public_publish = ["anon/**"] +public_subscribe = ["anon", "demo"] +public_publish = ["anon"] ``` A static grant with no expiry and no re-check. Nothing here verifies a token, so a session presenting one is refused rather than admitted on the public -grant. The patterns are rooted at `/`, like a token with an empty root (see -[Path matching](#path-matching)): `anon/**` admits a session dialed at `/`, -`/anon`, or `/anon/room`, and refuses one dialed anywhere else. A pattern with -no wildcard names one broadcast: `anon` is the broadcast `anon`, and `anon/**` is the subtree. -`public = "**"` opens everything and is for development only. With +grant. The prefixes are rooted at `/`, like a token with an empty root (see +[Path matching](#path-matching)): `anon` admits a session dialed at `/`, +`/anon`, or `/anon/room`, and refuses one dialed anywhere else, `/anonymous` +included. The empty prefix, `public = ""` (or `--auth-public ''`, or +`MOQ_AUTH_PUBLIC=` set but empty), opens everything and is for development only. +A wildcard such as `anon/**` or `**` is refused at startup. With `--auth-url` the anonymous rules live on the server instead (`moq auth serve --public-*`). @@ -355,7 +364,7 @@ client certificates under `listen.tls.root`; plain HTTPS verifies them under refuses `listen.tls.root`. Cluster peers are admitted the same way, so a mesh runs -`moq auth serve --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer` (or a +`moq auth serve --mtls-publish '' --mtls-subscribe '' --mtls-link peer` (or a server granting its cluster CA everything with `link: "peer"`); see [Clustering](/bin/relay/cluster). `--mtls-link peer` marks every certificate as another relay; leave it off when certificates identify clients. Without a @@ -391,9 +400,9 @@ chain in decides which rule applies. dotted key; a flag given on the command line wins. Only the file holds per-certificate rules, keyed by any hash in `tls.chain`. The rule naming the certificate nearest the leaf applies: it can set `publish` and `subscribe` -(each a pattern list, or `{ path = [...], rate = bps }` with a +(each a prefix list, or `{ path = [...], rate = bps }` with a [cap](#bitrate-caps)), `link` (`peer`, `upstream`, or `none`), and `tier`, -and each field it omits takes the server-wide value. Its patterns replace the +and each field it omits takes the server-wide value. Its prefixes replace the shared ones, so a rule with empty lists revokes. A certificate no rule names gets `--mtls-*`. The server watches the file: an edit takes effect at each session's next re-check, and one that fails to load keeps the last valid @@ -405,13 +414,13 @@ revalidate = "1m" expires = "1d" [mtls] -publish = ["**"] -subscribe = ["**"] +publish = [""] +subscribe = [""] link = "peer" # cluster CA: every other certificate is a relay [mtls.certificates.3f2a...] # an affiliate's intermediate CA: everything it issued -publish = { path = ["affiliate-a/**"], rate = 20000000 } -subscribe = ["affiliate-a/**"] +publish = { path = ["affiliate-a"], rate = 20000000 } +subscribe = ["affiliate-a"] link = "none" tier = "affiliate-a" @@ -435,8 +444,8 @@ Update any policy keyed on `tcp` for stream links to handle `tls` too. ```bash moq auth serve --listen 127.0.0.1:4440 \ --key-dir /etc/moq/keys \ - --public-subscribe 'anon/**' --public-publish 'anon/**' \ - --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer \ + --public-subscribe anon --public-publish anon \ + --mtls-publish '' --mtls-subscribe '' --mtls-link peer \ --tier edge --expires 1d --revalidate 1m --limit-remote 64 ``` @@ -479,25 +488,24 @@ also guards a loopback listener. See `moq auth serve --help` for the rest. ### Migrating from the relay flags The 0.14 relay's auth flags moved to `moq auth serve`, and the relay points -`--auth-url` at it. A relay without a server keeps `--auth-public`, as -patterns: `--auth-public 'PREFIX/**'`. +`--auth-url` at it. A relay without a server keeps `--auth-public PREFIX`. | 0.14 relay flag | `moq auth serve` | | --- | --- | | `--auth-key FILE` | `--key FILE` | | `--auth-key-dir DIR` | `--key-dir DIR` | -| `--auth-public PREFIX` | `--public-publish 'PREFIX/**' --public-subscribe 'PREFIX/**'` | -| `--auth-public-publish` / `--auth-public-subscribe` | `--public-publish` / `--public-subscribe`, as patterns | +| `--auth-public PREFIX` | `--public-publish PREFIX --public-subscribe PREFIX` | +| `--auth-public-publish` / `--auth-public-subscribe` | `--public-publish` / `--public-subscribe` | | `--auth-public-api URL` | your own server answering the contract | | `--auth-mtls-tier LABEL` | `--tier LABEL` (one tier per server) | -| `listen.tls.root` alone admitting a peer unscoped | `--mtls-publish '**' --mtls-subscribe '**' --mtls-link peer` | +| `listen.tls.root` alone admitting a peer unscoped | `--mtls-publish '' --mtls-subscribe '' --mtls-link peer` | | `--auth-api` (token or proxy mode), `Cache-Control` | `--auth-url` pointed at any server answering the contract; `revalidate` and `expires` in the grant | | `--auth-domain` | your server reads `host` (present over plaintext too) and decides | Both on one host: ```bash -moq auth serve --listen 127.0.0.1:4440 --key-dir /etc/moq/keys --public-subscribe 'anon/**' +moq auth serve --listen 127.0.0.1:4440 --key-dir /etc/moq/keys --public-subscribe anon moq-relay --auth-url http://127.0.0.1:4440/ ``` diff --git a/doc/bin/relay/cluster.md b/doc/bin/relay/cluster.md index d3ae9a76f39..402302fb995 100644 --- a/doc/bin/relay/cluster.md +++ b/doc/bin/relay/cluster.md @@ -334,7 +334,7 @@ URL, `token` on a peer object, or a shared `cluster.token` file for every listed peer, re-read on every dial so a rotated token applies at the next redial). The accepting relay admits a peer like any client, so a mesh needs an auth server that grants the cluster CA, such as -`moq auth serve --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer`. An +`moq auth serve --mtls-publish '' --mtls-subscribe '' --mtls-link peer`. An accepted peer counts as a cluster peer only when its grant sets a `link`, as `--mtls-link peer` does; otherwise what it announces counts as ingest here, like a client's, and it lists [hidden](/concept/moq-lite#hidden-broadcasts) routes diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index c9615c69654..bceca2fb7e2 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -257,9 +257,9 @@ See [HTTP endpoints](/bin/relay/http). # Exactly one of these: url = "http://127.0.0.1:4440/" # An auth server asked once per session event (`moq auth serve`, # or your own). https:// presents connect.tls; unix:// is a socket. -# public = "anon/**" # Or a static anonymous grant rooted at /, publish and subscribe alike. -# public_subscribe = ["anon/**", "demo/**"] # Or split them; patterns, `foo/**` for a subtree. -# public_publish = ["anon/**"] +# public = "anon" # Or a static anonymous grant rooted at /, publish and subscribe alike. +# public_subscribe = ["anon", "demo"] # Or split them; each prefix covers its subtree, "" covers everything. +# public_publish = ["anon"] ``` See [Authentication](/bin/relay/auth). diff --git a/doc/bin/relay/index.md b/doc/bin/relay/index.md index c0b5e9ad225..b444dc46bb3 100644 --- a/doc/bin/relay/index.md +++ b/doc/bin/relay/index.md @@ -46,7 +46,7 @@ tls.generate = ["localhost"] listen = "[::]:4443" # serves the certificate fingerprint for local browsers [auth] -public = "**" # anonymous access to everything; development only +public = "" # anonymous access to everything; development only ``` Every option is also a `--flag` or `MOQ_*` environment variable, and diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 38a72e4ff00..bf385f5f5d7 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -189,12 +189,21 @@ explicit opt-in on the announce request. On moq-transport that opt-in is the [hidden](/draft/moq-hidden) extension; a peer that never declares it receives every authorized namespace, dot-prefixed ones included. -## Path patterns - -Patterns describe sets of literal paths. They are how a token scopes what a -session may publish and subscribe to, and how a consumer filters the -announcements it is told about. They never travel as announcements: the wire -carries prefixes, and each side filters locally. +## Grants and filters + +A grant, what a token or an origin scope lets a session publish and subscribe +to, is a set of path prefixes. A prefix covers its path and everything beneath +it: `room` covers `room`, `room/alice`, and `room/alice/cam`, but not `roomy`. +The empty prefix covers everything. A grant holds no wildcard: `room/**`, `**`, +`*`, or `cam-*.hang` is refused where the grant is made, so write `room` for +`room/**` and the empty prefix for `**`. Tokens and auth server grants spell a +prefix as `room/**` or `**` until releases before 0.22 retire, since those read +a bare member as one path. Since every grant covers +whole subtrees, a route a grant admits always serves whole subtrees too. + +Patterns are discovery filters: they pick which announcements a consumer lists, +and never widen or narrow what it may request. They never travel as +announcements: the wire carries prefixes, and each side filters locally. A pattern matches the whole path. `room` matches only `room`, while `room/**` matches `room` and every descendant. Segments are separated by `/`: @@ -214,7 +223,7 @@ there is no escape for a literal `*`. The empty pattern matches the empty path. A subscriber watching under a root sees advertisements named relative to that root. When several routes advertise one prefix, each reader sees the best route its scope can use. A listing reports only routes whose literal name matches its -patterns: under `**/*.chat`, a claim at `room` is left out even though it could +filter: under `**/*.chat`, a claim at `room` is left out even though it could serve `room/lobby.chat`, and so is a route above the root. Requests still resolve through such covering routes, and a session still forwards them to its peer, which routes through them. Code that routes or forwards opts in with the @@ -224,9 +233,8 @@ JS. ## Authorization On moq-lite 07 (`moq-lite-07-wip`, opt-in) each side presents a token on its -own Auth stream and learns what it may publish and subscribe to: a union of -[path patterns](#path-patterns), delivered exactly as issued rather than widened -to a prefix. Right after setup both sides present the credential the connection +own Auth stream and learns what it may publish and subscribe to: a set of +[path prefixes](#grants-and-filters), delivered exactly as issued. Right after setup both sides present the credential the connection already carried (the URL token, a client certificate, or nothing), so a publisher learns before anyone subscribes whether its broadcasts can reach the peer. More tokens can be added without reconnecting; the session's scope is the diff --git a/doc/lib/index.md b/doc/lib/index.md index 78fb2806cfe..c5135de3be9 100644 --- a/doc/lib/index.md +++ b/doc/lib/index.md @@ -76,7 +76,7 @@ how it looks in that language: - **Catalog extensions**: write your own section next to `video` and `audio`, and read others' back. - **Routes**: see which relays a broadcast came through, and advertise a cost as a standby publisher. - **Connection stats** and **errors** that tell an auth rejection (don't retry) from a shutdown or a transport failure. -- **Authorization in band**: read the grant the relay sent back (the patterns this side may publish and subscribe to, and when it lapses), and present a fresh token before the current one expires, without reconnecting. It needs `moq-lite-07-wip`, which is opt-in. +- **Authorization in band**: read the grant the relay sent back (the path prefixes this side may publish and subscribe to, and when it lapses), and present a fresh token before the current one expires, without reconnecting. It needs `moq-lite-07-wip`, which is opt-in. C cannot fetch a single group by start. diff --git a/doc/lib/js/auth.md b/doc/lib/js/auth.md index 675cbaa1505..ee57607c5df 100644 --- a/doc/lib/js/auth.md +++ b/doc/lib/js/auth.md @@ -15,11 +15,16 @@ contract with one import. It also generates keys, signs and verifies JWTs, and checks a connection path against a token's claims exactly as `moq auth serve` does. A grant can label its session's stats independently of its authorization root. Tokens are interchangeable with the Rust side. Grants and claims are -[`Pattern`](https://www.npmjs.com/package/@moq/pattern) unions: `foo` is one -broadcast, `foo/**` is a subtree, `**` is everything. A claim or grant role is -an `Access`: its pattern array, or `{ path, rate }` with a bitrate cap. A -`Grant` is typed as its wire shape, so a typed `Grant` is the JSON to answer -with, and the relay refuses a field it does not know. `Claims` spell a token's +[`Prefix`](https://www.npmjs.com/package/@moq/pattern) sets: `foo` covers `foo` +and everything beneath it, and the empty prefix is everything. `Key.sign` and +the CLI refuse a wildcard such as `foo/**` or `*`. A token, key scope, or grant +spells each prefix as the subtree pattern releases before 0.22 read, `foo/**` +or `**`, and reads that spelling back as the prefix. A claim or grant role is +an `Access`: its prefix array, or `{ path, rate }` with a bitrate cap. A +`Grant` is typed as its wire shape: answer with `GrantSchema.parse(grant)`, +which spells it as every relay reads it, and the relay refuses a field it does +not know. A parsed key spells its scope the same way, and so does `Key.public`, +so write a key you scope by hand from `Key.parse` of it or from `Key.public`. `Claims` spell a token's `exp`, `iat`, `nbf`, and `jti` as `expires`, `issued`, `notBefore`, and `id`, as the Rust crate does. Key scopes bound paths only, and `Claims.authorize` checks paths only. @@ -27,7 +32,7 @@ paths only. ```bash bun add @moq/auth bunx @moq/auth generate --out root.jwk -bunx @moq/auth sign --key root.jwk --root "rooms/123" --publish 'alice/**' +bunx @moq/auth sign --key root.jwk --root "rooms/123" --publish alice ``` Mint tokens on a server, never in the browser, and prefer asymmetric keys so diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index e5bae61afee..387b33c5e27 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -73,8 +73,8 @@ Runs in the browser and, over WebSocket, in Node, Bun, and Deno; see [server-side](/lib/js/#server-side). The WebSocket fallback and the moq-transport stack load on demand as separate chunks, so a moq-lite session WebTransport wins within its head start never downloads them. -Path patterns are on the -[concept page](/concept/moq-lite#path-patterns). +Grant prefixes and discovery-filter patterns are on the +[concept page](/concept/moq-lite#grants-and-filters). Groups retain a 32 MiB live tail without limiting their lifetime or frame count. Readers that fall behind the shed front receive `Evicted`; tracks that diff --git a/doc/lib/rs/index.md b/doc/lib/rs/index.md index 9366e0abbc4..f11fc8b2294 100644 --- a/doc/lib/rs/index.md +++ b/doc/lib/rs/index.md @@ -24,7 +24,7 @@ missing required CPU feature before starting transport cryptography. | Crate | Does | | --- | --- | | [moq-net](/lib/rs/moq-net) | The pub/sub layer: sessions, origins, broadcasts, tracks, groups, frames. Transport-agnostic. | -| [moq-pattern](https://docs.rs/moq-pattern) | Path patterns such as `room/**`. Re-exported by moq-net and moq-auth. | +| [moq-pattern](https://docs.rs/moq-pattern) | Grant prefixes and discovery-filter patterns such as `room/*/chat`. Re-exported by moq-net and moq-auth. | | [moq-tokio](https://docs.rs/moq-tokio) | Connects and listens over QUIC, WebTransport, WebSocket, TCP, or Unix sockets (Unix only), from config or CLI flags. | | [qmux](https://docs.rs/qmux) | QUIC streams, datagrams, and flow control over TCP, TLS, WebSocket, and Unix sockets (Unix only), on moq-quic's state machine. | | [moq-sock](https://docs.rs/moq-sock) | Socket binding and CPU pinning for thread-per-core listeners. | diff --git a/doc/lib/rs/moq-auth.md b/doc/lib/rs/moq-auth.md index 03f4a625d1b..8b5e2eae6c5 100644 --- a/doc/lib/rs/moq-auth.md +++ b/doc/lib/rs/moq-auth.md @@ -13,7 +13,7 @@ Everything a party needs to ask for or answer an authorization on answers the relay, in a service that mints tokens for clients, or in your own accept loop that decides in process. -- **Request and grant.** `Request` is the JSON a relay POSTs on each session event (`connect`, `attach`, `revalidate`, `end`), carrying everything it knows about the session and the `relay` software asking. `Grant` is the answer: a publish and a subscribe `Access`, each a [`Pattern`](https://docs.rs/moq-pattern) union with an optional [bitrate cap](/bin/relay/auth#bitrate-caps), an expiry, a revalidation cadence, optional stats tier and label, and the `Link` that marks a cluster peer or an [upstream link](/bin/relay/cluster#upstream-links). A grant with a field this reader does not know is refused. `Grant::validate` checks the grant contract; the accept loop must enforce its caps or refuse it. Expiry is exact, with no allowance for clock skew. +- **Request and grant.** `Request` is the JSON a relay POSTs on each session event (`connect`, `attach`, `revalidate`, `end`), carrying everything it knows about the session and the `relay` software asking. `Grant` is the answer: a publish and a subscribe `Access`, each a [`Prefix`](https://docs.rs/moq-pattern) set (a prefix covers its path and everything beneath it, written `foo/**` as releases before 0.22 read it; any other wildcard is refused) with an optional [bitrate cap](/bin/relay/auth#bitrate-caps), an expiry, a revalidation cadence, optional stats tier and label, and the `Link` that marks a cluster peer or an [upstream link](/bin/relay/cluster#upstream-links). A grant with a field this reader does not know is refused. `Grant::validate` checks the grant contract; the accept loop must enforce its caps or refuse it. Expiry is exact, with no allowance for clock skew. - **Lease.** `lease::Producer` and `lease::Consumer` are the handle a session holds for its grant: read it, wait for a change, revoke it, and learn why it ended. Whoever runs the accept loop builds the producer, so an embedder can decide in process with no HTTP. - **Refusals.** `Refusal` carries a `refusal::Reason` (`Refused`, `Forbidden`, or `Expired`) and optional `root` and `tier`. An auth server returns it as JSON with HTTP 403; `Client` returns `Error::Refused(refusal)`. Legacy plain-text 401/403 bodies and unknown reason strings read as `Refused`. Set `root` only from a verified credential, never the dialed path. Embedded relay deciders pass the same shape in `auth::Error::Refused`. - **Client.** `Client` drives a lease against an auth server over `https://`, `unix://`, or loopback `http://`. It revalidates on cadence and retries through an outage until the grant expires, revokes on a 401, 403, or a grant it cannot read or accept, and reports `end` with the session's byte totals. diff --git a/doc/lib/rs/moq-net.md b/doc/lib/rs/moq-net.md index 3e36e0a09c4..73f2f86c582 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -41,5 +41,5 @@ cargo add moq-tokio See the [Rust quick start](/lib/rs/#quick-start) and [docs.rs/moq-net](https://docs.rs/moq-net). The TypeScript twin is -[`@moq/net`](/lib/js/net). Path patterns are on the -[concept page](/concept/moq-lite#path-patterns). +[`@moq/net`](/lib/js/net). Grant prefixes and discovery-filter +patterns are on the [concept page](/concept/moq-lite#grants-and-filters). diff --git a/doc/setup/upgrade.md b/doc/setup/upgrade.md index 2b9d44ef4ad..bd7170b05a8 100644 --- a/doc/setup/upgrade.md +++ b/doc/setup/upgrade.md @@ -34,9 +34,24 @@ replaces it. These land with the next breaking release. +- **Grants are prefixes.** A token, key scope, `moq auth serve` rule, relay + public rule, origin scope, and auth-server grant lists path prefixes, each + covering its path and everything beneath it. Config, flags, and environment + variables refuse a wildcard at startup: rewrite `foo/**` as `foo` and `**` as + the empty prefix `""` when you upgrade each binary. Tokens, key scopes, and + auth-server grants work in any upgrade order: new issuers and auth servers + spell each prefix as `foo/**` or `**`, which a released relay or verifier reads + as the same subtree, and an upgraded one reads what released ones write, + `put`/`get` and those subtree patterns alike. A bare `foo` minted since grants + became patterns widens from one broadcast to its subtree, and any other + wildcard, such as `*/chat`, is refused, so re-mint those tokens first. The + next breaking release writes bare prefixes, which only this release onward + reads. + - **WebRTC moves to `moq-webrtc`.** The `moq-rtc` crate is renamed `moq-webrtc`: replace `moq_rtc::` with `moq_webrtc::`. Its last release stays on crates.io. Nothing changes on the wire. + - **The default priority byte moves again.** Priorities are signed in every API, -128 to 127 with 0 unset, so nobody has to know that 127 is the middle of a byte. The wire keeps one byte and one urgency on both protocols, so an @@ -45,44 +60,54 @@ These land with the next breaking release. hang's media priorities now sit above that default rather than below it. Mixed versions only differ in how an unset priority ranks against a set one, so upgrade publishers and players in either order. + - **moq-transport drafts 14 through 17, 19, and 21 are dropped.** Rust and JavaScript speak moq-transport drafts 18, 20, and 22, and the `moql` SETUP negotiates moq-lite only. Before upgrading, move peers that offer only dropped drafts to draft 18, 20, or 22, or a moq-lite version, so they can still connect. + - **WebSocket needs a QMux subprotocol** (#6231). Relays answer a WebSocket upgrade that offers no `qmux-NN` subprotocol with 400 instead of falling back to the legacy `webtransport` format, so upgrade such clients before relays. + - **Data-track breaks are an object with no bytes** (#5815). A hang data track (TS private PES, sections, passthrough, text) marks a discontinuity with a group of one empty object instead of a skipped group. Older consumers fail to decode it, so upgrade exporters and players before publishers. + - **Auth requests name `webtransport`** (#5293). Relays report a WebTransport session as `webtransport` instead of `quic`, and older auth servers reject the new value, so upgrade auth servers before relays. + - **Auth roles are pattern arrays.** Uncapped tokens and grants keep their format. An object role, including one with a `rate` cap, is refused; use pattern arrays for `publish` and `subscribe`. + - **The join interval replaces archive timelines.** The hang catalog's root `archive` section is gone, and the root `clock` moved to `sync.clock`. The catalog's `sync.interval`, the longest GOP of any track, is the grid an HLS exporter numbers segments on from the media tracks themselves; Rust publishers and `@moq/publish` declare it. Neither side reads the other's shape, so upgrade HLS exporters and their publishers together. + - **Announce restarts.** On moq-lite 07 another instance at a path starts before the one it replaces ends, and `ANNOUNCE_UPDATE` carries a higher epoch of the same one. lite-06's `ANNOUNCE_RESTART` is named `ANNOUNCE_UPDATE`, as it always meant. Elsewhere, a restart is still an end then a start. + - **Publisher instances ride the route.** moq-lite 07 carries the instance and epoch after the path in `ANNOUNCE_START`, and the pinned instance in `TRACK`, `TRACKS`, `SUBSCRIBE`, and `FETCH`. Older wires carry none, so the receiving session mints an instance per announcement: default lite-06 sessions, including cluster GOAWAY redials, lose seamless cross-route failover until lite-07 is the default. + - **Route cost is one value** (#4661). Upgraded nodes write lite-06's cold cost at 2^62-1, so an older relay that still ranks on `(warm, cold)` breaks equal-warm ties against routes that crossed an upgraded node. It is only a tie-break and ends once the fleet is upgraded. + - **A track is timed or untimed, and nothing fills in a timestamp on receive.** Tracks from moq-lite before 05, and moq-transport tracks without `TIMESCALE` arrive untimed instead of @@ -90,6 +115,7 @@ These land with the next breaking release. carry a send time instead, which their receivers pass through as the track's time; see [untimed tracks](/concept/moq-lite#subscriptions). A new subscriber with no start on an untimed track starts at the latest group. + - **Max age and max delay count wall-clock time too.** A superseded group is stale once its successor arrived a full budget ago, as well as by media time, so an untimed track is no longer exempt. With the default zero diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index fd344f0455f..9c038767ef8 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -404,7 +404,7 @@ The initial set is relative to this hop: a relay answers with what it holds with The publisher MAY coalesce changes to a claim it has not yet sent, and sends no more claims before SYNCED than the initial set holds. A retraction of a claim or route already sent MAY precede SYNCED. A prefix outside the subscriber's [scope](#auth-stream) lists nothing and is SYNCED at once. -A prefix wider than the scope is answered as if each of the scope's regions inside it were requested, a region being the leading literal segments of one of the scope's [patterns](#path-pattern), and none lying within another: a claim above a region is presented at that region, sent only within the scope as any presented claim is (below), and the prefix's SYNCED follows every region's initial set. +A prefix wider than the scope is answered as if each of the scope's regions inside it were requested, a region being one of the scope's [prefixes](#path-prefix) not lying within another: a claim above a region is presented at that region, sent only within the scope as any presented claim is (below), and the prefix's SYNCED follows every region's initial set. So a subscriber whose scope's regions do not fit one ANNOUNCE_REQUEST asks for a prefix they share, the empty prefix at the widest, and still learns every region. A change to the scope takes effect on the publisher's stream before the ANNOUNCE_OK answering any edit the publisher reads after it sends or reads the AUTH_OK, AUTH_ERROR, or stream end making the change. An edit MAY change nothing, so a subscriber whose scope widens under a prefix it already asked for can send an empty edit and treat the region as synced at its ANNOUNCE_OK. @@ -431,7 +431,8 @@ When the session closes, an endpoint MUST assume that every route and claim on t A broadcast path has at most 32 segments and at most 4,096 bytes; a receiver MUST close the session with a PROTOCOL_VIOLATION on a longer one, except one resolved by [Compression](#announce-compression) or joined beneath a root of the receiver's own, whose claim it drops: the ANNOUNCE_START still assigns its Announce ID, the messages naming it stay valid, and the receiver SHOULD log and count it. A claim covers a path when its prefix is a leading run of the path's segments; matching is per path segment, so a prefix never matches half a segment, and equality is byte-by-byte within each segment. A publisher presents each claim inside a requested prefix and only the most specific claim at or above it, at that prefix itself, or at each region answering it, and sends those within the subscriber's scope, which is exactly the covered set the subscriber may see. -A presented claim is within the scope when the scope matches a path it covers, as a claim at `a` is under the scope `a/*`, which does not match `a` itself: a prefix answered with no claim has none covering a path the scope matches. +A presented claim is within the scope when the scope covers a path the claim covers: when one of the scope's prefixes lies at, beneath, or above the path the claim is presented at. +Since a scope covers whole subtrees, a claim within it is within it at every path it is presented at. A claim presented above its own prefix carries how many segments broader it is than the path it is presented at, its Above, so the subscriber can tell it from a claim exactly at that path. Every path on the stream is relative to the session, whichever requested prefix it lies in. @@ -2102,43 +2103,36 @@ AUTH_OK Message { Type (i) = 0x0 Message Length (i) Publish Count (i) - Publish Pattern (s) ... + Publish Prefix (s) ... Subscribe Count (i) - Subscribe Pattern (s) ... + Subscribe Prefix (s) ... Expires (i) } ~~~ -**Publish Pattern**: -A [pattern](#path-pattern) matching broadcast paths the opener may announce and serve. +**Publish Prefix**: +A [prefix](#path-prefix) covering broadcast paths the opener may announce and serve. A count of zero grants none. -**Subscribe Pattern**: -A [pattern](#path-pattern) matching broadcast paths the opener may subscribe to. +**Subscribe Prefix**: +A [prefix](#path-prefix) covering broadcast paths the opener may subscribe to. **Expires**: The number of milliseconds until the grant lapses, or 0 for never. A value greater than 2^53 - 1 is invalid and MUST close the session with PROTOCOL_VIOLATION. The acceptor revokes a lapsed grant with AUTH_ERROR; the opener uses Expires to present a replacement token in time. -### Path Pattern {#path-pattern} -A pattern is a set of broadcast paths, written as `/`-separated segments split as a path's are, so a literal names a segment holding a `/` by escaping it. -Each segment is one of: +### Path Prefix {#path-prefix} +A prefix is a broadcast path, written as `/`-separated segments split as a path's are, so a segment holding a `/` escapes it. +It covers itself and every path beneath it: `room` covers `room` and `room/alice` but not `roomy`, and the empty prefix covers every path. +Matching is per path segment, as a claim's is. -- a literal, matching that segment exactly; -- `*`, matching any one segment; -- a literal with one `*` inside it, such as `cam-*.hang`, matching any segment that starts with the bytes before the `*` and ends with the bytes after it, without overlapping them; -- `**`, matching zero or more segments. +A prefix has at most 32 segments and no leading, trailing, or repeated `/`. +It holds no `*`: a grant names whole subtrees and never a wildcard, so a route a grant admits always serves whole subtrees. +A prefix that breaks any of these rules is a PROTOCOL_VIOLATION. -A pattern matches a path when its segments match the path's segments in order, covering the whole path. -The empty pattern matches only the empty path, `room/**` matches `room` and every path beneath it, and `**` matches every path. - -A pattern has at most 32 segments and at most one `**`. -It has no leading, trailing, or repeated `/`, no segment with more than one `*`, and no `**` combined with other bytes in a segment. -It is canonical: a `**` is never immediately preceded by a `*` segment, since `*/**` and `**/*` match the same paths and only `**/*` is valid. -A pattern that breaks any of these rules is a PROTOCOL_VIOLATION. - -A grant covers a request when one of its patterns matches the path. +A grant covers a request when one of its prefixes covers the path. +A sender SHOULD omit a prefix lying within another of the same grant, since it covers nothing more. An endpoint MUST close the session with PROTOCOL_VIOLATION when an AUTH stream message fails to decode, has trailing body bytes, or has an unexpected type for its direction. The opener sends exactly one AUTH; any additional message on its side is a PROTOCOL_VIOLATION. @@ -2284,7 +2278,7 @@ A stream that FINs without a GROUP_END carries a closed Group, which may have he ### Authorization {#changelog-07-auth} -- Added the Auth Stream (0x7) with AUTH, AUTH_OK, and AUTH_ERROR: either endpoint presents a token on its own stream and learns the [path patterns](#path-pattern) it may publish and subscribe to. The union of a session's open grants is its scope. A shrink withdraws what it no longer covers, and an announcement outside the scope closes the session with UNAUTHORIZED. A peer without the stream resets it. +- Added the Auth Stream (0x7) with AUTH, AUTH_OK, and AUTH_ERROR: either endpoint presents a token on its own stream and learns the [path prefixes](#path-prefix) it may publish and subscribe to, each covering its whole subtree; a prefix holding a `*` is a PROTOCOL_VIOLATION. The union of a session's open grants is its scope. A shrink withdraws what it no longer covers, and an announcement outside the scope closes the session with UNAUTHORIZED. A peer without the stream resets it. - Scope widens before AUTH_OK. A presenter waits for unanswered non-empty tokens before sending an uncovered request or rejecting a publication, and renewal waits for AUTH_OK before withdrawing the old token. - An Auth Stream message that fails to decode, has trailing body bytes, or has an unexpected type, or a second AUTH from the opener, closes the session with PROTOCOL_VIOLATION, as do invalid UTF-8 or an oversized AUTH_ERROR reason. - An AUTH_ERROR with INTERNAL_ERROR, TOO_MANY_REQUESTS, or CONTROL_MESSAGE_TIMEOUT leaves the token undecided, and the presenter presents it again after a growing backoff; any other code ends the token. @@ -2314,7 +2308,7 @@ A stream that FINs without a GROUP_END carries a closed Group, which may have he - A node closes a session whose peer leaves its Announce Stream undrained for 10 seconds. - A publisher presents only the most specific claim covering the requested prefix. - A subscriber whose scope confines it to some prefixes SHOULD request those, and edit its set when they change. -- A prefix wider than the subscriber's scope is answered region by region, a claim above a region presented at the region, so a subscriber whose regions do not fit one request asks for a prefix they share. A claim is within the scope when the scope matches a path it covers. A scope change takes effect before the ANNOUNCE_OK of any edit read after it, and an edit may change nothing. +- A prefix wider than the subscriber's scope is answered region by region, a claim above a region presented at the region, so a subscriber whose regions do not fit one request asks for a prefix they share. A claim is within the scope when one of the scope's prefixes lies at, beneath, or above it. A scope change takes effect before the ANNOUNCE_OK of any edit read after it, and an edit may change nothing. - Added `Node` to TRACK, TRACKS, SUBSCRIBE, FETCH, and DATAGRAMS: the routing node announcing the claim the first hop selected, which every later hop follows. - Replaced Warm and Cold Route Cost with one cost, which each link adds to. - The Cost parameter must be at least 1. diff --git a/go/wrapper/auth_test.go b/go/wrapper/auth_test.go index 721ba406a46..7d346e9c53e 100644 --- a/go/wrapper/auth_test.go +++ b/go/wrapper/auth_test.go @@ -74,7 +74,7 @@ func TestSessionAuthReadsTheGrant(t *testing.T) { t.Fatal("no request") } - everything := moq.Grant{Publish: []string{"**"}, Subscribe: []string{"**"}} + everything := moq.Grant{Publish: []string{""}, Subscribe: []string{""}} awaitGrant(t, ctx, client.Session(), everything) var session *moq.Session diff --git a/go/wrapper/client.go b/go/wrapper/client.go index 90d984c598c..a2be1272fa3 100644 --- a/go/wrapper/client.go +++ b/go/wrapper/client.go @@ -292,7 +292,7 @@ func (c *Client) Route(path string, options RouteOptions) (*Route, error) { return c.publisher.Route(path, options) } -// Announced lists the paths the remote announces under a pattern scope, as each becomes present or absent. +// Announced lists the paths the remote announces matching a discovery filter, as each becomes present or absent. func (c *Client) Announced(options AnnounceOptions) (*AnnounceConsumer, error) { return c.consumer.Announced(options) } diff --git a/go/wrapper/records.go b/go/wrapper/records.go index 1bd60c941ad..dc2a1b74077 100644 --- a/go/wrapper/records.go +++ b/go/wrapper/records.go @@ -273,12 +273,13 @@ func connectionStatsFromFFI(s ffi.MoqConnectionStats) ConnectionStats { } } -// Grant is what the peer lets this side do, in this side's own paths. An empty -// pattern list grants nothing in that direction; "**" grants everything. +// Grant is what the peer lets this side do, in this side's own paths. Each prefix +// covers its path and everything beneath it. An empty list grants nothing in that +// direction; the empty prefix "" grants everything. type Grant struct { - // Publish lists the broadcast patterns this side may publish to the peer. + // Publish lists the broadcast prefixes this side may publish to the peer. Publish []string - // Subscribe lists the broadcast patterns this side may subscribe to from the peer. + // Subscribe lists the broadcast prefixes this side may subscribe to from the peer. Subscribe []string // Expires is how long until the grant lapses, measured when it was read, or nil for never. Expires *time.Duration diff --git a/js/auth/README.md b/js/auth/README.md index a4b179a6e96..1e984523328 100644 --- a/js/auth/README.md +++ b/js/auth/README.md @@ -6,9 +6,12 @@ a client presents in its query and the keys that sign and verify it. For the contract, the relay flags, and worked examples, see the [Authentication Documentation](../../doc/bin/relay/auth.md). -Grants and claims name paths with patterns from `@moq/pattern`: `foo` is one -broadcast, `foo/**` is a subtree, `**` is everything. A role is an `Access`: -its patterns, or `{"path": [...], "rate": bps}` with a bitrate cap, read and +Grants and claims name paths with prefixes from `@moq/pattern`: `foo` is `foo` +and everything beneath it, and the empty prefix `""` is everything. Signing takes +no wildcard, so `foo/**`, `**`, and `cam-*` are refused. Tokens, a parsed key's +scope, `Key.public`, and `GrantSchema`'s output spell each prefix as the subtree +pattern releases before 0.22 read, `foo/**` or `**`, and read that spelling back. A role is an `Access`: +its prefixes, or `{"path": [...], "rate": bps}` with a bitrate cap, read and written as it is. ## Installation @@ -68,8 +71,8 @@ const key = Key.parse(keyString); // See generate example above // Create claims const claims: Claims = { root: "demo", - publish: ["bbb/**"], // `demo/bbb` and everything beneath it - subscribe: ["**"], // Any broadcast under `demo/` + publish: ["bbb"], // `demo/bbb` and everything beneath it + subscribe: [""], // Any broadcast under `demo/` expires: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now (in seconds), written as `exp` issued: Math.floor(Date.now() / 1000), // Issued at (in seconds), written as `iat` }; @@ -83,8 +86,8 @@ Or you can sign as a CLI ```bash moq-auth sign --key "root.jwk" \ --root "rooms/meeting-123" \ - --subscribe "**" \ - --publish "alice/**" > "alice.jwt" + --subscribe "" \ + --publish "alice" > "alice.jwt" ``` The token expires in an hour unless `--ttl`, `--expires`, or `--unbounded` says otherwise. @@ -97,7 +100,7 @@ You can also verify a token, then scope it to a connection path the way `moq aut import { Claims, Key } from "@moq/auth"; const claims = await Key.verify(key, token); // signature, kid, strict claims, and expiry -const permissions = Claims.authorize(claims, "rooms/meeting-123"); // patterns relative to the path +const permissions = Claims.authorize(claims, "rooms/meeting-123"); // prefixes relative to the path ``` Or as a CLI: @@ -116,7 +119,7 @@ know, so check `request.relay` before sending one newer than that relay: import { type Grant, RequestSchema } from "@moq/auth"; const request = RequestSchema.parse(await body.json()); -const grant: Grant = { subscribe: { path: ["**"], rate: 8_000_000 }, expires: 4102444800, revalidate: 60 }; +const grant: Grant = { subscribe: { path: [""], rate: 8_000_000 }, expires: 4102444800, revalidate: 60 }; return Response.json(grant); ``` diff --git a/js/auth/api/index.api.md b/js/auth/api/index.api.md index 771c39a519c..622b45238c7 100644 --- a/js/auth/api/index.api.md +++ b/js/auth/api/index.api.md @@ -88,15 +88,15 @@ export const ClaimsSchema: ZodMiniPipe; jti: ZodMiniOptional>; nbf: ZodMiniOptional; - publish: ZodMiniOptional>, ZodMiniObject<{ - path: ZodMiniArray>; + publish: ZodMiniOptional, ZodMiniTransform>>, ZodMiniObject<{ + path: ZodMiniArray, ZodMiniTransform>>; rate: ZodMiniNumber; }, $strict>]>>; put: ZodMiniOptional, ZodMiniArray>]>>; root: ZodMiniOptional>; sub: ZodMiniOptional; - subscribe: ZodMiniOptional>, ZodMiniObject<{ - path: ZodMiniArray>; + subscribe: ZodMiniOptional, ZodMiniTransform>>, ZodMiniObject<{ + path: ZodMiniArray, ZodMiniTransform>>; rate: ZodMiniNumber; }, $strict>]>>; }, $strict>, ZodMiniTransform<{ @@ -158,20 +158,32 @@ export const GrantSchema: ZodMiniObject<{ }>>; mounts: ZodMiniOptional, ZodMiniString>>; peer: ZodMiniOptional>; - publish: ZodMiniOptional>, ZodMiniObject<{ - path: ZodMiniArray>; + publish: ZodMiniOptional, ZodMiniTransform>>, ZodMiniObject<{ + path: ZodMiniArray, ZodMiniTransform>>; rate: ZodMiniNumber; - }, $strict>]>>; + }, $strict>]>, ZodMiniTransform>>; revalidate: ZodMiniOptional; root: ZodMiniOptional>; - subscribe: ZodMiniOptional>, ZodMiniObject<{ - path: ZodMiniArray>; + subscribe: ZodMiniOptional, ZodMiniTransform>>, ZodMiniObject<{ + path: ZodMiniArray, ZodMiniTransform>>; rate: ZodMiniNumber; - }, $strict>]>>; + }, $strict>]>, ZodMiniTransform>>; tier: ZodMiniOptional>; }, $strict>; -export { InvalidPattern } from "@moq/pattern"; +export { InvalidPrefix } from "@moq/pattern"; export const Key: { generate: (algorithm: Algorithm, kid?: string) => Promise; @@ -209,12 +221,12 @@ export const KeySchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"oct">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; }, $strip>, ZodMiniObject<{ alg: ZodMiniEnum<{ ES256: "ES256"; @@ -258,12 +282,12 @@ export const KeySchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ p: ZodMiniOptional>; q: ZodMiniOptional>; qi: ZodMiniOptional>; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; }, $strip>, ZodMiniObject<{ alg: ZodMiniEnum<{ ES256: "ES256"; @@ -303,12 +339,12 @@ export const KeySchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"EC">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; x: ZodMiniString; y: ZodMiniString; }, $strip>, ZodMiniObject<{ @@ -347,12 +395,12 @@ export const KeySchema: ZodMiniDiscriminatedUnion<[ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"OKP">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; x: ZodMiniString; }, $strip>], "kty">; @@ -402,12 +462,12 @@ export const KeySetSchema: ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"oct">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; }, $strip>, ZodMiniObject<{ alg: ZodMiniEnum<{ ES256: "ES256"; @@ -451,12 +523,12 @@ export const KeySetSchema: ZodMiniObject<{ p: ZodMiniOptional>; q: ZodMiniOptional>; qi: ZodMiniOptional>; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; }, $strip>, ZodMiniObject<{ alg: ZodMiniEnum<{ ES256: "ES256"; @@ -496,12 +580,12 @@ export const KeySetSchema: ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"EC">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; x: ZodMiniString; y: ZodMiniString; }, $strip>, ZodMiniObject<{ @@ -540,12 +636,12 @@ export const KeySetSchema: ZodMiniObject<{ }>>>; kid: ZodMiniOptional>; kty: ZodMiniLiteral<"OKP">; - scope: ZodMiniOptional>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>>; + }>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; + }, "get" | "put">>>>; x: ZodMiniString; }, $strip>], "kty">>; }, $strip>; @@ -577,12 +685,6 @@ export const OperationSchema: ZodMiniEnum<{ verify: "verify"; }>; -export { Pattern } from "@moq/pattern"; - -export const PatternListSchema: ZodMiniArray>; - -export { Patterns } from "@moq/pattern"; - export type Peer = infer; export const PeerSchema: ZodMiniObject<{ @@ -594,10 +696,16 @@ export const PeerSchema: ZodMiniObject<{ }, $strip>; export interface Permissions { - publish: Patterns; - subscribe: Patterns; + publish: Prefixes; + subscribe: Prefixes; } +export { Prefix } from "@moq/pattern"; + +export const PrefixListSchema: ZodMiniArray>; + +export { Prefixes } from "@moq/pattern"; + export type PublicKey = Omit; export interface PublicKeySet { @@ -808,12 +916,12 @@ export const Scope: { export type Scope = output; -export const ScopeSchema: ZodMiniPipe>>; - publish: ZodMiniOptional>>; + publish: ZodMiniOptional, ZodMiniTransform>>>; put: ZodMiniOptional>>; root: ZodMiniDefault>; - subscribe: ZodMiniOptional>>; + subscribe: ZodMiniOptional, ZodMiniTransform>>>; }, $strict>, ZodMiniTransform>; - -export { Segment } from "@moq/pattern"; - -export { Specificity } from "@moq/pattern"; +}>>, ZodMiniTransform, Omit<{ + get?: string[] | undefined; + publish?: string[] | undefined; + put?: string[] | undefined; + root: string; + subscribe?: string[] | undefined; +}, "get" | "put">>>; export type SymmetricKey = Extract, ZodMiniCatch>>; -export { compareSpecificity } from "@moq/pattern"; - -export function patterns(texts: readonly string[] | undefined): Patterns; +export function prefixes(texts: readonly string[] | undefined): Prefixes; ``` diff --git a/js/auth/examples/sign-and-verify.ts b/js/auth/examples/sign-and-verify.ts index 7a98453bbf1..48765bac00c 100644 --- a/js/auth/examples/sign-and-verify.ts +++ b/js/auth/examples/sign-and-verify.ts @@ -20,8 +20,8 @@ async function main() { // Create claims const claims: Claims = { root: "demo", - publish: ["bbb/**"], // `demo/bbb` and everything beneath it - subscribe: ["**"], // Any broadcast under `demo/` + publish: ["bbb"], // `demo/bbb` and everything beneath it + subscribe: [""], // Any broadcast under `demo/` expires: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now (in seconds), written as `exp` issued: Math.floor(Date.now() / 1000), // Issued at (in seconds), written as `iat` }; @@ -35,7 +35,7 @@ async function main() { console.log("Verified claims:", verifiedClaims); // Scope the claims to a connection path, the way a relay does. Connecting to - // demo/bbb consumes the "bbb/**" grant, leaving `**`: the path and everything beneath. + // demo/bbb consumes the "bbb" grant, leaving `""`: the path and everything beneath. const permissions = Claims.authorize(verifiedClaims, "demo/bbb"); console.log("Permissions at demo/bbb:", permissions.publish.toJSON(), permissions.subscribe.toJSON()); } diff --git a/js/auth/src/claims.test.ts b/js/auth/src/claims.test.ts index dc2bcbf6811..eb2159da83b 100644 --- a/js/auth/src/claims.test.ts +++ b/js/auth/src/claims.test.ts @@ -21,17 +21,49 @@ test("claims granting nothing are rejected, matching Rust's useless-token rule", expect(() => ClaimsSchema.parse({ root: "demo", publish: [], subscribe: [] })).toThrow(); expect(() => ClaimsSchema.parse({ root: "demo" })).toThrow(); - // `**` is everything under the root, which is a real grant. - expect(ClaimsSchema.parse({ root: "demo", publish: ["**"] }).publish).toEqual(["**"]); + // The empty prefix is everything under the root, which is a real grant. + expect(ClaimsSchema.parse({ root: "demo", publish: [""] }).publish).toEqual([""]); }); -test("claims read legacy put/get prefixes as subtrees", () => { +test("claims read legacy put/get prefixes as the same prefixes", () => { const claims = ClaimsSchema.parse({ root: "demo", put: ["alice", "/a//b/"], get: "" }); - expect(claims.publish).toEqual(["alice/**", "a/b/**"]); - expect(claims.subscribe).toEqual(["**"]); + expect(claims.publish).toEqual(["alice", "a/b"]); + expect(claims.subscribe).toEqual([""]); + // TEMPORARY MIGRATION (see `writtenScope` in wire.ts): a parsed scope is spelled as written. expect(ScopeSchema.parse({ root: "demo", put: ["room"] }).publish).toEqual(["room/**"]); }); +// TEMPORARY MIGRATION (see `readPrefix` in wire.ts): released signers and auth servers spell a +// subtree as a pattern, which reads as its prefix. +test("every grant reads a subtree pattern as its prefix", () => { + for (const [text, prefix] of [ + ["**", ""], + ["alice/**", "alice"], + ["a\\/b/**", "a\\/b"], + ]) { + expect(ClaimsSchema.parse({ publish: [text] }).publish, text).toEqual([prefix]); + expect(ClaimsSchema.parse({ subscribe: { path: [text], rate: 8000 } }).subscribe, text).toEqual({ + path: [prefix], + rate: 8000, + }); + // A parsed scope and grant spell their prefixes as written, which parses back the same. + const written = [prefix ? `${prefix}/**` : "**"]; + expect(ScopeSchema.parse({ subscribe: [text] }).subscribe, text).toEqual(written); + expect(ScopeSchema.parse({ subscribe: [prefix] }).subscribe, text).toEqual(written); + expect(GrantSchema.parse({ publish: [text] }).publish, text).toEqual(written); + } +}); + +test("every grant refuses any other wildcard", () => { + for (const text of ["*", "cam-*.hang", "a*", "room/*/cam", "room/*/**", "**/room", "/**", "a\\/**"]) { + expect(ClaimsSchema.safeParse({ publish: [text] }).success, text).toBe(false); + expect(ClaimsSchema.safeParse({ subscribe: { path: [text], rate: 8000 } }).success, text).toBe(false); + expect(ScopeSchema.safeParse({ subscribe: [text] }).success, text).toBe(false); + expect(GrantSchema.safeParse({ publish: [text] }).success, text).toBe(false); + } + expect(() => ClaimsSchema.parse({ publish: ["room/*/**"] })).toThrow(/wildcards are refused/); +}); + test("claims refuse mixed encodings and wildcard legacy prefixes", () => { expect(() => ClaimsSchema.parse({ root: "demo", publish: ["alice"], get: [""] })).toThrow(); expect(() => ClaimsSchema.parse({ root: "demo", put: [], subscribe: ["alice"] })).toThrow(); @@ -41,16 +73,22 @@ test("claims refuse mixed encodings and wildcard legacy prefixes", () => { expect(() => ScopeSchema.parse({ root: "demo", put: "room" })).toThrow(); }); -test("grants are written as patterns, never legacy prefixes", () => { - expect(encodeGrants({ root: "live", publish: ["camera1/**"], subscribe: ["**"] })).toEqual({ +// TEMPORARY MIGRATION (see `readPrefix` in wire.ts): spelled as the subtree patterns a release +// before 0.22 reads. +test("grants are written as publish and subscribe, never legacy put and get", () => { + expect(encodeGrants({ root: "live", publish: ["camera1"], subscribe: [""] })).toEqual({ root: "live", publish: ["camera1/**"], subscribe: ["**"], }); expect(encodeGrants({ root: "live", publish: ["camera1"], subscribe: [] })).toEqual({ root: "live", - publish: ["camera1"], + publish: ["camera1/**"], }); + // Nothing lies beneath a prefix of the most segments a path holds, so it is written as its literal. + const deepest = Array(32).fill("a").join("/"); + expect(encodeGrants({ publish: [deepest] })).toEqual({ publish: [deepest] }); + expect(ClaimsSchema.parse(encodeGrants({ publish: [deepest] })).publish).toEqual([deepest]); }); test("claims read the registered names and write them back", () => { @@ -58,7 +96,7 @@ test("claims read the registered names and write them back", () => { const claims = ClaimsSchema.parse(wire); expect(claims).toEqual({ root: "live", - publish: ["**"], + publish: [""], expires: 4102444800, issued: 1, notBefore: 2, @@ -69,86 +107,89 @@ test("claims read the registered names and write them back", () => { test("a token ID is a bounded printable string", () => { for (const jti of ["a", "550e8400-e29b-41d4-a716-446655440000", "user:42/a.b+c=", "x".repeat(128)]) { - expect(ClaimsSchema.parse({ publish: ["**"], jti }).id).toBe(jti); + expect(ClaimsSchema.parse({ publish: [""], jti }).id).toBe(jti); } for (const jti of ["", "has space", "caf\u00e9", "x".repeat(129), 7, null, ["x"]]) { - expect(ClaimsSchema.safeParse({ publish: ["**"], jti }).success).toBe(false); + expect(ClaimsSchema.safeParse({ publish: [""], jti }).success).toBe(false); } }); test("claims exp and iat are whole seconds", () => { - expect(() => ClaimsSchema.parse({ publish: ["**"], exp: 1.5 })).toThrow(); - expect(() => ClaimsSchema.parse({ publish: ["**"], iat: 1.5 })).toThrow(); - expect(ClaimsSchema.parse({ publish: ["**"], exp: 4102444800 }).expires).toBe(4102444800); + expect(() => ClaimsSchema.parse({ publish: [""], exp: 1.5 })).toThrow(); + expect(() => ClaimsSchema.parse({ publish: [""], iat: 1.5 })).toThrow(); + expect(ClaimsSchema.parse({ publish: [""], exp: 4102444800 }).expires).toBe(4102444800); }); -test("claims refuse a bad pattern", () => { - expect(() => ClaimsSchema.parse({ publish: ["a/**/b/**"] })).toThrow(); - expect(() => ClaimsSchema.parse({ publish: ["/leading"] })).toThrow(); +test("claims refuse a malformed prefix", () => { + for (const text of ["/leading", "trailing/", "a//b", "a\\b"]) { + expect(ClaimsSchema.safeParse({ publish: [text] }).success, text).toBe(false); + } }); test("authorize - path equals root", () => { - const permissions = Claims.authorize(claims("room/123", ["**"], ["alice/**"]), "room/123"); - expect(texts(permissions)).toEqual({ subscribe: ["**"], publish: ["alice/**"] }); + const permissions = Claims.authorize(claims("room/123", [""], ["alice"]), "room/123"); + expect(texts(permissions)).toEqual({ subscribe: [""], publish: ["alice"] }); }); test("authorize - path extends root", () => { // Connecting below the root consumes the matching part of each grant. - const permissions = Claims.authorize(claims("room/123", ["bob/**"], ["alice/**"]), "room/123/alice"); - expect(texts(permissions)).toEqual({ subscribe: [], publish: ["**"] }); + const permissions = Claims.authorize(claims("room/123", ["bob"], ["alice"]), "room/123/alice"); + expect(texts(permissions)).toEqual({ subscribe: [], publish: [""] }); }); -test("authorize - a literal reached exactly is the path itself", () => { - const permissions = Claims.authorize(claims("room", [], ["alice"]), "room/alice"); +test("authorize - a path beneath a prefix keeps all of it", () => { + // A prefix covers its subtree, so connecting anywhere beneath it grants everything there. + const permissions = Claims.authorize(claims("room", [], ["alice"]), "room/alice/cam"); expect(texts(permissions)).toEqual({ subscribe: [], publish: [""] }); }); test("authorize - path is parent of root", () => { // Connecting above the root prepends it, keeping the grants anchored. - const permissions = Claims.authorize(claims("demo", ["**"], ["alice/**"]), "/"); - expect(texts(permissions)).toEqual({ subscribe: ["demo/**"], publish: ["demo/alice/**"] }); + const permissions = Claims.authorize(claims("demo", [""], ["alice"]), "/"); + expect(texts(permissions)).toEqual({ subscribe: ["demo"], publish: ["demo/alice"] }); }); test("authorize - empty root", () => { // A root-scoped token grants everything it lists, wherever it connects. - const permissions = Claims.authorize(claims("", ["demo/**"], []), "demo/room"); - expect(texts(permissions)).toEqual({ subscribe: ["**"], publish: [] }); + const permissions = Claims.authorize(claims("", ["demo"], []), "demo/room"); + expect(texts(permissions)).toEqual({ subscribe: [""], publish: [] }); }); test("authorize - slashes are implicit", () => { - const permissions = Claims.authorize(claims("/room/123/", ["bob/**"], []), "//room/123//"); - expect(permissions.subscribe.toJSON()).toEqual(["bob/**"]); + const permissions = Claims.authorize(claims("/room/123/", ["bob"], []), "//room/123//"); + expect(permissions.subscribe.toJSON()).toEqual(["bob"]); }); test("authorize - respects segment boundaries", () => { // "foo" must not cover "foobar". - expect(() => Claims.authorize(claims("foo", ["**"], ["**"]), "foobar")).toThrow(/does not overlap/); + expect(() => Claims.authorize(claims("foo", [""], [""]), "foobar")).toThrow(/does not overlap/); + // Nor does a prefix `alice` cover `alice-b`. + expect(Claims.authorize(claims("", [], ["alice"]), "").publish.matches("alice-b")).toBe(false); }); test("authorize - unrelated path", () => { - expect(() => Claims.authorize(claims("demo", ["**"], ["**"]), "other")).toThrow(/does not overlap/); + expect(() => Claims.authorize(claims("demo", [""], [""]), "other")).toThrow(/does not overlap/); }); test("authorize - no access at path", () => { // The path overlaps the root, but every grant sits outside it. - expect(() => Claims.authorize(claims("", ["demo/**"], []), "other")).toThrow(/grants no access/); + expect(() => Claims.authorize(claims("", ["demo"], []), "other")).toThrow(/grants no access/); +}); + +test("authorize - keeps only the prefixes beneath the path", () => { + const permissions = Claims.authorize(claims("", ["a/x", "a/y/z", "b"], []), "a"); + expect(permissions.subscribe.toJSON()).toEqual(["x", "y/z"]); }); test("authorize - reads an escaped separator as content", () => { // `a\/b` is one segment holding a `/`, the spelling of a moq-transport field. - const grant = claims("room", ["a\\/b/*"], ["*"]); - expect(texts(Claims.authorize(grant, "room/a\\/b"))).toEqual({ subscribe: ["*"], publish: [""] }); + const grant = claims("room", ["a\\/b/x"], ["a\\/b"]); + expect(texts(Claims.authorize(grant, "room/a\\/b"))).toEqual({ subscribe: ["x"], publish: [""] }); expect(Claims.authorize(grant, "room").publish.matches("a\\/b")).toBe(true); expect(Claims.authorize(grant, "room").publish.matches("a/b")).toBe(false); - expect(Claims.authorize(claims("room", ["**"], []), "//room/a\\///").subscribe.toJSON()).toEqual(["**"]); + expect(Claims.authorize(claims("room", [""], []), "//room/a\\///").subscribe.toJSON()).toEqual([""]); // `a\` ends inside that segment, so it is no path the root covers. - expect(() => Claims.authorize(claims("a\\/b", ["**"], []), "a\\")).toThrow(/does not overlap/); -}); - -test("authorize - wildcards rebase as a set", () => { - // `**/chat` reached at `chat` is both the path itself and deeper `**/chat`. - const permissions = Claims.authorize(claims("", ["**/chat"], []), "chat"); - expect(permissions.subscribe.toJSON()).toEqual(["", "**/chat"]); + expect(() => Claims.authorize(claims("a\\/b", [""], []), "a\\")).toThrow(/does not overlap/); }); test("shared rate vectors parse and re-encode like Rust", async () => { @@ -168,20 +209,20 @@ test("a capped role keeps its cap in the role and never encodes as a prefix", () for (const role of ["publish", "subscribe"] as const) { // Subtrees alone would be written as legacy prefixes, which a cap must not be. const claims = ClaimsSchema.parse({ - publish: ["alice/**"], - subscribe: ["**"], - [role]: { path: ["alice/**"], rate: 8000 }, + publish: ["alice"], + subscribe: [""], + [role]: { path: ["alice"], rate: 8000 }, }); - expect(claims[role]).toEqual({ path: ["alice/**"], rate: 8000 }); + expect(claims[role]).toEqual({ path: ["alice"], rate: 8000 }); const encoded = encodeGrants(claims); expect(encoded).not.toHaveProperty("put"); expect(encoded).not.toHaveProperty("get"); expect(encoded[role]).toEqual({ path: ["alice/**"], rate: 8000 }); } - expect(() => ClaimsSchema.parse({ publish: { path: ["**"], rate: 0 } })).toThrow(/rate must be a positive integer/); + expect(() => ClaimsSchema.parse({ publish: { path: [""], rate: 0 } })).toThrow(/rate must be a positive integer/); }); test("a key scope bounds paths, never bitrate", () => { - expect(ScopeSchema.safeParse({ publish: { path: ["**"], rate: 8000 } }).success).toBe(false); - expect(ScopeSchema.safeParse({ publish: ["**"], subscribe: { path: ["**"], rate: 8000 } }).success).toBe(false); + expect(ScopeSchema.safeParse({ publish: { path: [""], rate: 8000 } }).success).toBe(false); + expect(ScopeSchema.safeParse({ publish: [""], subscribe: { path: [""], rate: 8000 } }).success).toBe(false); }); diff --git a/js/auth/src/claims.ts b/js/auth/src/claims.ts index 3b7142101ff..ec1d92f1525 100644 --- a/js/auth/src/claims.ts +++ b/js/auth/src/claims.ts @@ -1,64 +1,79 @@ /** - * The payload of a token: a root, plus the publish/subscribe patterns granted beneath it + * The payload of a token: a root, plus the publish/subscribe prefixes granted beneath it * and an optional bitrate cap on each. * * @module */ -import { Pattern, Patterns } from "@moq/pattern"; +import { Prefixes } from "@moq/pattern"; import * as z from "zod/mini"; import * as Path from "./path.ts"; -import { AccessSchema, decodeGrants, PatternListSchema, paths, TokenIdSchema, UnixSecondsSchema } from "./wire.ts"; +import { + AccessSchema, + decodeGrants, + paths, + readPrefixes, + TokenIdSchema, + UnixSecondsSchema, + WireAccessSchema, + WirePrefixListSchema, + writtenScope, +} from "./wire.ts"; -export { type Access, AccessSchema, PatternListSchema, TokenIdSchema } from "./wire.ts"; +export { type Access, AccessSchema, PrefixListSchema, TokenIdSchema } from "./wire.ts"; /** A value parsed by {@link TokenIdSchema}. */ export type TokenId = z.output; -/** Parse a list of pattern texts into a reduced union. */ -export function patterns(texts: readonly string[] | undefined): Patterns { - return new Patterns((texts ?? []).map((text) => Pattern.parse(text))); +/** Parse a list of prefix texts into a reduced set; a wildcard throws `InvalidPrefix`. */ +export function prefixes(texts: readonly string[] | undefined): Prefixes { + return Prefixes.parse(texts ?? []); } -const ScopeFields = { - /** The root that `publish` and `subscribe` are relative to. Defaults to the empty string. */ - root: z._default(z.string(), ""), - /** Patterns this key may grant to publishers, relative to `root`. */ - publish: z.optional(PatternListSchema), - /** Patterns this key may grant to subscribers, relative to `root`. */ - subscribe: z.optional(PatternListSchema), -}; - /** * The immutable ceiling on what a key may grant, embedded in its JWK. * * `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 + * when the scope sits at the top level. A legacy `put`/`get` scope reads as the same + * prefixes, and so does a member spelled as a subtree pattern, `foo/**` or `**`, as released + * signers wrote it. A scope bounds paths, never bitrate, so a capped role is * refused, as is a malformed root (see `Path.validate`). The stricter `Path.signable` applies * only where a scope is minted, so keys minted before it still load. Any other field is refused. + * + * TEMPORARY MIGRATION (see `wire.ts`): the parsed scope spells each prefix as the subtree pattern + * a release before 0.22 reads, `foo/**` or `**`, as `GrantSchema` does. So a parsed key, and + * `Key.public` and `KeySet.public` of any key, write a scope every verifier reads as the same + * subtrees as the tokens the key signs. {@link Scope.allows} reads either spelling. */ -export const ScopeSchema = z - .pipe( - z.strictObject({ - ...ScopeFields, - put: z.optional(z.array(z.string())), - get: z.optional(z.array(z.string())), - }), - z.transform(decodeGrants), - ) - .check( - 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) }); - } - }), - ); +export const ScopeSchema = z.pipe( + z + .pipe( + z.strictObject({ + /** The root that `publish` and `subscribe` are relative to. Defaults to the empty string. */ + root: z._default(z.string(), ""), + /** Prefixes this key may grant to publishers, relative to `root`. */ + publish: z.optional(WirePrefixListSchema), + /** Prefixes this key may grant to subscribers, relative to `root`. */ + subscribe: z.optional(WirePrefixListSchema), + put: z.optional(z.array(z.string())), + get: z.optional(z.array(z.string())), + }), + z.transform(decodeGrants), + ) + .check( + z.refine((data) => (data.publish?.length ?? 0) > 0 || (data.subscribe?.length ?? 0) > 0, { + message: "Either publish or subscribe must contain at least one prefix", + }), + z.superRefine((data, ctx) => { + try { + Path.validate(data.root); + } catch (error) { + ctx.addIssue({ code: "custom", message: error instanceof Error ? error.message : String(error) }); + } + }), + ), + z.transform(writtenScope), +); /** A value parsed by {@link ScopeSchema}. */ export type Scope = z.output; @@ -91,15 +106,16 @@ export const ClaimsFieldsSchema = z // rejects such a token as useless. Checking `!== undefined` here would mint // tokens that Rust then refuses to verify. z.refine((data) => paths(data.publish).length > 0 || paths(data.subscribe).length > 0, { - message: "Either publish or subscribe must grant at least one pattern", + message: "Either publish or subscribe must grant at least one prefix", }), ); /** * The payload of a token, as {@link Key.sign} takes it and {@link Key.verify} returns it. * - * A pattern names exactly what it says: `alice` is one broadcast, `alice/**` is a - * subtree, and `**` is everything under the root. A role may instead be an + * A prefix covers itself and everything beneath it: `alice` is `alice` and its subtree, and + * the empty prefix `""` is everything under the root. A member holding a `*` is refused, + * at signing and at verification alike. A role may instead be an * {@link Access} with a cap, `{"path": [...], "rate": bps}`. The times and ID are written * as the registered `exp`, `iat`, `nbf`, and `jti` claims. */ @@ -113,15 +129,18 @@ const IgnoredFields = { sub: z.optional(z.unknown()), }; -const PrefixListSchema = z.union([z.string(), z.array(z.string())]); +// Legacy `put`/`get` took one prefix or a list. +const LegacyPrefixListSchema = z.union([z.string(), z.array(z.string())]); /** * A token's JSON payload, parsed into {@link Claims}. * * `root` is optional on the wire: a token scoped to the top-level path omits it, so * it defaults to the empty string to match the Rust `moq-auth` crate. Legacy `moq-token` - * claims read too, each `put`/`get` prefix `p` as the subtree `p/**`; signing always - * writes `publish` and `subscribe`. `exp`, `iat`, `nbf`, and `jti` read as `expires`, + * claims read too, each `put`/`get` prefix as the same prefix; signing always + * writes `publish` and `subscribe`, each prefix spelled as the subtree pattern a release before + * 0.22 reads (`foo/**`, `**`), and a member spelled that way reads back as its prefix. Any other + * wildcard fails verification. `exp`, `iat`, `nbf`, and `jti` read as `expires`, * `issued`, `notBefore`, and `id`. The registered `iss` and `sub` claims are read and * dropped; any other field fails verification. */ @@ -129,15 +148,15 @@ export const ClaimsSchema = z.pipe( z.pipe( z.strictObject({ root: z.optional(z.string()), - publish: z.optional(AccessSchema), - subscribe: z.optional(AccessSchema), + publish: z.optional(WireAccessSchema), + subscribe: z.optional(WireAccessSchema), exp: z.optional(UnixSecondsSchema), iat: z.optional(UnixSecondsSchema), nbf: z.optional(UnixSecondsSchema), jti: z.optional(TokenIdSchema), ...IgnoredFields, - put: z.optional(PrefixListSchema), - get: z.optional(PrefixListSchema), + put: z.optional(LegacyPrefixListSchema), + get: z.optional(LegacyPrefixListSchema), }), // Fed to the strict fields, so a claim misnamed here is refused rather than dropped. z.transform( @@ -154,34 +173,34 @@ export const ClaimsSchema = z.pipe( ); /** - * The access a {@link Claims} grants at a specific path, with every pattern rebased so + * The access a {@link Claims} grants at a specific path, with every prefix rebased so * it is relative to that path. * - * Produced by {@link Claims.authorize}. `**` grants the path itself and everything beneath - * it; the empty pattern grants exactly the path. + * Produced by {@link Claims.authorize}. The empty prefix grants the path itself and + * everything beneath it. */ export interface Permissions { - /** Patterns the holder may subscribe to, relative to the authorized path. */ - subscribe: Patterns; - /** Patterns the holder may publish to, relative to the authorized path. */ - publish: Patterns; + /** Prefixes the holder may subscribe to, relative to the authorized path. */ + subscribe: Prefixes; + /** Prefixes the holder may publish to, relative to the authorized path. */ + publish: Prefixes; } /** - * The access `claims` grants at `path`, rebased so each returned pattern is relative + * The access `claims` grants at `path`, rebased so each returned prefix is relative * to `path`. * * `path` and `claims.root` must overlap, in either direction: * * - `path` extends the root (root `demo`, path `demo/room`), so the extra `room` - * narrows each pattern and drops the ones outside it. + * narrows each prefix and drops the ones outside it. * - `path` is a parent of the root (root `demo`, empty path), so `demo` is prepended to - * each pattern to keep it anchored where the token points. + * each prefix to keep it anchored where the token points. * * Matching is segment-aware, so a root of `foo` does not cover `foobar`. Slashes at * the boundaries are implicit: `/demo/` and `demo` are the same path. * - * Throws when the two don't overlap, and when they do but every pattern falls outside + * Throws when the two don't overlap, and when they do but every prefix falls outside * `path`. * * This is authorization only. Verify the signature first with {@link Key.verify} or @@ -196,9 +215,9 @@ function authorize(claims: Claims, path: string): Permissions { const beyondRoot = Path.stripPrefix(target, root); const beyondPath = Path.stripPrefix(root, target); - let scope: (granted: Patterns) => Patterns; + let scope: (granted: Prefixes) => Prefixes; if (beyondRoot !== undefined) { - // The path reaches into the grant; keep what each pattern says below it. + // The path reaches into the grant; keep what each prefix says below it. scope = (granted) => granted.rebase(beyondRoot); } else if (beyondPath !== undefined) { // The grant sits below the path; name it from there. @@ -208,8 +227,8 @@ function authorize(claims: Claims, path: string): Permissions { } const permissions: Permissions = { - subscribe: scope(patterns(paths(claims.subscribe))), - publish: scope(patterns(paths(claims.publish))), + subscribe: scope(prefixes(paths(claims.subscribe))), + publish: scope(prefixes(paths(claims.publish))), }; if (permissions.subscribe.size === 0 && permissions.publish.size === 0) { throw new Error(`token grants no access to path "${target}"`); @@ -219,13 +238,13 @@ function authorize(claims: Claims, path: string): Permissions { } /** - * Whether every pattern `claims` grants is covered by `scope`, per role. + * Whether every prefix `claims` grants is covered by `scope`, per role. * * Both sides are placed beneath their own root before comparing, so the same grant - * expressed as `root: "demo"` + `publish: ["room/**"]` or as `publish: ["demo/room/**"]` - * is treated identically. Containment is per pattern, so a scope of `live/**` does - * not cover `lively/**`, and the roles are checked independently: a publish-only - * scope never authorizes a subscribe grant. + * expressed as `root: "demo"` + `publish: ["room"]` or as `publish: ["demo/room"]` + * is treated identically. Coverage is segment-aware, so a scope of `live` does not + * cover `lively`, and the roles are checked independently: a publish-only scope never + * authorizes a subscribe grant. * * Must stay in lockstep with `Scope::allows` in the Rust `moq-auth` crate, which * checks the same keys. @@ -233,9 +252,10 @@ function authorize(claims: Claims, path: string): Permissions { function allows(scope: Scope, claims: Claims): boolean { const covers = (granted: string[] | undefined, requested: string[] | undefined) => { try { - return patterns(granted).rooted(scope.root).covers(patterns(requested).rooted(claims.root)); + // TEMPORARY MIGRATION (see `wire.ts`): a scope spells its prefixes as subtree patterns. + return prefixes(readPrefixes(granted)).rooted(scope.root).covers(prefixes(requested).rooted(claims.root)); } catch { - // A root too deep to place the patterns beneath cannot be granted either way. + // A root too deep to place the prefixes beneath cannot be granted either way. return false; } }; @@ -249,7 +269,7 @@ function allows(scope: Scope, claims: Claims): boolean { * The counterpart of the Rust `moq-auth` crate's `Claims` methods. */ export const Claims = { - /** The access `claims` grants at `path`, rebased so each returned pattern is relative to `path`. */ + /** The access `claims` grants at `path`, rebased so each returned prefix is relative to `path`. */ authorize, }; @@ -259,6 +279,6 @@ export const Claims = { * The counterpart of the Rust `moq-auth` crate's `Scope` methods. */ export const Scope = { - /** Whether every pattern `claims` grants is covered by `scope`, per role. */ + /** Whether every prefix `claims` grants is covered by `scope`, per role. */ allows, }; diff --git a/js/auth/src/cli.test.ts b/js/auth/src/cli.test.ts index 23b6752b97a..b721a195018 100644 --- a/js/auth/src/cli.test.ts +++ b/js/auth/src/cli.test.ts @@ -73,7 +73,7 @@ describe("sign", () => { }); function payload(...args: string[]) { - const result = Bun.spawnSync(["bun", CLI, "sign", "--key", key, "--publish", "**", ...args]); + const result = Bun.spawnSync(["bun", CLI, "sign", "--key", key, "--publish", "", ...args]); expect(result.exitCode).toBe(0); const encoded = result.stdout.toString().trim().split(".")[1]; return JSON.parse(Buffer.from(encoded, "base64url").toString()); @@ -110,7 +110,45 @@ describe("sign", () => { test("writes a jti only when --id gives one", () => { expect(payload()).not.toHaveProperty("jti"); expect(payload("--id", "t-1").jti).toBe("t-1"); - const invalid = Bun.spawnSync(["bun", CLI, "sign", "--key", key, "--publish", "**", "--id", "a b"]); + const invalid = Bun.spawnSync(["bun", CLI, "sign", "--key", key, "--publish", "", "--id", "a b"]); expect(invalid.exitCode).not.toBe(0); }); }); + +describe("grants", () => { + let dir: string; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), "moq-auth-cli-")); + }); + + afterEach(() => { + rmSync(dir, { recursive: true, force: true }); + }); + + test("flags take prefixes alone", () => { + const key = join(dir, "key.jwk"); + run("generate", "--out", key); + for (const args of [ + ["generate", "--out", join(dir, "scoped.jwk"), "--publish", "room/**"], + ["generate", "--out", join(dir, "scoped.jwk"), "--subscribe", "**"], + ["sign", "--key", key, "--publish", "room/**"], + ["sign", "--key", key, "--subscribe", "**"], + ]) { + const result = Bun.spawnSync(["bun", CLI, ...args]); + expect(result.exitCode, args.join(" ")).not.toBe(0); + expect(result.stderr.toString(), args.join(" ")).toContain("wildcard"); + } + }); + + // TEMPORARY MIGRATION (see `readPrefix` in wire.ts): the scope a release before 0.22 reads. + test("a key scope is written as the subtree patterns every release reads", async () => { + const key = join(dir, "scoped.jwk"); + run("generate", "--out", key, "--root", "demo", "--publish", "room", "--subscribe", ""); + expect(JSON.parse(await Bun.file(key).text()).scope).toEqual({ + root: "demo", + publish: ["room/**"], + subscribe: ["**"], + }); + }); +}); diff --git a/js/auth/src/cli.ts b/js/auth/src/cli.ts index 7b381ac3048..8a573fa001f 100755 --- a/js/auth/src/cli.ts +++ b/js/auth/src/cli.ts @@ -5,7 +5,7 @@ import { wall } from "@moq/clock"; import { Command, Option } from "commander"; import { base64url } from "jose"; import type { Algorithm } from "./algorithm.ts"; -import { Claims, type Scope, ScopeSchema } from "./claims.ts"; +import { Claims, prefixes, type Scope, ScopeSchema } from "./claims.ts"; import { Key } from "./key.ts"; import * as Path from "./path.ts"; import { encodeGrants } from "./wire.ts"; @@ -23,16 +23,22 @@ program .option("--public ", "Path to save the public key (for asymmetric algorithms)") .option("--base64", "Output as base64url instead of JSON", false) .option("--root ", "Root path for the optional key scope", "") - .option("--publish ", "Publish patterns the key may grant (foo/** for a subtree)") - .option("--subscribe ", "Subscribe patterns the key may grant (foo/** for a subtree)") + .option("--publish ", 'Publish prefixes the key may grant, each with its subtree ("" for everything)') + .option( + "--subscribe ", + 'Subscribe prefixes the key may grant, each with its subtree ("" for everything)', + ) .action(async (options) => { try { const algorithm = options.algorithm as Algorithm; let key = await Key.generate(algorithm, options.id); if (options.publish || options.subscribe) { // Parse rather than cast, so a useless scope fails here like it does - // in the Rust CLI instead of writing an unusable key to disk. + // in the Rust CLI instead of writing an unusable key to disk. Flags take + // prefixes alone; only a key file reads the subtree patterns releases wrote. Path.signable(options.root); + prefixes(options.publish); + prefixes(options.subscribe); const scope: Scope = ScopeSchema.parse({ root: options.root, ...(options.publish && { publish: options.publish }), @@ -70,8 +76,11 @@ program .description("Sign a token to stdout") .requiredOption("--key ", "Path to the key file") .option("--root ", "Root path for the token", "") - .option("--publish ", "Patterns the holder may publish (foo/** for a subtree)") - .option("--subscribe ", "Patterns the holder may subscribe to (foo/** for a subtree)") + .option("--publish ", 'Prefixes the holder may publish, each with its subtree ("" for everything)') + .option( + "--subscribe ", + 'Prefixes the holder may subscribe to, each with its subtree ("" for everything)', + ) .addOption( new Option("--expires ", "Expiration time as unix timestamp, instead of --ttl from now") .argParser(parseUnixTimestamp) diff --git a/js/auth/src/contract.test.ts b/js/auth/src/contract.test.ts index 6c366a17f64..6b1779737d9 100644 --- a/js/auth/src/contract.test.ts +++ b/js/auth/src/contract.test.ts @@ -136,54 +136,61 @@ test("malformed transport values are refused", () => { test("a grant round trips and is validated", () => { const grant = GrantSchema.parse({ - publish: ["alice/**"], - subscribe: ["**"], + publish: ["alice"], + subscribe: [""], root: "pid/room", expires: 4102444800, revalidate: 60, tier: "websocket", label: "project/key/123", }); + // TEMPORARY MIGRATION (see `readPrefix` in wire.ts): spelled as a relay before 0.22 reads it, + // which parses back to itself. expect(grant.publish).toEqual(["alice/**"]); + expect(grant.subscribe).toEqual(["**"]); + expect(GrantSchema.parse(grant)).toEqual(grant); expect(() => GrantSchema.parse({})).toThrow(/name something/); - expect(() => GrantSchema.parse({ publish: ["**"], revalidate: 60 })).toThrow(/must expire/); - expect(() => GrantSchema.parse({ publish: ["**"], expires: 4102444800, revalidate: 0 })).toThrow(); + expect(() => GrantSchema.parse({ publish: [""], revalidate: 60 })).toThrow(/must expire/); + expect(() => GrantSchema.parse({ publish: [""], expires: 4102444800, revalidate: 0 })).toThrow(); // The Rust side reads whole seconds; a fraction would be refused there. - expect(() => GrantSchema.parse({ publish: ["**"], expires: 4102444800, revalidate: 0.5 })).toThrow(); - expect(() => GrantSchema.parse({ publish: ["**"], expires: 4102444800.5 })).toThrow(); + expect(() => GrantSchema.parse({ publish: [""], expires: 4102444800, revalidate: 0.5 })).toThrow(); + expect(() => GrantSchema.parse({ publish: [""], expires: 4102444800.5 })).toThrow(); expect(() => GrantSchema.parse({ publish: ["a/**/b/**"] })).toThrow(); - expect(() => GrantSchema.parse({ publish: ["**"], label: "project/*" })).toThrow(/invalid path/); + expect(() => GrantSchema.parse({ publish: [""], label: "project/*" })).toThrow(/invalid path/); }); test("a capped grant is typed as the relay reads it", () => { // Typed, so a server cannot put the cap anywhere the relay would skip it. - const grant: Grant = { publish: { path: ["alice/**"], rate: 8_000_000 }, subscribe: ["**"] }; - expect(GrantSchema.parse(JSON.parse(JSON.stringify(grant)))).toEqual(grant); + const grant: Grant = { publish: { path: ["alice"], rate: 8_000_000 }, subscribe: [""] }; + expect(GrantSchema.parse(JSON.parse(JSON.stringify(grant)))).toEqual({ + publish: { path: ["alice/**"], rate: 8_000_000 }, + subscribe: ["**"], + }); // A cap alone names nothing. expect(() => GrantSchema.parse({ publish: { path: [], rate: 8000 } })).toThrow(/name something/); }); test("a grant with a field the relay does not know is refused", () => { for (const extra of [{ publish_rate: 8000 }, { upstream: true }, { future: {} }, { link: "downstream" }]) { - expect(GrantSchema.safeParse({ publish: ["**"], ...extra }).success).toBe(false); + expect(GrantSchema.safeParse({ publish: [""], ...extra }).success).toBe(false); } }); test("peer is a temporary alias that must agree with link", () => { for (const link of [{ peer: true }, { link: "peer" }, { link: "upstream", peer: true }, { peer: false }]) { - expect(GrantSchema.safeParse({ publish: ["**"], ...link }).success).toBe(true); + expect(GrantSchema.safeParse({ publish: [""], ...link }).success).toBe(true); } - expect(() => GrantSchema.parse({ publish: ["**"], link: "upstream", peer: false })).toThrow(/not a peer/); + expect(() => GrantSchema.parse({ publish: [""], link: "upstream", peer: false })).toThrow(/not a peer/); }); test("a tier a metrics label would escape is refused", () => { for (const tier of ['a"b', "a\\b", "a\nb", "/edge", "a//b"]) { - expect(() => GrantSchema.parse({ publish: ["**"], tier })).toThrow(/invalid tier/); + expect(() => GrantSchema.parse({ publish: [""], tier })).toThrow(/invalid tier/); expect(RefusalSchema.safeParse({ reason: "expired", tier }).success).toBe(false); } for (const tier of ["", "region/sjc", "a.b_c-D"]) { - expect(GrantSchema.parse({ publish: ["**"], tier }).tier).toBe(tier); + expect(GrantSchema.parse({ publish: [""], tier }).tier).toBe(tier); } }); diff --git a/js/auth/src/contract.ts b/js/auth/src/contract.ts index b7f5bb24f2e..6dbbc218f8d 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 { AccessSchema, paths, UnixSecondsSchema } from "./wire.ts"; +import { paths, UnixSecondsSchema, WireAccessSchema, writtenAccess } from "./wire.ts"; const PathSchema = z.string().check( z.refine( @@ -39,6 +39,9 @@ export const TransportSchema = z.pipe( /** A value parsed by {@link TransportSchema}. */ export type Transport = z.infer; +// TEMPORARY MIGRATION: a grant's role, read through the alias and spelled as a release before 0.22 reads it. +const WrittenAccessSchema = z.pipe(WireAccessSchema, z.transform(writtenAccess)); + /** How a cluster peer (another relay) links to this one. */ export const LinkSchema = z.enum(["peer", "upstream"]); /** A value parsed by {@link LinkSchema}. */ @@ -190,18 +193,23 @@ export type Request = z.infer; * nothing is a refusal too, and one that asks to be revalidated must say when it * expires, so an outage always has a bound the server chose. * - * The type is the wire shape, so a parsed or typed grant is the JSON to answer with. A field - * this schema does not know is refused, as the relay refuses it: a field added later may - * narrow the grant, and a reader that skipped it would widen the session. Read the - * request's `relay` before sending a field newer than the relay that asks. + * The type is the wire shape, so a parsed grant is the JSON to answer with. A field this + * schema does not know is refused, as the relay refuses it: a field added later may narrow + * the grant, and a reader that skipped it would widen the session. Read the request's + * `relay` before sending a field newer than the relay that asks. + * + * TEMPORARY MIGRATION: parsing spells each prefix as the subtree pattern a relay before 0.22 + * reads (`foo/**`, `**`), and reads one spelled that way back as its prefix, so answer with + * the parsed grant: such a relay reads a bare `foo` as the one path `foo`. Any other wildcard + * is refused. Removed with `readPrefix` in `wire.ts` over the next two version migrations. */ export const GrantSchema = z .strictObject({ - /** What the session may publish, relative to the root: patterns, or `{path, rate}` with a bitrate cap. */ - publish: z.optional(AccessSchema), - /** What the session may subscribe to, relative to the root: patterns, or `{path, rate}` with a bitrate cap. */ - subscribe: z.optional(AccessSchema), - /** The path the patterns are relative to, replacing the dialed one. Absent means the dialed path. */ + /** What the session may publish, relative to the root: prefixes, or `{path, rate}` with a bitrate cap. */ + publish: z.optional(WrittenAccessSchema), + /** What the session may subscribe to, relative to the root: prefixes, or `{path, rate}` with a bitrate cap. */ + subscribe: z.optional(WrittenAccessSchema), + /** The path the prefixes are relative to, replacing the dialed one. Absent means the dialed path. */ root: z.optional(z.string()), /** 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())), diff --git a/js/auth/src/corpus.test.ts b/js/auth/src/corpus.test.ts index 66621598cd4..14fdc32e762 100644 --- a/js/auth/src/corpus.test.ts +++ b/js/auth/src/corpus.test.ts @@ -2,7 +2,7 @@ // parsers cannot disagree on a token, key, key set, grant, or signable root without both failing. import { describe, expect, test } from "bun:test"; import * as jose from "jose"; -import { type Access, type Claims, patterns } from "./claims.ts"; +import { type Access, type Claims, prefixes } from "./claims.ts"; import { GrantSchema } from "./contract.ts"; import { Key } from "./key.ts"; import { KeySet } from "./set.ts"; @@ -18,13 +18,13 @@ const corpus = (await Bun.file(new URL("../../../rs/moq-auth/tests/corpus.json", roots: { root: string; signable: boolean }[]; }; -// The claims as the corpus writes them: patterns reduced, absent roles empty, a capped role +// The claims as the corpus writes them: prefixes reduced, absent roles empty, a capped role // as its wire object, and times and the token ID only when present. function canonical(claims: Claims): unknown { const role = (access: Access | undefined) => access === undefined || Array.isArray(access) - ? patterns(access).toJSON() - : { path: patterns(access.path).toJSON(), rate: access.rate }; + ? prefixes(access).toJSON() + : { path: prefixes(access.path).toJSON(), rate: access.rate }; return { root: claims.root, publish: role(claims.publish), @@ -84,7 +84,7 @@ describe("roots", () => { const key = Key.parse(JSON.stringify(corpus.keys.hs256)); for (const { root, signable } of corpus.roots) { test(JSON.stringify(root), async () => { - const signed = Key.sign(key, { root, publish: ["**"] }); + const signed = Key.sign(key, { root, publish: [""] }); if (signable) await signed; else await expect(signed).rejects.toThrow(); }); diff --git a/js/auth/src/index.ts b/js/auth/src/index.ts index 6b9cde472b6..df306ecc6de 100644 --- a/js/auth/src/index.ts +++ b/js/auth/src/index.ts @@ -6,20 +6,16 @@ * gets back, and the JWT {@link Claims} a client presents in its query with the * {@link Key} that signs and verifies it. * - * Grants and claims name paths with patterns from `@moq/pattern`, re-exported here: - * `foo` is one broadcast, `foo/**` is a subtree, `**` is everything. + * Grants and claims name paths with prefixes from `@moq/pattern`, re-exported here: + * `foo` is `foo` and everything beneath it, and the empty prefix is everything. Signing + * takes no wildcard: `foo/**` and `**` are refused, written `foo` and `""` instead. Until the + * next two version migrations, a token, key scope, or parsed grant spells each prefix as the + * subtree pattern releases before 0.22 read, `foo/**` or `**`, and reads that spelling back. * * @module */ -export { - compareSpecificity, - InvalidPattern, - Pattern, - Patterns, - type Segment, - type Specificity, -} from "@moq/pattern"; +export { InvalidPrefix, Prefix, Prefixes } from "@moq/pattern"; export * from "./algorithm.ts"; export * from "./claims.ts"; export * from "./contract.ts"; diff --git a/js/auth/src/interop.test.ts b/js/auth/src/interop.test.ts index e95f724ce3c..f0ac9c34334 100644 --- a/js/auth/src/interop.test.ts +++ b/js/auth/src/interop.test.ts @@ -51,8 +51,8 @@ describe("Rust-serialized contract", () => { test("a grant typed here is what a Rust relay reads", () => { const grant: Grant = { - publish: { path: ["alice/**"], rate: 8_000_000 }, - subscribe: ["**"], + publish: { path: ["alice"], rate: 8_000_000 }, + subscribe: [""], root: "pid/room", expires: 4102444800, revalidate: 60, diff --git a/js/auth/src/key.test.ts b/js/auth/src/key.test.ts index dceeab548a0..fbd7f198c25 100644 --- a/js/auth/src/key.test.ts +++ b/js/auth/src/key.test.ts @@ -6,6 +6,7 @@ import type { Algorithm } from "./algorithm.ts"; import { Claims } from "./claims.ts"; import { INSECURE_TEST_RS256_OTHER, INSECURE_TEST_RSA_KEYS } from "./insecure-test-keys.ts"; import { Key, KeyIdSchema } from "./key.ts"; +import { KeySet } from "./set.ts"; // Helper function to encode JSON to base64url function encodeJwk(obj: unknown): string { @@ -24,8 +25,8 @@ const testKey = { const testClaims = { root: "test-path", - publish: ["test-pub/**"], - subscribe: ["test-sub/**"], + publish: ["test-pub"], + subscribe: ["test-sub"], expires: Math.floor(wall().asUnixMillis() / 1000 + 60), // 1 minute from now in seconds issued: Math.floor(wall().asUnixMillis() / 1000), // now in seconds }; @@ -68,22 +69,22 @@ test("verify refuses unsafe signed payloads without dropping their restrictions" { custom: "refused", exp: 1 }, {}, { publish: [] }, - { publish: ["**"], exp: 1000 }, - { publish: ["**"], nbf: 1001 }, - { publish: ["**"], subscribe: { path: "alice/**", rate: 8000 } }, - { publish: { path: ["alice/**"], rate: 0 }, subscribe: ["**"] }, - { publish: { path: ["alice/**"] }, subscribe: ["**"] }, + { publish: [""], exp: 1000 }, + { publish: [""], nbf: 1001 }, + { publish: [""], subscribe: { path: "alice", rate: 8000 } }, + { publish: { path: ["alice"], rate: 0 }, subscribe: [""] }, + { publish: { path: ["alice"] }, subscribe: [""] }, ]) { await expect(Key.verify(key, await signed(payload))).rejects.toThrow(); } - await expect(Key.verify(key, await signed({ publish: ["**"], exp: 1001, nbf: 1000 }))).resolves.toMatchObject({ - publish: ["**"], + await expect(Key.verify(key, await signed({ publish: [""], exp: 1001, nbf: 1000 }))).resolves.toMatchObject({ + publish: [""], }); + await expect(Key.verify(key, await signed({ publish: [""] }, { alg: "HS256", kid: "other" }))).rejects.toThrow( + /kid/, + ); await expect( - Key.verify(key, await signed({ publish: ["**"] }, { alg: "HS256", kid: "other" })), - ).rejects.toThrow(/kid/); - await expect( - Key.verify(key, await signed({ publish: ["**"] }, { alg: "HS384", kid: testKey.kid })), + Key.verify(key, await signed({ publish: [""] }, { alg: "HS384", kid: testKey.kid })), ).rejects.toThrow(); } finally { installed.close(); @@ -300,7 +301,7 @@ test("verify - expired token", async () => { test("verify - token without exp field", async () => { const claimsWithoutExp = { root: "test-path", - publish: ["test-pub/**"], + publish: ["test-pub"], }; const key = Key.parse(encodeJwk(testKey)); @@ -308,7 +309,7 @@ test("verify - token without exp field", async () => { const claims = await Key.verify(key, token); expect(claims.root).toBe("test-path"); - expect(claims.publish).toEqual(["test-pub/**"]); + expect(claims.publish).toEqual(["test-pub"]); expect(claims.expires).toBe(undefined); }); @@ -331,8 +332,8 @@ test("round-trip - sign and verify", async () => { const key = Key.parse(encodeJwk(testKey)); const originalClaims = { root: "test-path", - publish: ["test-pub/**"], - subscribe: ["test-sub/**"], + publish: ["test-pub"], + subscribe: ["test-sub"], expires: Math.floor(wall().asUnixMillis() / 1000 + 60), issued: Math.floor(wall().asUnixMillis() / 1000), id: "round-trip", @@ -378,7 +379,7 @@ test("sign - claims validation path not prefix absolute sub", async () => { const key = Key.parse(encodeJwk(testKey)); const validClaims = { root: "test-path", - subscribe: ["absolute-sub/**"], + subscribe: ["absolute-sub"], }; const token = await Key.sign(key, validClaims); @@ -390,8 +391,8 @@ test("sign - claims validation path is prefix with relative paths", async () => const key = Key.parse(encodeJwk(testKey)); const validClaims = { root: "test-path", - publish: ["relative-pub/**"], - subscribe: ["relative-sub/**"], + publish: ["relative-pub"], + subscribe: ["relative-sub"], }; const token = await Key.sign(key, validClaims); @@ -403,7 +404,7 @@ test("sign - claims validation empty root", async () => { const key = Key.parse(encodeJwk(testKey)); const validClaims = { root: "", - publish: ["test-pub/**"], + publish: ["test-pub"], }; const token = await Key.sign(key, validClaims); @@ -723,11 +724,11 @@ test("sign - writes iat and jti only when the claims carry them", async () => { }; // Unset stays off the wire, matching the Rust crate rather than stamping now(). - const withoutIat = await Key.sign(key, { root: "test-path", publish: ["test-pub/**"] }); + const withoutIat = await Key.sign(key, { root: "test-path", publish: ["test-pub"] }); expect(decodePayload(withoutIat)).toEqual({ root: "test-path", publish: ["test-pub/**"] }); // A caller-supplied time and ID are written under their registered names. - const withIat = await Key.sign(key, { root: "test-path", publish: ["test-pub/**"], issued: 1700000000, id: "t-1" }); + const withIat = await Key.sign(key, { root: "test-path", publish: ["test-pub"], issued: 1700000000, id: "t-1" }); expect(decodePayload(withIat)).toEqual({ root: "test-path", publish: ["test-pub/**"], @@ -737,9 +738,7 @@ test("sign - writes iat and jti only when the claims carry them", async () => { // The wire names, and the legacy grants, are not claims to sign. for (const field of [{ iat: 1700000000 }, { put: ["test-pub"] }, { jti: "t-1" }]) { - await expect( - Key.sign(key, { root: "test-path", publish: ["test-pub/**"], ...field } as Claims), - ).rejects.toThrow(); + await expect(Key.sign(key, { root: "test-path", publish: ["test-pub"], ...field } as Claims)).rejects.toThrow(); } }); @@ -788,7 +787,7 @@ test("verify - claims validation during verification", async () => { const key = Key.parse(encodeJwk(testKey)); // We need to create a token with valid claims since Key.sign() would reject invalid ones - const token = await Key.sign(key, { root: "test-path", publish: ["absolute-pub/**"] }); + const token = await Key.sign(key, { root: "test-path", publish: ["absolute-pub"] }); // Test that valid tokens pass verification const verifiedClaims = await Key.verify(key, token); @@ -809,21 +808,53 @@ test("verify - a legacy put/get prefix token reads as subtrees", async () => { .setProtectedHeader({ alg: "HS256", kid: testKey.kid }) .sign(secret); const claims = await Key.verify(key, legacy); - expect(claims.publish).toEqual(["alice/**"]); - expect(claims.subscribe).toEqual(["**"]); + expect(claims.publish).toEqual(["alice"]); + expect(claims.subscribe).toEqual([""]); }); -test("sign - subtree grants are written as patterns", async () => { +// TEMPORARY MIGRATION (see `readPrefix` in wire.ts): a release before 0.22 reads a bare member +// as one exact path, so each prefix is written as its subtree pattern. +test("sign - prefix grants are written as the subtree patterns every release reads", async () => { const key = Key.parse(encodeJwk(testKey)); - const token = await Key.sign(key, { root: "test-path", publish: ["alice/**"], subscribe: ["**"] }); + const token = await Key.sign(key, { root: "test-path", publish: ["alice"], subscribe: [""] }); const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString()); expect(payload).toEqual({ root: "test-path", publish: ["alice/**"], subscribe: ["**"] }); + const claims = await Key.verify(key, token); + expect([claims.publish, claims.subscribe]).toEqual([["alice"], [""]]); +}); + +test("a wildcard is refused at signing and at verification", async () => { + const key = Key.parse(encodeJwk(testKey)); + await expect(Key.sign(key, { root: "test-path", publish: ["alice/**"] })).rejects.toThrow( + /write \\"alice\\" for its subtree/, + ); + await expect(Key.sign(key, { root: "test-path", subscribe: ["**"] })).rejects.toThrow( + /write the empty prefix \\"\\" for everything/, + ); + await expect(Key.sign(key, { root: "test-path", publish: { path: ["cam-*.hang"], rate: 8000 } })).rejects.toThrow(); + + // A token minted with patterns verifies when each is a subtree, and is refused otherwise, + // rather than read differently. + const secret = await crypto.subtle.importKey( + "raw", + Buffer.from(testKey.k, "base64url"), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign"], + ); + const mint = (publish: string[]) => + new SignJWT({ root: "test-path", publish }).setProtectedHeader({ alg: "HS256", kid: testKey.kid }).sign(secret); + expect((await Key.verify(key, await mint(["**", "alice/**"]))).publish).toEqual(["", "alice"]); + expect((await Key.verify(key, await mint(["alice/**", "bob"]))).publish).toEqual(["alice", "bob"]); + for (const publish of [["room/*/cam"], ["*/chat"], ["room/*/**"]]) { + await expect(Key.verify(key, await mint(publish))).rejects.toThrow(); + } }); test("sign - refuses the legacy put/get spelling, even within scope", async () => { const scoped: Key = { ...Key.parse(encodeJwk(testKey)), - scope: { root: "demo", publish: ["inside/**"] }, + scope: { root: "demo", publish: ["inside"] }, }; const inside = { root: "demo", put: ["inside"] } as unknown as Claims; await expect(Key.sign(scoped, inside)).rejects.toThrow('"put"'); @@ -835,10 +866,10 @@ test("key scope is enforced when signing and verifying", async () => { const unrestricted = Key.parse(encodeJwk(testKey)); const scoped: Key = { ...unrestricted, - scope: { root: "project", publish: ["live/**"] }, + scope: { root: "project", publish: ["live"] }, }; - const allowed = { root: "project", publish: ["live/room/**"] }; - const denied = { root: "project", publish: ["other/**"] }; + const allowed = { root: "project", publish: ["live/room"] }; + const denied = { root: "project", publish: ["other"] }; await expect(Key.sign(scoped, allowed)).resolves.toBeString(); await expect(Key.sign(scoped, denied)).rejects.toThrow("exceed the key scope"); @@ -850,11 +881,11 @@ test("key scope is enforced when signing and verifying", async () => { }); test("caps survive signing, and a key scope bounds only their paths", async () => { - const key: Key = { ...Key.parse(encodeJwk(testKey)), scope: { root: "room", publish: ["**"], subscribe: ["**"] } }; + const key: Key = { ...Key.parse(encodeJwk(testKey)), scope: { root: "room", publish: [""], subscribe: [""] } }; const capped = { root: "room", - publish: { path: ["alice/**"], rate: 8000 }, - subscribe: { path: ["bob/**"], rate: 16000 }, + publish: { path: ["alice"], rate: 8000 }, + subscribe: { path: ["bob"], rate: 16000 }, }; const claims = await Key.verify(key, await Key.sign(key, capped)); expect(claims).toMatchObject(capped); @@ -862,25 +893,25 @@ test("caps survive signing, and a key scope bounds only their paths", async () = // The decoded claims sign again with their caps. const resigned = await Key.verify(key, await Key.sign(key, claims)); expect(resigned).toMatchObject(capped); - await expect(Key.sign(key, { ...claims, publish: { path: ["alice/**"], rate: 0 } })).rejects.toThrow( + await expect(Key.sign(key, { ...claims, publish: { path: ["alice"], rate: 0 } })).rejects.toThrow( /rate must be a positive integer/, ); // A flat cap beside the role is not a spelling; it is refused, never dropped. - const flat = { root: "room", publish: ["alice/**"], publish_rate: 8000 } as unknown as Claims; + const flat = { root: "room", publish: ["alice"], publish_rate: 8000 } as unknown as Claims; await expect(Key.sign(key, flat)).rejects.toThrow(); }); test("key scope treats absolute and rooted grants alike", async () => { - const scoped: Key = { ...Key.parse(encodeJwk(testKey)), scope: { root: "project", publish: ["live/**"] } }; + const scoped: Key = { ...Key.parse(encodeJwk(testKey)), scope: { root: "project", publish: ["live"] } }; - await expect(Key.sign(scoped, { root: "", publish: ["project/live/room/**"] })).resolves.toBeString(); - await expect(Key.sign(scoped, { root: "project/live", publish: ["room/**"] })).resolves.toBeString(); + await expect(Key.sign(scoped, { root: "", publish: ["project/live/room"] })).resolves.toBeString(); + await expect(Key.sign(scoped, { root: "project/live", publish: ["room"] })).resolves.toBeString(); // Segment-aware: "live" must not cover "lively". - await expect(Key.sign(scoped, { root: "project", publish: ["lively/**"] })).rejects.toThrow("exceed the key scope"); + await expect(Key.sign(scoped, { root: "project", publish: ["lively"] })).rejects.toThrow("exceed the key scope"); // A root above the scope does not widen it. - await expect(Key.sign(scoped, { root: "", publish: ["**"] })).rejects.toThrow("exceed the key scope"); + await expect(Key.sign(scoped, { root: "", publish: [""] })).rejects.toThrow("exceed the key scope"); // Roles are independent: a publish-only scope grants no subscribe. - await expect(Key.sign(scoped, { root: "project", subscribe: ["live/**"] })).rejects.toThrow("exceed the key scope"); + await expect(Key.sign(scoped, { root: "project", subscribe: ["live"] })).rejects.toThrow("exceed the key scope"); }); /** Sign an arbitrary payload with `testKey`, bypassing the claims schema, as another issuer might. */ @@ -895,17 +926,17 @@ test("verify - only registered claims", async () => { const key = Key.parse(encodeJwk(testKey)); const now = Math.floor(wall().asUnixMillis() / 1000); - const token = await signRaw({ root: "room", publish: ["**"], iss: "api", sub: "alice", jti: "1", iat: now }); + const token = await signRaw({ root: "room", publish: [""], iss: "api", sub: "alice", jti: "1", iat: now }); const claims = await Key.verify(key, token); expect(claims.root).toBe("room"); expect(claims.id).toBe("1"); expect(claims).not.toHaveProperty("iss"); for (const [claim, payload] of [ - ["rooot", { rooot: "room/123", publish: ["**"] }], - ["user_id", { root: "room", publish: ["**"], user_id: 7 }], + ["rooot", { rooot: "room/123", publish: [""] }], + ["user_id", { root: "room", publish: [""], user_id: 7 }], ["cluster", { root: "room", put: [""], cluster: true }], - ["aud", { root: "room", publish: ["**"], aud: "relay" }], + ["aud", { root: "room", publish: [""], aud: "relay" }], ] as const) { await expect(Key.verify(key, await signRaw(payload))).rejects.toThrow(claim); } @@ -914,8 +945,8 @@ test("verify - only registered claims", async () => { test("verify - enforces not before", async () => { const key = Key.parse(encodeJwk(testKey)); const now = Math.floor(wall().asUnixMillis() / 1000); - expect((await Key.verify(key, await signRaw({ publish: ["**"], nbf: now - 60 }))).notBefore).toBe(now - 60); - await expect(Key.verify(key, await signRaw({ publish: ["**"], nbf: now + 3600 }))).rejects.toThrow(); + expect((await Key.verify(key, await signRaw({ publish: [""], nbf: now - 60 }))).notBefore).toBe(now - 60); + await expect(Key.verify(key, await signRaw({ publish: [""], nbf: now + 3600 }))).rejects.toThrow(); }); test("key IDs share Rust's grammar and 128-character limit", async () => { @@ -933,10 +964,10 @@ test("key IDs share Rust's grammar and 128-character limit", async () => { test("sign refuses a root no path can spell", async () => { const key = Key.parse(encodeJwk(testKey)); for (const root of ["/room", "room/", "a//b", "a\\b", "a\\", "a".repeat(4097), "a\uD800"]) { - await expect(Key.sign(key, { root, publish: ["**"] })).rejects.toThrow("invalid root"); + await expect(Key.sign(key, { root, publish: [""] })).rejects.toThrow("invalid root"); } for (const root of ["", "room", "a\\\\b", "a\\/b"]) { - await expect(Key.sign(key, { root, publish: ["**"] })).resolves.toBeString(); + await expect(Key.sign(key, { root, publish: [""] })).resolves.toBeString(); } }); @@ -950,9 +981,36 @@ test("public - shares no scope with the signer, and parses back", async () => { const key = await Key.generate("EdDSA"); key.scope = { root: "", publish: ["a"] }; const publicKey = Key.public(key); - expect(publicKey.scope).toEqual(key.scope); + // TEMPORARY MIGRATION (see `writtenScope` in wire.ts): spelled as a parsed scope is. + expect(publicKey.scope).toEqual({ root: "", publish: ["a/**"] }); + expect(key.scope).toEqual({ root: "", publish: ["a"] }); expect(publicKey.scope).not.toBe(key.scope); - expect(Key.parse(JSON.stringify(publicKey)).key_ops).toEqual(["verify"]); + const parsed = Key.parse(JSON.stringify(publicKey)); + expect([parsed.key_ops, parsed.scope]).toEqual([["verify"], publicKey.scope]); +}); + +// TEMPORARY MIGRATION (see `writtenScope` in wire.ts): a release before 0.22 reads a scope member +// as a pattern, the bare `video` as that one path, and refuses a token whose grant exceeds it. So +// the scope a key publishes spells each prefix as the subtree its tokens spell. The wire-compat +// lane (`test/interop/compat.sh`) checks the same key against released verifiers. +test("public - a scoped key publishes the scope its tokens are spelled in", async () => { + const generated = await Key.generate("ES256"); + const typed = { ...generated, scope: { root: "demo", publish: ["video"], subscribe: [""] } }; + const parsed = Key.parse(JSON.stringify(typed)); + const written = { root: "demo", publish: ["video/**"], subscribe: ["**"] }; + expect(parsed.scope).toEqual(written); + for (const key of [typed, parsed]) { + const published = JSON.parse(JSON.stringify(KeySet.public({ keys: [key] }))).keys[0]; + expect(published.scope).toEqual(written); + expect(Key.public(key).scope).toEqual(written); + + const token = await Key.sign(key, { root: "demo", publish: ["video"], subscribe: [""] }); + const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString()); + expect(payload).toEqual({ root: "demo", publish: ["video/**"], subscribe: ["**"] }); + const claims = await Key.verify(Key.parse(JSON.stringify(published)), token); + expect([claims.publish, claims.subscribe]).toEqual([["video"], [""]]); + await expect(Key.sign(key, { root: "demo", publish: ["audio"] })).rejects.toThrow(/exceed the key scope/); + } }); test("parse - refuses an algorithm another key type owns", async () => { diff --git a/js/auth/src/key.ts b/js/auth/src/key.ts index b919cd8f9c6..0e46e1f9d11 100644 --- a/js/auth/src/key.ts +++ b/js/auth/src/key.ts @@ -5,7 +5,7 @@ import { type Algorithm, AlgorithmSchema } from "./algorithm.ts"; import { type Claims, ClaimsFieldsSchema, ClaimsSchema, Scope, ScopeSchema } from "./claims.ts"; import * as Json from "./json.ts"; import * as Path from "./path.ts"; -import { encodeClaims } from "./wire.ts"; +import { encodeClaims, writtenScope } from "./wire.ts"; /** * A validated key identifier (kid). At most 128 alphanumeric, hyphen, or underscore characters. @@ -126,6 +126,9 @@ function toPublic(key: Key): PublicKey { const copy: Record = structuredClone(key); for (const field of ["d", "p", "q", "dp", "dq", "qi"]) delete copy[field]; copy.key_ops = ["verify"]; + // TEMPORARY MIGRATION (see `wire.ts`): a scope typed by hand is spelled as a parsed one is, so + // a published key reads as the same subtrees as the tokens it verifies. + if (key.scope) copy.scope = writtenScope(key.scope); return copy as PublicKey; } @@ -168,7 +171,7 @@ function parse(jwk: string): Key { * `issued` is written only when the claims carry one, so an unset field stays off the * wire rather than being stamped with the current time; nor is an `id` invented. * - * Takes each role as {@link Claims} spell it: its patterns, or `{path, rate}` when capped. + * Takes each role as {@link Claims} spell it: its prefixes, or `{path, rate}` when capped. * A field {@link Claims} lacks is refused, the wire's `exp` and the legacy `put`/`get` * included. * diff --git a/js/auth/src/path.ts b/js/auth/src/path.ts index 426949a8a92..492575e761a 100644 --- a/js/auth/src/path.ts +++ b/js/auth/src/path.ts @@ -2,7 +2,7 @@ * Segment-aware root matching, mirroring `@moq/net`'s path module. * * A token's root and a connection's path are literal paths, so overlapping them is - * prefix arithmetic; everything beneath is a pattern from `@moq/pattern`, re-exported + * prefix arithmetic; everything beneath is a prefix from `@moq/pattern`, re-exported * from this package so a token minting service does not pull in the networking * stack. The normalization and boundary rules must stay identical to the Rust * `moq-auth` crate's `path` module, which mints and checks the same tokens. Minting diff --git a/js/auth/src/set.test.ts b/js/auth/src/set.test.ts index 77a01a21c45..7c907c780a4 100644 --- a/js/auth/src/set.test.ts +++ b/js/auth/src/set.test.ts @@ -3,7 +3,7 @@ import { decodeProtectedHeader } from "jose"; import { Key } from "./key.ts"; import { KeySet } from "./set.ts"; -const CLAIMS = { root: "demo", publish: ["alice/**"], subscribe: ["**"] }; +const CLAIMS = { root: "demo", publish: ["alice"], subscribe: [""] }; async function keySet(...kids: (string | undefined)[]): Promise { return { keys: await Promise.all(kids.map((kid) => Key.generate("HS256", kid))) }; @@ -29,7 +29,7 @@ test("sign / verify - round trip selects the key by kid", async () => { const token = await KeySet.sign(set, CLAIMS); const claims = await KeySet.verify(set, token); expect(claims.root).toBe("demo"); - expect(claims.publish).toEqual(["alice/**"]); + expect(claims.publish).toEqual(["alice"]); }); test("verify - a token signed by a key outside the set is rejected", async () => { diff --git a/js/auth/src/wire.ts b/js/auth/src/wire.ts index f5ea30003bc..5690621afdd 100644 --- a/js/auth/src/wire.ts +++ b/js/auth/src/wire.ts @@ -1,32 +1,89 @@ /** * The JSON encoding of claims, key scopes, and auth server grants. * - * Grants are written as `publish` and `subscribe`. They were once prefix lists named - * `put` and `get`, which are still read. A role with a bitrate cap is written as an - * object, `{"path": [...], "rate": bps}`, which every older reader refuses rather than - * drop the cap. Must stay in lockstep with `wire.rs` in the Rust `moq-auth` crate. + * Grants are written as `publish` and `subscribe` prefix lists. They were once named + * `put` and `get`, which are still read as the same prefixes. A role with a bitrate cap is + * written as an object, `{"path": [...], "rate": bps}`, which every older reader refuses + * rather than drop the cap. Until the version migrations {@link readPrefix} names, a prefix + * is written and read as the subtree pattern releases before 0.22 spell it. Must stay in + * lockstep with `wire.rs` in the Rust `moq-auth` crate. * * @module */ -import { Pattern } from "@moq/pattern"; +import { Prefix, parts } from "@moq/pattern"; import * as z from "zod/mini"; import type { Claims } from "./claims.ts"; -/** A list of pattern texts, each validated by `Pattern.parse`. */ -export const PatternListSchema = z.array( +/** + * A list of prefix texts, each validated by `Prefix.parse`. A member holding a `*` is refused + * with the prefix that replaces it, since a grant takes no wildcard. + */ +export const PrefixListSchema = z.array( z.string().check( - z.refine( - (text) => { - try { - Pattern.parse(text); - return true; - } catch { - return false; - } - }, - { message: "invalid path pattern" }, - ), + z.superRefine((text, ctx) => { + try { + Prefix.parse(text); + } catch (error) { + ctx.addIssue({ code: "custom", message: error instanceof Error ? error.message : String(error) }); + } + }), + ), +); + +/** + * TEMPORARY MIGRATION: the prefix a grant member names, as releases before 0.22 spell it. + * Mirrors `alias` in the Rust crate's `wire.rs`. + * + * Those releases read a member as a pattern: a bare `foo` is the one path `foo`, and `""` the + * root alone. So every list one of them reads, in a token, a key scope, or an auth server's + * grant, is written as the same subtrees in pattern form ({@link written}), and a member + * spelled that way, `foo/**` or `**`, reads back as its prefix. Any other wildcard is still + * refused, and nothing else takes the alias: `Key.sign`'s claims and the CLI's flags stay strict. + * + * Deleted in two steps (`quest/m4/grant-alias-delete.md`): the first version migration after + * 0.22 writes bare prefixes, and the one after it deletes the read, once tokens and key scopes + * minted with `/**` have expired or been re-minted. + */ +function readPrefix(text: string): Prefix { + const head = text === "**" ? "" : text.length > 3 && text.endsWith("/**") ? text.slice(0, -3) : text; + // A wildcard left in the head names something other than one subtree; parsing all of it says so. + return Prefix.parse(head.includes("*") ? text : head); +} + +/** TEMPORARY MIGRATION (see {@link readPrefix}): the prefixes a list names, in either spelling. */ +export function readPrefixes(texts: readonly string[] | undefined): string[] { + return (texts ?? []).map((text) => readPrefix(text).text); +} + +/** + * TEMPORARY MIGRATION (see {@link readPrefix}): each prefix spelled as the subtree pattern a release + * before 0.22 reads, `foo/**` or `**`, from either spelling. A prefix as deep as a path may be is its + * literal, which a release reads as that one path: nothing lies beneath it. + */ +function written(texts: readonly string[]): string[] { + return texts.map((text) => readPrefix(text).pattern().toString()); +} + +/** + * TEMPORARY MIGRATION (see {@link readPrefix}): a prefix list as a token, key scope, or grant + * spells it, read as its prefixes. + */ +export const WirePrefixListSchema = z.array( + z.pipe( + z.string(), + z.transform((text, ctx) => { + try { + return readPrefix(text).text; + } catch (error) { + ctx.issues.push({ + code: "custom", + message: error instanceof Error ? error.message : String(error), + input: text, + }); + return text; + } + }), ), ); @@ -44,7 +101,7 @@ const RateSchema = z.number().check( ); /** - * What one role (publish or subscribe) reaches: a list of patterns, or exactly `path` and + * What one role (publish or subscribe) reaches: a list of prefixes, or exactly `path` and * `rate` (bits per second) when capped, so a misspelled cap is refused rather than read as * none. Every older reader refuses the capped form rather than drop the cap. A key scope * bounds paths, never bitrate, so it takes only the list. @@ -52,10 +109,10 @@ const RateSchema = z.number().check( * Mirrors `moq_auth::Access` in the Rust crate. */ export const AccessSchema = z.union([ - PatternListSchema, + PrefixListSchema, z.strictObject({ - /** Patterns the role reaches, relative to the root. */ - path: PatternListSchema, + /** Prefixes the role reaches, relative to the root, each covering its subtree. */ + path: PrefixListSchema, /** The most the role may move, in bits per second. */ rate: RateSchema, }), @@ -63,7 +120,31 @@ export const AccessSchema = z.union([ /** A value parsed by {@link AccessSchema}. */ export type Access = z.output; -/** The patterns `access` reaches, empty when the role is absent. */ +/** TEMPORARY MIGRATION (see {@link readPrefix}): a role as a token or grant spells it, read as its prefixes. */ +export const WireAccessSchema = z.union([ + WirePrefixListSchema, + z.strictObject({ path: WirePrefixListSchema, rate: RateSchema }), +]); + +/** TEMPORARY MIGRATION (see {@link readPrefix}): `access` with each prefix spelled as {@link written} does. */ +export function writtenAccess(access: Access): Access { + return Array.isArray(access) ? written(access) : { ...access, path: written(access.path) }; +} + +/** + * TEMPORARY MIGRATION (see {@link readPrefix}): a key scope with each prefix spelled as + * {@link written} does, so a key's JWK reads as the same subtrees as the tokens it signs. + */ +export function writtenScope(scope: T): T { + const { publish, subscribe } = scope; + return { + ...scope, + ...(publish && { publish: written(publish) }), + ...(subscribe && { subscribe: written(subscribe) }), + }; +} + +/** The prefixes `access` reaches, empty when the role is absent. */ export function paths(access: Access | undefined): string[] { if (access === undefined) return []; return Array.isArray(access) ? access : access.path; @@ -78,11 +159,11 @@ type WireGrants = { }; /** - * Read legacy `moq-token` prefix grants as the subtree patterns they always meant. + * Read legacy `moq-token` prefix grants as the prefixes they always were. * - * 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. + * Their slashes were implicit at the boundaries, so `/room/` reads as `room`. A document never + * mixes the two encodings, and a legacy prefix containing `*` is refused: it had no wildcards, + * so reading one now would hand out a grant nobody wrote. */ export function decodeGrants(wire: T, ctx: { issues: unknown[] }): Omit { const { put, get, ...rest } = wire; @@ -96,24 +177,34 @@ export function decodeGrants(wire: T, ctx: { issues: unkno return fail("mixes the legacy put/get fields with publish/subscribe"); } - const subtrees = (prefixes: string | string[] | undefined) => - prefixes === undefined + const prefixes = (texts: string | string[] | undefined) => + texts === undefined ? undefined - : (typeof prefixes === "string" ? [prefixes] : prefixes).map((prefix) => Pattern.subtree(prefix).text); + : (typeof texts === "string" ? [texts] : texts).map( + (text) => + Prefix.parse( + parts(text) + .filter((part) => part !== "") + .join("/"), + ).text, + ); try { - return { ...rest, publish: subtrees(put), subscribe: subtrees(get) }; + return { ...rest, publish: prefixes(put), subscribe: prefixes(get) }; } catch (error) { return fail(`legacy prefix: ${error instanceof Error ? error.message : String(error)}`); } } -/** Omit a role that grants nothing and carries no cap, as both libraries write it. */ +/** + * Omit a role that grants nothing and carries no cap, as both libraries write it, and spell the + * rest as {@link writtenAccess} does. + */ export function encodeGrants( grants: T, ): Omit & { publish?: Access; subscribe?: Access } { const { publish, subscribe, ...rest } = grants; const kept = (access: Access | undefined) => - access !== undefined && (!Array.isArray(access) || access.length > 0) ? access : undefined; + access !== undefined && (!Array.isArray(access) || access.length > 0) ? writtenAccess(access) : undefined; const pub = kept(publish); const sub = kept(subscribe); return { ...rest, ...(pub && { publish: pub }), ...(sub && { subscribe: sub }) }; diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 4b5a18626e3..08c0d6a9c19 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -35,8 +35,8 @@ export namespace Auth { } export interface Grant { expires?: Clock.Wall; - publish: Path.Patterns; - subscribe: Path.Patterns; + publish: Path.Prefixes; + subscribe: Path.Prefixes; } export class Handle { constructor(); @@ -204,7 +204,7 @@ export namespace Connection { export type Redirect = "follow" | "same-host" | "ignore"; export interface Session { abort(): void; - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; readonly auth: Auth.Handle; close(): Promise; readonly closed: Signals.GetPromise; @@ -247,7 +247,7 @@ export class Connection { constructor(props?: Connection.Props); static readonly accept: ({ ... }: Connection.AcceptProps) => Promise; readonly allocator: Signals.Getter; - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; answered(): Promise; readonly auth: Signals.Getter; static readonly certificateHash: (cert: string | BufferSource) => Promise>; @@ -473,7 +473,7 @@ export namespace Origin { withdraw(): void; } export class Consumer implements Origin.Reader { - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; get closed(): Signals.GetPromise; get discovery(): Signals.Getter; follow(path: Path.Valid, options?: Origin.FollowOptions): Origin.Following; @@ -491,7 +491,7 @@ export namespace Origin { } export class Producer implements Origin.Table { constructor(); - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; claim(prefix: Path.Valid, options?: Origin.RouteOptions): Origin.Claim; close(abort?: Error): void; get closed(): Signals.GetPromise; @@ -500,10 +500,10 @@ export namespace Origin { follow(path: Path.Valid, options?: Origin.FollowOptions): Origin.Following; request(path: Path.Valid, options?: Origin.RequestOptions): Origin.Requesting; route(path: Path.Valid, options?: Origin.RouteOptions): Origin.Route; - scope(root: Path.Valid, patterns: Path.Patterns): Origin.Producer; + scope(root: Path.Valid, prefixes: Path.Prefixes): Origin.Producer; } export interface Reader { - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; readonly closed: Signals.GetPromise; readonly discovery: Signals.Getter; follow(path: Path.Valid, options?: Origin.FollowOptions): Origin.Following; @@ -558,8 +558,11 @@ export namespace Origin { export namespace Path { export { InvalidPattern } from "@moq/pattern"; + export { InvalidPrefix } from "@moq/pattern"; export { Pattern } from "@moq/pattern"; export { Patterns } from "@moq/pattern"; + export { Prefix } from "@moq/pattern"; + export { Prefixes } from "@moq/pattern"; export type Relative = string & { __brand: "Relative"; }; diff --git a/js/net/bench/broadcasts.ts b/js/net/bench/broadcasts.ts index c2cfea9530b..c0d721e6f80 100644 --- a/js/net/bench/broadcasts.ts +++ b/js/net/bench/broadcasts.ts @@ -49,19 +49,13 @@ for (const scope of [ scope === "full-name" || scope === "overlap-only" ? origin.consume().routes(Path.Pattern.parse("**/*.chat")) : (scope === "producer-scopes" - ? origin.scope( - Path.empty(), - new Path.Patterns([Path.Pattern.parse(`room/**/tag-${index}`)]), - ) + ? origin.scope(Path.empty(), Path.Prefixes.parse([`room/0/tag-${index}`])) : scope === "rooted-producers" - ? origin.scope( - Path.from("room"), - new Path.Patterns([Path.Pattern.parse(`**/tag-${index}`)]), - ) + ? origin.scope(Path.from("room"), Path.Prefixes.parse([`0/tag-${index}`])) : origin ).routes( scope === "distinct-scopes" ? Path.Pattern.parse(`room/**/tag-${index}`) : undefined, - // These scopes only overlap `room/`, so they measure the covering view. + // These scopes only lie beneath the touched route, so they measure the covering view. { covering: scope !== "unscoped" }, ), ); @@ -194,24 +188,24 @@ const quic = { }; }, } as unknown as WebTransport; -console.log("auth,patterns,fetches,grant_update_us"); -for (const patternCount of [8, 32, 128]) { +console.log("auth,prefixes,fetches,grant_update_us"); +for (const prefixCount of [8, 32, 128]) { for (const fetchCount of [1, 8, 32]) { - const subscribe = new Path.Patterns([ - Path.Pattern.subtree("room"), - ...Array.from({ length: patternCount - 1 }, (_, i) => Path.Pattern.literal(`other/${i}`)), + const subscribe = Path.Prefixes.parse([ + "room", + ...Array.from({ length: prefixCount - 1 }, (_, i) => `other/${i}`), ]); - const grant = new Signal({ publish: new Path.Patterns(), subscribe }); + const grant = new Signal({ publish: new Path.Prefixes(), subscribe }); const subscriber = new Subscriber(quic, Version.DRAFT_05, toHop(1n), undefined, undefined, undefined, grant); const groups = await Promise.all( Array.from({ length: fetchCount }, (_, i) => subscriber.fetchGroup(Path.from(`room/${i}`), "video", 0n)), ); const start = performance.now(); for (let i = 0; i < 1024; i++) { - grant.set({ publish: new Path.Patterns(), subscribe, expires: Wall.unixMillis(i) }); + grant.set({ publish: new Path.Prefixes(), subscribe, expires: Wall.unixMillis(i) }); await Promise.resolve(); } - console.log(`fetch,${patternCount},${fetchCount},${(((performance.now() - start) * 1000) / 1024).toFixed(2)}`); + console.log(`fetch,${prefixCount},${fetchCount},${(((performance.now() - start) * 1000) / 1024).toFixed(2)}`); for (const group of groups) { if (group.closed.peek() !== undefined) throw new Error("allowed fetch ended"); group.close(); diff --git a/js/net/bench/forward.ts b/js/net/bench/forward.ts index 8abcc0c67e5..1bd6547e9fb 100644 --- a/js/net/bench/forward.ts +++ b/js/net/bench/forward.ts @@ -36,7 +36,7 @@ class FakeSession { consume: () => new BroadcastProducer().consume(), exclusive: false, leave: () => {}, - auth: new AuthSession({ peerGrant: { publish: new Path.Patterns(), subscribe: new Path.Patterns() } }), + auth: new AuthSession({ peerGrant: { publish: new Path.Prefixes(), subscribe: new Path.Prefixes() } }), }); this.closed = new Promise((resolve) => { this.die = () => resolve(null); @@ -61,10 +61,7 @@ for (const touched of ["covering", "single"] as const) { for (const routeCount of routeCounts) { const origin = new Producer(); const heads = Array.from({ length: headCount }, (_, index) => Path.from(`h${index}`)); - const scoped = origin.scope( - Path.empty(), - new Path.Patterns(heads.map((head) => Path.Pattern.subtree(head))), - ); + const scoped = origin.scope(Path.empty(), Path.Prefixes.parse(heads)); const session = new FakeSession(); forwardAnnounced(session as unknown as Session, scoped); diff --git a/js/net/src/announced.test-util.ts b/js/net/src/announced.test-util.ts index 32de92725fe..b5952f53d79 100644 --- a/js/net/src/announced.test-util.ts +++ b/js/net/src/announced.test-util.ts @@ -5,11 +5,11 @@ import type * as Path from "./path"; /** A closeable announcement snapshot for tests and benchmarks. @internal */ export async function announcedView( - origin: { routes(scope?: Path.Pattern, options?: Options): Routes }, - scope?: Path.Pattern, + origin: { routes(filter?: Path.Pattern, options?: Options): Routes }, + filter?: Path.Pattern, options?: Options, ) { - const stream = origin.routes(scope, options); + const stream = origin.routes(filter, options); const value = new Signal>(new Map()); const done = (async () => { for await (const event of stream) diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 1339d5209cf..6938599ab64 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -45,8 +45,8 @@ async function nextRoute(announced: { } } -function patterns(...prefixes: string[]): Path.Patterns { - return new Path.Patterns(prefixes.map((prefix) => Path.Pattern.subtree(prefix))); +function patterns(...prefixes: string[]): Path.Prefixes { + return Path.Prefixes.parse(prefixes); } function grant(publish: string[], subscribe: string[]): Grant { @@ -290,9 +290,7 @@ describe.each([Lite.ALPN_07_WIP])("%s", (protocol) => { await waitFor(client.auth.grant, (g) => g?.publish.equals(patterns("a")) === true); const token = await answered(client.auth.add("update")); await waitFor(client.auth.grant, (g) => g?.publish.equals(patterns("a", "b")) === true); - const publish = new Path.Patterns( - Array.from({ length: 20 }, (_, i) => Path.Pattern.subtree(`${i}${"x".repeat(4000)}`)), - ); + const publish = Path.Prefixes.parse(Array.from({ length: 20 }, (_, i) => `${i}${"x".repeat(4000)}`)); issued[issued.length - 1].update({ publish, subscribe: patterns() }); expect(await token.closed).toBeInstanceOf(Unsupported); expect(token.grant.peek()).toBeUndefined(); @@ -1198,17 +1196,16 @@ test.each(Array.from({ length: 17 }, (_, gap) => gap))( }, ); -test.each([Lite.ALPN_07_WIP])("%s carries literal and wildcard grants exactly", async (protocol) => { +test.each([Lite.ALPN_07_WIP])("%s carries prefix grants exactly", async (protocol) => { const { client, server } = await connect({ publish: new OriginProducer(), protocol }); const requests = server.auth.requests(); const unions: Record = { - "": [["a/**"], []], + "": [["a"], []], exact: [["room/alice"], []], - wildcard: [["room/*/cam"], ["**/demo.hang"]], - mixed: [["room/**", "lobby", "cam-*.hang"], []], - root: [[""], ["**"]], + nested: [["room", "room/alice", "lobby", "cam.hang"], ["demo.hang"]], + root: [[""], [""]], }; - const parse = (texts: string[]) => new Path.Patterns(texts.map((text) => Path.Pattern.parse(text))); + const parse = (texts: string[]) => Path.Prefixes.parse(texts); const issued: Issued[] = []; void (async () => { for (;;) { @@ -1220,7 +1217,7 @@ test.each([Lite.ALPN_07_WIP])("%s carries literal and wildcard grants exactly", })(); await waitFor(client.auth.grant, (g) => g !== undefined); - for (const token of ["exact", "wildcard", "mixed", "root"]) { + for (const token of ["exact", "nested", "root"]) { const [publish, subscribe] = unions[token]; const added = await answered(client.auth.add(token)); const got = added.grant.peek(); diff --git a/js/net/src/auth.ts b/js/net/src/auth.ts index 1ba445cd366..a07fe0dca37 100644 --- a/js/net/src/auth.ts +++ b/js/net/src/auth.ts @@ -27,13 +27,14 @@ export { Duplicate, Handle, Unsupported } from "./auth_session.ts"; /** * What a peer lets this side do, in this side's own paths (relative to the session). * - * An empty {@link Path.Patterns} grants nothing; `**` grants everything. + * Each {@link Path.Prefix} covers its whole subtree. An empty {@link Path.Prefixes} grants + * nothing; the empty prefix grants everything. */ export interface Grant { /** The broadcasts this side may publish to the peer. */ - publish: Path.Patterns; + publish: Path.Prefixes; /** The broadcasts this side may subscribe to from the peer. */ - subscribe: Path.Patterns; + subscribe: Path.Prefixes; /** When the grant lapses on the `@moq/clock` wall clock, or undefined for never. */ expires?: Wall; } diff --git a/js/net/src/auth_gate.test.ts b/js/net/src/auth_gate.test.ts index 440ba17be39..6ba03edf173 100644 --- a/js/net/src/auth_gate.test.ts +++ b/js/net/src/auth_gate.test.ts @@ -5,8 +5,8 @@ import { Gate } from "./auth_gate.ts"; import * as Path from "./path.ts"; const grant = (prefix: string): Grant => ({ - publish: new Path.Patterns([Path.Pattern.subtree(prefix)]), - subscribe: new Path.Patterns([]), + publish: new Path.Prefixes([Path.Prefix.parse(prefix)]), + subscribe: new Path.Prefixes(), }); test("a gate allows everything until the peer answers", () => { diff --git a/js/net/src/auth_session.test.ts b/js/net/src/auth_session.test.ts index db54086676f..394bd1a0096 100644 --- a/js/net/src/auth_session.test.ts +++ b/js/net/src/auth_session.test.ts @@ -24,7 +24,7 @@ async function reactions(run: () => Promise): Promise { } test.each([2, 64])("churn over %i live broadcasts leaves no reaction behind per change", async (live) => { - const everything = new Path.Patterns([Path.Pattern.subtree("")]); + const everything = new Path.Prefixes([Path.Prefix.parse("")]); const counts = await reactions(async () => { const { promise: closed, resolve: close } = Promise.withResolvers(); const quic = { closed, close: () => {} } as unknown as WebTransport; @@ -47,7 +47,7 @@ test("a session that closes during an authorization wait ends enforcement withou let closes = 0; const quic = { closed, close: () => closes++ } as unknown as WebTransport; // Nothing granted yet, and an authorization that is never answered. - const grant = new Signal({ publish: new Path.Patterns(), subscribe: new Path.Patterns() }); + const grant = new Signal({ publish: new Path.Prefixes(), subscribe: new Path.Prefixes() }); const table = new Signal | undefined>(new Map([[Path.from("live"), 0]])); // Counts the change callbacks the enforcer holds on the table, which must go with it. let registered = 0; diff --git a/js/net/src/auth_session.ts b/js/net/src/auth_session.ts index 98e1f5ea9e3..b6cb00b3f44 100644 --- a/js/net/src/auth_session.ts +++ b/js/net/src/auth_session.ts @@ -5,25 +5,19 @@ import { closeReason, error, ProtocolViolation, SessionCode, SessionError, Strea import * as Path from "./path.ts"; import type { Reader, Stream, Writer } from "./stream.ts"; -/** Write a grant's patterns as a count and each one's canonical text, the encoding both wires' AUTH_OK share. @internal */ -export async function encodePatterns(w: Writer, patterns: Path.Patterns) { - await w.u53(patterns.size); - for (const pattern of patterns) await w.string(pattern.text); +/** Write a grant's prefixes as a count and each one's text, the encoding both wires' AUTH_OK share. @internal */ +export async function encodePrefixes(w: Writer, prefixes: Path.Prefixes) { + await w.u53(prefixes.size); + for (const prefix of prefixes) await w.string(prefix.text); } -/** Read a grant's patterns, refusing any that is invalid or not canonical. @internal */ -export async function decodePatterns(r: Reader): Promise { +/** Read a grant's prefixes, refusing any that is a wildcard or not a valid path. @internal */ +export async function decodePrefixes(r: Reader): Promise { const count = await r.u53(); - const patterns: Path.Pattern[] = []; - for (let i = 0; i < count; i++) { - const text = await r.string(); - const pattern = Path.Pattern.parse(text); - // Only the canonical spelling is valid, so each pattern has one encoding. - if (pattern.text !== text) throw new Error(`non-canonical pattern: ${text}`); - patterns.push(pattern); - } - // Reduce once, so a peer's count never turns into quadratic work. - return new Path.Patterns(patterns); + const prefixes: Path.Prefix[] = []; + for (let i = 0; i < count; i++) prefixes.push(Path.Prefix.parse(await r.string())); + // Sort and sweep once, so a peer's count costs n log n whatever its shape. + return new Path.Prefixes(prefixes); } /** Whether two grants allow the same paths and lapse together. */ @@ -75,8 +69,8 @@ type Refused = "final" | "undecided"; /** A grant as the wire carries it: the expiry is milliseconds from now. @internal */ export interface WireGrant { - publish: Path.Patterns; - subscribe: Path.Patterns; + publish: Path.Prefixes; + subscribe: Path.Prefixes; expires?: number; } @@ -111,17 +105,17 @@ export interface AuthWire { /** The paths any of `grants` allows. Reduced once over all of them, not pairwise. */ function union(grants: Iterable): Grant { - const publish: Path.Pattern[] = []; - const subscribe: Path.Pattern[] = []; + const publish: Path.Prefix[] = []; + const subscribe: Path.Prefix[] = []; let expires: Wall | undefined; for (const grant of grants) { - for (const pattern of grant.publish) publish.push(pattern); - for (const pattern of grant.subscribe) subscribe.push(pattern); + for (const prefix of grant.publish) publish.push(prefix); + for (const prefix of grant.subscribe) subscribe.push(prefix); // The earliest expiry is when the union next shrinks. if (grant.expires !== undefined && (expires === undefined || grant.expires.lt(expires))) expires = grant.expires; } - return { publish: new Path.Patterns(publish), subscribe: new Path.Patterns(subscribe), expires }; + return { publish: new Path.Prefixes(publish), subscribe: new Path.Prefixes(subscribe), expires }; } // The transport closed under the stream, which ends the whole session rather than one token. diff --git a/js/net/src/connection/answered.test.ts b/js/net/src/connection/answered.test.ts index ef91a22a3d1..bd60b1a154c 100644 --- a/js/net/src/connection/answered.test.ts +++ b/js/net/src/connection/answered.test.ts @@ -20,7 +20,7 @@ function named(request: Request): string { } /** Grants everything and refuses nothing. */ -const GRANT = { publish: new Path.Patterns([Path.Pattern.all()]), subscribe: new Path.Patterns() }; +const GRANT = { publish: Path.Prefixes.all(), subscribe: new Path.Prefixes() }; /** * A lite-07 relay that answers each token itself: `decide` accepts or rejects the request, and diff --git a/js/net/src/connection/forward.test.ts b/js/net/src/connection/forward.test.ts index 4f617cea7a1..ae092ef44ed 100644 --- a/js/net/src/connection/forward.test.ts +++ b/js/net/src/connection/forward.test.ts @@ -78,7 +78,7 @@ class FakeSession { consume: (path, instance) => this.consume(path, instance), exclusive, leave: () => {}, - auth: new AuthSession({ peerGrant: { publish: new Path.Patterns(), subscribe: new Path.Patterns() } }), + auth: new AuthSession({ peerGrant: { publish: new Path.Prefixes(), subscribe: new Path.Prefixes() } }), }); this.closed = new Promise((resolve) => { this.#die = resolve; @@ -690,12 +690,9 @@ test("a request replaced across one coalesced wakeup still gets answered", async origin.close(); }); -test("a scoped origin narrows a root watch to its disjoint literal heads", async () => { +test("a scoped origin narrows a root watch to its prefixes", async () => { const origin = new OriginProducer(); - const scoped = origin.scope( - Path.empty(), - new Path.Patterns(["room/*/chat", "room/nested/**", "other/**"].map(Path.Pattern.parse)), - ); + const scoped = origin.scope(Path.empty(), Path.Prefixes.parse(["room", "room/nested", "other"])); const session = new FakeSession(); forwardAnnounced(session.session, scoped); @@ -704,14 +701,14 @@ test("a scoped origin narrows a root watch to its disjoint literal heads", async expect(session.open()).toEqual(["other/**", "room/**"]); all.close(); - // A watch inside a head rides the head's open request, and one outside the scope asks for - // nothing. + // A watch inside a prefix rides the prefix's open request, and one outside the scope asks + // for nothing. let inside = origin.routes(Path.Pattern.subtree(Path.from("room/alice/chat"))); const outside = origin.routes(Path.Pattern.subtree(Path.from("elsewhere"))); await advance(LINGER); expect(session.open()).toEqual(["room/**"]); - // With nothing open above it, a watch inside a head asks for itself. + // With nothing open above it, a watch inside a prefix asks for itself. inside.close(); await advance(LINGER); expect(session.open()).toEqual([]); @@ -727,17 +724,17 @@ test("a scoped origin narrows a root watch to its disjoint literal heads", async test("a scoped session filters discovered routes and requests under its root", async () => { const origin = new OriginProducer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room/alice/chat", "room/bob/chat"])); const session = new FakeSession(); forwardAnnounced(session.session, scoped); const all = origin.routes(); const events = record(all); await settle(); - // Session-relative: the scope's root is the session's. - expect(session.open()).toEqual(["room/**"]); - for (const prefix of ["room/alice/video", "room/alice/chat", "room/bob/chat"]) - start(session.stream("room/**"), prefix); + // Session-relative: the scope's root is the session's, and it asks for each prefix. + expect(session.open()).toEqual(["room/alice/chat/**", "room/bob/chat/**"]); + start(session.stream("room/alice/chat/**"), "room/alice/chat"); + start(session.stream("room/bob/chat/**"), "room/bob/chat"); const allowed = origin.request(Path.from("tenant/room/bob/chat")); const denied = origin.request(Path.from("tenant/room/bob/video")); @@ -851,7 +848,7 @@ test("a request synced as it opens releases the watcher", async () => { test("a scoped session refuses blind requests outside its allowed paths", async () => { const origin = new OriginProducer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room/bob/chat"])); const session = new FakeSession(); forwardAnnounced(session.session, scoped); const allowed = origin.request(Path.from("tenant/room/bob/chat")); diff --git a/js/net/src/connection/migrate.test.ts b/js/net/src/connection/migrate.test.ts index f9908f52d5b..9794ab0ab51 100644 --- a/js/net/src/connection/migrate.test.ts +++ b/js/net/src/connection/migrate.test.ts @@ -418,8 +418,8 @@ test("the connection's tokens ride every session across a GOAWAY", async () => { const token = new TextDecoder().decode(request.token); relay.tokens.push(token); const issued = request.accept({ - publish: new Path.Patterns([Path.Pattern.all()]), - subscribe: new Path.Patterns(), + publish: Path.Prefixes.all(), + subscribe: new Path.Prefixes(), }); void issued.closed.then(() => relay.withdrawn.push(token)); } diff --git a/js/net/src/connection/pool.ts b/js/net/src/connection/pool.ts index 1848bb6c5e3..75d9ee1b418 100644 --- a/js/net/src/connection/pool.ts +++ b/js/net/src/connection/pool.ts @@ -356,20 +356,20 @@ export class Connection { } /** - * Subscribe to broadcast announcements matching `scope`, spanning reconnects + * Subscribe to broadcast announcements matching `filter`, spanning reconnects * and URL switches: a switch retracts everything from * the old relay's origin, then the new one's arrivals stream in. */ - announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { - return list(this.routes(scope, options)); + announced(filter: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { + return list(this.routes(filter, options)); } /** - * Every route change matching `scope`, spanning reconnects like {@link announced}. + * Every route change matching `filter`, spanning reconnects like {@link announced}. * * @internal */ - routes(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Routes { + routes(filter: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Routes { const producer = new AnnounceProducer(); const consumer = producer.consume(); @@ -386,7 +386,7 @@ export class Connection { return; } // A URL switch retracts what the old origin announced. - pumpAnnounced(effect, origin.routes(scope, options), producer); + pumpAnnounced(effect, origin.routes(filter, options), producer); }); closeUnwanted(producer); diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index d4e4b0a8036..1df3de2a8a1 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -1083,8 +1083,8 @@ test("closing the loop keeps the tokens until the session drains", async () => { if (!request) break; const token = new TextDecoder().decode(request.token); request.accept({ - publish: new Path.Patterns(token === "token" ? [Path.Pattern.all()] : []), - subscribe: new Path.Patterns(), + publish: token === "token" ? Path.Prefixes.all() : new Path.Prefixes(), + subscribe: new Path.Prefixes(), }); } }); diff --git a/js/net/src/connection/scope.test.ts b/js/net/src/connection/scope.test.ts index 2244b0225d7..a943a7269f8 100644 --- a/js/net/src/connection/scope.test.ts +++ b/js/net/src/connection/scope.test.ts @@ -16,11 +16,8 @@ for (const protocol of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18]) { const url = new URL("https://localhost/test"); const source = new Producer(); const destination = new Producer(); - const publish = source.scope(Path.from("server"), new Path.Patterns([Path.Pattern.all()])); - const consume = destination.scope( - Path.from("client"), - new Path.Patterns(["room/**", "other/**"].map(Path.Pattern.parse)), - ); + const publish = source.scope(Path.from("server"), Path.Prefixes.all()); + const consume = destination.scope(Path.from("client"), Path.Prefixes.parse(["room", "other"])); const broadcasts = ["room/live", "room/.hidden", "other/live", "outside"].map((path) => { const broadcastRoute = publish.route(Path.from(path)); const broadcast = broadcastRoute.broadcast; diff --git a/js/net/src/connection/session.ts b/js/net/src/connection/session.ts index de180aeb068..03f60db2f61 100644 --- a/js/net/src/connection/session.ts +++ b/js/net/src/connection/session.ts @@ -50,17 +50,17 @@ export interface Session { readonly peerImplementation: Promise; /** - * Subscribe to broadcast announcements matching `scope`, any pattern (`foo/**` + * Subscribe to broadcast announcements matching `filter`, any pattern (`foo/**` * for a subtree, `room/* /chat` for each room's chat, default `**`). Paths are - * relative to the session; captures report what the scope's wildcards stood for. + * relative to the session; captures report what the filter's wildcards stood for. * Only routes whose literal name matches are reported unless `options.covering` * opts in, and hidden routes unless `options.hidden` does. A peer without * discovery refuses or resets the stream. */ - announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer; + announced(filter?: Path.Pattern, options?: announce.Options): announce.Consumer; - /** Every route change matching `scope`, for code that forwards routes. @internal */ - routes(scope?: Path.Pattern, options?: RouteOptions): Routes; + /** Every route change matching `filter`, for code that forwards routes. @internal */ + routes(filter?: Path.Pattern, options?: RouteOptions): Routes; /** * Snapshot the transport's counters, querying it fresh on each call. diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index 46fb4933ab9..aa4c2c8dede 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -180,8 +180,8 @@ export class Connection implements Session { this.#auth = new AuthSession({ requests: true, peerGrant: { - publish: new Path.Patterns([Path.Pattern.all()]), - subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), + publish: Path.Prefixes.all(), + subscribe: publish ? Path.Prefixes.all() : new Path.Prefixes(), }, handle, draining: () => this.#goaway.peek() !== undefined || this.#phase.kind !== "open", @@ -322,14 +322,14 @@ export class Connection implements Session { } } - /** The paths announced matching `scope`, each present or absent; see {@link Session.announced}. */ - announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer { - return announce.list(this.#subscriber.routes(scope, options)); + /** The paths announced matching `filter`, each present or absent; see {@link Session.announced}. */ + announced(filter?: Path.Pattern, options?: announce.Options): announce.Consumer { + return announce.list(this.#subscriber.routes(filter, options)); } - /** Every route change matching `scope`, for code that forwards routes. @internal */ - routes(scope?: Path.Pattern, options?: announce.Options): announce.Routes { - return this.#subscriber.routes(scope, options); + /** Every route change matching `filter`, for code that forwards routes. @internal */ + routes(filter?: Path.Pattern, options?: announce.Options): announce.Routes { + return this.#subscriber.routes(filter, options); } /** Accepts request streams and dispatches them. */ diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index e27a2c6f2a5..3fd0333a3a6 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -319,7 +319,7 @@ test("TRACK_STATUS for an empty track names no largest", async () => { // Refused before anything resolves when the grant excludes the broadcast, and refused when // the grant narrows while the broadcast resolves. test("TRACK_STATUS outside the grant is refused on every draft", async () => { - const nothing = (): Grant => ({ publish: new Path.Patterns([]), subscribe: new Path.Patterns([]) }); + const nothing = (): Grant => ({ publish: new Path.Prefixes(), subscribe: new Path.Prefixes() }); for (const version of IETF_VERSIONS) { const unauthorized = toRequestCode("unauthorized", "track_status", version); const pair = createMockTransportPair(ALPNS[version]); @@ -792,8 +792,8 @@ test("a request token does not widen the grant", async () => { const pair = createMockTransportPair(ALPNS[version]); const session = new NativeSession(pair.server, version, true); const nothing = new Signal({ - publish: new Path.Patterns([]), - subscribe: new Path.Patterns([]), + publish: new Path.Prefixes(), + subscribe: new Path.Prefixes(), }); const { pub, origin } = publisher(pair.server, { session, grant: nothing }); publish(origin, Path.from("room")); @@ -1273,7 +1273,7 @@ test("a subscription below an advertised route hears it as the empty suffix", as test("a subscription outside a covering route's claim does not hear it", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const { pub, origin } = publisher(pair.server, { requiresSolicitation: true }); - const chat = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("room/chat"))])); + const chat = origin.scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from("room/chat"))])); const dynamic = chat.claim(Path.from("room")); const subscription = await Stream.open(pair.client, { version: VERSION }); @@ -1305,7 +1305,7 @@ test("a subscription hears the best covering route its claim allows", async () = const pair = createMockTransportPair(ALPN.DRAFT_18); const { pub, origin } = publisher(pair.server, { requiresSolicitation: true }); const scoped = (path: string) => - origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from(path))])); + origin.scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from(path))])); const chat = scoped("room/chat").claim(Path.from("room"), { cost: 1n }); const video = scoped("room/video").claim(Path.from("room"), { cost: 5n }); @@ -1338,7 +1338,7 @@ test("a subscription hears the best covering route its claim allows", async () = test("a subscription hears a broader covering route when the narrowest cannot serve it", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const origin = new OriginProducer(); - const tenant = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.all()])); + const tenant = origin.scope(Path.from("tenant"), Path.Prefixes.all()); const pub = new Publisher({ quic: pair.server, session: new NativeSession(pair.server, VERSION, true), @@ -1346,7 +1346,7 @@ test("a subscription hears a broader covering route when the narrowest cannot se requiresSolicitation: true, }); const chat = origin - .scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("tenant/chat"))])) + .scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from("tenant/chat"))])) .claim(Path.from("tenant")); const broad = origin.claim(Path.empty(), { cost: 5n }); @@ -1377,14 +1377,14 @@ test("a subscription hears a broader covering route when the narrowest cannot se test("a covering route's claim is compared relative to the served root", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const origin = new OriginProducer(); - const tenant = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.all()])); + const tenant = origin.scope(Path.from("tenant"), Path.Prefixes.all()); const pub = new Publisher({ quic: pair.server, session: new NativeSession(pair.server, VERSION, true), publish: tenant.consume(), requiresSolicitation: true, }); - const chat = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("tenant/room/chat"))])); + const chat = origin.scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from("tenant/room/chat"))])); const dynamic = chat.claim(Path.from("tenant/room")); const subscription = await Stream.open(pair.client, { version: VERSION }); @@ -3518,10 +3518,7 @@ for (const version of [Version.DRAFT_18] as const) { const pair = createMockTransportPair(ALPN.DRAFT_18); const origin = new OriginProducer(); // Authorization heads must not replace the request's empty visibility prefix. - const scope = new Path.Patterns([ - Path.Pattern.subtree(Path.from(".stats")), - Path.Pattern.subtree(Path.from("visible")), - ]); + const scope = Path.Prefixes.parse([".stats", "visible"]); const pub = new Publisher({ quic: pair.server, session: new NativeSession(pair.server, version, true), @@ -4048,7 +4045,7 @@ test("a paused SUBSCRIBE ends once its grant is revoked", async () => { }), ); try { - grant.set({ publish: new Path.Patterns([]), subscribe: new Path.Patterns([]) }); + grant.set({ publish: new Path.Prefixes(), subscribe: new Path.Prefixes() }); expect(await client.reader.u53()).toBe(PublishDone.id); expect((await PublishDone.decode(client.reader, V22)).statusCode).toBe(PublishDoneStatus.UNAUTHORIZED); } finally { diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index ab99b26ec0a..f440641d0bc 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -128,7 +128,6 @@ export function presented( ): Map { const out = new Map(); let rootLen = -1; - const requested = Path.Pattern.subtree(prefix); for (const [covered, candidates] of table) { if (!carries(covered)) continue; if (Path.hasPrefix(covered, prefix)) { @@ -136,7 +135,7 @@ export function presented( // A scoped route covers only what it claims, so the best one that can serve the prefix wins. const snap = firstEligible( candidates, - (candidate) => (!candidate.claim || candidate.claim.overlaps(requested)) && shows(candidate, prefix), + (candidate) => (!candidate.claim || candidate.claim.overlaps(prefix)) && shows(candidate, prefix), ); if (!snap) continue; rootLen = covered.length; @@ -176,6 +175,23 @@ export function firstEligible !epochs.get(candidate.prefix)?.supersedes(candidate.route)); } +/** + * What of `path`'s subtree every one of `scopes` serves: undefined when all of it, and otherwise + * the prefixes beneath `path` that all of them share, possibly none. Asked about a path rather + * than a {@link Path.Prefix}, so a path no prefix can spell still gets an answer. + */ +export function within(path: Path.Valid, scopes: Iterable): Path.Prefixes | undefined { + let out: Path.Prefixes | undefined; + for (const scope of scopes) { + if (!scope || scope.matches(path)) continue; + const beneath = new Path.Prefixes( + scope.toArray().filter((member) => Path.hasPrefix(path, member.text as Path.Valid)), + ); + out = out ? out.intersect(beneath) : beneath; + } + return out; +} + /** Whether the announced prefix's subtree overlaps `scope`. */ export function scopeOverlaps(scope: Path.Pattern, prefix: Path.Valid): boolean { return scope.overlaps(Path.Pattern.subtree(prefix)); diff --git a/js/net/src/lite/answer.test.ts b/js/net/src/lite/answer.test.ts index 2b151c85434..2d07b7c427c 100644 --- a/js/net/src/lite/answer.test.ts +++ b/js/net/src/lite/answer.test.ts @@ -28,16 +28,16 @@ import { Version } from "./version.ts"; /** Where claims are advertised: some above what grants and prefixes name, and one hidden. */ const PATHS = ["a", "a/x", "a/q/x", "a/.h", "b", "b/c"].map((path) => Path.from(path)); /** The scopes claims are advertised with, which bound what each may serve. */ -const SCOPES: (string[] | undefined)[] = [undefined, ["a/*/x"], ["*/c/**"]]; +const SCOPES: (string[] | undefined)[] = [undefined, ["a/q/x"], ["a/x", "b/c"]]; /** The grants: widen, narrow, move, and revoke between them. */ -const GRANTS: (string[] | undefined)[] = [undefined, ["**"], ["a/**"], ["b/**"], ["a/*"], ["a/x/**"], []]; +const GRANTS: (string[] | undefined)[] = [undefined, [""], ["a"], ["b"], ["a/x", "a/q"], ["a/x"], []]; /** The prefixes the peer asks for. */ const PREFIXES = ["", "a", "a/x", "a/q", "b"].map((path) => Path.from(path)); /** The handle's own scope. */ -const HANDLES: (string[] | undefined)[] = [undefined, ["a/**", "b/**"], ["*/q/**", "b/**"]]; +const HANDLES: (string[] | undefined)[] = [undefined, ["a", "b"], ["a/q", "b"]]; -function patterns(members: string[]): Path.Patterns { - return new Path.Patterns(members.map((member) => Path.Pattern.parse(member))); +function patterns(members: string[]): Path.Prefixes { + return Path.Prefixes.parse(members); } function grant(index: number): Grant | undefined { @@ -165,7 +165,7 @@ function stream(onSend: (msgs: AnnounceBroadcast[]) => void, gated: boolean) { function answer( advertised: Getter, grant: Getter, - allowed: Path.Patterns | undefined, + allowed: Path.Prefixes | undefined, stream: AnnounceStream, ) { return new Answer({ @@ -183,7 +183,7 @@ function answer( async function shown( table: Advertisements, grant: Grant | undefined, - allowed: Path.Patterns | undefined, + allowed: Path.Prefixes | undefined, prefix: Path.Valid, hidden: boolean, ): Promise> { @@ -392,7 +392,7 @@ test("a claim is shown only where its scope and the handle's share a path", asyn ], ]); const allowed = patterns(["a/q/y", "a/r/y"]); - for (const grant of [undefined, { publish: patterns(["**"]), subscribe: patterns([]) }]) { + for (const grant of [undefined, { publish: patterns([""]), subscribe: patterns([]) }]) { expect(await shown(table, grant, allowed, Path.from("a/q"), false)).toEqual(new Set()); expect(await shown(table, grant, allowed, Path.from("a/r"), false)).toEqual(new Set(["a/r i1"])); } diff --git a/js/net/src/lite/answer.ts b/js/net/src/lite/answer.ts index e8dfdedfa6d..939843e52f2 100644 --- a/js/net/src/lite/answer.ts +++ b/js/net/src/lite/answer.ts @@ -15,7 +15,7 @@ import { type GetPromise, type Getter, race } from "@moq/signals"; import type { Grant } from "../auth.ts"; import { ProtocolViolation } from "../error.ts"; import type * as Instance from "../instance.ts"; -import { coveringPrefixes, hiddenBelow, presented, scopeHead } from "../internal.ts"; +import { coveringPrefixes, hiddenBelow, presented, within } from "../internal.ts"; import * as Path from "../path.ts"; import { type Advertised, type Advertisements, type OriginConsumer, sameInstance } from "../wire.ts"; import { type Add, type AnnounceBroadcast, type Claim, claimsEqual, ownClaim, requestFits } from "./announce.ts"; @@ -228,17 +228,13 @@ export class Answer { // prefix, less the hidden ones it did not ask for and whatever our grant does not let us publish. const latest = new Map(); // A claim is shown where some path it covers is served by the claim's own scope and the - // handle's, and our grant, if any, lets us publish it, as one at `a` serves `a/*` though `a` - // itself is not granted. An intersection too large to spell covers nothing, never more. + // handle's, and our grant, if any, lets us publish it, as one at `a` serves `a/b` though `a` + // itself is not granted. const shows = (snap: Advertised, path: Path.Valid) => { - let beneath = new Path.Patterns([Path.Pattern.subtree(path)]); - try { - for (const scope of [snap.claim, origin?.allowed]) if (scope) beneath = scope.intersect(beneath); - } catch { - return false; - } - const paths = [...beneath]; - return grant ? paths.some((pattern) => grant.publish.overlaps(pattern)) : paths.length > 0; + const served = within(path, [snap.claim, origin?.allowed]); + if (!grant) return served === undefined || served.size > 0; + if (served === undefined) return grant.publish.overlaps(path); + return served.toArray().some((prefix) => grant.publish.overlaps(prefix.text)); }; // Whether the advertisement a claim the peer holds came from is still in the table, as when // its presentation moved: the claim is lost to the peer rather than ended, since its publisher @@ -305,18 +301,15 @@ export class Answer { } } -// The regions `patterns` confine the peer to, none within another, or undefined when they allow -// every path. A head too long or deep to be a path holds none. Mirrors rs/moq-net's -// `interest_prefixes`. -function regions(patterns: Path.Patterns): Path.Valid[] | undefined { - if (patterns.contains(Path.Pattern.all())) return undefined; - const heads = new Set( - patterns - .toArray() - .map(scopeHead) - .filter((head) => Path.check(head) === undefined), - ); - return [...heads].filter((head) => ![...heads].some((other) => other !== head && Path.hasPrefix(other, head))); +// The regions `prefixes` confine the peer to, or undefined when they allow every path: the +// prefixes themselves, already none within another. One too long or deep to be a path holds none. +// Mirrors rs/moq-net's `Permit::regions`. +function regions(prefixes: Path.Prefixes): Path.Valid[] | undefined { + if (prefixes.matches(Path.empty())) return undefined; + return prefixes + .toArray() + .map((prefix) => prefix.text as Path.Valid) + .filter((region) => Path.check(region) === undefined); } // What answers the requested `prefix`: the prefix itself when a region holds it or nothing confines diff --git a/js/net/src/lite/auth.test.ts b/js/net/src/lite/auth.test.ts index ad0e9e17b94..c94e702cee9 100644 --- a/js/net/src/lite/auth.test.ts +++ b/js/net/src/lite/auth.test.ts @@ -6,8 +6,8 @@ import { AuthError, AuthMessage, AuthOk, decodeAuthReplyMaybe, LiteAuthWire } fr import * as Lite from "./index.ts"; import * as Message from "./message.ts"; -function patterns(...texts: string[]): Path.Patterns { - return new Path.Patterns(texts.map((text) => Path.Pattern.parse(text))); +function prefixes(...texts: string[]): Path.Prefixes { + return Path.Prefixes.parse(texts); } /** Round-trip bytes through a writer and back out of a reader. */ @@ -41,7 +41,7 @@ test("AUTH round-trips its token", async () => { }); test("AUTH_OK and AUTH_ERROR round-trip behind their type", async () => { - const ok = new AuthOk(patterns("**"), patterns(), 60_000); + const ok = new AuthOk(prefixes(""), prefixes(), 60_000); const err = new AuthError(SessionCode.Unauthorized, "expired"); const r = await roundTrip(async (w) => { await ok.encode(w, Lite.Version.DRAFT_07); @@ -50,8 +50,8 @@ test("AUTH_OK and AUTH_ERROR round-trip behind their type", async () => { const first = await decodeAuthReplyMaybe(r, Lite.Version.DRAFT_07); expect(first).toBeInstanceOf(AuthOk); if (!(first instanceof AuthOk)) throw new Error("unreachable"); - // `**` grants everything; the empty list grants nothing. - expect(first.publish.equals(new Path.Patterns([Path.Pattern.all()]))).toBe(true); + // The empty prefix grants everything; the empty list grants nothing. + expect(first.publish.equals(Path.Prefixes.all())).toBe(true); expect(first.subscribe.size).toBe(0); expect(first.expires).toBe(60_000); @@ -60,8 +60,8 @@ test("AUTH_OK and AUTH_ERROR round-trip behind their type", async () => { expect(await decodeAuthReplyMaybe(r, Lite.Version.DRAFT_07)).toBeUndefined(); }); -test("literal and wildcard grants travel exactly, never widened", async () => { - const ok = new AuthOk(patterns("room/alice", "room/*/cam", "**/demo.hang"), patterns("", "lobby/**")); +test("prefix grants travel exactly, reduced", async () => { + const ok = new AuthOk(prefixes("room/alice", "room/bob/cam", "demo.hang"), prefixes("lobby", "lobby/x")); const r = await roundTrip((w) => ok.encode(w, Lite.Version.DRAFT_07)); const got = await decodeAuthReplyMaybe(r, Lite.Version.DRAFT_07); if (!(got instanceof AuthOk)) throw new Error("expected AUTH_OK"); @@ -71,17 +71,17 @@ test("literal and wildcard grants travel exactly, never widened", async () => { // The same bytes as `auth_ok_golden` in `rs/moq-net/src/lite/auth.rs`. test("AUTH_OK matches the Rust encoding", async () => { - const ok = new AuthOk(patterns("room/*/cam", "**/b.hang"), patterns(""), 1000); + const ok = new AuthOk(prefixes("room-b", "room/a"), prefixes(""), 1000); const r = await roundTrip((w) => ok.encode(w, Lite.Version.DRAFT_07)); const text = (s: string) => [s.length, ...new TextEncoder().encode(s)]; expect([...(await r.readAll())]).toEqual([ 0x00, // AUTH_OK - 0x1a, // length - 0x02, // publish count, in canonical order - ...text("**/b.hang"), - ...text("room/*/cam"), + 0x13, // length + 0x02, // publish count, in segment order: `room` sorts before `room-b` + ...text("room/a"), + ...text("room-b"), 0x01, // subscribe count - 0x00, // the empty pattern: the root alone + 0x00, // the empty prefix: every path 0x83, 0xe8, // expires: 1000ms, as a leading-ones varint ]); @@ -97,8 +97,9 @@ test("a reply larger than the peer reads writes nothing", async () => { expect((await r.readAll()).byteLength).toBe(0); }); -test("only valid, canonical patterns decode", async () => { - for (const text of ["*/**", "/room", "room/", "room//a", "a*b*c", "**/**", "a**"]) { +// Mirrors the refused texts in `rs/moq-net/src/lite/auth.rs`: a wildcard or a malformed path. +test("only valid prefixes decode", async () => { + for (const text of ["**", "room/**", "*", "room/*/cam", "cam-*.hang", "/room", "room/", "room//a", "a\\b"]) { const r = await roundTrip(async (w) => { const body = new TextEncoder().encode(text); await w.u53(0); // AUTH_OK @@ -131,7 +132,7 @@ test("unsupported after AUTH_OK is an AUTH_ERROR on the wire", async () => { version: Lite.Version.DRAFT_07, }); const wire = new LiteAuthWire({} as WebTransport, Lite.Version.DRAFT_07); - await wire.grant(stream, { publish: patterns("room/**"), subscribe: patterns() }); + await wire.grant(stream, { publish: prefixes("room"), subscribe: prefixes() }); await wire.unsupported(stream, "grant cannot be encoded"); stream.close(); await stream.writer.closed; @@ -143,7 +144,7 @@ test("unsupported after AUTH_OK is an AUTH_ERROR on the wire", async () => { }); test("malformed AUTH_OK closes its session", async () => { - for (const pattern of ["*/**", "/room", Array(33).fill("room").join("/"), undefined]) { + for (const pattern of ["room/**", "/room", Array(33).fill("room").join("/"), undefined]) { const reader = await roundTrip(async (w) => { await Message.encode( w, @@ -161,7 +162,7 @@ test("malformed AUTH_OK closes its session", async () => { }); test("the largest shared expiry remains valid", async () => { - const ok = new AuthOk(patterns(), patterns(), Number.MAX_SAFE_INTEGER); + const ok = new AuthOk(prefixes(), prefixes(), Number.MAX_SAFE_INTEGER); const reader = await roundTrip((w) => ok.encode(w, Lite.Version.DRAFT_07)); expect(await decodeAuthReplyMaybe(reader, Lite.Version.DRAFT_07)).toEqual(ok); }); diff --git a/js/net/src/lite/auth.ts b/js/net/src/lite/auth.ts index d1e28087c17..3f4d5227896 100644 --- a/js/net/src/lite/auth.ts +++ b/js/net/src/lite/auth.ts @@ -1,5 +1,5 @@ import { Unsupported } from "../auth.ts"; -import { type AuthWire, decodePatterns, encodePatterns, type WireGrant, type WireReply } from "../auth_session.ts"; +import { type AuthWire, decodePrefixes, encodePrefixes, type WireGrant, type WireReply } from "../auth_session.ts"; import { ProtocolViolation, SessionCode, SessionError } from "../error.ts"; import type * as Path from "../path.ts"; import { type Reader, Stream, type Writer } from "../stream.ts"; @@ -49,20 +49,20 @@ export class AuthMessage { export class AuthOk { static id = 0; - publish: Path.Patterns; - subscribe: Path.Patterns; + publish: Path.Prefixes; + subscribe: Path.Prefixes; /** Milliseconds until the grant lapses, or undefined for never. */ expires?: number; - constructor(publish: Path.Patterns, subscribe: Path.Patterns, expires?: number) { + constructor(publish: Path.Prefixes, subscribe: Path.Prefixes, expires?: number) { this.publish = publish; this.subscribe = subscribe; this.expires = expires; } async #encode(w: Writer) { - await encodePatterns(w, this.publish); - await encodePatterns(w, this.subscribe); + await encodePrefixes(w, this.publish); + await encodePrefixes(w, this.subscribe); // 0 means never, so a lapsed grant rounds up to the smallest real expiry. const expires = this.expires === undefined ? 0 : Math.min(Math.max(Math.ceil(this.expires), 1), Number.MAX_SAFE_INTEGER); @@ -70,8 +70,8 @@ export class AuthOk { } static async #decode(r: Reader): Promise { - const publish = await decodePatterns(r); - const subscribe = await decodePatterns(r); + const publish = await decodePrefixes(r); + const subscribe = await decodePrefixes(r); const expires = await r.u53(); return new AuthOk(publish, subscribe, expires === 0 ? undefined : expires); } diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index ffbd136ce27..98698373f98 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -1060,7 +1060,7 @@ for (const [name, violate] of [ if (!auth || (await auth.reader.u53()) !== StreamId.Auth) throw new Error("the peer never presented a credential"); await AuthMessage.decode(auth.reader, Version.DRAFT_07); - const all = new Path.Patterns([Path.Pattern.all()]); + const all = Path.Prefixes.all(); await new AuthOk(all, all).encode(auth.writer, Version.DRAFT_07); const { writer, send } = await announceStream(pair); await send({ status: "request", more: false, remove: [], add: [{ prefix: ROOM, hidden: false }] }); diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index 77f8c3de3f3..64be08830e0 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -201,8 +201,8 @@ export class Connection implements Session { this.#auth = new AuthSession({ wire: hasAuth(version) ? new LiteAuthWire(quic, version) : undefined, peerGrant: { - publish: new Path.Patterns([Path.Pattern.all()]), - subscribe: new Path.Patterns(publish ? [Path.Pattern.all()] : []), + publish: Path.Prefixes.all(), + subscribe: publish ? Path.Prefixes.all() : new Path.Prefixes(), }, handle, draining: () => this.#goaway.peek() !== undefined || this.#phase.kind !== "open", @@ -395,14 +395,14 @@ export class Connection implements Session { } } - /** The paths announced matching `scope`, each present or absent; see {@link Session.announced}. */ - announced(scope?: Path.Pattern, options?: announce.Options): announce.Consumer { - return announce.list(this.#subscriber.routes(scope, options)); + /** The paths announced matching `filter`, each present or absent; see {@link Session.announced}. */ + announced(filter?: Path.Pattern, options?: announce.Options): announce.Consumer { + return announce.list(this.#subscriber.routes(filter, options)); } - /** Every route change matching `scope`, for code that forwards routes. @internal */ - routes(scope?: Path.Pattern, options?: announce.RouteOptions): announce.Routes { - return this.#subscriber.routes(scope, options); + /** Every route change matching `filter`, for code that forwards routes. @internal */ + routes(filter?: Path.Pattern, options?: announce.RouteOptions): announce.Routes { + return this.#subscriber.routes(filter, options); } async #runSession() { diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 0b890438499..dc1078f24af 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -3190,7 +3190,7 @@ test("lite draft-05: a group that goes stale while its stream opens writes nothi }); function grantOf(publish: string): Grant { - return { publish: new Path.Patterns([Path.Pattern.subtree(publish)]), subscribe: new Path.Patterns([]) }; + return { publish: new Path.Prefixes([Path.Prefix.parse(publish)]), subscribe: new Path.Prefixes() }; } // The resets carrying our UNAUTHORIZED code, by message. @@ -4180,8 +4180,8 @@ test("a lite-07 edit answers each added prefix under its own Prefix ID", async ( test("a lite-07 prefix wider than the grant is answered region by region", async () => { const origin = new OriginProducer(); const regions = (...paths: string[]): Grant => ({ - publish: new Path.Patterns(paths.map((path) => Path.Pattern.subtree(path))), - subscribe: new Path.Patterns([]), + publish: Path.Prefixes.parse(paths), + subscribe: new Path.Prefixes(), }); const grant = new Signal(regions("a/b")); const broadcasts = ["a", "a/b/x", "c/y", "d/z"].map((path) => publish(origin, Path.from(path))); @@ -4222,14 +4222,14 @@ test("a lite-07 prefix wider than the grant is answered region by region", async } }); -// A grant whose head is too long to be a path holds no region: a claim above it is not presented +// A granted prefix too long to be a path holds no region: a claim above it is not presented // there, which the wire could not carry, and the session stays up. -test("a lite-07 grant head too long for a path presents nothing", async () => { +test("a lite-07 granted prefix too long for a path presents nothing", async () => { const origin = new OriginProducer(); const long = `a/${"x".repeat(Path.MAX_BYTES)}`; const grant = new Signal({ - publish: new Path.Patterns([Path.Pattern.parse(`${long}/**`), Path.Pattern.subtree("c")]), - subscribe: new Path.Patterns([]), + publish: Path.Prefixes.parse([long, "c"]), + subscribe: new Path.Prefixes(), }); const broadcasts = ["a", "c/y"].map((path) => publish(origin, Path.from(path))); const { next, ok, close } = routedAnswer(origin, grant); @@ -4249,15 +4249,14 @@ test("a lite-07 grant head too long for a path presents nothing", async () => { } }); -// A claim at a path the grant does not match, but above paths it does, is shown where it is -// presented: `a` under `a/*`, at the region and at its own path. One beneath every path the grant -// matches stays hidden. -test("a lite-07 claim above what the grant matches is shown", async () => { +// A claim above the grant is shown at the granted prefix, whichever prefix above it the peer +// asks for, and one outside the grant stays hidden. +test("a lite-07 claim above the grant is shown at the granted prefix", async () => { for (const asked of ["", "a"]) { const origin = new OriginProducer(); const grant = new Signal({ - publish: new Path.Patterns([Path.Pattern.parse("a/*")]), - subscribe: new Path.Patterns([]), + publish: Path.Prefixes.parse(["a/x"]), + subscribe: new Path.Prefixes(), }); const broadcasts = ["a", "a/x/y", "b"].map((path) => publish(origin, Path.from(path))); const { next, ok, close } = routedAnswer(origin, grant, Path.from(asked)); @@ -4272,7 +4271,7 @@ test("a lite-07 claim above what the grant matches is shown", async () => { const told = msgs.flatMap((msg) => msg.status === "start" ? [`${msg.suffix} above ${msg.claim.above}`] : [], ); - expect(told).toEqual(["a above 0"]); + expect(told.sort()).toEqual(["a/x above 1", "a/x/y above 0"]); } finally { for (const broadcast of broadcasts) broadcast.close(); close(); @@ -4282,19 +4281,20 @@ test("a lite-07 claim above what the grant matches is shown", async () => { }); // A scoped claim is shown only where its scope and the grant share a path: one at `a` scoped to -// `a/*/x` serves something `a/**` grants, and nothing `a/*` does. +// `a/q/x` serves something `a` or `a/q` grants, and nothing `a/r` does. test("a lite-07 scoped claim sharing no path with the grant is hidden", async () => { const cases: [string, string[]][] = [ - ["a/*", []], - ["a/**", ["a"]], + ["a/r", []], + ["a/q", ["a/q"]], + ["a", ["a"]], ]; for (const [granted, told] of cases) { const origin = new OriginProducer(); const grant = new Signal({ - publish: new Path.Patterns([Path.Pattern.parse(granted)]), - subscribe: new Path.Patterns([]), + publish: Path.Prefixes.parse([granted]), + subscribe: new Path.Prefixes(), }); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("a/*/x")])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.parse(["a/q/x"])); const claim = scoped.claim(Path.from("a")); const { next, ok, close } = routedAnswer(origin, grant); try { @@ -4317,12 +4317,12 @@ test("a lite-07 scoped claim sharing no path with the grant is hidden", async () }); // A claim is shown only where the grant, the claim's scope, and the answering handle's scope share -// a path. Beneath a handle scoped to `a/*/x`, an unscoped claim at `a` serves nothing `a/*/y` -// grants; and of two claims at `a`, the cheaper scoped to `a/*/x` gives way to the one `a/*/y` -// shows. +// a path. Beneath a handle scoped to `a/q/x`, an unscoped claim at `a` serves nothing `a/q/y` +// grants; and of two claims at `a`, the cheaper scoped to `a/q/x` gives way to the one scoped to +// `a/q/y`, shown at the granted prefix. test("a lite-07 claim is shown only where the handle and the grant share a path", async () => { - const patterns = (pattern: string) => new Path.Patterns([Path.Pattern.parse(pattern)]); - const grant = new Signal({ publish: patterns("a/*/y"), subscribe: new Path.Patterns([]) }); + const prefixes = (prefix: string) => Path.Prefixes.parse([prefix]); + const grant = new Signal({ publish: prefixes("a/q/y"), subscribe: new Path.Prefixes() }); const synced = async (next: () => Promise) => { const msgs: AnnounceBroadcast[] = []; for (;;) { @@ -4334,7 +4334,7 @@ test("a lite-07 claim is shown only where the handle and the grant share a path" const origin = new OriginProducer(); const claim = origin.claim(Path.from("a")); - const handle = routedAnswer(origin.scope(Path.empty(), patterns("a/*/x")), grant); + const handle = routedAnswer(origin.scope(Path.empty(), prefixes("a/q/x")), grant); try { await handle.ok(); expect(await synced(handle.next)).toEqual([]); @@ -4345,14 +4345,14 @@ test("a lite-07 claim is shown only where the handle and the grant share a path" } const both = new OriginProducer(); - const cheap = both.scope(Path.empty(), patterns("a/*/x")).claim(Path.from("a"), { cost: 1n }); - const dear = both.scope(Path.empty(), patterns("a/*/y")).claim(Path.from("a"), { cost: 5n }); + const cheap = both.scope(Path.empty(), prefixes("a/q/x")).claim(Path.from("a"), { cost: 1n }); + const dear = both.scope(Path.empty(), prefixes("a/q/y")).claim(Path.from("a"), { cost: 5n }); const answer = routedAnswer(both, grant); try { await answer.ok(); const starts = await synced(answer.next); expect(starts.map((msg) => (msg.status === "start" ? [msg.suffix, msg.claim.cost] : []))).toEqual([ - [Path.from("a"), 5n], + [Path.from("a/q/y"), 5n], ]); } finally { cheap.close(); diff --git a/js/net/src/lite/routed.ts b/js/net/src/lite/routed.ts index e9f59950aee..d5e4991af8a 100644 --- a/js/net/src/lite/routed.ts +++ b/js/net/src/lite/routed.ts @@ -18,7 +18,7 @@ import type { BroadcastCache } from "../consume.ts"; import { error, ProtocolViolation } from "../error.ts"; import { routesEqual, WireRoute } from "../hop.ts"; import type * as Instance from "../instance.ts"; -import { bestCandidate, coveringPrefixes, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; +import { bestCandidate, coveringPrefixes, hiddenBelow, scopeCaptures, scopeOverlaps } from "../internal.ts"; import * as Path from "../path.ts"; import { SlotMap, type Slots } from "../util/limits.ts"; import { @@ -116,7 +116,7 @@ export class Routed { * nothing, since its ANNOUNCE_OK follows what the change shows under a wider prefix. */ #regranted = false; /** What our grant lets us subscribe to, as last seen. */ - #granted?: Path.Patterns; + #granted?: Path.Prefixes; // Each live claim holds one of the session's announce slots. #claims: SlotMap; @@ -545,14 +545,15 @@ function presentedAt(reader: Reader, path: Path.Valid): Path.Valid | undefined { return Path.hasPrefix(path, reader.prefix) ? reader.prefix : undefined; } -// The prefixes holding what the grant `next` shows that `before` did not, judged pattern by -// pattern, so one may be spurious but none is missed. Empty for a narrowing. Mirrors rs/moq-net's -// `Permit::gained`. -function gained(before: Path.Patterns | undefined, next: Path.Patterns | undefined): Path.Valid[] { +// The prefixes holding what the grant `next` shows that `before` did not: each of its prefixes +// `before` does not cover, less any too long for a path, which covers nothing a peer could name. +// Empty for a narrowing. Mirrors rs/moq-net's `Permit::beyond`. +function gained(before: Path.Prefixes | undefined, next: Path.Prefixes | undefined): Path.Valid[] { if (!before) return []; if (!next) return [Path.empty()]; return next .toArray() - .filter((pattern) => !before.contains(pattern)) - .map(scopeHead); + .filter((prefix) => !before.matches(prefix.text)) + .map((prefix) => prefix.text as Path.Valid) + .filter((region) => Path.check(region) === undefined); } diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index f8b086a45fb..e5115f36edc 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -449,8 +449,8 @@ test("a lite-07 restart replaces the instance, and the next consume subscribes f test("a lite-07 region granted under a wider prefix syncs at the next answer", async () => { const v = Version.DRAFT_07; const subscribe = (...paths: string[]): Grant => ({ - publish: new Path.Patterns([]), - subscribe: new Path.Patterns(paths.map((path) => Path.Pattern.subtree(path))), + publish: new Path.Prefixes(), + subscribe: Path.Prefixes.parse(paths), }); const grant = new Signal(subscribe("a")); const { subscriber, send, ok, settle } = announceHarness(v, 1n, grant); @@ -493,16 +493,40 @@ test("a lite-07 region granted under a wider prefix syncs at the next answer", a subscriber.close(); }); +test("a lite-07 grant gaining a prefix too long for a path keeps every reader synced", async () => { + const v = Version.DRAFT_07; + const subscribe = (...paths: string[]): Grant => ({ + publish: new Path.Prefixes(), + subscribe: Path.Prefixes.parse(paths), + }); + const grant = new Signal(subscribe("a")); + const { subscriber, send, ok, settle } = announceHarness(v, 1n, grant); + const everything = subscriber.routes(); + await settle(); + await ok(); + await send((w) => encodeAnnounceBroadcast(w, { status: "synced", prefix: 0n }, v)); + await settle(); + expect(everything.synced.peek()).toBe(true); + + // Like Rust's `Permit::beyond`, a prefix no path can name covers nothing the peer could answer. + grant.set(subscribe("a", `b${"x".repeat(Path.MAX_BYTES)}`)); + await settle(); + expect(everything.synced.peek()).toBe(true); + + everything.close(); + subscriber.close(); +}); + // Readers too many to ask for one by one are asked for at the root. A grant change that shows more // within it, though within no reader's prefix, still sends an edit: a reader arriving there before // its answer waits for it, since the peer may already have sent what the change shows. test("a lite-07 grant change under a coarsened ask holds the readers that arrive", async () => { const v = Version.DRAFT_07; - const grant = (...patterns: string[]): Grant => ({ - publish: new Path.Patterns([]), - subscribe: new Path.Patterns(patterns.map((pattern) => Path.Pattern.parse(pattern))), + const grant = (...prefixes: string[]): Grant => ({ + publish: new Path.Prefixes(), + subscribe: Path.Prefixes.parse(prefixes), }); - const signal = new Signal(grant("a/*")); + const signal = new Signal(grant("a")); const { subscriber, send, ok, settle } = announceHarness(v, 1n, signal); const readers = Array.from({ length: 17 }, (_, i) => subscriber.routes(Path.Pattern.subtree(Path.from(`a/${i}${"x".repeat(4000)}`))), @@ -516,7 +540,7 @@ test("a lite-07 grant change under a coarsened ask holds the readers that arrive await settle(); expect(readers.every((reader) => reader.synced.peek())).toBe(true); - signal.set(grant("a/*", "b/**")); + signal.set(grant("a", "b")); // Noticed before the reader arrives, while no reader's prefix holds `b`. await settle(); const b = subscriber.routes(Path.Pattern.subtree(Path.from("b"))); @@ -1894,8 +1918,8 @@ test("a fetch started after the subscriber closes rejects without opening a stre test("a grant that shrinks while the subscription sets up refuses it", async () => { const { quic, streams, opened } = fakeSession(); const scoped = (prefix: string): Grant => ({ - publish: new Path.Patterns([]), - subscribe: new Path.Patterns([Path.Pattern.subtree(prefix)]), + publish: new Path.Prefixes(), + subscribe: new Path.Prefixes([Path.Prefix.parse(prefix)]), }); const grant = new Signal(scoped("room")); const subscriber = new Subscriber(quic, Version.DRAFT_05, toHop(1n), undefined, undefined, undefined, grant); @@ -2142,8 +2166,8 @@ test.each(["initial", "track", "fetch", "body"] as const)( async (stage) => { const { quic, streams, opened } = fakeSession(); const scoped = (prefix: string): Grant => ({ - publish: new Path.Patterns(), - subscribe: new Path.Patterns([Path.Pattern.subtree(prefix)]), + publish: new Path.Prefixes(), + subscribe: new Path.Prefixes([Path.Prefix.parse(prefix)]), }); const grant = new Signal(scoped(stage === "initial" ? "other" : "room")); const subscriber = new Subscriber(quic, Version.DRAFT_05, toHop(1n), undefined, undefined, undefined, grant); diff --git a/js/net/src/lite/tracks.test.ts b/js/net/src/lite/tracks.test.ts index 7e14799091e..576f17fe66c 100644 --- a/js/net/src/lite/tracks.test.ts +++ b/js/net/src/lite/tracks.test.ts @@ -348,7 +348,7 @@ test("a version without the Tracks Stream refuses the listing", async () => { }); function scoped(prefix: string): Grant { - return { publish: new Path.Patterns(), subscribe: new Path.Patterns([Path.Pattern.subtree(prefix)]) }; + return { publish: new Path.Prefixes(), subscribe: new Path.Prefixes([Path.Prefix.parse(prefix)]) }; } test("a listing our grant does not cover is refused, and never reaches the wire", async () => { diff --git a/js/net/src/origin-request.test.ts b/js/net/src/origin-request.test.ts index c595627cfba..9709b917086 100644 --- a/js/net/src/origin-request.test.ts +++ b/js/net/src/origin-request.test.ts @@ -91,7 +91,7 @@ test.each(["request", "follow"] as const)( (method) => { const origin = new Producer(); expect(() => origin[method](Path.from("camera*main"))).toThrow(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("pool/job/cam"))])); + const scoped = origin.scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from("pool/job/cam"))])); expect(() => scoped[method](Path.from("pool/job"))).toThrow(); expect(wireOf(origin).requests.peek()?.size).toBe(0); origin.close(); @@ -197,7 +197,7 @@ test("followers track the serving prefix, ignoring descendants and re-prices", a test("a follower ignores a covering route whose scope excludes its path", async () => { const origin = new Producer(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.parse(["room/alice/chat"])); const prefix = scoped.claim(Path.from("room/alice")); const exactRoute = origin.route(Path.from("room/alice"), { cost: 5n }); const exact = exactRoute.broadcast; diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index 21242ebcc5a..587b5933bf2 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -485,7 +485,7 @@ test("a route received through a scoped handle goes offline when the handle clos const origin = new Producer(); const announced = origin.consume().routes(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.all()); const route = wireOf(scoped).receive(Path.from("room"), WireRoute.default); expect(await announced.next()).toMatchObject({ prefix: Path.from("room"), kind: "start" }); @@ -2140,15 +2140,15 @@ test("announced filters by arbitrary patterns and reports captures", async () => test("announced lists full names while covering views and session advertisements keep overlaps", async () => { const origin = new Producer(); - const patterns = new Path.Patterns([Path.Pattern.parse("*.hang"), Path.Pattern.parse("**/*.chat")]); - const reader = origin.scope(Path.from("project"), patterns).consume(); + const filter = Path.Pattern.parse("**/*.chat"); + const reader = origin.scope(Path.from("project"), Path.Prefixes.all()).consume(); const cam = origin.claim(Path.from("project/cam.hang")); const overlap = origin.claim(Path.from("project/room/mic.hang")); const chat = origin.claim(Path.from("project/lobby.chat")); - const selected = await announcedView(reader); - const covering = await announcedView(reader, undefined, { covering: true }); + const selected = await announcedView(reader, filter); + const covering = await announcedView(reader, filter, { covering: true }); const all = [Path.from("cam.hang"), Path.from("lobby.chat"), Path.from("room/mic.hang")]; - expect([...selected.peek().keys()]).toEqual([Path.from("cam.hang"), Path.from("lobby.chat")]); + expect([...selected.peek().keys()]).toEqual([Path.from("lobby.chat")]); expect([...covering.peek().keys()].sort()).toEqual(all); expect(Array.from(wireOf(reader).advertised.peek()?.keys() ?? []).sort()).toEqual(all); @@ -2159,7 +2159,7 @@ test("announced lists full names while covering views and session advertisements overlap.close(); nested.close(); await new Promise((resolve) => setImmediate(resolve)); - expect([...selected.peek().keys()]).toEqual([Path.from("cam.hang"), Path.from("lobby.chat")]); + expect([...selected.peek().keys()]).toEqual([Path.from("lobby.chat")]); await selected.close(); await covering.close(); cam.close(); @@ -2170,7 +2170,7 @@ test("announced lists full names while covering views and session advertisements test("announced filters before rooting and retains lifecycle, routing and watch cleanup", async () => { const origin = new Producer(); const root = Path.from("room/alice"); - const scoped = origin.scope(root, new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(root, Path.Prefixes.all()); const reader = scoped.consume(); const interests: string[] = []; const unlisten = wireOf(origin).interests((prefix, wanted) => interests.push(`${wanted ? "+" : "-"}${prefix}`)); @@ -2393,9 +2393,9 @@ test("a serving session closing releases quiet origin change listeners", async ( } }); -test("a rooted producer shares the table and enforces its pattern union", async () => { +test("a rooted producer shares the table and enforces its prefixes", async () => { const origin = new Producer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room/alice", "room/carol"])); const broadcast = publish(scoped, Path.from("room/alice")); await new Promise((resolve) => setImmediate(resolve)); expect((await announcedView(origin)).peek().has(Path.from("tenant/room/alice"))).toBe(true); @@ -2409,21 +2409,19 @@ test("a rooted producer shares the table and enforces its pattern union", async await new Promise((resolve) => setImmediate(resolve)); expect(() => scoped.route(Path.from("other"), { hidden: true }).broadcast).toThrow("outside the origin scope"); await new Promise((resolve) => setImmediate(resolve)); - expect(() => scoped.request(Path.from("room/alice/deep"))).toThrow("outside the origin scope"); + expect(() => scoped.request(Path.from("room/bob"))).toThrow("outside the origin scope"); await new Promise((resolve) => setImmediate(resolve)); expect(() => scoped.claim(Path.from("other"))).toThrow("outside the origin scope"); await new Promise((resolve) => setImmediate(resolve)); - expect(() => scoped.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("other/**")]))).toThrow( - "do not overlap", - ); + expect(() => scoped.scope(Path.empty(), Path.Prefixes.parse(["other"]))).toThrow("do not overlap"); await new Promise((resolve) => setImmediate(resolve)); - expect(() => origin.scope(Path.empty(), new Path.Patterns())).toThrow("do not overlap"); + expect(() => origin.scope(Path.empty(), new Path.Prefixes())).toThrow("do not overlap"); - const nested = scoped.scope(Path.from("room"), new Path.Patterns([Path.Pattern.all()])); + const nested = scoped.scope(Path.from("room"), Path.Prefixes.all()); await new Promise((resolve) => setImmediate(resolve)); expect([...(await announcedView(nested)).peek().keys()]).toEqual([Path.from("alice")]); await new Promise((resolve) => setImmediate(resolve)); - expect(() => nested.route(Path.from("alice/deep"), { hidden: true }).broadcast).toThrow("outside the origin scope"); + expect(() => nested.route(Path.from("bob"), { hidden: true }).broadcast).toThrow("outside the origin scope"); const announced = nested.routes(); await new Promise((resolve) => setImmediate(resolve)); expect(await announced.next()).toMatchObject({ kind: "start", prefix: Path.from("alice") }); @@ -2436,8 +2434,8 @@ test("a rooted producer shares the table and enforces its pattern union", async test("closing a derived handle releases only what it registered", async () => { const origin = new Producer(); const owned = publish(origin, Path.from("lobby")); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("room"))])); - const wide = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(Path.empty(), new Path.Prefixes([Path.Prefix.parse(Path.from("room"))])); + const wide = origin.scope(Path.empty(), Path.Prefixes.all()); const inside = publish(scoped, Path.from("room/alice")); const claim = scoped.claim(Path.from("room/claim")); const ranked = wide.claim(Path.from("ranked")); @@ -2458,7 +2456,7 @@ test("closing a derived handle releases only what it registered", async () => { expect((await announcedView(origin)).peek().has(Path.from("room/late"))).toBe(false); // The owner and fresh handles keep working. - const again = publish(origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.all()])), Path.from("room/bob")); + const again = publish(origin.scope(Path.empty(), Path.Prefixes.all()), Path.from("room/bob")); expect((await announcedView(origin)).peek().has(Path.from("room/bob"))).toBe(true); // Closing what was already released is a no-op. @@ -2474,7 +2472,7 @@ test("closing a derived handle releases only what it registered", async () => { test("a broadcast a derived handle released cannot announce again", async () => { const origin = new Producer(); const consumer = origin.consume(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.all()); const path = Path.from("room"); const publication = scoped.route(path); const broadcast = publication.broadcast; @@ -2494,7 +2492,7 @@ test("a broadcast a derived handle released cannot announce again", async () => test("closing a derived handle withdraws the sessions and answers attached through it", async () => { const origin = new Producer(); const consumer = origin.consume(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.all()); const path = Path.from("blind"); const request = consumer.request(path); @@ -2551,7 +2549,7 @@ test("closing a derived handle withdraws the sessions and answers attached throu test("a scoped dynamic only serves allowed paths and presents relative requests", async () => { const origin = new Producer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room/alice/chat"])); const dynamic = scoped.claim(Path.from("room")); await new Promise((resolve) => setImmediate(resolve)); const denied = origin.request(Path.from("tenant/room/alice/video")); @@ -2579,7 +2577,7 @@ test("a scoped dynamic only serves allowed paths and presents relative requests" test("scoped wire views filter and rebase advertisements and blind requests", () => { const origin = new Producer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/**")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room"])); const local = publish(origin, Path.from("tenant/room/live")); const hidden = publish(origin, Path.from("tenant/other/live")); const allowed = origin.request(Path.from("tenant/room/missing")); @@ -2597,9 +2595,9 @@ test("scoped wire views filter and rebase advertisements and blind requests", () test("a scoped dynamic is announced only to readers its scope can serve", async () => { const origin = new Producer(); - const dynamic = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("*/chat")])).claim(Path.empty()); - const chat = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/chat")])); - const video = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/video")])); + const dynamic = origin.scope(Path.empty(), Path.Prefixes.parse(["room/chat"])).claim(Path.empty()); + const chat = origin.scope(Path.empty(), Path.Prefixes.parse(["room/chat"])); + const video = origin.scope(Path.empty(), Path.Prefixes.parse(["room/video"])); await new Promise((resolve) => setImmediate(resolve)); expect([...(await announcedView(chat, undefined, { covering: true })).peek().keys()]).toEqual([Path.empty()]); await new Promise((resolve) => setImmediate(resolve)); @@ -2616,7 +2614,7 @@ test("a rooted reader presents the most specific covering route", async () => { const origin = new Producer(); const narrow = origin.claim(Path.from("room/alice"), { cost: 9n }); const broad = origin.claim(Path.from("room"), { cost: 1n }); - const rooted = origin.scope(Path.from("room/alice"), new Path.Patterns([Path.Pattern.all()])); + const rooted = origin.scope(Path.from("room/alice"), Path.Prefixes.all()); await new Promise((resolve) => setImmediate(resolve)); expect((await announcedView(rooted)).peek().get(Path.empty())?.cost).toBe(9n); await new Promise((resolve) => setImmediate(resolve)); @@ -2628,9 +2626,9 @@ test("a rooted reader presents the most specific covering route", async () => { test("a scoped reader picks the best route its scope can see at a prefix", async () => { const origin = new Producer(); - const scoped = (pattern: string) => origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse(pattern)])); - const chat = scoped("*/chat").claim(Path.empty(), { cost: 1n }); - const video = scoped("*/video").claim(Path.empty(), { cost: 5n }); + const scoped = (prefix: string) => origin.scope(Path.empty(), Path.Prefixes.parse([prefix])); + const chat = scoped("room/chat").claim(Path.empty(), { cost: 1n }); + const video = scoped("room/video").claim(Path.empty(), { cost: 5n }); const reader = scoped("room/video"); await new Promise((resolve) => setImmediate(resolve)); @@ -2656,32 +2654,11 @@ test("a scoped reader picks the best route its scope can see at a prefix", async origin.close(); }); -test("a scoped reader sees a local broadcast that only loses to a route outside its scope", async () => { - const origin = new Producer(); - const chat = origin - .scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*/chat")])) - .claim(Path.from("room/alice"), { instance: { id: INSTANCE, epoch: 0n }, cost: 0n }); - const localRoute = origin.route(Path.from("room/alice"), { instance: { id: INSTANCE, epoch: 0n }, cost: 5n }); - const local = localRoute.broadcast; - const reader = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*")])); - - await new Promise((resolve) => setImmediate(resolve)); - - expect((await announcedView(reader)).peek().get(Path.from("room/alice"))?.cost).toBe(5n); - // Unscoped, the cheaper route still wins the prefix. - await new Promise((resolve) => setImmediate(resolve)); - expect((await announcedView(origin)).peek().get(Path.from("room/alice"))?.cost).toBe(0n); - - local.close(); - chat.close(); - origin.close(); -}); - test("broadcast handles carry the path they were created or requested at", async () => { expect(new BroadcastProducer().consume().path).toBe(Path.empty()); const origin = new Producer(); - const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*")])); + const scoped = origin.scope(Path.from("tenant"), Path.Prefixes.parse(["room"])); const broadcast = publish(scoped, Path.from("room/alice")); expect(broadcast.consume().path).toBe(Path.from("tenant/room/alice")); @@ -2777,7 +2754,7 @@ test("a scope's merged covering prefixes never supersede across prefixes", async const instance = Instance.random(); const room = origin.claim(Path.from("room"), { instance: { id: instance, epoch: 2n } }); const video = origin.claim(Path.from("room/video"), { instance: { id: instance, epoch: 1n } }); - const reader = origin.scope(Path.from("room/video"), new Path.Patterns([Path.Pattern.all()])).consume(); + const reader = origin.scope(Path.from("room/video"), Path.Prefixes.all()).consume(); await settle(); const table = wireOf(reader).advertised.peek(); if (!table) throw new Error("expected a table"); @@ -2792,14 +2769,14 @@ test("supersession only counts routes the reader can see", async () => { const origin = new Producer(); const instance = Instance.random(); const video = origin - .scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("*/video")])) + .scope(Path.empty(), Path.Prefixes.parse(["room/video"])) .claim(Path.empty(), { instance: { id: instance, epoch: 1n } }); const live = await announcedView(origin, Path.Pattern.parse("room/video"), { covering: true }); await settle(); expect(live.peek().size).toBe(1); // A higher epoch the reader cannot see never hides the route it can. const chat = origin - .scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("*/chat")])) + .scope(Path.empty(), Path.Prefixes.parse(["room/chat"])) .claim(Path.empty(), { instance: { id: instance, epoch: 2n } }); await settle(); expect([...live.peek().values()].map((route) => route.epoch)).toEqual([1n]); @@ -3262,7 +3239,7 @@ test("published names refuse navigation segments", () => { expect(() => origin.claim(path)).toThrow(); expect(() => wireOf(origin).receive(path, WireRoute.default)).toThrow(); } - const scoped = origin.scope(Path.from("room/.."), new Path.Patterns([Path.Pattern.all()])); + const scoped = origin.scope(Path.from("room/.."), Path.Prefixes.all()); expect(() => scoped.route(Path.from("cam"), { hidden: true }).broadcast).toThrow(); for (const path of ["room/.pro/cam", "room/a..b", "room/cam"]) { origin.route(Path.from(path), { hidden: true }).broadcast.close(); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index f9e74d1afe7..16cd16e5ca6 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -53,16 +53,17 @@ class Scope { static readonly all = new Scope(Path.empty()); readonly root: Path.Valid; - readonly allowed?: Path.Patterns; + /** The absolute prefixes this handle reaches; unset for every path. */ + readonly allowed?: Path.Prefixes; - constructor(root: Path.Valid, allowed?: Path.Patterns) { + constructor(root: Path.Valid, allowed?: Path.Prefixes) { this.root = root; this.allowed = allowed; } - narrow(root: Path.Valid, patterns: Path.Patterns): Scope { + narrow(root: Path.Valid, prefixes: Path.Prefixes): Scope { const joined = Path.encode(Path.join(this.root, root)); - const rooted = patterns.rooted(joined); + const rooted = prefixes.rooted(joined); const allowed = this.allowed?.intersect(rooted) ?? rooted; if (allowed.size === 0) throw new Error("origin scopes do not overlap"); return new Scope(joined, allowed); @@ -80,22 +81,22 @@ class Scope { prefix(prefix: Path.Valid): Path.Valid { const joined = Path.encode(Path.join(this.root, prefix)); - if (this.allowed && !this.allowed.overlaps(Path.Pattern.subtree(joined))) { - throw new Error("prefix is outside the origin scope"); - } + if (!this.overlaps(joined)) throw new Error("prefix is outside the origin scope"); return joined; } - patterns(pattern: Path.Pattern = Path.Pattern.all()): Path.Patterns { - const rooted = new Path.Patterns([pattern.rooted(this.root)]); - return this.allowed?.intersect(rooted) ?? rooted; + /** The absolute paths a reader's `filter`, relative to the root, lists within this scope. */ + patterns(filter: Path.Pattern = Path.Pattern.all()): Path.Patterns { + const rooted = new Path.Patterns([filter.rooted(this.root)]); + return this.allowed?.patterns().intersect(rooted) ?? rooted; } - /** Omit nested heads because the outer subscription already carries their routes. */ + /** The root-relative prefixes this scope asks its peer about: its own, already disjoint. */ #heads(): Path.Valid[] { if (!this.allowed) return [Path.empty()]; - const heads = [...new Set([...this.allowed.rebase(this.root)].map(scopeHead))].sort(); - return heads.filter((head) => !heads.some((other) => other !== head && Path.hasPrefix(other, head))); + return [...this.allowed.rebase(this.root)] + .map((prefix) => prefix.text as Path.Valid) + .filter((head) => Path.check(head) === undefined); } #headsCache?: Path.Valid[]; @@ -123,12 +124,12 @@ class Scope { if (!this.allowed) return true; // A peer's prefix may fit the wire alone and overflow once joined: nothing allowed lies there. const path = Path.join(this.root, prefix); - return !Path.check(path) && this.allowed.contains(Path.Pattern.subtree(path)); + return !Path.check(path) && this.allowed.matches(path); } /** Whether this scope allows anything beneath the absolute `prefix`. */ overlaps(prefix: Path.Valid): boolean { - return this.allowed?.overlaps(Path.Pattern.subtree(prefix)) ?? true; + return this.allowed?.overlaps(prefix) ?? true; } /** The exact paths within this scope, relative to its root. */ @@ -153,13 +154,13 @@ class Scope { if (!values || this === Scope.all) return values; const out = new Map(); const covering: [Path.Valid, Advertised[]][] = []; - const allowed = this.allowed && [...this.allowed]; + const allowed = this.allowed; for (const [path, candidates] of values) { const relative = Path.stripPrefix(this.root, path); const above = relative === null || relative === Path.empty(); if (above && !Path.hasPrefix(path, this.root)) continue; const visible = candidates - .filter((value) => !allowed || allowed.some((pattern) => advertOverlaps(value, path, pattern))) + .filter((value) => !allowed || advertReaches(value, path, allowed)) // The claim moves with the key, so it compares against root-relative requests. .map((value) => (value.claim ? { ...value, claim: value.claim.rebase(this.root) } : value)); if (visible.length === 0) continue; @@ -185,7 +186,16 @@ class Scope { /** Whether the route advertised at `prefix` may serve any path `pattern` admits. */ function advertOverlaps(advert: Advertised, prefix: Path.Valid, pattern: Path.Pattern): boolean { - return advert.claim ? advert.claim.overlaps(pattern) : scopeOverlaps(pattern, prefix); + return advert.claim + ? advert.claim.toArray().some((member) => pattern.overlaps(member.pattern())) + : scopeOverlaps(pattern, prefix); +} + +/** Whether the route advertised at `prefix` may serve any path `allowed` reaches. */ +function advertReaches(advert: Advertised, prefix: Path.Valid, allowed: Path.Prefixes): boolean { + return advert.claim + ? advert.claim.toArray().some((member) => allowed.overlaps(member.text)) + : allowed.overlaps(prefix); } /** One segment of the watched-prefix trie, kept while it or anything beneath it is watched. */ @@ -303,8 +313,8 @@ interface Resolution { interface RouteEntry { readonly identity: object; readonly scope: Scope; - /** The paths the entry may serve beneath its prefix, when its producer is scoped. */ - readonly claim?: Path.Patterns; + /** The prefixes beneath its own the entry may serve, when its producer's scope covers less. */ + readonly claim?: Path.Prefixes; readonly route: Signal; readonly originated: boolean; readonly server?: ServeState; @@ -320,6 +330,13 @@ function admits(entry: RouteEntry, path: Path.Valid): boolean { return entry.scope.matches(path); } +/** What a route at the absolute `prefix` may serve under `scope`: unset for its whole subtree. */ +function claimed(scope: Scope, prefix: Path.Valid): Path.Prefixes | undefined { + const allowed = scope.allowed; + if (!allowed || allowed.matches(prefix)) return undefined; + return new Path.Prefixes(allowed.toArray().filter((member) => Path.hasPrefix(prefix, member.text as Path.Valid))); +} + /** One advertisement at a prefix. `exact` marks an announced local broadcast, which is only its own path. */ interface Candidate extends Advertised { readonly exact: boolean; @@ -1279,8 +1296,8 @@ export interface Reader { /** Follow `path` across publisher instances; see {@link Consumer.follow}. */ follow(path: Path.Valid, options?: FollowOptions): Following; - /** The available broadcasts under `scope`, as a live stream; see {@link Consumer.announced}. */ - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer; + /** The available broadcasts matching `filter`, as a live stream; see {@link Consumer.announced}. */ + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer; } /** @@ -1432,12 +1449,12 @@ export class Producer implements Table { } /** - * Narrow this handle to patterns beneath root, presenting paths relative to that root. - * The result borrows the origin; see {@link close}. + * Narrow this handle to `prefixes` beneath `root`, presenting paths relative to that root. + * Each prefix covers its whole subtree. The result borrows the origin; see {@link close}. */ - scope(root: Path.Valid, patterns: Path.Patterns): Producer { + scope(root: Path.Valid, prefixes: Path.Prefixes): Producer { if (this.#borrowed?.closed) throw new Error("origin handle is closed"); - const scope = this.#scope.narrow(root, patterns); + const scope = this.#scope.narrow(root, prefixes); const producer = new Producer(); producer.#state = this.#state; producer.#scope = scope; @@ -1671,7 +1688,7 @@ export class Producer implements Table { const entry: RouteEntry = { identity: {}, scope: this.#scope, - claim: this.#scope.allowed?.intersect(new Path.Patterns([Path.Pattern.subtree(prefix)])), + claim: claimed(this.#scope, prefix), route: new Signal(route), originated, server, @@ -1914,14 +1931,14 @@ export class Producer implements Table { return this.#reader.follow(path, options); } - /** The available broadcasts under `scope`, as a live stream; see {@link Consumer.announced}. */ - announced(scope?: Path.Pattern, options?: Announce.Options): Announce.Consumer { - return this.#reader.announced(scope, options); + /** The available broadcasts matching `filter`, as a live stream; see {@link Consumer.announced}. */ + announced(filter?: Path.Pattern, options?: Announce.Options): Announce.Consumer { + return this.#reader.announced(filter, options); } - /** Every route change under `scope`; see {@link Consumer.routes}. @internal */ - routes(scope?: Path.Pattern, options?: Announce.Options): announce.Routes { - return this.#reader.routes(scope, options); + /** Every route change matching `filter`; see {@link Consumer.routes}. @internal */ + routes(filter?: Path.Pattern, options?: Announce.Options): announce.Routes { + return this.#reader.routes(filter, options); } /** @@ -2574,31 +2591,32 @@ export class Consumer implements Reader { } /** - * The paths announced matching `scope`, each reported as it becomes present or absent: - * every currently advertised one arrives first as `present`. Another instance taking a - * path over keeps it present; {@link follow} it to see the broadcast change. - * Any pattern is accepted. A local broadcast appears once it announces, exactly as a - * peer sees it. Only routes whose literal name matches the scope are listed, unless - * `options.covering` also asks for every dynamic or received route whose subtree - * overlaps it. The stream ends when the origin closes or the consumer is closed. Hidden - * routes are left out unless `options.hidden` opts in (see {@link Announce.Options}). - * While open, it watches the scope's literal head: a `consume` session asks its peer for - * announcements there. + * The paths announced matching `filter` within this handle's scope, each reported as it + * becomes present or absent: every currently advertised one arrives first as `present`. + * Another instance taking a path over keeps it present; {@link follow} it to see the + * broadcast change. The filter is any pattern, relative to the root, and only picks what is + * listed: what the wildcards stood for is each announcement's `captures`. A local broadcast + * appears once it announces, exactly as a peer sees it. Only routes whose literal name + * matches the filter are listed, unless `options.covering` also asks for every dynamic or + * received route whose subtree overlaps it. The stream ends when the origin closes or the + * consumer is closed. Hidden routes are left out unless `options.hidden` opts in (see + * {@link Announce.Options}). While open, it watches the filter's literal head: a `consume` + * session asks its peer for announcements there. */ - announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { - return announce.list(this.routes(scope, options)); + announced(filter: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { + return announce.list(this.routes(filter, options)); } /** - * Every change to the routes matching `scope`, for code that forwards routes: a re-price - * is an `update`, and another instance serving a path a `restart`. Scope, options and the - * watch are as for {@link announced}. + * Every change to the routes matching `filter`, for code that forwards routes: a re-price + * is an `update`, and another instance serving a path a `restart`. The filter, options and + * the watch are as for {@link announced}. * * @internal */ - routes(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): announce.Routes { + routes(filter: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): announce.Routes { const producer = new announce.Producer(); - const patterns = this.#scope.patterns(scope); + const patterns = this.#scope.patterns(filter); void producer.closed.then(this.#state.watch([...patterns].map(scopeHead))); void this.#runAnnounced(producer, patterns, options?.hidden ?? false, options?.covering ?? false); const consumer = producer.consume(); diff --git a/js/net/src/path.ts b/js/net/src/path.ts index a3110b21adf..faf8e46f472 100644 --- a/js/net/src/path.ts +++ b/js/net/src/path.ts @@ -18,8 +18,8 @@ * broadcast and the prefix a route or announcement covers, so a broadcast's own path is * the prefix it announces. See {@link Relative} for `..`-style references. * - * {@link Pattern} and {@link Patterns} are re-exported from `@moq/pattern`, the owner of - * the v1 grammar and algebra. + * {@link Prefix} and {@link Prefixes} (grants) and {@link Pattern} and {@link Patterns} + * (discovery filters) are re-exported from `@moq/pattern`, the owner of their grammar and algebra. * * @example * ```typescript @@ -419,13 +419,16 @@ export function relative(target: Valid, base: Valid): Relative | undefined { return (rel.length === 0 ? "." : rel.join("/")) as Relative; } -/** Path patterns, and a segment's escaping, owned by `@moq/pattern`. */ +/** Grant prefixes, path patterns, and a segment's escaping, owned by `@moq/pattern`. */ export { compareSpecificity, escape, InvalidPattern, + InvalidPrefix, Pattern, Patterns, + Prefix, + Prefixes, type Segment, type Specificity, unescape, diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 9ec749c5816..95152889641 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -108,16 +108,16 @@ export interface OriginConsumer { demand(path: Path.Valid, instance?: Instance.Valid, signal?: AbortSignal): Promise; /** Whether the handle's scope admits everything beneath the root-relative `prefix`. */ covers(prefix: Path.Valid): boolean; - /** The root-relative paths the handle's scope admits; unset for every path. */ - readonly allowed?: Path.Patterns; + /** The root-relative prefixes the handle's scope admits; unset for every path. */ + readonly allowed?: Path.Prefixes; } /** One originated advertisement exposed to the publishing wire. */ export interface Advertised { readonly identity: object; readonly route: WireRoute; - /** The paths a scoped route may serve beneath its prefix, relative like its key; unset for the whole subtree. */ - readonly claim?: Path.Patterns; + /** The prefixes beneath its own a scoped route may serve, relative like its key; unset for the whole subtree. */ + readonly claim?: Path.Prefixes; /** * The prefix the route was advertised at, when a scope's projection merged several covering * prefixes under one key; unset when the key is its prefix. Supersession compares only routes diff --git a/js/pattern/README.md b/js/pattern/README.md index 48f9f3c12a6..4c6cc6bbdb7 100644 --- a/js/pattern/README.md +++ b/js/pattern/README.md @@ -7,11 +7,14 @@ [![npm version](https://img.shields.io/npm/v/@moq/pattern)](https://www.npmjs.com/package/@moq/pattern) [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue.svg)](https://www.typescriptlang.org/) -Exact path patterns for [Media over QUIC](https://moq.dev): grammar, matching, and set algebra. - -A pattern describes a set of broadcast paths. This package provides a shared grammar -for tokens, origin scopes, and announce interests. Integrating -patterns into those consumers is separate work. +Exact path patterns and prefix grants for [Media over QUIC](https://moq.dev): grammar, +matching, and set algebra. + +A `Pattern` describes a set of broadcast paths, for discovery filters that pick which +announcements a reader looks at. A `Prefix` is a grant member: a path covering itself and +everything beneath it, the empty prefix covering everything. `Prefixes`, a reduced set of +them, is what tokens and origin scopes grant. A grant takes no wildcard: `foo/**` and `**` +are refused, written `foo` and `""` instead. Literal paths stay coordinates; `@moq/net`'s path module keeps construction, joins, and prefix operations. @@ -23,11 +26,15 @@ npm add @moq/pattern ``` ```ts -import { Pattern, Patterns } from "@moq/pattern"; +import { Pattern, Prefixes } from "@moq/pattern"; -const grant = Pattern.parse("pid/*/chat"); +const grant = Prefixes.parse(["pid/room1", "pid/room2"]); grant.matches("pid/room1/chat"); // true -grant.contains(Pattern.parse("pid/room1/chat")); // true +grant.overlaps("pid"); // true +grant.rebase("pid").toJSON(); // ["room1", "room2"] + +const filter = Pattern.parse("pid/*/chat"); +filter.matches("pid/room1/chat"); // true const under = Pattern.parse("**/a").rebase("a"); [...under].map((p) => p.text); // ["", "**/a"] diff --git a/js/pattern/api/index.api.md b/js/pattern/api/index.api.md index 92406f64f55..0949785d773 100644 --- a/js/pattern/api/index.api.md +++ b/js/pattern/api/index.api.md @@ -14,6 +14,15 @@ export class InvalidPattern extends Error { readonly code: InvalidPattern.Code; } +export namespace InvalidPrefix { + export type Code = "wildcard" | "path"; +} + +export class InvalidPrefix extends Error { + constructor(code: InvalidPrefix.Code, message: string, options?: ErrorOptions); + readonly code: InvalidPrefix.Code; +} + export class Pattern { static get MAX_INTERSECTIONS(): number; static get MAX_SEGMENTS(): number; @@ -60,6 +69,42 @@ export class Patterns implements Iterable { toJSON(): string[]; } +export class Prefix { + static all(): Prefix; + static compare(a: Prefix, b: Prefix): number; + equals(other: Prefix): boolean; + get isEmpty(): boolean; + matches(path: string): boolean; + overlaps(path: string): boolean; + static parse(text: string): Prefix; + pattern(): Pattern; + rebase(root: string): Prefix | undefined; + rooted(root: string): Prefix; + readonly segments: readonly string[]; + readonly text: string; + toJSON(): string; + toString(): string; +} + +export class Prefixes implements Iterable { + constructor(prefixes?: Iterable); + [Symbol.iterator](): Iterator; + static all(): Prefixes; + covers(other: Prefixes): boolean; + equals(other: Prefixes): boolean; + insert(prefix: Prefix): boolean; + intersect(other: Prefixes): Prefixes; + matches(path: string): boolean; + overlaps(path: string): boolean; + static parse(texts: Iterable): Prefixes; + patterns(): Patterns; + rebase(root: string): Prefixes; + rooted(root: string): Prefixes; + get size(): number; + toArray(): Prefix[]; + toJSON(): string[]; +} + export type Segment = { readonly kind: "literal"; readonly value: string; diff --git a/js/pattern/bench/algebra.ts b/js/pattern/bench/algebra.ts index 3d90ffa2f64..e0bce99fdde 100644 --- a/js/pattern/bench/algebra.ts +++ b/js/pattern/bench/algebra.ts @@ -1,8 +1,9 @@ /** - * Reduce and compare a peer grant shaped against the union index, swept over its encoded size - * up to a whole AUTH_OK. Mirrors the Shape cases of the Rust `pattern-algebra` benchmark. + * Reduce and compare a peer grant swept over its encoded size up to a whole AUTH_OK: the + * pattern unions shaped against the union index mirror the Shape cases of the Rust + * `pattern-algebra` benchmark, and the prefix rows are the grant AUTH_OK carries now. */ -import { Pattern, Patterns } from "@moq/pattern"; +import { Pattern, Patterns, Prefix, Prefixes } from "@moq/pattern"; /** The largest moq-lite control message, and so the largest AUTH_OK. */ const MAX_MESSAGE = 65535; @@ -58,6 +59,38 @@ function grant(shape: "mask" | "chain", bytes: number): { texts: string[]; previ return { texts: [...masks, ...queries], previous }; } +/** + * Distinct prefix members two segments beneath a shared parent, filling at most `bytes` encoded + * and given in reverse order so collecting sorts them, with a previous grant of their parents. + */ +function prefixGrant(bytes: number): { texts: string[]; previous: Prefixes } { + const texts: string[] = []; + const parents: string[] = []; + let size = 0; + for (let index = 0; ; index++) { + const parent = `room/${String(index).padStart(5, "0")}`; + const text = `${parent}/cam`; + size += varint(text.length) + text.length; + if (varint(texts.length + 1) + size > bytes) break; + texts.push(text); + parents.push(parent); + } + return { texts: texts.reverse(), previous: Prefixes.parse(parents) }; +} + +/** Time `body`, repeated to at least 100 ms, and print its row. */ +function time(bytes: number, shape: string, op: string, members: number, body: () => unknown): void { + let iterations = 0; + const start = performance.now(); + let elapsed = 0; + while (elapsed < 100 || iterations < 3) { + body(); + iterations++; + elapsed = performance.now() - start; + } + console.log(`${bytes},${shape},${op},${members},${(elapsed / iterations).toFixed(3)}`); +} + /** Time each shape's reduction and regrant comparison, repeated to at least 100 ms per row. */ export function run(): void { console.log("bytes,shape,op,members,ms_per_op"); @@ -70,23 +103,22 @@ export function run(): void { const reduced = new Patterns(message); if (reduced.size !== message.length) throw new Error(`${shape} members are not incomparable`); if (!previous.covers(reduced)) throw new Error(`${shape} previous grant does not cover it`); - const ops: [string, () => unknown][] = [ - ["collect", () => new Patterns(message).size], - ["covers", () => previous.covers(reduced)], - ]; - for (const [op, body] of ops) { - let iterations = 0; - const start = performance.now(); - let elapsed = 0; - while (elapsed < 100 || iterations < 3) { - body(); - iterations++; - elapsed = performance.now() - start; - } - console.log(`${bytes},${shape},${op},${message.length},${(elapsed / iterations).toFixed(3)}`); - } + time(bytes, shape, "collect", message.length, () => new Patterns(message).size); + time(bytes, shape, "covers", message.length, () => previous.covers(reduced)); } } + for (const bytes of [4096, 16384, MAX_MESSAGE]) { + const { texts, previous } = prefixGrant(bytes); + const size = wire(texts); + if (size > bytes || size * 16 <= bytes * 15) throw new Error(`prefix fills ${size} of ${bytes} bytes`); + const message = texts.map((text) => Prefix.parse(text)); + const reduced = new Prefixes(message); + if (reduced.size !== message.length) throw new Error("prefix members are not disjoint"); + if (!previous.covers(reduced)) throw new Error("prefix previous grant does not cover it"); + time(bytes, "prefix", "collect", message.length, () => new Prefixes(message).size); + time(bytes, "prefix", "covers", message.length, () => previous.covers(reduced)); + time(bytes, "prefix", "intersect", message.length, () => previous.intersect(reduced).size); + } } if (import.meta.main) run(); diff --git a/js/pattern/src/index.ts b/js/pattern/src/index.ts index cd4eb7d95ad..2d4c0862677 100644 --- a/js/pattern/src/index.ts +++ b/js/pattern/src/index.ts @@ -1,9 +1,11 @@ /** - * Exact path patterns for Media over QUIC: grammar, matching, and set algebra. + * Path grants and exact path patterns for Media over QUIC: grammar, matching, and set algebra. * - * A {@link Pattern} describes a set of broadcast paths. {@link Patterns} is an unordered - * union reduced by exact containment. Matching is linear. {@link Pattern.literal} - * rejects `*` because it is reserved for pattern syntax. + * A grant is a {@link Prefixes} set: each {@link Prefix} covers a path and everything beneath + * it, and a grant never holds a wildcard. A {@link Pattern} describes a set of broadcast paths + * for discovery filters, such as listing the announcements matching `room/ * /chat`. + * {@link Patterns} is an unordered union reduced by exact containment. Matching is linear. + * {@link Pattern.literal} rejects `*` because it is reserved for pattern syntax. * * `@moq/net` and `@moq/auth` re-export this package. The Rust twin is `moq-pattern`. * @@ -18,7 +20,7 @@ * * Segments are split as {@link parts} splits a path: a `\` escapes the `\` or `/` after it, * so `a\/b` is one literal segment holding a `/`, and any other `\` is refused. A literal - * matches the segment spelled the same way, which is how a grant names a moq-transport + * matches the segment spelled the same way, which is how a pattern names a moq-transport * namespace field holding a `/`. There is no escape for `*`. * * Patterns are exact: `foo` matches only `foo`, `foo/**` matches its subtree including @@ -583,8 +585,8 @@ export class Pattern { /** * Whether every path `other` matches, this pattern matches too. * - * This is the authorization check: a grant contains a request when the request - * cannot name a path outside it. A pattern contains itself. + * A filter contains a candidate when the candidate cannot name a path outside it. A + * pattern contains itself. */ contains(other: Pattern): boolean { if (this.#globstar === undefined) { @@ -659,7 +661,7 @@ export class Pattern { * The patterns that, relative to `root`, match exactly the paths this pattern matches * beneath `root`. * - * This is how a grant or an advertisement is presented inside a rooted view. It is a + * This is how a filter is presented inside a rooted view. It is a * set because `**` may consume the root or stop short of it: `** /a` rebased at `a` is * both the empty pattern (the root itself) and `** /a` (deeper paths ending in `a`). * Empty when nothing under `root` matches. The root is normalized like a broadcast path. @@ -700,8 +702,8 @@ export class Pattern { /** * The patterns matching exactly the paths both patterns match. * - * This is how a claim is clamped to a scope: the covered paths inside the grant, as - * patterns of their own. It is a set because two partial segments or two `**` runs can + * This is how a filter is clamped to a scope: the paths inside both, as patterns of their + * own. It is a set because two partial segments or two `**` runs can * meet in more than one way: `ab*` and `*b` meet at `ab*b` and at `ab`, and `a/**` and * `** /a` meet at `a/** /a` and at `a`. Empty when the two do not {@link overlaps | overlap}. */ @@ -1113,18 +1115,18 @@ function bounded(patterns: Iterable): Pattern[] { /** * A union of patterns, reduced so no member is contained by another. * - * This is the shape of a grant (the paths a token may publish) and of a rebased pattern - * (see {@link Pattern.rebase}). Members are kept in canonical order, so two unions - * describing the same reduced set are {@link Patterns.equals | equal}. + * This is the shape of a discovery filter and of a rebased pattern (see + * {@link Pattern.rebase}). Members are kept in canonical order, so two unions describing the + * same reduced set are {@link Patterns.equals | equal}. * * Containment is per member: {@link Patterns.contains} holds when one pattern in the union * contains the candidate. A candidate covered only jointly by several members (`a/**` * against `a`, `a/*`, and `a/* /**`) is refused, which keeps the check linear and its answer - * easy to predict. A grant that means a subtree writes `a/**`. + * easy to predict. A union that means a subtree writes `a/**`. * * Operations over a whole union (construction, {@link Patterns.covers}, and * {@link Patterns.intersect}) index one side by segment first, so their cost follows the - * members a query could match rather than every pair. That keeps common grants near their own + * members a query could match rather than every pair. That keeps common unions near their own * size, but containment between wildcard patterns reduces to Orthogonal Vectors, so some shapes * stay super-linear (the `mask` and `chain` cases in `bench/algebra.ts`). * @@ -1244,3 +1246,328 @@ export class Patterns implements Iterable { return this.size === other.size && this.#members.every((member, i) => member.equals(other.#members[i])); } } + +/** Thrown when a grant member is not a valid {@link Prefix}. */ +export class InvalidPrefix extends Error { + /** Which rule was broken. */ + readonly code: InvalidPrefix.Code; + + /** Create an error with the broken rule in `code` and a human-readable `message`. */ + constructor(code: InvalidPrefix.Code, message: string, options?: ErrorOptions) { + super(message, options); + this.name = "InvalidPrefix"; + this.code = code; + } +} + +/** Types for {@link InvalidPrefix}. */ +export namespace InvalidPrefix { + /** Why a string is not a valid {@link Prefix}. */ + export type Code = + /** It holds a `*`: a grant is a prefix, never a wildcard. */ + | "wildcard" + /** It is not a valid path: an empty segment, a bad escape, or too many segments. */ + | "path"; +} + +// Order two strings by code point, which agrees with UTF-8 byte order and so with the Rust crate. +function compareText(a: string, b: string): number { + const n = Math.min(a.length, b.length); + for (let i = 0; i < n; i++) { + let x = a.charCodeAt(i); + let y = b.charCodeAt(i); + if (x === y) continue; + // UTF-16 sorts surrogates below U+E000..U+FFFF; code point order puts them above. + if (x >= 0xd800 && y >= 0xd800) { + x = x < 0xe000 ? x + 0x2000 : x - 0x800; + y = y < 0xe000 ? y + 0x2000 : y - 0x800; + } + return x - y; + } + return a.length - b.length; +} + +// Order two segment lists segment by segment, so a list sorts right before every list it starts. +function compareSegments(a: readonly string[], b: readonly string[]): number { + const n = Math.min(a.length, b.length); + for (let i = 0; i < n; i++) { + const order = compareText(a[i], b[i]); + if (order !== 0) return order; + } + return a.length - b.length; +} + +// Whether `inner` starts with every segment of `outer`. +function startsWith(outer: readonly string[], inner: readonly string[]): boolean { + return outer.length <= inner.length && outer.every((segment, i) => segment === inner[i]); +} + +function wildcard(text: string): InvalidPrefix { + const hint = + text === "**" + ? 'write the empty prefix "" for everything' + : text.endsWith("/**") && !text.slice(0, -3).includes("*") + ? `write ${JSON.stringify(text.slice(0, -3))} for its subtree` + : "wildcards are refused"; + return new InvalidPrefix( + "wildcard", + `${JSON.stringify(text)} is a wildcard, but a grant is a prefix covering its whole subtree: ${hint}`, + ); +} + +// Whether the path `path` is `prefix` or beneath it. A valid prefix never ends inside an +// escape, so the character after it separates exactly when it is a `/`. +function beneath(path: string, prefix: string): boolean { + return prefix === "" || (path.startsWith(prefix) && (path.length === prefix.length || path[prefix.length] === "/")); +} + +/** + * One grant member: a path covering itself and every path beneath it. The empty prefix + * covers every path. + * + * A prefix is never a wildcard: `foo` covers `foo` and its subtree, so a grant writes `foo` + * where a pattern would write `foo/**`, and the empty prefix where it would write `**`. + * Build one with {@link Prefix.parse} or {@link Prefix.all}. + * + * @public + */ +export class Prefix { + /** The canonical text: segments joined by `/`, escapes as written. */ + readonly text: string; + /** The segments, still escaped as written. */ + readonly segments: readonly string[]; + + private constructor(segments: readonly string[]) { + this.segments = Object.freeze([...segments]); + this.text = segments.join("/"); + Object.freeze(this); + } + + /** + * Parse a grant member. Throws {@link InvalidPrefix}: `wildcard` for any `*`, and `path` + * for a leading, trailing, or doubled `/`, a `\` that escapes neither `\` nor `/`, or more + * than {@link Pattern.MAX_SEGMENTS} segments. + */ + static parse(text: string): Prefix { + if (text.includes("*")) throw wildcard(text); + // The pattern grammar owns path syntax; a text with no `*` parses only as literals. + try { + Pattern.parse(text); + } catch (err) { + if (!(err instanceof InvalidPattern)) throw err; + throw new InvalidPrefix("path", `invalid prefix ${JSON.stringify(text)}: ${err.message}`, { cause: err }); + } + return new Prefix(parts(text)); + } + + /** The empty prefix, which covers every path. */ + static all(): Prefix { + return new Prefix([]); + } + + /** Order two prefixes segment by segment, so a prefix sorts right before everything beneath it. */ + static compare(a: Prefix, b: Prefix): number { + return compareSegments(a.segments, b.segments); + } + + /** The canonical text. */ + toString(): string { + return this.text; + } + + /** Serializes as the canonical text, matching the Rust crate's serde form. */ + toJSON(): string { + return this.text; + } + + /** Whether the two are the same prefix. */ + equals(other: Prefix): boolean { + return this.text === other.text; + } + + /** Whether this is the empty prefix, covering every path. */ + get isEmpty(): boolean { + return this.text === ""; + } + + /** Whether `path`, spelled as a broadcast path, is this prefix or beneath it. */ + matches(path: string): boolean { + return beneath(path, this.text); + } + + /** Whether `path`, spelled as a broadcast path, and this prefix share a path: one lies beneath the other. */ + overlaps(path: string): boolean { + return beneath(path, this.text) || beneath(this.text, path); + } + + /** + * This prefix placed beneath the path `root`, normalized like a broadcast path (slashes + * trimmed and collapsed). Throws {@link InvalidPrefix} when `root` holds a wildcard or a + * malformed escape, or the result has more than {@link Pattern.MAX_SEGMENTS} segments. + */ + rooted(root: string): Prefix { + const segments = [...Prefix.parse(splitPath(root).join("/")).segments, ...this.segments]; + if (segments.length > MAX_PATTERN_SEGMENTS) { + throw new InvalidPrefix("path", `more than ${MAX_PATTERN_SEGMENTS} segments`); + } + return new Prefix(segments); + } + + /** + * This prefix as seen from the path `root`, normalized like a broadcast path: the rest + * beneath it, or the empty prefix when this one covers `root` already. Undefined when the + * two share no path. + */ + rebase(root: string): Prefix | undefined { + const base = splitPath(root); + if (startsWith(this.segments, base)) return Prefix.all(); + if (startsWith(base, this.segments)) return new Prefix(this.segments.slice(base.length)); + return undefined; + } + + /** Its subtree as a {@link Pattern}: `prefix/**`, or the literal at {@link Pattern.MAX_SEGMENTS}. */ + pattern(): Pattern { + return Pattern.subtree(this.text); + } +} + +/** + * A grant: a set of {@link Prefix}es, reduced so no member lies beneath another, and sorted + * segment by segment, so two sets covering the same paths are {@link Prefixes.equals | equal}. + * The empty set grants nothing. + * + * Every operation is near-linear in the members on every shape: a prefix's ancestors sort + * right before it, so finding the member covering a path is one binary search. + * + * @public + */ +export class Prefixes implements Iterable { + #members: Prefix[] = []; + + /** The reduced set of the given prefixes. Empty when none are given. */ + constructor(prefixes?: Iterable) { + if (!prefixes) return; + const sorted = [...prefixes].sort(Prefix.compare); + // Sorted, a prefix's subtree is the run right after it, so one sweep against the last + // member kept drops every covered one. + for (const prefix of sorted) { + const last = this.#members[this.#members.length - 1]; + if (!last || !startsWith(last.segments, prefix.segments)) this.#members.push(prefix); + } + } + + /** The set holding the empty prefix, which covers every path. */ + static all(): Prefixes { + return new Prefixes([Prefix.all()]); + } + + /** Parse each text as a {@link Prefix} into one set. Throws {@link InvalidPrefix}. */ + static parse(texts: Iterable): Prefixes { + return new Prefixes(Array.from(texts, (text) => Prefix.parse(text))); + } + + /** + * Add a prefix, dropping the members beneath it. + * + * Returns false when a member already covers it, leaving the set unchanged. + */ + insert(prefix: Prefix): boolean { + if (this.#covering(prefix.segments)) return false; + const at = this.#after(prefix.segments); + let end = at; + while (end < this.#members.length && startsWith(prefix.segments, this.#members[end].segments)) end++; + this.#members.splice(at, end - at, prefix); + return true; + } + + /** + * Whether some member covers `path`, spelled as a broadcast path. A member covering a path + * covers its whole subtree, so this also says whether the set grants everything beneath it. + */ + matches(path: string): boolean { + return this.#covering(parts(path)); + } + + /** Whether some member lies at, above, or beneath `path`, spelled as a broadcast path. */ + overlaps(path: string): boolean { + const segments = parts(path); + if (this.#covering(segments)) return true; + // Anything beneath the path sorts right after it. + const next = this.#members[this.#after(segments)]; + return next !== undefined && startsWith(segments, next.segments); + } + + /** Whether every member of `other` is covered here: `other` grants nothing this set does not. */ + covers(other: Prefixes): boolean { + return other.#members.every((prefix) => this.#covering(prefix.segments)); + } + + /** The paths in both sets: each member of one that the other covers. */ + intersect(other: Prefixes): Prefixes { + return new Prefixes([ + ...this.#members.filter((prefix) => other.#covering(prefix.segments)), + ...other.#members.filter((prefix) => this.#covering(prefix.segments)), + ]); + } + + /** What this set covers beneath `root`, named relative to it. See {@link Prefix.rebase}. */ + rebase(root: string): Prefixes { + return new Prefixes(this.#members.flatMap((member) => member.rebase(root) ?? [])); + } + + /** Every member placed beneath `root`. See {@link Prefix.rooted}. */ + rooted(root: string): Prefixes { + return new Prefixes(this.#members.map((member) => member.rooted(root))); + } + + /** Each member's subtree as one {@link Patterns} union, to intersect with a pattern filter. */ + patterns(): Patterns { + return new Patterns(this.#members.map((member) => member.pattern())); + } + + /** The number of members. */ + get size(): number { + return this.#members.length; + } + + /** The members, in canonical order. */ + [Symbol.iterator](): Iterator { + return this.#members[Symbol.iterator](); + } + + /** The members as a fresh array, in canonical order. */ + toArray(): Prefix[] { + return [...this.#members]; + } + + /** Serializes as a list of texts, matching the Rust crate's serde form. */ + toJSON(): string[] { + return this.#members.map((member) => member.text); + } + + /** Whether the two sets cover the same paths. */ + equals(other: Prefixes): boolean { + return this.size === other.size && this.#members.every((member, i) => member.equals(other.#members[i])); + } + + // Whether the last member at or before `segments` covers them. In a reduced, sorted set it is + // the only candidate: any covering member sorts before them, with only its own subtree, which + // holds no other member, in between. + #covering(segments: readonly string[]): boolean { + const at = this.#after(segments); + const member = this.#members[at] && compareSegments(this.#members[at].segments, segments) === 0 ? at : at - 1; + return member >= 0 && startsWith(this.#members[member].segments, segments); + } + + // The index of the first member sorting at or after `segments`. + #after(segments: readonly string[]): number { + let lo = 0; + let hi = this.#members.length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (compareSegments(this.#members[mid].segments, segments) < 0) lo = mid + 1; + else hi = mid; + } + return lo; + } +} diff --git a/js/pattern/src/prefix.test.ts b/js/pattern/src/prefix.test.ts new file mode 100644 index 00000000000..36068b8ede4 --- /dev/null +++ b/js/pattern/src/prefix.test.ts @@ -0,0 +1,185 @@ +import { describe, expect, test } from "bun:test"; +import { InvalidPrefix, Pattern, Prefix, Prefixes } from "./index.ts"; + +function code(fn: () => unknown): InvalidPrefix.Code | "ok" { + try { + fn(); + return "ok"; + } catch (err) { + if (err instanceof InvalidPrefix) return err.code; + throw err; + } +} + +function set(...texts: string[]): Prefixes { + return Prefixes.parse(texts); +} + +describe("Prefix", () => { + test("refuses every wildcard loudly", () => { + for (const text of ["**", "a/**", "*", "cam-*.hang", "a*", "a/*/b", "**/a", "a\\*"]) { + expect( + code(() => Prefix.parse(text)), + text, + ).toBe("wildcard"); + } + expect(() => Prefix.parse("live/**")).toThrow('write "live" for its subtree'); + expect(() => Prefix.parse("**")).toThrow('write the empty prefix "" for everything'); + }); + + test("refuses malformed paths", () => { + for (const text of ["/a", "a/", "a//b", "a\\b", Array(33).fill("a").join("/")]) { + expect( + code(() => Prefix.parse(text)), + text, + ).toBe("path"); + } + expect(code(() => Prefix.parse(Array(32).fill("a").join("/")))).toBe("ok"); + }); + + test("the empty prefix covers everything", () => { + const all = Prefix.parse(""); + expect(all.equals(Prefix.all())).toBe(true); + for (const path of ["", "a", "a/b/c"]) expect(all.matches(path)).toBe(true); + expect(all.pattern().text).toBe("**"); + expect(all.isEmpty).toBe(true); + expect(Prefix.parse("a").isEmpty).toBe(false); + }); + + test("matches segment by segment", () => { + const room = Prefix.parse("room"); + expect(room.matches("room")).toBe(true); + expect(room.matches("room/alice")).toBe(true); + expect(room.matches("roomy")).toBe(false); + expect(room.matches("room-b/alice")).toBe(false); + expect(room.matches("")).toBe(false); + expect(room.pattern().text).toBe("room/**"); + }); + + test("respects escapes", () => { + const escaped = Prefix.parse("a\\/b"); + expect(escaped.segments).toEqual(["a\\/b"]); + expect(escaped.matches("a\\/b/c")).toBe(true); + expect(escaped.matches("a/b/c")).toBe(false); + expect(Prefix.parse("a").matches("a\\/b")).toBe(false); + expect(Prefix.parse("a\\\\").matches("a\\\\/b")).toBe(true); + }); + + test("overlaps a path above or beneath it", () => { + const ab = Prefix.parse("a/b"); + expect(ab.overlaps("a")).toBe(true); + expect(ab.overlaps("a/b/c")).toBe(true); + expect(ab.overlaps("")).toBe(true); + expect(ab.overlaps("a/c")).toBe(false); + expect(ab.overlaps("a/bc")).toBe(false); + // A path no prefix can spell still gets an answer. + expect(Prefix.parse("cam").matches("cam/*")).toBe(true); + expect(Prefix.parse("cam/x").overlaps("cam/*")).toBe(false); + }); + + test("rooted and rebase", () => { + expect(Prefix.parse("b").rooted("a").text).toBe("a/b"); + expect(Prefix.all().rooted("a").text).toBe("a"); + expect(code(() => Prefix.parse("b").rooted("a/*"))).toBe("wildcard"); + // A root is normalized like a broadcast path, as a pattern's is. + expect(Prefix.parse("b").rooted("/a//c/").text).toBe("a/c/b"); + expect(Prefix.parse("a/b").rebase("/a//")?.text).toBe("b"); + // A root ending inside an escape is no segment of a prefix. + expect(Prefix.parse("a\\/b").rebase("a\\")).toBeUndefined(); + expect(Prefix.parse("a\\/b").rebase("a\\/b")?.text).toBe(""); + expect(code(() => Prefix.parse(Array(30).fill("a").join("/")).rooted("x/y/z"))).toBe("path"); + + expect(Prefix.parse("a/b").rebase("a")?.text).toBe("b"); + expect(Prefix.parse("a").rebase("a")?.text).toBe(""); + expect(Prefix.parse("a").rebase("a/b")?.text).toBe(""); + expect(Prefix.parse("a").rebase("c")).toBeUndefined(); + expect(Prefix.parse("a").rebase("")?.text).toBe("a"); + }); + + test("orders segment by segment", () => { + const sorted = ["a-b", "a/b", "a", "ab", "a/b/c"].map((text) => Prefix.parse(text)).sort(Prefix.compare); + expect(sorted.map((prefix) => prefix.text)).toEqual(["a", "a/b", "a/b/c", "a-b", "ab"]); + }); +}); + +describe("Prefixes", () => { + test("reduces and sorts", () => { + expect(set("a-b", "a/b", "a", "ab").toJSON()).toEqual(["a", "a-b", "ab"]); + expect(set("x", "", "y").toJSON()).toEqual([""]); + expect(set().size).toBe(0); + expect(set("b", "a").equals(set("a", "b", "a/c"))).toBe(true); + expect(Prefixes.all().toJSON()).toEqual([""]); + }); + + test("insert drops the members beneath it", () => { + const prefixes = set("a/b", "a/c", "a-b", "b"); + expect(prefixes.insert(Prefix.parse("a/b/c"))).toBe(false); + expect(prefixes.insert(Prefix.parse("a"))).toBe(true); + expect(prefixes.toJSON()).toEqual(["a", "a-b", "b"]); + expect(prefixes.insert(Prefix.parse("0"))).toBe(true); + expect(prefixes.toJSON()).toEqual(["0", "a", "a-b", "b"]); + }); + + test("matches and overlaps paths", () => { + const grant = set("a/b", "a-b", "c"); + expect(grant.matches("a/b/x")).toBe(true); + expect(grant.matches("a/b")).toBe(true); + expect(grant.matches("a")).toBe(false); + expect(grant.matches("a/c")).toBe(false); + expect(grant.matches("a-b")).toBe(true); + expect(grant.matches("c/d")).toBe(true); + expect(grant.overlaps("a")).toBe(true); + expect(grant.overlaps("")).toBe(true); + expect(grant.overlaps("a/b/c")).toBe(true); + expect(grant.overlaps("a/c")).toBe(false); + expect(grant.overlaps("b")).toBe(false); + expect(grant.overlaps("a/bc")).toBe(false); + expect(grant.matches("c/*")).toBe(true); + expect(set().matches("")).toBe(false); + expect(set().overlaps("")).toBe(false); + expect(Prefixes.all().matches("anything/at/all")).toBe(true); + }); + + test("covers", () => { + expect(set("a", "b").covers(set("a/x", "b"))).toBe(true); + expect(set("a/x").covers(set("a"))).toBe(false); + expect(set("a").covers(set())).toBe(true); + expect(set().covers(set("a"))).toBe(false); + }); + + test("intersect keeps the deeper of each overlapping pair", () => { + expect( + set("a", "b/c") + .intersect(set("a/x", "b", "d")) + .toJSON(), + ).toEqual(["a/x", "b/c"]); + expect(set("a").intersect(set("a")).toJSON()).toEqual(["a"]); + expect(Prefixes.all().intersect(set("x", "y")).toJSON()).toEqual(["x", "y"]); + expect(set("a").intersect(set("b")).size).toBe(0); + }); + + test("rebase and rooted", () => { + expect(set("a/b", "a/c", "d").rebase("a").toJSON()).toEqual(["b", "c"]); + expect(set("a", "d").rebase("a/b").toJSON()).toEqual([""]); + expect(set("b", "c").rooted("a").toJSON()).toEqual(["a/b", "a/c"]); + }); + + test("patterns are each member's subtree", () => { + expect(set("", "x").patterns().toJSON()).toEqual(["**"]); + expect(set("a", "b/c").patterns().toJSON()).toEqual(["a/**", "b/c/**"]); + expect(set("a").patterns().matches("a/z")).toBe(true); + expect(set("a").patterns().contains(Pattern.parse("a/*"))).toBe(true); + }); + + test("near-linear on a large grant", () => { + // A member per path and a query per member: quadratic work would take seconds here. + const n = 50_000; + const texts = Array.from({ length: n }, (_, i) => `room/${i}/cam`); + const grant = Prefixes.parse(texts); + const wider = Prefixes.parse(Array.from({ length: n }, (_, i) => `room/${i}`)); + expect(grant.size).toBe(n); + expect(wider.covers(grant)).toBe(true); + expect(grant.covers(wider)).toBe(false); + expect(wider.intersect(grant).size).toBe(n); + }); +}); diff --git a/js/room/README.md b/js/room/README.md index c8e4f5bac0a..effe25c5ac5 100644 --- a/js/room/README.md +++ b/js/room/README.md @@ -24,7 +24,7 @@ bun add @moq/room ## Token -Sign with [`@moq/auth`](../auth). `root` is the room, `subscribe: ["**"]` subscribes to everyone, `publish: ["/**"]` so a participant cannot publish at someone else's paths. +Sign with [`@moq/auth`](../auth). `root` is the room, `subscribe: [""]` subscribes to everyone, `publish: [""]` so a participant cannot publish at someone else's paths. ```ts import { claims } from "@moq/room"; diff --git a/js/room/src/room.test.ts b/js/room/src/room.test.ts index f0db998052c..8d79f2b70fa 100644 --- a/js/room/src/room.test.ts +++ b/js/room/src/room.test.ts @@ -15,8 +15,8 @@ test("room restores the announce prefix and reconciles local identity changes", const streams: object[] = []; const connection = { origin: new Signal({ - announced(scope: Net.Path.Pattern) { - expect(scope.equals(Net.Path.Pattern.subtree(Net.Path.from("room-a")))).toBe(true); + announced(filter: Net.Path.Pattern) { + expect(filter.equals(Net.Path.Pattern.subtree(Net.Path.from("room-a")))).toBe(true); let update: Net.Announce.Event | undefined = { prefix: Net.Path.from("room-a/bob/camera.hang"), captures: [Net.Path.Pattern.literal(Net.Path.from("bob/camera.hang"))], diff --git a/js/room/src/token.test.ts b/js/room/src/token.test.ts index f75dc0febcd..2db3e08627e 100644 --- a/js/room/src/token.test.ts +++ b/js/room/src/token.test.ts @@ -4,8 +4,8 @@ import { claims } from "./token.ts"; test("claims root the token at the room and scope publish to the identity", () => { expect(claims("meet/demo", "alice")).toEqual({ root: "meet/demo", - subscribe: ["**"], - publish: ["alice/**"], + subscribe: [""], + publish: ["alice"], }); }); @@ -18,8 +18,8 @@ test("claims rejects an identity with an empty segment", () => { test("claims accepts a multi-segment identity", () => { expect(claims("hang/room", "guest/uuid")).toEqual({ root: "hang/room", - subscribe: ["**"], - publish: ["guest/uuid/**"], + subscribe: [""], + publish: ["guest/uuid"], }); }); diff --git a/js/room/src/token.ts b/js/room/src/token.ts index 71e9dd771f8..bf57645e282 100644 --- a/js/room/src/token.ts +++ b/js/room/src/token.ts @@ -22,16 +22,16 @@ export type Claims = { /** * Token claims for a participant in `room`. * - * `root` is the room prefix, `subscribe` is `**` (everything under the room), and - * `publish` is `/**` so a participant cannot publish at anyone else's - * paths. Throws on an identity that is empty, has an empty segment, or contains `*`, - * since a wildcard would let the participant publish as someone else. + * `root` is the room prefix, `subscribe` is the empty prefix (everything under the room), + * and `publish` is the prefix `` (it and everything beneath it) so a participant + * cannot publish at anyone else's paths. Throws on an identity that is empty, has an empty + * segment, or contains `*`, which no grant takes. */ export function claims(room: string, identity: string): Claims { if (!Path.from(identity)) throw new Error("participant identity must not be empty"); return { root: room, - subscribe: [Path.Pattern.all().text], - publish: [Path.Pattern.subtree(identity).text], + subscribe: [Path.Prefix.all().text], + publish: [Path.Prefix.parse(identity).text], }; } diff --git a/js/watch/src/broadcast.test.ts b/js/watch/src/broadcast.test.ts index 44e37267d31..ee985b1e764 100644 --- a/js/watch/src/broadcast.test.ts +++ b/js/watch/src/broadcast.test.ts @@ -772,7 +772,7 @@ describe("refusal", () => { it("refuses a name outside the origin's scope", async () => { const owner = new Origin.Producer(); - const scoped = owner.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("room"))])); + const scoped = owner.scope(Path.empty(), Path.Prefixes.parse(["room"])); const source = new Broadcast({ origin: scoped, name: Path.from("lobby/live.hang"), @@ -893,9 +893,9 @@ describe("cross-broadcast renditions", () => { const owner = new Origin.Producer(); const scopes: string[] = []; const announced = owner.announced.bind(owner); - owner.announced = (scope, options) => { - scopes.push(scope?.text ?? "**"); - return announced(scope, options); + owner.announced = (filter, options) => { + scopes.push(filter?.text ?? "**"); + return announced(filter, options); }; const source = new Broadcast({ origin: owner, diff --git a/js/watch/src/include.test.ts b/js/watch/src/include.test.ts index 9a0eaba68aa..31e64900314 100644 --- a/js/watch/src/include.test.ts +++ b/js/watch/src/include.test.ts @@ -160,7 +160,7 @@ it("reads an unavailable include as offline without touching the parent", async it("reports an include outside the origin's scope as an error, leaving the parent live", async () => { const origin = new Origin.Producer(); - const scoped = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*")])); + const scoped = origin.scope(Path.empty(), Path.Prefixes.parse(["room"])); publish(scoped, "room/main.hang"); const parent = new Broadcast({ origin: scoped, diff --git a/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt b/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt index 9eac51263ce..544a5dadd63 100644 --- a/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt +++ b/kt/moq/src/jvmAndAndroidTest/kotlin/dev/moq/SmokeTest.kt @@ -1208,7 +1208,7 @@ class SmokeTest { ) moq.use { val everything = - Grant(publish = listOf("**"), subscribe = listOf("**"), expires = null) + Grant(publish = listOf(""), subscribe = listOf(""), expires = null) assertEquals(everything, moq.grants().updates().first { it != null }) val session = accepted.await() assertEquals(everything, session.grants().updates().first { it != null }) diff --git a/nix/modules/moq-relay.nix b/nix/modules/moq-relay.nix index 4283fc555cd..26c7d5a7de8 100644 --- a/nix/modules/moq-relay.nix +++ b/nix/modules/moq-relay.nix @@ -7,12 +7,8 @@ let cfg = config.services.moq-relay; authKey = if cfg.auth.keyFile != null then cfg.auth.keyFile else "${cfg.stateDir}/root.jwk"; - # A prefix becomes the subtree pattern the relay and the auth server grant. - publicPattern = - if cfg.auth.publicPath == null || cfg.auth.publicPath == "" then - "**" - else - "${cfg.auth.publicPath}/**"; + # The prefix the relay and the auth server grant; the empty prefix is everything. + publicPrefix = if cfg.auth.publicPath == null then "" else cfg.auth.publicPath; authUrl = "http://127.0.0.1:${toString cfg.auth.port}/"; in { @@ -108,7 +104,7 @@ in type = lib.types.nullOr lib.types.str; default = null; example = "anon"; - description = "Path prefix anonymous sessions may publish and subscribe under (`anon` grants `anon/**`)"; + description = "Path prefix anonymous sessions may publish and subscribe under (`anon` grants `anon` and everything beneath it; empty grants everything)"; }; }; @@ -283,10 +279,10 @@ in "${pkgs.moq-cli}/bin/moq auth serve" "--listen 127.0.0.1:${toString cfg.auth.port}" "--key ${authKey}" - "--mtls-publish '**' --mtls-subscribe '**' --mtls-link peer" + "--mtls-publish '' --mtls-subscribe '' --mtls-link peer" ] ++ lib.optionals (cfg.auth.publicPath != null) [ - "--public-publish '${publicPattern}' --public-subscribe '${publicPattern}'" + "--public-publish '${publicPrefix}' --public-subscribe '${publicPrefix}'" ] ); Restart = "on-failure"; @@ -380,7 +376,7 @@ in MOQ_AUTH_URL = authUrl; } // lib.optionalAttrs (!cfg.auth.enable) { - MOQ_AUTH_PUBLIC = publicPattern; + MOQ_AUTH_PUBLIC = publicPrefix; } // lib.optionalAttrs (cfg.cluster.connect != null) { # Cluster configuration diff --git a/packaging/moq-relay/relay.toml b/packaging/moq-relay/relay.toml index 801d3b409e5..1243c205b43 100644 --- a/packaging/moq-relay/relay.toml +++ b/packaging/moq-relay/relay.toml @@ -29,15 +29,15 @@ key = "/var/lib/moq-relay/key.pem" # listen = "[::]:80" # Who is admitted. The relay asks an auth server once per session event; the -# policy (keys, public patterns, the grant for mTLS peers) lives there: +# policy (keys, public prefixes, the grant for mTLS peers) lives there: # # moq auth serve --listen 127.0.0.1:4440 --key /var/lib/moq-relay/root.jwk \ -# --public-subscribe 'anon/**' --public-subscribe 'demo/**' \ -# --public-publish 'anon/**' --public-publish 'demo/viewer/**' +# --public-subscribe 'anon' --public-subscribe 'demo' \ +# --public-publish 'anon' --public-publish 'demo/viewer' # # Generate the key with `moq auth generate`. See https://moq.dev/docs/auth. [auth] url = "http://127.0.0.1:4440/" # Or, with no server, a static anonymous grant: -# public = "anon/**" +# public = "anon" diff --git a/py/moq-rs/moq/_records.py b/py/moq-rs/moq/_records.py index 1043e778437..8ab2fe39b83 100644 --- a/py/moq-rs/moq/_records.py +++ b/py/moq-rs/moq/_records.py @@ -513,13 +513,14 @@ def _from_ffi(stats: MoqConnectionStats) -> ConnectionStats: class Grant: """What the peer lets this side do, in this side's own paths. - An empty pattern list grants nothing in that direction; ``**`` grants everything. + Each prefix covers its path and everything beneath it. An empty list grants + nothing in that direction; the empty prefix ``""`` grants everything. """ publish: tuple[str, ...] - """The broadcast patterns this side may publish to the peer.""" + """The broadcast prefixes this side may publish to the peer.""" subscribe: tuple[str, ...] - """The broadcast patterns this side may subscribe to from the peer.""" + """The broadcast prefixes this side may subscribe to from the peer.""" expires: timedelta | None = None """How long until the grant lapses, measured when it was read, or ``None`` for never.""" diff --git a/py/moq-rs/tests/test_server.py b/py/moq-rs/tests/test_server.py index d687d31070d..84672b3edfd 100644 --- a/py/moq-rs/tests/test_server.py +++ b/py/moq-rs/tests/test_server.py @@ -638,7 +638,7 @@ async def accept_loop() -> None: ) as client: session = client.session assert session is not None - everything = moq.Grant(publish=("**",), subscribe=("**",)) + everything = moq.Grant(publish=("",), subscribe=("",)) await _await_grant(session, everything) await asyncio.wait_for(accepted.wait(), timeout=5.0) await _await_grant(sessions[0], everything) diff --git a/quest/m2/lite07-claim-reach-routes.md b/quest/m2/lite07-claim-reach-routes.md index 17c91f18b95..f580bc97d4f 100644 --- a/quest/m2/lite07-claim-reach-routes.md +++ b/quest/m2/lite07-claim-reach-routes.md @@ -20,7 +20,7 @@ the reader as the splice-stall did. Decided 2026-11-20 (maintainer, on [moq-dev/moq#9229](https://github.com/moq-dev/moq/pull/9229)): ✅ the lite-07 cut waits on this quest, and #9229 lands without it (rejected: fixing it after the cut; holding #9229 until it is fixed). ✅ Grants become prefixes -([prefix grants](/quest/m2/prefix-grants.md)), so a route serves whole subtrees and a claim's +([moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251)), so a route serves whole subtrees and a claim's reach is where it is filed and presented (rejected: carrying the reach as patterns on ANNOUNCE_START and ANNOUNCE_UPDATE, a wire field and its machinery that only wildcard grants needed; judging reach from the routes a node knows, which a relay cannot do, so in-process and @@ -59,8 +59,3 @@ on). judgment over sessions and claim churn. Public API: none. Wire: the draft's Claims rule for where a claim is presented. - -## Required - -- [Prefix grants](/quest/m2/prefix-grants.md) - a grant is a set of prefixes, so a route serves - whole subtrees diff --git a/quest/m2/prefix-grants.md b/quest/m2/prefix-grants.md deleted file mode 100644 index f1b1518a05e..00000000000 --- a/quest/m2/prefix-grants.md +++ /dev/null @@ -1,46 +0,0 @@ -# [L] A grant is a set of path prefixes again - -## Goal - -Every grant is a set of path prefixes, each covering its whole subtree, as before -[moq-dev/moq#3746](https://github.com/moq-dev/moq/pull/3746). That covers a token's publish -and subscribe lists, `moq auth serve` rules, the relay's public rules, an origin handle's -scope, and lite-07's AUTH_OK. A wildcard grant (`live/*`, `*/chat`, `cam-*.hang`) is refused -where it is made. So a route a grant admits always serves whole subtrees, and a claim's reach -is the path it is shown at ([claim reach](/quest/m2/lite07-claim-reach-routes.md)). - -## Plan - -Decided 2026-11-20 (maintainer, on [moq-dev/moq#9229](https://github.com/moq-dev/moq/pull/9229)): -✅ revert to prefix grants before the lite-07 cut. No use case needed a wildcard grant, and one -makes a claim's reach inexpressible through a relay (rejected: keeping wildcard grants and -carrying a claim's reach as patterns on ANNOUNCE_START and ANNOUNCE_UPDATE). - -- Back out the grant half of #3746 and the pattern-valued scopes of - [moq-dev/moq#3672](https://github.com/moq-dev/moq/pull/3672): - - `Producer::scope`, `Consumer::scope`, and their `allowed` take and return prefixes, in - Rust (`rs/moq-net/src/model/origin.rs`, `OriginScope`) and JS (`js/net`). - - `Permit` (`rs/moq-net/src/auth.rs`) holds prefixes. - - moq-auth and @moq/auth tokens, `moq auth serve` rules, the relay's `public*` config, - moq-ffi, and the bindings carry prefixes. - - Anything that only existed to intersect wildcard grants goes with it, such as - `Consumer::reaches_any` where a prefix test now does. - - Discovery filters, which choose the claims a reader looks at (announcement captures), keep - their patterns: reach does not concern them. -- Wire: lite-07's AUTH_OK carries Publish and Subscribe Prefixes, and the region rule's regions - (`drafts/draft-lcurley-moq-lite.md`, Announce Request) are the scope's prefixes. The Path - Pattern section goes, with any other use the draft keeps. lite-07 is unpublished, so nothing - stays compatible with the pattern form. -- Syntax, recommended: a member `foo` covers `foo` and its subtree, and the empty member covers - everything. A trailing `/**` (and a bare `**`) parses as the same prefix, since it names exactly - that set, so deployed `anon/**` tokens and configs keep their meaning. Every other wildcard is - refused where it is parsed. The migration note warns that a bare member minted since #3746 - widens from one broadcast to its subtree. -- Align the quests that assume wildcard members: `quest/m4/regrant-route-recheck.md` (a member's - literal prefix before its first wildcard) and `quest/m5/peer-grant-bound.md` (the Orthogonal - Vectors bound, which prefixes remove). -- Docs: `doc/bin/relay/auth.md` (Path matching and the rule tables), and each library page that - shows a wildcard grant. - -Public API: breaking (grant types and token contents, in Rust, JS, and the bindings). Wire: -lite-07 AUTH_OK. diff --git a/quest/m4/grant-alias-delete.md b/quest/m4/grant-alias-delete.md new file mode 100644 index 00000000000..a7478c4df93 --- /dev/null +++ b/quest/m4/grant-alias-delete.md @@ -0,0 +1,48 @@ +# [S] Grants stop spelling prefixes as subtree patterns + +## Goal + +Tokens, key scopes, and auth server grants carry bare prefixes, and `moq-auth` and +`@moq/auth` hold no `foo/**` alias. [moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251) +made grants prefixes and added the alias both ways, so 0.22 works beside releases that read +a grant member as a pattern. It comes out in two steps, one per version migration, so no +pair of releases a deployment runs side by side ever narrows or refuses a grant. + +## Plan + +Decided 2026-11-20 (maintainer, on moq-dev/moq#9251): ✅ a temporary read alias, with the +write alias releases before 0.22 need to read the same subtrees (rejected: shipping strict and +upgrading an auth server with the relays it answers together). + +The alias is `alias` in `rs/moq-auth/src/wire.rs`. In JS it is `readPrefix`, `readPrefixes`, +`WirePrefixListSchema`, `WireAccessSchema`, `writtenAccess`, and `writtenScope` in +`js/auth/src/wire.ts`, with their uses: `WrittenAccessSchema` in `js/auth/src/contract.ts`, +`ScopeSchema`'s last transform and `Scope.allows` in `js/auth/src/claims.ts`, and `toPublic` in +`js/auth/src/key.ts`. Each is marked `TEMPORARY MIGRATION`. + +1. **Write bare prefixes** in the first version migration after 0.22, so it ships in 0.23.0 + and never in a 0.22 patch (see Waiting). Delete Rust's `Written` and JS's `writtenAccess` + and `writtenScope`, so `GrantSchema`, `ScopeSchema`, and `Key.public` output bare prefixes, + and a typed `Grant` or key is again the JSON to write. Keep the read: 0.22 issuers and auth + servers still write `foo/**`, and `Scope.allows` still reads it. Flip the pins + (`tests/released_token.rs`, `rate.json`, the grant cross-language vector, the corpus) and + the wire-compat token lane, where a released (then 0.22) verifier reads both spellings. + The same PR deletes interop.sh's compat `**` probe, since the released relay then reads + `public = ""`, and rewrites this quest to step 2. +2. **Delete the read** in the version migration after that: `foo/**` and `**` are refused in + tokens, key scopes, and grants like every other wildcard, with the message naming the + prefix to write. Tokens 0.22 minted with an `exp` have expired by then. Delete this quest. + +## Waiting + +- Maintainer: open the API or wire phase of the cycle after 0.22, before step 1 lands. A + change landed in that cycle's additive phase would ship in a 0.22 patch, which a relay or + verifier from before 0.22 reads narrower. Check: `version.json` reads `"minor": "0.22"` + and `"phase"` is `"api"` or `"wire"`. +- Maintainer: decide what step 2 does with a `/**` spelling nothing re-mints. A key scope + 0.22 wrote, or a token it signed with no `exp`, keeps `foo/**` until someone rewrites it, + and deleting the read refuses it. Recommended: delete the read everywhere, with a + migration note to regenerate such keys (or edit `foo/**` to `foo` in the JWK, which is + plain JSON) and re-mint unbounded tokens before upgrading; the refusal is loud at load. + Rejected: keeping the read for key scopes alone, which keeps a wildcard spelling alive in + the one place it is never rewritten. Check: this item is answered in the Plan. diff --git a/quest/m4/js-listing-rebuild.md b/quest/m4/js-listing-rebuild.md new file mode 100644 index 00000000000..f23612f16c5 --- /dev/null +++ b/quest/m4/js-listing-rebuild.md @@ -0,0 +1,28 @@ +# [M] JS origin listings update per changed path + +## Goal + +Each update to an `@moq/net` origin listing (`announced()`, `routes()`) costs +work for the paths the change touched, not a rebuild that checks every route +against every scope member. + +## Plan + +`#runAnnounced` in `js/net/src/origin.ts` calls `#listed` on every wake. +`#listed` walks every route in the snapshot, and each candidate scans the +scope's members (`advertReaches`, `advertOverlaps`, `claimed`). The +`producer-scopes` row of `js/net/bench/broadcasts.ts`, with one scope member +per route, measured 22 ms per update at 128 routes and 128 members in +[moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251), as it did with +pattern scopes before. + +- Rebuild only the paths a change touches, keeping the diff against `active`. +- Look members up through `Path.Prefixes`' sorted order instead of scanning + them, as `matches` and `overlaps` already do. +- Sweep the bench over routes and scope members, and report before and after. + +Public API: none. Wire: none. + +## Related + +- [JS announced coalescing](/quest/m3/js-announced-coalesce.md) - the same route stream's pending events diff --git a/quest/m4/regrant-route-recheck.md b/quest/m4/regrant-route-recheck.md index 910479fedde..f0552268c71 100644 --- a/quest/m4/regrant-route-recheck.md +++ b/quest/m4/regrant-route-recheck.md @@ -8,10 +8,12 @@ When a peer's grant changes (a new `AUTH_OK`), a moq-lite announce stream's cost Found by [moq-dev/moq#7536](https://github.com/moq-dev/moq/pull/7536), which swept the worst known peer grant shapes over `AUTH_OK` size and left this scope question to a follow-up (its decision 2, recommended there because it is route fan-out rather than grant algebra). On a regrant, each announce stream's `regrant` (`AnnounceRun::regrant` in `rs/moq-net/src/lite/publisher.rs`, line 1207 at 4bf501b535): -- filters every live route through `permitted` (a match against every member of the new permit) to find what the new grant revokes, and +- filters every live route through `permitted` (a match against the new permit) to find what the new grant revokes, and - when the grant grew, takes a fresh `origin.announced()` snapshot of every route under its prefix and matches each one it doesn't hold. -So one `AUTH_OK` costs routes × members per stream, times K announce streams, a product none of [peer grant bound](/quest/m5/peer-grant-bound.md)'s options remove. +So one `AUTH_OK` costs routes × members per stream, times K announce streams, a product [peer grant bound](/quest/m5/peer-grant-bound.md), which bounds decoding and comparing the grant itself, leaves alone. + +Since [moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251) a grant is a sorted set of prefixes (`Prefixes` in `moq-pattern`), and matching one path costs a binary search per segment of the path, so the per-route factor is log members rather than members. The walk over every live route remains, and is what this quest bounds. Decided 2026-11-01 (maintainer, follow-up planning after the 2026-11-01 /quest-afk run): its own quest beside the grant-algebra bound. @@ -19,14 +21,14 @@ Re-scoped 2026-11-12 (/quest-audit): [Each side says everything about routing on As shipped by [moq-dev/moq#8987](https://github.com/moq-dev/moq/pull/8987), lite-07 regrants in `Routed::poll_permit` (`rs/moq-net/src/lite/publisher/routed.rs`): a routing peer's grant must stay whole or the session closes, and on a claims-only session every claim told or live is re-synced against the new permit, so lite-07 still pays its claims times grant members once per session. The `AnnounceRun::regrant` path above now serves lite-06 and older only. -- Derive the change once per `AUTH_OK`, not per stream: the members added and removed between the old and new grant. Each stream then re-checks only the routes under a changed member's prefix, using the origin's prefix structure to find them instead of walking every route. A member's prefix here is its literal segments before its first wildcard (`*` or `**`), since grant members are `Patterns` (`Permit` in `rs/moq-net/src/auth.rs`); a member that starts with a wildcard has no literal prefix and re-checks every route under the stream's prefix, as today. Bench that shape on its own row, and test wildcard members, removed members, and old and new grants that overlap. +- Derive the change once per `AUTH_OK`, not per stream: the members added and removed between the old and new grant. Each stream then re-checks only the routes under a changed member, using the origin's prefix structure to find them instead of walking every route. A member is a prefix (`Permit` in `rs/moq-net/src/auth.rs` holds `Prefixes`), so the routes it can change are exactly those at, beneath, or above it. Test removed members, members nested in old ones, and old and new grants that overlap. - A grown grant needs only routes under the added members, not a whole-origin snapshot per stream. - Keep today's semantics: a revoked route retracts, a newly covered route advertises, and a route both grants cover is untouched. -- Bench with #7536's grant shapes over announce streams and live routes, both swept; report before/after. +- Bench over announce streams, live routes, and grant members, all swept; report before/after. Public API: none. Wire: none. ## Related -- [Bound peer grant processing by design](/quest/m5/peer-grant-bound.md) - may change the received-grant type this compares against; whichever lands second adapts +- [Bound peer grant processing by design](/quest/m5/peer-grant-bound.md) - the cost of decoding and comparing one grant, which this builds on - [Cap the legacy announce interest streams](/quest/m4/announce-interest-memory.md) - the per-stream route state that this walk reads on lite-06 and older diff --git a/quest/m5/peer-grant-bound.md b/quest/m5/peer-grant-bound.md index 6ea92d20a0e..38625db12e7 100644 --- a/quest/m5/peer-grant-bound.md +++ b/quest/m5/peer-grant-bound.md @@ -1,4 +1,4 @@ -# [L] Bound peer grant processing by design +# [S] Bound peer grant processing by design ## Goal @@ -11,52 +11,35 @@ Re-matching live routes after a regrant is out of scope; it is ## Plan -[moq-dev/moq#6356](https://github.com/moq-dev/moq/pull/6356) indexed -whole-union operations in `moq-pattern` and `@moq/pattern` by segment. That -made every benchmarked shape linear, but containment between patterns with -wildcards reduces to Orthogonal Vectors, so no index closes the gap. -[moq-dev/moq#7536](https://github.com/moq-dev/moq/pull/7536) added the -`pattern-algebra` cases (Rust Criterion) and `js/pattern/bench/algebra.ts` -(Chromium through `bench/browser/micro.ts --bench pattern`) that sweep the two -worst known shapes over message size up to a whole `AUTH_OK`: - -- `mask`: equal-weight `*`/`y` masks beside literal queries that fail only at - the last segment. At 64 KiB one reduction costs about 40 ms in Rust and - 250 ms in Chromium, growing as bytes^1.7. -- `chain`: correlated partials `a^i*z^(n-i)`. At 64 KiB about 9 ms in Rust and - 24 ms in Chromium, growing as bytes^1.3. - -Each `AUTH_OK` pays this more than once: Rust reduces on decode -(`Patterns::decode`) and again in `Grant::union`, then every moq-lite announce -stream's regrant runs `Permit::covers` (the same walk) against its previous -permit. JS reduces on decode (`decodePatterns`) and in `union`. - -Decided 2026-11-01 (maintainer, on #7536): - -- ✅ Keep received grants unreduced, in their own type. `AUTH_OK` decodes into - a sorted, deduplicated pattern list in O(n log n). The union of received - grants is a merge, and equality and `Permit::covers` become syntactic - (`Permit::covers` already allows a spurious false, which only re-reads the - origin once). Matching stays O(members) per path. `Patterns` keeps its - reduced, set-equality promise for the grants this side issues and verifies, - including moq-auth's `granted.covers(requested)`. Rejected: weakening - `Patterns` everywhere (loses set equality, or splits `covers` into two - behaviors), a joint prefix/suffix index alone (leaves `mask` at bytes^1.7), - and refusing an `AUTH_OK` past a work budget (a runtime refusal by constant - that changes wire behavior for valid grants). -- ✅ The regrant's route re-match is a follow-up quest, not this one. - -Open, for a `/quest-plan` before implementation: the received-grant type's -name and crate, and whether `Grant` splits into issued and received types -(today one `Grant` serves `Handle::grant()` and `Credential::grant` as well as -`Request::accept`, `Issued::update`, and `Handle::authorize`, in Rust and in -`@moq/net`). Either is a breaking change to the received side's API. - -Prove it with the same benches: the received-grant decode, union, and -`Permit::covers` swept over the `mask` and `chain` shapes, linear in bytes, -before and after. +Since [moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251), `AUTH_OK` +carries prefixes, not patterns. Containment between patterns with wildcards +reduces to Orthogonal Vectors, which made the `mask` and `chain` shapes cost +bytes^1.7 and bytes^1.3, but no peer input carries a wildcard any more. That +also retires the 2026-11-01 decision to keep received grants unreduced in +their own type: `Prefixes` reduces in one sort and one sweep in `moq-pattern` +and `@moq/pattern`, so issued and received grants keep one type. + +[moq-dev/moq#9251](https://github.com/moq-dev/moq/pull/9251) added prefix rows +(`collect`, `covers`, `intersect`) to the `pattern-algebra` group in +`rs/moq-pattern/benches/matching.rs` and to `js/pattern/bench/algebra.ts`: +distinct members two segments beneath their parents, filling 4 KiB, 16 KiB, +and a whole 64 KiB `AUTH_OK`. One local Rust run at 64 KiB took 0.27 ms to +collect, 2.8 ms for `covers`, and 6.2 ms to intersect. From 4 KiB to 64 KiB, +`collect` grew 16x and the other two about 22x, so n log n. + +Remaining: + +- Run `bench/browser/micro.ts --bench pattern` in Chromium and record the + prefix rows. +- Sweep the shapes that stress prefixes instead: deep members (32 segments) + and long segments. `matches` binary-searches each ancestor of a path, up to + 33 searches per member. If a shape grows faster than n log n, make it one + predecessor search: in a reduced set, the only member that can cover a path + is the greatest member not after it. +- Decide whether the `mask` and `chain` rows still earn their place, now that + they measure only local discovery filters. ## Related -- [A regrant re-checks only the routes it can change](/quest/m4/regrant-route-recheck.md) - the route fan-out after a regrant, which this leaves alone; whichever lands second adapts to the other +- [A regrant re-checks only the routes it can change](/quest/m4/regrant-route-recheck.md) - the route fan-out after a regrant, which this leaves alone - [Fair share across broadcasts](/quest/m5/broadcast-fairness.md) - the other per-session CPU and bandwidth fairness concern diff --git a/rs/libmoq/src/api.rs b/rs/libmoq/src/api.rs index 3a5d79429aa..eb99fb08308 100644 --- a/rs/libmoq/src/api.rs +++ b/rs/libmoq/src/api.rs @@ -2613,7 +2613,7 @@ pub extern "C" fn moq_broadcast_request_free(request: u32) -> i32 { }) } -/// Learn about broadcasts matching a pattern scope under an origin. +/// Learn about broadcasts matching a discovery filter under an origin. /// /// `config` selects the paths; NULL lists every visible one. Delivered /// [moq_announce_event] prefixes remain relative to the origin. @@ -2643,7 +2643,7 @@ pub unsafe extern "C" fn moq_origin_announced( ) -> i32 { ffi::enter(move || { let origin = ffi::parse_id(origin)?; - let (scope, hidden) = match unsafe { config.as_ref() } { + let (filter, hidden) = match unsafe { config.as_ref() } { None => (moq_net::Pattern::all(), false), Some(config) => { let prefix = unsafe { ffi::parse_str(config.prefix, config.prefix_len)? }; @@ -2656,7 +2656,7 @@ pub unsafe extern "C" fn moq_origin_announced( } }; let on_announce = unsafe { ffi::OnStatus::new(user_data, on_announce)? }; - State::lock().origin.announced(origin, scope, hidden, on_announce) + State::lock().origin.announced(origin, filter, hidden, on_announce) }) } diff --git a/rs/libmoq/src/auth.rs b/rs/libmoq/src/auth.rs index 1cda7e66ef8..bda29b38b94 100644 --- a/rs/libmoq/src/auth.rs +++ b/rs/libmoq/src/auth.rs @@ -39,11 +39,11 @@ unsafe impl Send for GrantRecord {} impl GrantRecord { fn new(grant: Option) -> Self { - let patterns = |patterns: &moq_net::Patterns| patterns.iter().map(ToString::to_string).collect::>(); - let publish = grant.as_ref().map(|grant| patterns(&grant.publish)).unwrap_or_default(); + let prefixes = |prefixes: &moq_net::Prefixes| prefixes.iter().map(ToString::to_string).collect::>(); + let publish = grant.as_ref().map(|grant| prefixes(&grant.publish)).unwrap_or_default(); let subscribe = grant .as_ref() - .map(|grant| patterns(&grant.subscribe)) + .map(|grant| prefixes(&grant.subscribe)) .unwrap_or_default(); let views = |strings: &[String]| { strings @@ -66,17 +66,18 @@ impl GrantRecord { /// What the peer lets this side do, in this side's own paths. /// -/// An empty pattern list grants nothing in that direction; `**` grants everything. +/// Each entry is a path prefix covering that path and everything beneath it. An empty list +/// grants nothing in that direction; the empty prefix `""` grants everything. #[repr(C)] pub struct moq_grant { /// False until the peer first answers a token, and while a client reconnects; the other /// fields are then empty. pub present: bool, - /// The broadcast patterns this side may publish to the peer. + /// The broadcast prefixes this side may publish to the peer. pub publish: *const moq_string, /// The number of entries in `publish`. pub publish_len: usize, - /// The broadcast patterns this side may subscribe to from the peer. + /// The broadcast prefixes this side may subscribe to from the peer. pub subscribe: *const moq_string, /// The number of entries in `subscribe`. pub subscribe_len: usize, diff --git a/rs/libmoq/src/origin.rs b/rs/libmoq/src/origin.rs index db2de2af36f..462bc79b983 100644 --- a/rs/libmoq/src/origin.rs +++ b/rs/libmoq/src/origin.rs @@ -193,7 +193,7 @@ impl Origin { pub fn announced( &mut self, origin: Id, - scope: moq_net::Pattern, + filter: moq_net::Pattern, hidden: bool, on_announce: OnStatus, ) -> Result { @@ -203,7 +203,7 @@ impl Origin { let consumer = origin .producer .consume() - .scope("", &moq_net::Patterns::from(scope))? + .filter(&filter)? .with_hidden(hidden) .announced(); self.announced_task.spawn( diff --git a/rs/moq-auth/README.md b/rs/moq-auth/README.md index 20f594d289f..4bdfb74d954 100644 --- a/rs/moq-auth/README.md +++ b/rs/moq-auth/README.md @@ -11,8 +11,11 @@ Gateway sessions may identify `rtmp`, `srt`, or `webrtc` transport. A lease prod owns its re-check and expiry clock through `due()`, `update()`, and `failed()`. `Key::verify` checks signature, algorithm, key ID, strict claims, key scope, expiry, and not-before. -Grants and claims name paths with patterns: `foo` is one broadcast, `foo/**` is -a subtree, `**` is everything. +Grants and claims name paths with prefixes: `foo` covers `foo` and everything +beneath it, and the empty prefix is everything. A `Prefix` refuses a wildcard +such as `foo/**` or `*`. Tokens, key scopes, and grants spell each prefix as the +subtree pattern releases before 0.22 read, `foo/**` or `**`, and read that +spelling back as the prefix; any other wildcard there is refused. ## Quick Usage (symmetric keys) @@ -20,7 +23,7 @@ a subtree, `**` is everything. # generate secret key moq auth generate --out key.jwk # sign a new JWT -moq auth sign --key key.jwk --root demo --publish 'bbb/**' > token.jwt +moq auth sign --key key.jwk --root demo --publish bbb > token.jwt # verify the JWT moq auth verify --key key.jwk < token.jwt ``` @@ -31,7 +34,7 @@ moq auth verify --key key.jwk < token.jwt # generate private and public keys moq auth generate --algorithm RS256 --out private.jwk --public public.jwk # sign a new JWT (using private key) -moq auth sign --key private.jwk --root demo --publish 'bbb/**' > token.jwt +moq auth sign --key private.jwk --root demo --publish bbb > token.jwt # verify the JWT (using public key) moq auth verify --key public.jwk < token.jwt ``` diff --git a/rs/moq-auth/benches/jwt.rs b/rs/moq-auth/benches/jwt.rs index 1d218378cb3..87ecab9f800 100644 --- a/rs/moq-auth/benches/jwt.rs +++ b/rs/moq-auth/benches/jwt.rs @@ -10,7 +10,7 @@ use moq_auth::{Algorithm, Claims, Key}; fn bench_jwt(c: &mut Criterion) { let claims = Claims::default() .with_root("room") - .with_publish(["alice/**".parse::().unwrap()]); + .with_publish(["alice".parse::().unwrap()]); let mut group = c.benchmark_group("jwt"); for algorithm in [Algorithm::HS256, Algorithm::ES256, Algorithm::EdDSA, Algorithm::RS256] { diff --git a/rs/moq-auth/benches/verify.rs b/rs/moq-auth/benches/verify.rs index 1e21ac3297b..f8feabd6072 100644 --- a/rs/moq-auth/benches/verify.rs +++ b/rs/moq-auth/benches/verify.rs @@ -112,7 +112,7 @@ const SAMPLES: usize = 1 << 16; fn claims(expires: kio::clock::Wall) -> Claims { Claims::default() .with_root("demo") - .with_publish(["**".parse().unwrap()]) + .with_publish([moq_auth::Prefix::all()]) .with_expires(expires) } @@ -353,7 +353,7 @@ impl Bench { let grant = runtime.block_on(post()); assert_eq!( grant.publish.path, - ["**".parse().unwrap()].into_iter().collect(), + [moq_auth::Prefix::all()].into_iter().collect(), "{name}" ); self.add(name, move || { @@ -377,7 +377,7 @@ impl Bench { let [_, a, b, c] = (index as u32).to_be_bytes(); let mut request = Request::new("bench", Transport::Quic, "/demo"); request.remote = Some(([10, a, b, c], 4433).into()); - let claims = valid().with_publish([format!("s{index}/**").parse().unwrap()]); + let claims = valid().with_publish([format!("s{index}").parse().unwrap()]); request.query = Some(format!("jwt={}", key.sign(&claims).unwrap())); request }; diff --git a/rs/moq-auth/examples/asymmetric.rs b/rs/moq-auth/examples/asymmetric.rs index 131a3b59fff..956657888dd 100644 --- a/rs/moq-auth/examples/asymmetric.rs +++ b/rs/moq-auth/examples/asymmetric.rs @@ -18,8 +18,8 @@ fn main() -> anyhow::Result<()> { // Sign a token with the private key. let claims = moq_auth::Claims::default() .with_root("rooms/meeting-123") - .with_publish(["alice/**".parse()?]) - .with_subscribe(["**".parse()?]) + .with_publish(["alice".parse()?]) + .with_subscribe([moq_auth::Prefix::all()]) .with_expires(kio::clock::wall() + Duration::from_secs(3600)) .with_issued(kio::clock::wall()); diff --git a/rs/moq-auth/examples/basic.rs b/rs/moq-auth/examples/basic.rs index a875799eeee..96aad1c9909 100644 --- a/rs/moq-auth/examples/basic.rs +++ b/rs/moq-auth/examples/basic.rs @@ -13,8 +13,8 @@ fn main() -> anyhow::Result<()> { // Create claims for the token. let claims = moq_auth::Claims::default() .with_root("demo") - .with_publish(["my-stream/**".parse()?]) // Can publish to demo/my-stream and below - .with_subscribe(["**".parse()?]) // Can subscribe to anything under demo/ + .with_publish(["my-stream".parse()?]) // Can publish to demo/my-stream and below + .with_subscribe([moq_auth::Prefix::all()]) // Can subscribe to anything under demo/ .with_expires(kio::clock::wall() + Duration::from_secs(3600)) .with_issued(kio::clock::wall()); diff --git a/rs/moq-auth/rate.json b/rs/moq-auth/rate.json index fbaaadd128e..2b6366350eb 100644 --- a/rs/moq-auth/rate.json +++ b/rs/moq-auth/rate.json @@ -3,8 +3,8 @@ { "input": { "root": "room", - "publish": { "path": ["alice/**"], "rate": 8000000 }, - "subscribe": ["**"] + "publish": { "path": ["alice"], "rate": 8000000 }, + "subscribe": [""] }, "claims": { "root": "room", @@ -24,17 +24,17 @@ }, "claims": { "root": "room", - "subscribe": { "path": ["bob"], "rate": 1 } + "subscribe": { "path": ["bob/**"], "rate": 1 } }, "grant": { "root": "room", - "subscribe": { "path": ["bob"], "rate": 1 } + "subscribe": { "path": ["bob/**"], "rate": 1 } } }, { "input": { "root": "room", - "publish": { "path": ["**"], "rate": 9007199254740991 } + "publish": { "path": [""], "rate": 9007199254740991 } }, "claims": { "root": "room", @@ -49,7 +49,7 @@ "input": { "root": "room", "publish": { "path": [], "rate": 8000 }, - "subscribe": ["**"] + "subscribe": [""] }, "claims": { "root": "room", @@ -64,17 +64,17 @@ } ], "invalid": [ - { "publish": { "path": ["**"], "rate": 0 } }, - { "publish": { "path": ["**"], "rate": -1 } }, - { "publish": { "path": ["**"], "rate": 1.5 } }, - { "publish": { "path": ["**"], "rate": 9007199254740992 } }, - { "publish": { "path": ["**"], "rate": null } }, - { "publish": { "path": ["**"], "rate": "8000" } }, + { "publish": { "path": [""], "rate": 0 } }, + { "publish": { "path": [""], "rate": -1 } }, + { "publish": { "path": [""], "rate": 1.5 } }, + { "publish": { "path": [""], "rate": 9007199254740992 } }, + { "publish": { "path": [""], "rate": null } }, + { "publish": { "path": [""], "rate": "8000" } }, { "publish": { "rate": 8000 } }, - { "publish": { "path": ["**"] } }, - { "publish": { "path": "**", "rate": 8000 } }, - { "publish": { "path": ["**"], "raet": 8000 } }, - { "publish": { "path": ["**"], "rate": 8000, "extra": true } }, + { "publish": { "path": [""] } }, + { "publish": { "path": "", "rate": 8000 } }, + { "publish": { "path": [""], "raet": 8000 } }, + { "publish": { "path": [""], "rate": 8000, "extra": true } }, { "subscribe": { "path": ["not//valid"], "rate": 8000 } } ] } diff --git a/rs/moq-auth/src/access.rs b/rs/moq-auth/src/access.rs index bc0f3da26ee..316966941c9 100644 --- a/rs/moq-auth/src/access.rs +++ b/rs/moq-auth/src/access.rs @@ -1,26 +1,27 @@ use crate::Rate; -use moq_pattern::Patterns; +use moq_pattern::Prefixes; use serde::{Deserialize, Deserializer, Serialize}; -/// What one role (publish or subscribe) of a token or grant reaches: path patterns, -/// relative to the root, and an optional bitrate cap. +/// What one role (publish or subscribe) of a token or grant reaches: path prefixes, +/// relative to the root, each covering its path and everything beneath it, and an +/// optional bitrate cap. /// -/// On the wire it is the list of patterns, or `{"path": [...], "rate": bps}` when +/// On the wire it is the list of prefixes, or `{"path": [...], "rate": bps}` when /// capped, which every older reader refuses rather than drop the cap. The object takes /// exactly `path` and `rate`, so a misspelled cap is refused rather than read as none. #[derive(Debug, Clone, Default, PartialEq, Eq)] #[non_exhaustive] pub struct Access { - /// Patterns the role may reach, relative to the root. - pub path: Patterns, + /// Prefixes the role may reach, relative to the root. + pub path: Prefixes, /// The most the role may move, or no cap when absent. pub rate: Option, } impl Access { - /// Access to these patterns, uncapped. - pub fn new(path: impl Into) -> Self { + /// Access to these prefixes, uncapped. + pub fn new(path: impl Into) -> Self { Self { path: path.into(), rate: None, @@ -39,8 +40,8 @@ impl Access { } } -impl From for Access { - fn from(path: Patterns) -> Self { +impl From for Access { + fn from(path: Prefixes) -> Self { Self::new(path) } } @@ -52,38 +53,59 @@ struct Capped

{ rate: Rate, } -impl Serialize for Access { - fn serialize(&self, serializer: S) -> Result { +impl Access { + /// Write the role in its wire shape, with `path` standing in for its prefix list. + pub(crate) fn serialize_as( + &self, + path: P, + serializer: S, + ) -> Result { match self.rate { - None => self.path.serialize(serializer), - Some(rate) => Capped { path: &self.path, rate }.serialize(serializer), + None => path.serialize(serializer), + Some(rate) => Capped { path, rate }.serialize(serializer), } } -} -impl<'de> Deserialize<'de> for Access { - fn deserialize>(deserializer: D) -> Result { + /// Read the role from its wire shape, its prefix list read as `P`. + pub(crate) fn deserialize_as<'de, P, D>(deserializer: D) -> Result + where + P: Deserialize<'de> + Into, + D: Deserializer<'de>, + { use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer}; + use std::marker::PhantomData; - // Dispatch on the shape, so a bad pattern or cap reports its own error rather + // Dispatch on the shape, so a bad prefix or cap reports its own error rather // than an untagged enum's "matched no variant". - struct Visitor; - impl<'de> serde::de::Visitor<'de> for Visitor { + struct Visitor

(PhantomData

); + impl<'de, P: Deserialize<'de> + Into> serde::de::Visitor<'de> for Visitor

{ type Value = Access; fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("a list of path patterns, or an object of a `path` list and a `rate`") + f.write_str("a list of path prefixes, or an object of a `path` list and a `rate`") } fn visit_seq>(self, seq: A) -> Result { - Ok(Access::new(Patterns::deserialize(SeqAccessDeserializer::new(seq))?)) + Ok(Access::new(P::deserialize(SeqAccessDeserializer::new(seq))?)) } fn visit_map>(self, map: A) -> Result { - let Capped { path, rate } = Capped::::deserialize(MapAccessDeserializer::new(map))?; + let Capped { path, rate } = Capped::

::deserialize(MapAccessDeserializer::new(map))?; Ok(Access::new(path).with_rate(rate)) } } - deserializer.deserialize_any(Visitor) + deserializer.deserialize_any(Visitor::

(PhantomData)) + } +} + +impl Serialize for Access { + fn serialize(&self, serializer: S) -> Result { + self.serialize_as(&self.path, serializer) + } +} + +impl<'de> Deserialize<'de> for Access { + fn deserialize>(deserializer: D) -> Result { + Self::deserialize_as::(deserializer) } } diff --git a/rs/moq-auth/src/claims.rs b/rs/moq-auth/src/claims.rs index 5a382ef4a30..d96c2ecd913 100644 --- a/rs/moq-auth/src/claims.rs +++ b/rs/moq-auth/src/claims.rs @@ -1,31 +1,31 @@ use crate::{Access, path}; -use moq_pattern::Patterns; +use moq_pattern::{Prefix, Prefixes}; use serde::{Deserialize, Serialize}; /// The immutable ceiling on what a key may grant, embedded in its JWK. /// -/// Patterns in `publish` and `subscribe` are relative to `root`, matching token claim -/// semantics. A key signs a token only when every pattern the token grants is -/// contained by one the scope allows, in the same role; see [`allows`](Self::allows). +/// Prefixes in `publish` and `subscribe` are relative to `root`, matching token claim +/// semantics. A key signs a token only when every prefix the token grants lies at or +/// beneath one the scope allows, in the same role; see [`allows`](Self::allows). /// /// The scope is fixed at key generation. Widening it means minting a new key, which /// is the point: a leaked scoped key can never be talked into signing more than it /// already could. A key with no scope at all is unrestricted, so keys minted before /// scopes existed keep working. /// -/// Legacy `put`/`get` prefix scopes load as subtree patterns; a scope is always written -/// as `publish` and `subscribe`. +/// Legacy `put`/`get` scopes load as the same prefixes; a scope is always written as +/// `publish` and `subscribe`, spelled as [`Claims`] are. #[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)] #[serde(try_from = "crate::wire::Object", into = "crate::wire::Scope")] pub struct Scope { - /// The root for the publish/subscribe patterns below. + /// The root for the publish/subscribe prefixes below. pub root: String, - /// Patterns this key may grant to publishers. - pub publish: Patterns, + /// Prefixes this key may grant to publishers. + pub publish: Prefixes, - /// Patterns this key may grant to subscribers. - pub subscribe: Patterns, + /// Prefixes this key may grant to subscribers. + pub subscribe: Prefixes, } impl Scope { @@ -41,21 +41,21 @@ impl Scope { Ok(()) } - /// Whether every pattern `claims` grants is covered by this scope, per role. + /// Whether every prefix `claims` grants is covered by this scope, per role. /// /// Both sides are placed beneath their own root before comparing, so the same - /// grant expressed as `root: "demo"` + `publish: ["room/**"]` or as - /// `publish: ["demo/room/**"]` is treated identically. Containment is per pattern, - /// so a scope of `live/**` does not cover `lively/**`, and the roles are checked + /// grant expressed as `root: "demo"` + `publish: ["room"]` or as + /// `publish: ["demo/room"]` is treated identically. Coverage is by segment, so a + /// scope of `live` does not cover `lively`, and the roles are checked /// independently: a publish-only scope never authorizes a subscribe grant. /// - /// `**` covers everything beneath the scope root, so a scope of `root: "demo"` + - /// `publish: ["**"]` grants publish anywhere under `demo`. + /// The empty prefix covers everything beneath the scope root, so a scope of + /// `root: "demo"` + `publish: [""]` grants publish anywhere under `demo`. pub fn allows(&self, claims: &Claims) -> bool { - let covers = |granted: &Patterns, requested: &Patterns| { + let covers = |granted: &Prefixes, requested: &Prefixes| { match (granted.rooted(&self.root), requested.rooted(&claims.root)) { (Ok(granted), Ok(requested)) => granted.covers(&requested), - // A root too deep to place the patterns beneath cannot be granted either way. + // A root too deep to place the prefixes beneath cannot be granted either way. _ => false, } }; @@ -64,24 +64,24 @@ impl Scope { } } -/// The access a [`Claims`] grants at a specific path, with every pattern rebased so -/// it is relative to that path. +/// The access a [`Claims`] grants at a specific path, with every prefix rebased so it +/// is relative to that path. /// -/// Produced by [`Claims::authorize`]. `**` grants the path itself and everything -/// beneath it; the empty pattern grants exactly the path. The reference server's -/// policy holds its anonymous and mTLS rules as this pair too, authorized at `/`. +/// Produced by [`Claims::authorize`]. The empty prefix grants the path itself and +/// everything beneath it. The reference server's policy holds its anonymous and mTLS +/// rules as this pair too, authorized at `/`. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct Permissions { - /// Patterns the holder may subscribe to, relative to the authorized path. - pub subscribe: Patterns, + /// Prefixes the holder may subscribe to, relative to the authorized path. + pub subscribe: Prefixes, - /// Patterns the holder may publish to, relative to the authorized path. - pub publish: Patterns, + /// Prefixes the holder may publish to, relative to the authorized path. + pub publish: Prefixes, } impl Permissions { - /// Access granted as these pattern unions. - pub fn new(publish: impl Into, subscribe: impl Into) -> Self { + /// Access granted as these prefix sets. + pub fn new(publish: impl Into, subscribe: impl Into) -> Self { Self { publish: publish.into(), subscribe: subscribe.into(), @@ -94,22 +94,25 @@ impl Permissions { } } -/// The payload of a token: a root, plus the publish/subscribe patterns granted beneath it. +/// The payload of a token: a root, plus the publish/subscribe prefixes granted beneath it. /// /// Build one from [`Default`] with the `with_*` setters, sign it with /// [`Key::sign`](crate::Key::sign), and scope it to a connection with -/// [`authorize`](Self::authorize). A pattern names exactly what it says: `alice` -/// is one broadcast, `alice/**` is a subtree, and `**` is everything under the root. +/// [`authorize`](Self::authorize). A prefix covers its path and everything beneath it: +/// `alice` is `alice` and every broadcast under it, and the empty prefix is everything +/// under the root. A [`Prefix`] holds no wildcard: `alice/**` or `*` fails to parse. /// /// ```no_run /// let claims = moq_auth::Claims::default() /// .with_root("room/123") -/// .with_publish(["alice/**".parse().unwrap()]) -/// .with_subscribe(["**".parse().unwrap()]); +/// .with_publish(["alice".parse().unwrap()]) +/// .with_subscribe([moq_auth::Prefix::all()]); /// ``` /// -/// Grants are written as `publish` and `subscribe`. Legacy `moq-token` claims are read -/// too: each `put`/`get` prefix `p` is the subtree `p/**`. A capped role is written as +/// Grants are written as `publish` and `subscribe`, each prefix spelled as the subtree +/// pattern a release before 0.22 reads (`alice/**`, `**`), and read back alike; any +/// other wildcard fails verification. Legacy `moq-token` claims are read too: each +/// `put`/`get` entry is the same prefix. A capped role is written as /// `{"path": [...], "rate": bps}`, which every older verifier refuses rather than drop /// the cap. The registered `iss` and `sub` claims are read and ignored; any other field /// fails verification, since it might narrow the grant, and a misspelled `root` would @@ -118,7 +121,7 @@ impl Permissions { #[serde(try_from = "crate::wire::Object", into = "crate::wire::Claims")] #[non_exhaustive] pub struct Claims { - /// The root for the publish/subscribe patterns below. + /// The root for the publish/subscribe prefixes below. /// It's mostly for compression and is optional, defaulting to the empty string. pub root: String, @@ -144,21 +147,21 @@ pub struct Claims { } impl Claims { - /// Set the root that the publish/subscribe patterns are relative to. + /// Set the root that the publish/subscribe prefixes are relative to. pub fn with_root(mut self, root: impl Into) -> Self { self.root = root.into(); self } - /// Grant publish access to these patterns, relative to the root, keeping any cap. - pub fn with_publish(mut self, patterns: impl IntoIterator) -> Self { - self.publish.path = patterns.into_iter().collect(); + /// Grant publish access to these prefixes, relative to the root, keeping any cap. + pub fn with_publish(mut self, prefixes: impl IntoIterator) -> Self { + self.publish.path = prefixes.into_iter().collect(); self } - /// Grant subscribe access to these patterns, relative to the root, keeping any cap. - pub fn with_subscribe(mut self, patterns: impl IntoIterator) -> Self { - self.subscribe.path = patterns.into_iter().collect(); + /// Grant subscribe access to these prefixes, relative to the root, keeping any cap. + pub fn with_subscribe(mut self, prefixes: impl IntoIterator) -> Self { + self.subscribe.path = prefixes.into_iter().collect(); self } @@ -208,22 +211,22 @@ impl Claims { Ok(()) } - /// The access these claims grant at `path`, rebased so each returned pattern is + /// The access these claims grant at `path`, rebased so each returned prefix is /// relative to `path`. /// /// `path` and [`root`](Self::root) must overlap, in either direction: /// /// - `path` extends the root (root `demo`, path `demo/room`), so the extra - /// `room` narrows each pattern and drops the ones outside it. + /// `room` narrows each prefix and drops the ones outside it. /// - `path` is a parent of the root (root `demo`, path ``), so `demo` is - /// prepended to each pattern to keep it anchored where the token points. + /// prepended to each prefix to keep it anchored where the token points. /// /// Matching is segment-aware, so a root of `foo` does not cover `foobar`. /// Slashes at the boundaries are implicit: `/demo/` and `demo` are the same path. /// /// Returns [`Error::RootMismatch`](crate::Error::RootMismatch) when the two don't /// overlap, and [`Error::NoAccess`](crate::Error::NoAccess) when they do but every - /// pattern falls outside `path`. + /// prefix falls outside `path`. /// /// This is authorization only. Verify the signature first with /// [`Key::verify`](crate::Key::verify), which is where expiry is enforced. @@ -241,13 +244,13 @@ impl Claims { return Err(crate::Error::RootMismatch(path)); }; - let scope = |patterns: &Patterns| -> crate::Result { + let scope = |prefixes: &Prefixes| -> crate::Result { if prefix.is_empty() { - // The path reaches into the grant; keep what each pattern says below it. - Ok(patterns.rebase(suffix)) + // The path reaches into the grant; keep what each prefix covers below it. + Ok(prefixes.rebase(suffix)) } else { // The grant sits below the path; name it from there. - Ok(patterns.rooted(prefix)?) + Ok(prefixes.rooted(prefix)?) } }; @@ -270,7 +273,7 @@ mod tests { use std::time::Duration; - fn patterns(texts: &[&str]) -> Patterns { + fn prefixes(texts: &[&str]) -> Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } @@ -280,7 +283,7 @@ mod tests { fn fractional_times_narrow_the_window() { let at = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(1500); let claims = Claims { - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), expires: Some(at), not_before: Some(at), ..Default::default() @@ -292,8 +295,8 @@ mod tests { fn create_test_claims() -> Claims { Claims { root: "test-path".to_string(), - publish: patterns(&["test-pub/**"]).into(), - subscribe: patterns(&["test-sub/**"]).into(), + publish: prefixes(&["test-pub"]).into(), + subscribe: prefixes(&["test-sub"]).into(), expires: Some(kio::clock::wall() + Duration::from_secs(3600)), issued: Some(kio::clock::wall()), not_before: None, @@ -305,12 +308,12 @@ mod tests { fn scope_allows_contained_claims() { let scope = Scope { root: "project".into(), - publish: patterns(&["live/**"]), - subscribe: patterns(&["watch/**"]), + publish: prefixes(&["live"]), + subscribe: prefixes(&["watch"]), }; let claims = Claims { root: "project/live/room".into(), - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), ..Default::default() }; assert!(scope.allows(&claims)); @@ -320,17 +323,17 @@ mod tests { fn scope_rejects_sibling_and_role_escalation() { let scope = Scope { root: "project".into(), - publish: patterns(&["live/**"]), - subscribe: Patterns::new(), + publish: prefixes(&["live"]), + subscribe: Prefixes::new(), }; let sibling = Claims { root: "project/lively".into(), - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), ..Default::default() }; let role = Claims { root: "project/live".into(), - subscribe: patterns(&["**"]).into(), + subscribe: prefixes(&[""]).into(), ..Default::default() }; assert!(!scope.allows(&sibling)); @@ -342,24 +345,24 @@ mod tests { // The same grant, expressed three ways, must compare identically. let scope = Scope { root: "project".into(), - publish: patterns(&["live/**"]), - subscribe: Patterns::new(), + publish: prefixes(&["live"]), + subscribe: Prefixes::new(), }; for claims in [ Claims { root: "project".into(), - publish: patterns(&["live/room/**"]).into(), + publish: prefixes(&["live/room"]).into(), ..Default::default() }, Claims { root: String::new(), - publish: patterns(&["project/live/room/**"]).into(), + publish: prefixes(&["project/live/room"]).into(), ..Default::default() }, Claims { root: "/project/live/".into(), - publish: patterns(&["room/**"]).into(), + publish: prefixes(&["room"]).into(), ..Default::default() }, ] { @@ -371,15 +374,15 @@ mod tests { fn scope_rejects_escaping_above_its_root() { let scope = Scope { root: "project".into(), - publish: patterns(&["live/**"]), - subscribe: Patterns::new(), + publish: prefixes(&["live"]), + subscribe: Prefixes::new(), }; // A root above the scope's does not widen it, even though `**` would grant // everything within the scope. let claims = Claims { root: String::new(), - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), ..Default::default() }; assert!(!scope.allows(&claims)); @@ -389,53 +392,50 @@ mod tests { fn scope_globstar_grants_everything_beneath_it() { let scope = Scope { root: "project".into(), - publish: patterns(&["**"]), - subscribe: Patterns::new(), + publish: prefixes(&[""]), + subscribe: Prefixes::new(), }; let claims = Claims { root: "project/anything/deep".into(), - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), ..Default::default() }; assert!(scope.allows(&claims)); } #[test] - fn scope_requires_every_requested_pattern() { - // One allowed pattern does not carry an unallowed sibling along with it. + fn scope_requires_every_requested_prefix() { + // One allowed prefix does not carry an unallowed sibling along with it. let scope = Scope { root: "project".into(), - publish: patterns(&["live/**"]), - subscribe: Patterns::new(), + publish: prefixes(&["live"]), + subscribe: Prefixes::new(), }; let claims = Claims { root: "project".into(), - publish: patterns(&["live/room/**", "other/**"]).into(), + publish: prefixes(&["live/room", "other"]).into(), ..Default::default() }; assert!(!scope.allows(&claims)); } #[test] - fn scope_is_exact_about_a_literal() { - // `live` is one broadcast; a subtree beneath it is more than the scope grants. + fn scope_covers_by_segment() { + // `live` covers itself and everything beneath it, but not `lively` or its parent. let scope = Scope { root: "project".into(), - publish: patterns(&["live"]), - subscribe: Patterns::new(), + publish: prefixes(&["live"]), + subscribe: Prefixes::new(), }; - let exact = Claims { + let claims = |publish: &[&str]| Claims { root: "project".into(), - publish: patterns(&["live"]).into(), + publish: prefixes(publish).into(), ..Default::default() }; - let subtree = Claims { - root: "project".into(), - publish: patterns(&["live/**"]).into(), - ..Default::default() - }; - assert!(scope.allows(&exact)); - assert!(!scope.allows(&subtree)); + assert!(scope.allows(&claims(&["live"]))); + assert!(scope.allows(&claims(&["live/room"]))); + assert!(!scope.allows(&claims(&["lively"]))); + assert!(!scope.allows(&claims(&[""]))); } #[test] @@ -449,35 +449,51 @@ mod tests { } #[test] - fn scope_reads_legacy_prefixes_as_subtrees() { + fn scope_reads_legacy_prefixes_as_prefixes() { let scope: Scope = serde_json::from_str(r#"{"root":"demo","put":["room"],"get":[""]}"#).unwrap(); - assert_eq!(scope.publish, patterns(&["room/**"])); - assert_eq!(scope.subscribe, patterns(&["**"])); + assert_eq!(scope.publish, prefixes(&["room"])); + assert_eq!(scope.subscribe, prefixes(&[""])); } + /// TEMPORARY MIGRATION (see `wire::alias`): written as the subtree patterns a release + /// before 0.22 reads. #[test] - fn scope_writes_patterns() { - let subtrees = Scope { + fn scope_writes_subtree_patterns() { + let both = Scope { root: "demo".into(), - publish: patterns(&["room/**"]), - subscribe: patterns(&["**"]), + publish: prefixes(&["room"]), + subscribe: prefixes(&[""]), }; - assert_eq!( - serde_json::to_string(&subtrees).unwrap(), - r#"{"root":"demo","publish":["room/**"],"subscribe":["**"]}"# - ); + let json = serde_json::to_string(&both).unwrap(); + assert_eq!(json, r#"{"root":"demo","publish":["room/**"],"subscribe":["**"]}"#); + assert_eq!(serde_json::from_str::(&json).unwrap(), both); - let exact = Scope { + let publish = Scope { root: "demo".into(), - publish: patterns(&["room"]), - subscribe: Patterns::new(), + publish: prefixes(&["room"]), + subscribe: Prefixes::new(), }; assert_eq!( - serde_json::to_string(&exact).unwrap(), - r#"{"root":"demo","publish":["room"]}"# + serde_json::to_string(&publish).unwrap(), + r#"{"root":"demo","publish":["room/**"]}"# ); } + #[test] + fn scope_reads_bare_prefixes() { + let scope: Scope = serde_json::from_str(r#"{"root":"demo","publish":["room"],"subscribe":[""]}"#).unwrap(); + assert_eq!(scope.publish, prefixes(&["room"])); + assert_eq!(scope.subscribe, prefixes(&[""])); + } + + #[test] + fn scope_refuses_a_wildcard() { + for json in [r#"{"publish":["room/*"]}"#, r#"{"subscribe":["*/chat"]}"#] { + let err = serde_json::from_str::(json).unwrap_err(); + assert!(err.to_string().contains("wildcard"), "{json}: {err}"); + } + } + #[test] fn test_claims_validation_success() { let claims = create_test_claims(); @@ -505,7 +521,7 @@ mod tests { fn test_claims_validation_only_publish() { let claims = Claims { root: "test-path".to_string(), - publish: patterns(&["test-pub"]).into(), + publish: prefixes(&["test-pub"]).into(), ..Default::default() }; @@ -516,7 +532,7 @@ mod tests { fn test_claims_validation_only_subscribe() { let claims = Claims { root: "test-path".to_string(), - subscribe: patterns(&["test-sub"]).into(), + subscribe: prefixes(&["test-sub"]).into(), ..Default::default() }; @@ -538,13 +554,13 @@ mod tests { fn test_claims_serde_names() { let claims = Claims { root: "live".into(), - publish: patterns(&["camera1"]).into(), - subscribe: patterns(&["camera1", "camera2"]).into(), + publish: prefixes(&["camera1"]).into(), + subscribe: prefixes(&["camera1", "camera2"]).into(), ..Default::default() }; assert_eq!( serde_json::to_string(&claims).unwrap(), - r#"{"root":"live","publish":["camera1"],"subscribe":["camera1","camera2"]}"# + r#"{"root":"live","publish":["camera1/**"],"subscribe":["camera1/**","camera2/**"]}"# ); } @@ -552,8 +568,8 @@ mod tests { fn test_claims_read_legacy_prefixes_as_subtrees() { let claims: Claims = serde_json::from_str(r#"{"root":"test","put":["pub1","/a//b/"],"get":"","exp":1700000000}"#).unwrap(); - assert_eq!(claims.publish.path, patterns(&["pub1/**", "a/b/**"])); - assert_eq!(claims.subscribe.path, patterns(&["**"])); + assert_eq!(claims.publish.path, prefixes(&["pub1", "a/b"])); + assert_eq!(claims.subscribe.path, prefixes(&[""])); assert!(claims.expires.is_some()); } @@ -561,8 +577,8 @@ mod tests { fn test_claims_write_patterns_and_id() { let subtrees = Claims { root: "live".into(), - publish: patterns(&["camera1/**"]).into(), - subscribe: patterns(&["**"]).into(), + publish: prefixes(&["camera1"]).into(), + subscribe: prefixes(&[""]).into(), ..Default::default() } .with_id(crate::TokenId::decode("abc-123").unwrap()); @@ -579,12 +595,12 @@ mod tests { #[test] fn test_claims_refuse_an_unbounded_id() { - let long = format!(r#"{{"publish":["**"],"jti":"{}"}}"#, "x".repeat(129)); + let long = format!(r#"{{"publish":[""],"jti":"{}"}}"#, "x".repeat(129)); for json in [ - r#"{"publish":["**"],"jti":7}"#, - r#"{"publish":["**"],"jti":null}"#, - r#"{"publish":["**"],"jti":""}"#, - r#"{"publish":["**"],"jti":"has space"}"#, + r#"{"publish":[""],"jti":7}"#, + r#"{"publish":[""],"jti":null}"#, + r#"{"publish":[""],"jti":""}"#, + r#"{"publish":[""],"jti":"has space"}"#, long.as_str(), ] { assert!(serde_json::from_str::(json).is_err(), "{json}"); @@ -610,10 +626,40 @@ mod tests { assert!(serde_json::from_str::(r#"{"put":["a/*"]}"#).is_err()); } + /// TEMPORARY MIGRATION (see `wire::alias`): the subtree patterns released issuers + /// write read as their prefixes, and a bare member as its prefix too. + #[test] + fn test_claims_read_subtree_patterns() { + let claims: Claims = + serde_json::from_str(r#"{"publish":["alice/**","bob"],"subscribe":{"path":["**"],"rate":1000}}"#).unwrap(); + assert_eq!(claims.publish.path, prefixes(&["alice", "bob"])); + assert_eq!(claims.subscribe.path, prefixes(&[""])); + } + + #[test] + fn test_claims_refuse_any_other_wildcard() { + for json in [ + r#"{"publish":["*"]}"#, + r#"{"publish":["cam-*.hang"]}"#, + r#"{"publish":["a/**/b"]}"#, + r#"{"publish":["a/*/**"]}"#, + r#"{"publish":["/**"]}"#, + r#"{"publish":{"path":["*/a"],"rate":1000}}"#, + ] { + let err = serde_json::from_str::(json).unwrap_err(); + assert!(err.to_string().contains("wildcard"), "{json}: {err}"); + } + } + #[test] - fn test_claims_refuse_a_bad_pattern() { - let err = serde_json::from_str::(r#"{"publish":["a/**/b/**"]}"#).unwrap_err(); - assert!(err.to_string().contains("**"), "{err}"); + fn test_claims_refuse_a_malformed_prefix() { + for json in [ + r#"{"publish":["/a"]}"#, + r#"{"publish":["a/"]}"#, + r#"{"publish":["a//b"]}"#, + ] { + assert!(serde_json::from_str::(json).is_err(), "{json}"); + } } #[test] @@ -629,110 +675,112 @@ mod tests { fn authorize_claims(root: &str, subscribe: &[&str], publish: &[&str]) -> Claims { Claims { root: root.to_string(), - subscribe: patterns(subscribe).into(), - publish: patterns(publish).into(), + subscribe: prefixes(subscribe).into(), + publish: prefixes(publish).into(), ..Default::default() } } #[test] fn test_authorize_path_equals_root() { - let claims = authorize_claims("room/123", &["**"], &["alice/**"]); + let claims = authorize_claims("room/123", &[""], &["alice"]); let permissions = claims.authorize("room/123").unwrap(); - assert_eq!(permissions.subscribe, patterns(&["**"])); - assert_eq!(permissions.publish, patterns(&["alice/**"])); + assert_eq!(permissions.subscribe, prefixes(&[""])); + assert_eq!(permissions.publish, prefixes(&["alice"])); } #[test] fn test_authorize_path_extends_root() { // Connecting below the root consumes the matching part of each grant. - let claims = authorize_claims("room/123", &["bob/**"], &["alice/**"]); + let claims = authorize_claims("room/123", &["bob"], &["alice"]); let permissions = claims.authorize("room/123/alice").unwrap(); - assert_eq!(permissions.subscribe, Patterns::new()); - assert_eq!(permissions.publish, patterns(&["**"])); + assert_eq!(permissions.subscribe, Prefixes::new()); + assert_eq!(permissions.publish, prefixes(&[""])); } #[test] - fn test_authorize_literal_becomes_the_path_itself() { - // A literal grant reached exactly is the empty pattern: this path, nothing below. + fn test_authorize_within_a_prefix_grants_everything_there() { + // A prefix reached at or beneath it is the empty prefix: this path and all below. let claims = authorize_claims("room", &[], &["alice"]); - let permissions = claims.authorize("room/alice").unwrap(); - - assert_eq!(permissions.publish, patterns(&[""])); + for path in ["room/alice", "room/alice/cam"] { + let permissions = claims.authorize(path).unwrap(); + assert_eq!(permissions.publish, prefixes(&[""]), "{path}"); + } } #[test] fn test_authorize_path_is_parent_of_root() { // Connecting above the root prepends it, keeping the grants anchored. - let claims = authorize_claims("demo", &["**"], &["alice/**"]); + let claims = authorize_claims("demo", &[""], &["alice"]); let permissions = claims.authorize("/").unwrap(); - assert_eq!(permissions.subscribe, patterns(&["demo/**"])); - assert_eq!(permissions.publish, patterns(&["demo/alice/**"])); + assert_eq!(permissions.subscribe, prefixes(&["demo"])); + assert_eq!(permissions.publish, prefixes(&["demo/alice"])); } #[test] fn test_authorize_empty_root() { // A root-scoped token grants everything it lists, wherever it connects. - let claims = authorize_claims("", &["demo/**"], &[]); + let claims = authorize_claims("", &["demo"], &[]); let permissions = claims.authorize("demo/room").unwrap(); - assert_eq!(permissions.subscribe, patterns(&["**"])); - assert_eq!(permissions.publish, Patterns::new()); + assert_eq!(permissions.subscribe, prefixes(&[""])); + assert_eq!(permissions.publish, Prefixes::new()); } #[test] fn test_authorize_slashes_are_implicit() { - let claims = authorize_claims("/room/123/", &["bob/**"], &[]); + let claims = authorize_claims("/room/123/", &["bob"], &[]); let permissions = claims.authorize("//room/123//").unwrap(); - assert_eq!(permissions.subscribe, patterns(&["bob/**"])); + assert_eq!(permissions.subscribe, prefixes(&["bob"])); } #[test] fn test_authorize_respects_segment_boundaries() { // "foo" must not cover "foobar". - let claims = authorize_claims("foo", &["**"], &["**"]); + let claims = authorize_claims("foo", &[""], &[""]); assert!(matches!(claims.authorize("foobar"), Err(crate::Error::RootMismatch(_)))); } #[test] fn test_authorize_unrelated_path() { - let claims = authorize_claims("demo", &["**"], &["**"]); + let claims = authorize_claims("demo", &[""], &[""]); assert!(matches!(claims.authorize("other"), Err(crate::Error::RootMismatch(_)))); } #[test] fn test_authorize_no_access_at_path() { // The path overlaps the root, but every grant sits outside it. - let claims = authorize_claims("", &["demo/**"], &[]); + let claims = authorize_claims("", &["demo"], &[]); assert!(matches!(claims.authorize("other"), Err(crate::Error::NoAccess(_)))); } #[test] fn test_authorize_reads_an_escaped_separator_as_content() { // `a\/b` is one segment holding a `/`, the spelling of a moq-transport field. - let claims = authorize_claims("room", &[r"a\/b/*"], &["*"]); + let claims = authorize_claims("room", &[r"a\/b/c"], &[r"a\/b"]); let permissions = claims.authorize(r"room/a\/b").unwrap(); - assert_eq!(permissions.subscribe, patterns(&["*"])); - assert_eq!(permissions.publish, patterns(&[""])); + assert_eq!(permissions.subscribe, prefixes(&["c"])); + assert_eq!(permissions.publish, prefixes(&[""])); assert!(claims.authorize("room").unwrap().publish.matches(r"a\/b")); assert!(!claims.authorize("room").unwrap().publish.matches("a/b")); // `a\` ends inside that segment, so it is no path the root covers. assert!(matches!( - authorize_claims(r"a\/b", &["**"], &[]).authorize(r"a\"), + authorize_claims(r"a\/b", &[""], &[]).authorize(r"a\"), Err(crate::Error::RootMismatch(_)) )); } #[test] - fn test_authorize_wildcards_rebase_as_a_set() { - // `**/chat` reached at `chat` is both the path itself and deeper `**/chat`. - let claims = authorize_claims("", &["**/chat"], &[]); - let permissions = claims.authorize("chat").unwrap(); - assert_eq!(permissions.subscribe.len(), 2); - assert_eq!(permissions.subscribe, patterns(&["", "**/chat"])); + fn test_authorize_rebases_each_prefix() { + // Reached at `room`, a grant keeps only what lies beneath it, and anything above + // it grants the whole room. + let claims = authorize_claims("", &["room/a", "room/b/c", "lobby"], &[""]); + let permissions = claims.authorize("room").unwrap(); + assert_eq!(permissions.subscribe, prefixes(&["a", "b/c"])); + assert_eq!(permissions.publish, prefixes(&[""])); } } diff --git a/rs/moq-auth/src/client.rs b/rs/moq-auth/src/client.rs index bd40dc55a34..7c29a22d076 100644 --- a/rs/moq-auth/src/client.rs +++ b/rs/moq-auth/src/client.rs @@ -523,12 +523,12 @@ impl Driver { #[cfg(test)] mod tests { use super::*; - use moq_pattern::Patterns; + use moq_pattern::Prefixes; use std::task::Poll; use wiremock::matchers::{method, path}; use wiremock::{Mock, MockServer, Request as Received, ResponseTemplate}; - fn patterns(texts: &[&str]) -> Patterns { + fn prefixes(texts: &[&str]) -> Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } @@ -645,7 +645,7 @@ mod tests { /// The wire carries whole seconds, so a cadence under one second would serialize /// as zero and be refused; these tests run on a one-second cadence. fn grant(expires_in: Option, revalidate: Option) -> Grant { - let mut grant = Grant::new(patterns(&["**"]), Patterns::new()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::new()); grant.expires = expires_in.map(|d| kio::clock::wall() + d); grant.revalidate = revalidate; grant @@ -660,7 +660,7 @@ mod tests { .await; let consumer = client(&server).connect(request()).await.unwrap(); - assert_eq!(consumer.grant().publish.path, patterns(&["**"])); + assert_eq!(consumer.grant().publish.path, prefixes(&[""])); assert_eq!(log.events(), [Event::Connect]); consumer.close("disconnected", Bytes { sent: 7, received: 11 }); @@ -1062,7 +1062,7 @@ mod tests { } // A grant the relay cannot read is the server's fault and final, not an outage. - for body in ["nope", r#"{"publish":["**"],"publish_rate":8000}"#] { + for body in ["nope", r#"{"publish":[""],"publish_rate":8000}"#] { let unreadable = server(Log::default(), move |_| { ResponseTemplate::new(200).set_body_string(body) }) @@ -1297,7 +1297,7 @@ mod tests { #[tokio::test] async fn an_expired_grant_is_refused() { tokio::time::pause(); - let mut grant = Grant::new(patterns(&["**"]), Patterns::new()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::new()); grant.expires = Some(kio::clock::wall() - Duration::from_secs(1)); let script = Script::new(Log::default(), move |_| Some(Ok(grant.clone()))); assert!(matches!(script.connect().await, Err(Error::GrantExpired))); @@ -1362,7 +1362,7 @@ mod tests { assert!(log.revalidates() >= 1, "the outage was answered before expires"); assert_eq!( consumer.grant().publish.path, - patterns(&["**"]), + prefixes(&[""]), "the grant stands through the outage" ); let reason = kio::clock::timeout_at(latest.saturating_add(tolerance), consumer.closed()) diff --git a/rs/moq-auth/src/corpus.rs b/rs/moq-auth/src/corpus.rs index fe68b9a5ba0..e93b01d5a71 100644 --- a/rs/moq-auth/src/corpus.rs +++ b/rs/moq-auth/src/corpus.rs @@ -150,7 +150,7 @@ fn documents() { fn roots() { let corpus = corpus(); let key: Key = serde_json::from_value(corpus.keys["hs256"].clone()).unwrap(); - let all: crate::Patterns = ["**".parse().unwrap()].into_iter().collect(); + let all = crate::Prefixes::all(); for case in corpus.roots { let claims = Claims::default().with_root(&case.root).with_publish(all.clone()); let scope = crate::Scope { diff --git a/rs/moq-auth/src/error.rs b/rs/moq-auth/src/error.rs index 289b78bf784..4dcdf841d97 100644 --- a/rs/moq-auth/src/error.rs +++ b/rs/moq-auth/src/error.rs @@ -112,9 +112,9 @@ pub enum Error { #[error("token is not valid yet")] TokenNotYetValid, - /// A path pattern did not parse. + /// A grant prefix did not parse, such as one holding a wildcard. #[error(transparent)] - Pattern(#[from] moq_pattern::InvalidPattern), + Prefix(#[from] moq_pattern::InvalidPrefix), /// The grant allows nothing, so the session is refused. #[error("grant names nothing; the session is refused")] diff --git a/rs/moq-auth/src/fuzz.rs b/rs/moq-auth/src/fuzz.rs index f2423baf979..ce56c40a8a6 100644 --- a/rs/moq-auth/src/fuzz.rs +++ b/rs/moq-auth/src/fuzz.rs @@ -14,7 +14,7 @@ use std::sync::{Arc, OnceLock}; use serde_json::Value; -use crate::{Claims, Key, KeyOperation, KeySet, Pattern, Rate}; +use crate::{Claims, Key, KeyOperation, KeySet, Prefix, Rate}; /// One fuzz target body: it returns whether the input parsed. pub type Target = fn(&[u8]) -> bool; @@ -42,14 +42,14 @@ fn keys() -> &'static [Key] { /// What the keys signed, as the targets compare it: the claims, encoded. fn claims() -> Vec { - let pattern = |text: &str| text.parse::().expect("a fixed pattern parses"); + let prefix = |text: &str| text.parse::().expect("a fixed prefix parses"); let at = |secs| kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_secs(secs); vec![ - Claims::default().with_root("demo").with_publish([pattern("alice/**")]), + Claims::default().with_root("demo").with_publish([prefix("alice")]), Claims::default() .with_root("room/123") - .with_publish([pattern("alice")]) - .with_subscribe([pattern("**")]) + .with_publish([prefix("alice/cam")]) + .with_subscribe([Prefix::all()]) .with_subscribe_rate(Rate::from_bps(1_000_000).expect("a fixed rate")) .with_expires(at(4_000_000_000)) .with_issued(at(1_700_000_000)), diff --git a/rs/moq-auth/src/grant.rs b/rs/moq-auth/src/grant.rs index 99b24d6559b..688cd51fe46 100644 --- a/rs/moq-auth/src/grant.rs +++ b/rs/moq-auth/src/grant.rs @@ -1,5 +1,5 @@ use kio::clock::Wall; -use moq_pattern::Patterns; +use moq_pattern::Prefixes; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::time::Duration; @@ -16,6 +16,9 @@ use crate::Access; /// A field this reader does not know is refused, as an unknown claim is: a field added /// later may narrow the grant, and reading past it would widen the session. A server /// tells which fields a relay reads from [`Request::relay`](crate::Request::relay). +/// +/// Each prefix is written as the subtree pattern a relay before 0.22 reads (`alice/**`, +/// `**`), and read back alike, as [`Claims`](crate::Claims) are. #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] #[serde(try_from = "crate::wire::Object", into = "crate::wire::Grant")] #[non_exhaustive] @@ -114,7 +117,7 @@ impl<'de> Deserialize<'de> for Link { impl Grant { /// A grant of these patterns, uncapped, with no other restrictions. - pub fn new(publish: impl Into, subscribe: impl Into) -> Self { + pub fn new(publish: impl Into, subscribe: impl Into) -> Self { Self { publish: Access::new(publish), subscribe: Access::new(subscribe), @@ -163,14 +166,14 @@ impl Grant { mod tests { use super::*; - fn patterns(texts: &[&str]) -> Patterns { + fn prefixes(texts: &[&str]) -> Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } fn full() -> Grant { Grant { - publish: Access::new(patterns(&["alice/**"])).with_rate(crate::Rate::from_bps(8_000_000).unwrap()), - subscribe: Access::new(patterns(&["**"])), + publish: Access::new(prefixes(&["alice"])).with_rate(crate::Rate::from_bps(8_000_000).unwrap()), + subscribe: Access::new(prefixes(&[""])), root: Some("pid/room".into()), mounts: BTreeMap::new(), expires: Some(Wall::UNIX_EPOCH + Duration::from_secs(4_102_444_800)), @@ -221,10 +224,10 @@ mod tests { #[test] fn an_unknown_field_is_refused() { for json in [ - r#"{"publish":["**"],"publish_rate":8000}"#, - r#"{"publish":["**"],"upstream":true}"#, - r#"{"publish":["**"],"future":{}}"#, - r#"{"publish":["**"],"link":"downstream"}"#, + r#"{"publish":[""],"publish_rate":8000}"#, + r#"{"publish":[""],"upstream":true}"#, + r#"{"publish":[""],"future":{}}"#, + r#"{"publish":[""],"link":"downstream"}"#, ] { assert!(serde_json::from_str::(json).is_err(), "{json}"); } @@ -235,17 +238,17 @@ mod tests { #[test] fn peer_is_an_alias_for_a_link() { let read = |json: &str| serde_json::from_str::(json).map(|grant| grant.link); - assert_eq!(read(r#"{"publish":["**"],"peer":true}"#).unwrap(), Some(Link::Peer)); - assert_eq!(read(r#"{"publish":["**"],"peer":false}"#).unwrap(), None); - assert_eq!(read(r#"{"publish":["**"],"link":"peer"}"#).unwrap(), Some(Link::Peer)); + assert_eq!(read(r#"{"publish":[""],"peer":true}"#).unwrap(), Some(Link::Peer)); + assert_eq!(read(r#"{"publish":[""],"peer":false}"#).unwrap(), None); + assert_eq!(read(r#"{"publish":[""],"link":"peer"}"#).unwrap(), Some(Link::Peer)); assert_eq!( - read(r#"{"publish":["**"],"link":"upstream","peer":true}"#).unwrap(), + read(r#"{"publish":[""],"link":"upstream","peer":true}"#).unwrap(), Some(Link::Upstream) ); - assert!(read(r#"{"publish":["**"],"link":"upstream","peer":false}"#).is_err()); - assert!(read(r#"{"publish":["**"],"link":null}"#).is_err()); + assert!(read(r#"{"publish":[""],"link":"upstream","peer":false}"#).is_err()); + assert!(read(r#"{"publish":[""],"link":null}"#).is_err()); - let mut grant = Grant::new(patterns(&["**"]), Patterns::default()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::default()); grant.link = Some(Link::Peer); assert_eq!( serde_json::to_string(&grant).unwrap(), @@ -255,7 +258,7 @@ mod tests { #[test] fn mounts_round_trip_as_an_object() { - let mut grant = Grant::new(Patterns::default(), patterns(&["**"])); + let mut grant = Grant::new(Prefixes::default(), prefixes(&[""])); grant.mounts.insert(".svc".into(), ".svc/pid".into()); let json = serde_json::to_string(&grant).unwrap(); assert_eq!(json, r#"{"subscribe":["**"],"mounts":{".svc":".svc/pid"}}"#); @@ -264,9 +267,20 @@ mod tests { #[test] fn empty_fields_are_omitted_and_defaulted() { - let grant = Grant::new(patterns(&["**"]), Patterns::default()); + let grant = Grant::new(prefixes(&[""]), Prefixes::default()); assert_eq!(serde_json::to_string(&grant).unwrap(), r#"{"publish":["**"]}"#); - assert_eq!(serde_json::from_str::(r#"{"publish":["**"]}"#).unwrap(), grant); + assert_eq!(serde_json::from_str::(r#"{"publish":[""]}"#).unwrap(), grant); + } + + /// TEMPORARY MIGRATION (see `wire::alias`): a released auth server's subtree patterns + /// read as their prefixes, and any other wildcard refuses the grant. + #[test] + fn subtree_patterns_read_as_prefixes() { + let grant: Grant = serde_json::from_str(r#"{"publish":["alice/**"],"subscribe":["**"]}"#).unwrap(); + assert_eq!(grant, Grant::new(prefixes(&["alice"]), prefixes(&[""]))); + for json in [r#"{"publish":["alice/*"]}"#, r#"{"subscribe":["**/chat"]}"#] { + assert!(serde_json::from_str::(json).is_err(), "{json}"); + } } #[kio::test] @@ -279,7 +293,7 @@ mod tests { "a cap on no paths admits nothing" ); - let mut grant = Grant::new(patterns(&["**"]), Patterns::default()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::default()); grant.validate().unwrap(); grant.revalidate = Some(Duration::from_secs(1)); @@ -308,14 +322,14 @@ mod tests { #[test] fn validate_refuses_an_invalid_stats_label() { - let mut grant = Grant::new(patterns(&["**"]), Patterns::default()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::default()); grant.label = Some("project/*".into()); assert!(matches!(grant.validate(), Err(crate::Error::InvalidLabel(_)))); } #[test] fn validate_refuses_a_tier_a_metrics_label_would_escape() { - let mut grant = Grant::new(patterns(&["**"]), Patterns::default()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::default()); for tier in ["a\"b", "a\\b", "a\nb"] { grant.tier = Some(tier.into()); assert!( @@ -333,7 +347,7 @@ mod tests { async fn deadline_is_the_exact_expiry() { let start = kio::clock::now(); let now = kio::clock::wall(); - let mut grant = Grant::new(patterns(&["**"]), Patterns::default()); + let mut grant = Grant::new(prefixes(&[""]), Prefixes::default()); assert_eq!(grant.deadline(), None); grant.expires = Some(now + Duration::from_secs(10)); @@ -352,7 +366,7 @@ mod tests { /// not overflow the instant. #[kio::test] async fn the_last_expiry_fits_the_clock() { - let mut last = Grant::new(patterns(&["**"]), Patterns::default()); + let mut last = Grant::new(prefixes(&[""]), Prefixes::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); diff --git a/rs/moq-auth/src/key.rs b/rs/moq-auth/src/key.rs index a4974863ddb..cb200078329 100644 --- a/rs/moq-auth/src/key.rs +++ b/rs/moq-auth/src/key.rs @@ -766,7 +766,7 @@ fn decode_base64url(s: &str) -> Result, String> { mod tests { use super::*; - fn patterns(texts: &[&str]) -> crate::Patterns { + fn prefixes(texts: &[&str]) -> crate::Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } use kio::clock::Wall; @@ -786,8 +786,8 @@ mod tests { fn create_test_claims() -> Claims { Claims { root: "test-path".to_string(), - publish: patterns(&["test-pub/**"]).into(), - subscribe: patterns(&["test-sub/**"]).into(), + publish: prefixes(&["test-pub"]).into(), + subscribe: prefixes(&["test-sub"]).into(), expires: Some(kio::clock::wall() + Duration::from_secs(3600)), issued: Some(kio::clock::wall()), not_before: None, @@ -941,18 +941,18 @@ mod tests { .clone() .with_scope(crate::Scope { root: "test-path".into(), - publish: patterns(&["allowed/**"]), - subscribe: patterns(&[]), + publish: prefixes(&["allowed"]), + subscribe: prefixes(&[]), }) .unwrap(); let allowed = Claims { root: "test-path".into(), - publish: patterns(&["allowed/room/**"]).into(), + publish: prefixes(&["allowed/room"]).into(), ..Default::default() }; let denied = Claims { root: "test-path".into(), - publish: patterns(&["other/**"]).into(), + publish: prefixes(&["other"]).into(), ..Default::default() }; @@ -970,13 +970,13 @@ mod tests { let key = create_test_key(); let claims = |root: &str| Claims { root: root.into(), - publish: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), ..Default::default() }; let scope = |root: &str| crate::Scope { root: root.into(), - publish: patterns(&["**"]), - subscribe: patterns(&[]), + publish: prefixes(&[""]), + subscribe: prefixes(&[]), }; for root in ["/room", "room/", "a//b", r"a\b"] { @@ -990,10 +990,10 @@ mod tests { ); } - let legacy = sign_raw(&key, serde_json::json!({"root": "/room/", "publish": ["**"]})); + let legacy = sign_raw(&key, serde_json::json!({"root": "/room/", "publish": [""]})); assert_eq!( key.verify(&legacy).unwrap().authorize("room").unwrap().publish, - patterns(&["**"]) + prefixes(&[""]) ); } @@ -1003,7 +1003,7 @@ mod tests { fn test_key_derived_material_never_stale() { let claims = Claims { root: "test-path".into(), - publish: patterns(&["test-pub/**"]).into(), + publish: prefixes(&["test-pub"]).into(), ..Default::default() }; @@ -1066,8 +1066,8 @@ mod tests { let key = create_test_key(); let invalid_claims = Claims { root: "test-path".to_string(), - publish: patterns(&[]).into(), - subscribe: patterns(&[]).into(), + publish: prefixes(&[]).into(), + subscribe: prefixes(&[]).into(), expires: None, issued: None, not_before: None, @@ -1148,15 +1148,15 @@ mod tests { let token = sign_raw( &key, - serde_json::json!({"root": "room", "publish": ["**"], "iss": "api", "sub": "alice", "jti": "1", "iat": now}), + serde_json::json!({"root": "room", "publish": [""], "iss": "api", "sub": "alice", "jti": "1", "iat": now}), ); assert_eq!(key.verify(&token).unwrap().root, "room"); for (claim, payload) in [ - ("rooot", serde_json::json!({"rooot": "room/123", "publish": ["**"]})), + ("rooot", serde_json::json!({"rooot": "room/123", "publish": [""]})), ( "user_id", - serde_json::json!({"root": "room", "publish": ["**"], "user_id": 7}), + serde_json::json!({"root": "room", "publish": [""], "user_id": 7}), ), ( "cluster", @@ -1170,7 +1170,7 @@ mod tests { // No audience is configured, so there is nothing to check one against. let token = sign_raw( &key, - serde_json::json!({"root": "room", "publish": ["**"], "aud": "relay"}), + serde_json::json!({"root": "room", "publish": [""], "aud": "relay"}), ); assert!(key.verify(&token).is_err()); } @@ -1185,7 +1185,7 @@ mod tests { .as_secs() as i64; sign_raw( &key, - serde_json::json!({"root": "room", "publish": ["**"], "nbf": now + offset}), + serde_json::json!({"root": "room", "publish": [""], "nbf": now + offset}), ) }; assert!(key.verify(&at(-60)).is_ok()); @@ -1245,8 +1245,8 @@ mod tests { let key = create_test_key(); let claims = Claims { root: "test-path".to_string(), - publish: patterns(&["**"]).into(), - subscribe: patterns(&["**"]).into(), + publish: prefixes(&[""]).into(), + subscribe: prefixes(&[""]).into(), expires: None, issued: None, not_before: None, @@ -1266,8 +1266,8 @@ mod tests { let key = create_test_key(); let original_claims = Claims { root: "test-path".to_string(), - publish: patterns(&["test-pub/**"]).into(), - subscribe: patterns(&["test-sub/**"]).into(), + publish: prefixes(&["test-pub"]).into(), + subscribe: prefixes(&["test-sub"]).into(), expires: Some(kio::clock::wall() + Duration::from_secs(3600)), issued: Some(kio::clock::wall()), not_before: None, @@ -1862,22 +1862,22 @@ mod tests { // A key minted by moq-token-cli with `--root demo --put room`. let json = r#"{"kty":"oct","alg":"HS256","key_ops":["sign","verify"],"k":"Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68","scope":{"root":"demo","put":["room"]}}"#; let key = Key::from_str(json).unwrap(); - assert_eq!(key.scope.as_ref().unwrap().publish, patterns(&["room/**"])); + assert_eq!(key.scope.as_ref().unwrap().publish, prefixes(&["room"])); let inside = Claims { root: "demo/room".into(), - publish: patterns(&["alice"]).into(), + publish: prefixes(&["alice"]).into(), ..Default::default() }; let outside = Claims { root: "demo".into(), - publish: patterns(&["lobby/**"]).into(), + publish: prefixes(&["lobby"]).into(), ..Default::default() }; assert!(key.verify(&key.sign(&inside).unwrap()).is_ok()); assert!(matches!(key.sign(&outside), Err(crate::Error::ScopeExceeded))); - // Written back as the pattern it reads as. + // Written back as the subtree pattern every release reads it as (`wire::alias`). assert!( serde_json::to_string(&key) .unwrap() diff --git a/rs/moq-auth/src/lease.rs b/rs/moq-auth/src/lease.rs index 80165b77eba..011108a7876 100644 --- a/rs/moq-auth/src/lease.rs +++ b/rs/moq-auth/src/lease.rs @@ -457,8 +457,8 @@ mod tests { fn grant(publish: &str) -> Grant { Grant::new( - publish.parse::().unwrap(), - moq_pattern::Patterns::new(), + publish.parse::().unwrap(), + moq_pattern::Prefixes::new(), ) } @@ -468,19 +468,19 @@ mod tests { #[test] fn update_wakes_the_consumer_once_per_change() { - let (producer, mut consumer) = Producer::new(grant("a/**")); - assert_eq!(consumer.grant(), grant("a/**")); + let (producer, mut consumer) = Producer::new(grant("a")); + assert_eq!(consumer.grant(), grant("a")); assert!(poll(consumer.changed()).is_pending()); - producer.update(grant("b/**")); - assert_eq!(poll(consumer.changed()), Poll::Ready(Ok(grant("b/**")))); - assert_eq!(consumer.grant(), grant("b/**")); + producer.update(grant("b")); + assert_eq!(poll(consumer.changed()), Poll::Ready(Ok(grant("b")))); + assert_eq!(consumer.grant(), grant("b")); assert!(poll(consumer.changed()).is_pending()); } #[test] fn revoke_reaches_the_consumer() { - let (producer, mut consumer) = Producer::new(grant("a/**")); + let (producer, mut consumer) = Producer::new(grant("a")); assert!(poll(consumer.closed()).is_pending()); producer.revoke(Reason::Refused); @@ -490,7 +490,7 @@ mod tests { #[test] fn the_session_close_hands_over_byte_totals() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); consumer.close("done", Bytes { sent: 3, received: 5 }); assert_eq!( poll(producer.closed()), @@ -500,7 +500,7 @@ mod tests { #[test] fn consumer_clones_hold_the_lease_until_the_last_drop() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); let retained = consumer.clone(); drop(consumer); assert!(poll(producer.closed()).is_pending()); @@ -513,7 +513,7 @@ mod tests { #[test] fn closing_one_consumer_ends_every_clone() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); let retained = consumer.clone(); consumer.close(Reason::Shutdown, Bytes { sent: 3, received: 5 }); assert_eq!(poll(retained.closed()), Poll::Ready(Reason::Shutdown)); @@ -525,14 +525,14 @@ mod tests { #[test] fn dropping_the_producer_revokes() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); drop(producer); assert_eq!(poll(consumer.closed()), Poll::Ready(Reason::Dropped)); } #[test] fn dropping_the_consumer_reports_zero_bytes() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); drop(consumer); assert_eq!( poll(producer.closed()), @@ -542,7 +542,7 @@ mod tests { #[test] fn the_session_close_reaches_the_producer_and_the_first_reason_wins() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); assert!(poll(producer.closed()).is_pending()); let recorded = consumer.close("disconnected", Bytes { sent: 1, received: 2 }); @@ -558,12 +558,12 @@ mod tests { #[test] fn a_fixed_lease_only_ends_by_the_holder() { - let mut consumer = Consumer::fixed(grant("a/**")); - assert_eq!(consumer.grant(), grant("a/**")); + let mut consumer = Consumer::fixed(grant("a")); + assert_eq!(consumer.grant(), grant("a")); assert!(poll(consumer.changed()).is_pending()); assert!(poll(consumer.closed()).is_pending()); consumer.revalidate(); - assert_eq!(consumer.grant(), grant("a/**")); + assert_eq!(consumer.grant(), grant("a")); assert!(poll(consumer.changed()).is_pending()); assert!(poll(consumer.closed()).is_pending()); assert_eq!(consumer.close("done", Bytes::default()), Reason::Session("done".into())); @@ -571,7 +571,7 @@ mod tests { #[test] fn n_nudges_wake_the_producer_once() { - let (producer, consumer) = Producer::new(grant("a/**")); + let (producer, consumer) = Producer::new(grant("a")); assert!(poll(producer.revalidate_requested()).is_pending()); for _ in 0..8 { @@ -588,7 +588,7 @@ mod tests { #[tokio::test] async fn due_follows_cadence_nudges_and_backoff() { tokio::time::pause(); - let mut grant = grant("a/**"); + let mut grant = grant("a"); grant.revalidate = Some(Duration::from_secs(10)); grant.expires = Some(kio::clock::wall() + Duration::from_secs(60)); let (producer, consumer) = Producer::new(grant.clone()); @@ -625,7 +625,7 @@ mod tests { #[tokio::test] async fn due_reschedules_an_outstanding_wait_after_update() { tokio::time::pause(); - let mut grant = grant("a/**"); + let mut grant = grant("a"); grant.revalidate = Some(Duration::from_secs(30)); grant.expires = Some(kio::clock::wall() + Duration::from_secs(3600)); let (producer, _consumer) = Producer::new(grant.clone()); @@ -645,7 +645,7 @@ mod tests { #[tokio::test] async fn backoff_grows_and_stays_bounded() { tokio::time::pause(); - let mut grant = grant("a/**"); + let mut grant = grant("a"); grant.revalidate = Some(Duration::from_secs(30)); grant.expires = Some(kio::clock::wall() + Duration::from_secs(3600)); let (producer, _consumer) = Producer::new(grant); @@ -662,7 +662,7 @@ mod tests { #[tokio::test] async fn an_unschedulable_cadence_never_rechecks() { tokio::time::pause(); - let mut grant = grant("a/**"); + let mut grant = grant("a"); grant.revalidate = Some(Duration::MAX); let (producer, _consumer) = Producer::new(grant); tokio::time::advance(Duration::from_secs(3600)).await; @@ -677,7 +677,7 @@ mod tests { #[tokio::test] async fn due_expires_even_without_a_recheck() { tokio::time::pause(); - let mut grant = grant("a/**"); + let mut grant = grant("a"); grant.expires = Some(kio::clock::wall() - Duration::from_secs(4)); let (producer, _consumer) = Producer::new(grant); let task = tokio::spawn(async move { producer.due(&mut DueTimers::default()).await }); diff --git a/rs/moq-auth/src/lib.rs b/rs/moq-auth/src/lib.rs index c688dc5031f..c2ad4f78a95 100644 --- a/rs/moq-auth/src/lib.rs +++ b/rs/moq-auth/src/lib.rs @@ -13,9 +13,13 @@ //! - [`Claims`], [`Key`], and [`KeySet`]: the JWT a client presents in its query, with //! the keys that sign and verify it. `moq auth generate|sign|verify` is the CLI. //! -//! Grants and claims name paths with [`Pattern`]s from `moq-pattern`, re-exported here: -//! `foo` is one broadcast, `foo/**` is a subtree, `**` is everything. Each role is an -//! [`Access`]: its patterns, and optionally a bitrate cap, a [`Rate`]. +//! Grants and claims name paths with [`Prefixes`] from `moq-pattern`, re-exported here: +//! `foo` is `foo` and everything beneath it, and the empty prefix is everything. A grant +//! holds no wildcard: a [`Prefix`] refuses `foo/**`, `**`, and `*`. Until the next two +//! version migrations, a token, key scope, or [`Grant`] is written with each prefix as +//! the subtree pattern releases before 0.22 read, `foo/**` or `**`, and reads one back +//! as its prefix; any other wildcard there is refused. +//! Each role is an [`Access`]: its prefixes, and optionally a bitrate cap, a [`Rate`]. //! //! The lease `poll_*` methods are always available. Enable the `async` feature for their //! generated async wrappers (`changed`, `closed`, and `revalidate_requested`) and @@ -64,7 +68,7 @@ pub use error::*; pub use grant::*; pub use key::*; pub use key_id::*; -pub use moq_pattern::{InvalidPattern, Pattern, Patterns, Segment, Specificity}; +pub use moq_pattern::{InvalidPattern, InvalidPrefix, Prefix, Prefixes}; pub use rate::*; pub use request::*; pub use set::*; diff --git a/rs/moq-auth/src/rate.rs b/rs/moq-auth/src/rate.rs index d8a6f86a690..f0e2bcba4dd 100644 --- a/rs/moq-auth/src/rate.rs +++ b/rs/moq-auth/src/rate.rs @@ -94,8 +94,8 @@ mod tests { fn caps_survive_signing_but_scopes_refuse_them() { let wire = serde_json::json!({ "root": "room", - "publish": {"path": ["alice/**"], "rate": 8000}, - "subscribe": {"path": ["bob/**"], "rate": 16000}, + "publish": {"path": ["alice"], "rate": 8000}, + "subscribe": {"path": ["bob"], "rate": 16000}, }); assert!(serde_json::from_value::(wire.clone()).is_err()); let claims: crate::Claims = serde_json::from_value(wire).unwrap(); @@ -103,8 +103,7 @@ mod tests { let claims = key.verify(&key.sign(&claims).unwrap()).unwrap(); assert_eq!(claims.publish.rate, Some(Rate::from_bps(8000).unwrap())); assert_eq!(claims.subscribe.rate, Some(Rate::from_bps(16000).unwrap())); - let scope: crate::Scope = - serde_json::from_str(r#"{"root":"room","publish":["**"],"subscribe":["**"]}"#).unwrap(); + let scope: crate::Scope = serde_json::from_str(r#"{"root":"room","publish":[""],"subscribe":[""]}"#).unwrap(); assert!(scope.allows(&claims)); } } diff --git a/rs/moq-auth/src/serve.rs b/rs/moq-auth/src/serve.rs index 5b82c0b924a..69e20f01a88 100644 --- a/rs/moq-auth/src/serve.rs +++ b/rs/moq-auth/src/serve.rs @@ -143,8 +143,8 @@ pub struct Rule { /// A malformed or expired token is a refusal, never a fall through. /// /// The anonymous and mTLS rules name what they grant from `/`, like a token with an -/// empty root: `anon/**` admits a session dialed at `/`, `/anon`, or `/anon/room`, -/// each scoped to `anon/`, and refuses one dialed at `/other`. +/// empty root: `anon` admits a session dialed at `/`, `/anon`, or `/anon/room`, +/// each scoped to `anon`, and refuses one dialed at `/other`. /// /// The JWT is the `jwt` query parameter or a SETUP token of type 0 /// ([`Token::OUT_OF_BAND`]), verified alike. A credential attached in band is only the @@ -819,14 +819,14 @@ async fn handle(State(server): State, Json(request): Json) -> R mod tests { use super::*; use crate::{Algorithm, Bytes, Claims, Client, Error, Peer, Transport, lease::Reason}; - use moq_pattern::Patterns; + use moq_pattern::Prefixes; - fn patterns(texts: &[&str]) -> Patterns { + fn prefixes(texts: &[&str]) -> Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } fn rules(publish: &[&str], subscribe: &[&str]) -> Permissions { - Permissions::new(patterns(publish), patterns(subscribe)) + Permissions::new(prefixes(publish), prefixes(subscribe)) } fn request(path: &str) -> Request { @@ -857,7 +857,7 @@ mod tests { let (dir, key) = key_dir(); let root = "root\nforged-root\r\x1b[31m"; - let jwt = sign(&key, root, &["**"], &[], None); + let jwt = sign(&key, root, &[""], &[], None); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() @@ -892,7 +892,7 @@ mod tests { #[kio::test] async fn default_expiry_uses_the_ambient_wall() { let policy = Policy { - public: rules(&["**"], &[]), + public: rules(&[""], &[]), expires: Some(Duration::from_secs(30)), ..Default::default() }; @@ -940,8 +940,8 @@ mod tests { fn rule(publish: &[&str], subscribe: &[&str]) -> Rule { Rule { - publish: Some(patterns(publish).into()), - subscribe: Some(patterns(subscribe).into()), + publish: Some(prefixes(publish).into()), + subscribe: Some(prefixes(subscribe).into()), ..Default::default() } } @@ -964,8 +964,8 @@ mod tests { fn sign(key: &Key, root: &str, publish: &[&str], subscribe: &[&str], expires: Option) -> String { let claims = Claims::default() .with_root(root) - .with_publish(patterns(publish)) - .with_subscribe(patterns(subscribe)) + .with_publish(prefixes(publish)) + .with_subscribe(prefixes(subscribe)) .with_expires(expires); key.sign(&claims).unwrap() } @@ -1011,11 +1011,11 @@ mod tests { .as_secs() + 600; let exp = kio::clock::Wall::UNIX_EPOCH + Duration::from_secs(secs); - let jwt = sign(&key, "demo/room", &["alice/**"], &["**"], Some(exp)); + let jwt = sign(&key, "demo/room", &["alice"], &[""], Some(exp)); let grant = policy.decide(&with_token(request("/demo/room/"), &jwt)).await.unwrap(); - assert_eq!(grant.publish.path, patterns(&["alice/**"])); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.publish.path, prefixes(&["alice"])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); assert_eq!(grant.expires, Some(exp)); assert_eq!(grant.revalidate, None); assert_eq!(grant.tier.as_deref(), Some("gold")); @@ -1025,7 +1025,7 @@ mod tests { #[tokio::test] async fn the_secret_guards_the_route() { let server = Server::new(Policy { - public: rules(&["**"], &[]), + public: rules(&[""], &[]), ..Default::default() }) .unwrap() @@ -1085,7 +1085,7 @@ mod tests { expires: Some(Duration::from_secs(3600)), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); let before = kio::clock::wall(); let grant = policy.decide(&with_token(request("/demo"), &jwt)).await.unwrap(); let expires = grant.expires.unwrap(); @@ -1100,15 +1100,15 @@ mod tests { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() }; - let jwt = sign(&key, "demo", &["alice/**"], &["**"], None); + let jwt = sign(&key, "demo", &["alice"], &[""], None); let grant = policy.decide(&with_token(request("/demo/alice"), &jwt)).await.unwrap(); - assert_eq!(grant.publish.path, patterns(&["**"])); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.publish.path, prefixes(&[""])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); let err = policy.decide(&with_token(request("/other"), &jwt)).await.unwrap_err(); assert!(matches!(err, Denial::RootMismatch { .. }), "{err}"); - let jwt = sign(&key, "", &[], &["demo/**"], None); + let jwt = sign(&key, "", &[], &["demo"], None); let err = policy.decide(&with_token(request("/other"), &jwt)).await.unwrap_err(); assert!(matches!(err, Denial::NoAccess { .. }), "{err}"); } @@ -1118,7 +1118,7 @@ mod tests { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }; @@ -1126,7 +1126,7 @@ mod tests { let jwt = sign( &key, "demo", - &["**"], + &[""], &[], Some(kio::clock::wall() - Duration::from_secs(1)), ); @@ -1135,19 +1135,19 @@ mod tests { // Signed by a stranger. let stranger = Key::generate(Algorithm::HS256, Some(KeyId::decode("kid1").unwrap())).unwrap(); - let jwt = sign(&stranger, "demo", &["**"], &[], None); + let jwt = sign(&stranger, "demo", &[""], &[], None); let err = policy.decide(&with_token(request("/demo"), &jwt)).await.unwrap_err(); assert!(matches!(err, Denial::InvalidToken(_)), "{err}"); // Unknown kid, and no kid at all. let other = Key::generate(Algorithm::HS256, Some(KeyId::decode("kid2").unwrap())).unwrap(); - let jwt = sign(&other, "demo", &["**"], &[], None); + let jwt = sign(&other, "demo", &[""], &[], None); assert_eq!( policy.decide(&with_token(request("/demo"), &jwt)).await.unwrap_err(), Denial::UnknownKey ); let bare = Key::generate(Algorithm::HS256, None).unwrap(); - let jwt = sign(&bare, "demo", &["**"], &[], None); + let jwt = sign(&bare, "demo", &[""], &[], None); assert_eq!( policy.decide(&with_token(request("/demo"), &jwt)).await.unwrap_err(), Denial::MissingKeyId @@ -1157,7 +1157,7 @@ mod tests { let err = policy.decide(&with_token(request("/demo"), "nope")).await.unwrap_err(); assert!(matches!(err, Denial::MalformedToken(_)), "{err}"); let keyless = Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }; assert_eq!( @@ -1179,7 +1179,7 @@ mod tests { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() }; - let old = with_token(request("/demo"), &sign(&key, "demo", &["**"], &[], None)); + 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. @@ -1195,7 +1195,7 @@ mod tests { 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)); + let new = with_token(request("/demo"), &sign(&rotated, "demo", &[""], &[], None)); policy.decide(&new).await.unwrap(); // A removed file forgets its key. @@ -1213,13 +1213,13 @@ mod tests { keys: Some(Keys::File(path)), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); assert!(policy.decide(&with_token(request("/demo"), &jwt)).await.is_ok()); let mut wrong = key.export(); wrong.kid = Some(KeyId::decode("unexpected").unwrap()); let wrong = wrong.import().unwrap(); - let mismatched = sign(&wrong, "demo", &["**"], &[], None); + let mismatched = sign(&wrong, "demo", &[""], &[], None); assert_eq!( policy .decide(&with_token(request("/demo"), &mismatched)) @@ -1243,7 +1243,7 @@ mod tests { keys: Some(Keys::Set(path)), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + 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. @@ -1257,7 +1257,7 @@ mod tests { let mut wrong = key.export(); wrong.kid = Some(KeyId::decode("kid2").unwrap()); let wrong = wrong.import().unwrap(); - let mismatched = sign(&wrong, "demo", &["**"], &[], None); + let mismatched = sign(&wrong, "demo", &[""], &[], None); assert!(matches!( policy.decide(&with_token(request("/demo"), &mismatched)).await, Err(Denial::InvalidToken(_)) @@ -1267,7 +1267,7 @@ mod tests { #[tokio::test] async fn gateway_transports_count_toward_session_limits() { let server = Server::new(Policy { - public: rules(&["**"], &[]), + public: rules(&[""], &[]), limits: Limits { remote: Some(1), ..Default::default() @@ -1310,16 +1310,16 @@ mod tests { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() }; - let jwt = sign(&key, "demo", &["alice/**"], &[], None); + let jwt = sign(&key, "demo", &["alice"], &[], None); let grant = policy .decide(&with_setup_token(request("/demo"), Token::OUT_OF_BAND, &jwt)) .await .unwrap(); - assert_eq!(grant.publish.path, patterns(&["alice/**"])); + assert_eq!(grant.publish.path, prefixes(&["alice"])); // And refused like one: a stranger's key never falls through to public. let stranger = Key::generate(Algorithm::HS256, Some(KeyId::decode("kid1").unwrap())).unwrap(); - let forged = sign(&stranger, "demo", &["**"], &[], None); + let forged = sign(&stranger, "demo", &[""], &[], None); let err = policy .decide(&with_setup_token(request("/demo"), Token::OUT_OF_BAND, &forged)) .await @@ -1341,10 +1341,10 @@ mod tests { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); let err = policy .decide(&with_setup_token(request("/demo"), Token::CAT, &jwt)) .await @@ -1364,10 +1364,10 @@ mod tests { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); let request = with_setup_token(with_token(request("/demo"), &jwt), Token::OUT_OF_BAND, &jwt); let grant = policy.decide(&request).await.unwrap(); - assert_eq!(grant.publish.path, patterns(&["**"])); + assert_eq!(grant.publish.path, prefixes(&[""])); } /// An attachment is its `token` alone: the dialed query's `jwt` is the connection's, so @@ -1382,13 +1382,13 @@ mod tests { .unwrap(); // The connection dialed with a credential that has since expired. let expired = Some(kio::clock::Wall::UNIX_EPOCH + Duration::from_secs(1)); - let dialed = sign(&key, "demo", &["**"], &[], expired); - let attached = sign(&key, "demo", &[], &["**"], None); + let dialed = sign(&key, "demo", &[""], &[], expired); + let attached = sign(&key, "demo", &[], &[""], None); let mut attach = with_setup_token(with_token(request("/demo"), &dialed), Token::OUT_OF_BAND, &attached); attach.auth = Some("attachment".into()); attach.event = Event::Attach; let grant = server.answer(&attach).await.unwrap().unwrap(); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); assert!( grant.publish.path.is_empty(), "the dialed query's grant leaked into the attachment" @@ -1422,13 +1422,13 @@ mod tests { ..Default::default() }) .unwrap(); - let attached = sign(&key, "demo", &[], &["**"], None); + let attached = sign(&key, "demo", &[], &[""], None); let mut attach = with_token(request("/demo"), &attached); attach.auth = Some("attachment".into()); for event in [Event::Attach, Event::Revalidate] { attach.event = event; let grant = server.answer(&attach).await.unwrap().unwrap(); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); assert!(grant.publish.path.is_empty()); } } @@ -1441,8 +1441,8 @@ mod tests { keys: Some(Keys::Dir(dir.path().into())), ..Default::default() }; - let setup = sign(&key, "demo", &["**"], &[], None); - let query = sign(&key, "demo", &[], &["**"], None); + let setup = sign(&key, "demo", &[""], &[], None); + let query = sign(&key, "demo", &[], &[""], None); let different = with_setup_token(with_token(request("/demo"), &query), Token::OUT_OF_BAND, &setup); assert_eq!(policy.decide(&different).await.unwrap_err(), Denial::TwoTokens); @@ -1457,10 +1457,10 @@ mod tests { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); let mut request = request("/demo"); request.query = Some(format!("a=1&jwt={jwt}")); @@ -1497,12 +1497,12 @@ mod tests { ); let policy = Policy { - mtls: rules(&["**"], &["**"]), + mtls: rules(&[""], &[""]), ..Default::default() }; let not_after = kio::clock::wall() + Duration::from_secs(86400 * 30); let grant = policy.decide(&with_peer(request("/"), Some(not_after))).await.unwrap(); - assert_eq!(grant.publish.path, patterns(&["**"])); + assert_eq!(grant.publish.path, prefixes(&[""])); assert_eq!(grant.expires, Some(not_after)); // A certificate without a bound gets none by default, like 0.14. @@ -1520,7 +1520,7 @@ mod tests { #[tokio::test] async fn certificate_rules_scope_by_the_nearest_named_certificate() { let policy = Policy { - mtls: rules(&["shared/**"], &[]), + mtls: rules(&["shared"], &[]), mtls_link: Some(Link::Peer), tier: Some("base".into()), certificates: certificates([ @@ -1530,16 +1530,15 @@ mod tests { tier: Some("affiliate".into()), link: Some(RuleLink::None), publish: Some( - Access::new(patterns(&["affiliate/**"])) - .with_rate(crate::Rate::from_bps(1_000_000).unwrap()), + Access::new(prefixes(&["affiliate"])).with_rate(crate::Rate::from_bps(1_000_000).unwrap()), ), - ..rule(&[], &["affiliate/**"]) + ..rule(&[], &["affiliate"]) }, ), ( "vip", Rule { - subscribe: Some(patterns(&["**"]).into()), + subscribe: Some(prefixes(&[""]).into()), ..Default::default() }, ), @@ -1552,8 +1551,8 @@ mod tests { .decide(&with_chain(request("/"), "alice", &["affiliate-ca", "root"])) .await .unwrap(); - assert_eq!(grant.publish.path, patterns(&["affiliate/**"])); - assert_eq!(grant.subscribe.path, patterns(&["affiliate/**"])); + assert_eq!(grant.publish.path, prefixes(&["affiliate"])); + assert_eq!(grant.subscribe.path, prefixes(&["affiliate"])); assert_eq!(grant.tier.as_deref(), Some("affiliate")); assert_eq!(grant.publish.rate, crate::Rate::from_bps(1_000_000).ok()); assert_eq!(grant.link, None, "the rule drops the shared link role"); @@ -1564,8 +1563,8 @@ mod tests { .decide(&with_chain(request("/"), "vip", &["affiliate-ca", "root"])) .await .unwrap(); - assert_eq!(grant.publish.path, patterns(&["shared/**"])); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.publish.path, prefixes(&["shared"])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); assert_eq!(grant.tier.as_deref(), Some("base")); assert_eq!(grant.publish.rate, None); assert_eq!(grant.link, Some(Link::Peer)); @@ -1575,14 +1574,14 @@ mod tests { .decide(&with_chain(request("/"), "bob", &["other-ca"])) .await .unwrap(); - assert_eq!(grant.publish.path, patterns(&["shared/**"])); + assert_eq!(grant.publish.path, prefixes(&["shared"])); assert!(grant.subscribe.path.is_empty()); // A relay that predates the chain reports only the leaf, which a leaf rule matches. let mut legacy = with_chain(request("/"), "vip", &[]); legacy.tls.as_mut().unwrap().chain.clear(); let grant = policy.decide(&legacy).await.unwrap(); - assert_eq!(grant.subscribe.path, patterns(&["**"])); + assert_eq!(grant.subscribe.path, prefixes(&[""])); // A rule that grants nothing refuses, even where the shared grant would admit. let revoked = Policy { @@ -1607,26 +1606,26 @@ mod tests { #[tokio::test] async fn an_update_applies_at_the_next_recheck_and_a_bad_one_is_refused() { let provisioned = Policy { - certificates: certificates([("alice", rule(&["**"], &["**"]))]), + certificates: certificates([("alice", rule(&[""], &[""]))]), ..Default::default() }; let server = Server::new(provisioned.clone()).unwrap(); let mut session = with_chain(request("/"), "alice", &["ca"]); assert_eq!( server.answer(&session).await.unwrap().unwrap().publish.path, - patterns(&["**"]) + prefixes(&[""]) ); session.event = Event::Revalidate; server .update(Policy { - certificates: certificates([("alice", rule(&["alice/**"], &[]))]), + certificates: certificates([("alice", rule(&["alice"], &[]))]), ..Default::default() }) .unwrap(); assert_eq!( server.answer(&session).await.unwrap().unwrap().publish.path, - patterns(&["alice/**"]) + prefixes(&["alice"]) ); let invalid = Policy { @@ -1634,7 +1633,7 @@ mod tests { "alice", Rule { tier: Some("not a tier!".into()), - ..rule(&["**"], &["**"]) + ..rule(&[""], &[""]) }, )]), ..Default::default() @@ -1642,7 +1641,7 @@ mod tests { assert!(server.update(invalid).is_err()); assert_eq!( server.answer(&session).await.unwrap().unwrap().publish.path, - patterns(&["alice/**"]) + prefixes(&["alice"]) ); server.update(Policy::default()).unwrap(); @@ -1671,8 +1670,8 @@ mod tests { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), - public: rules(&["**"], &["**"]), - mtls: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), + mtls: rules(&[""], &[""]), mtls_link: Some(Link::Peer), ..Default::default() }; @@ -1688,7 +1687,7 @@ mod tests { grant.validate().unwrap(); let grant = policy.decide(&request("/")).await.unwrap(); assert_eq!(grant.link, None); - let jwt = sign(&key, "", &["**"], &[], None); + let jwt = sign(&key, "", &[""], &[], None); let grant = policy.decide(&with_token(request("/"), &jwt)).await.unwrap(); assert_eq!(grant.link, None); } @@ -1703,7 +1702,7 @@ mod tests { (Some(Link::Upstream), Some(true)), ] { let policy = Policy { - mtls: rules(&["**"], &["**"]), + mtls: rules(&[""], &[""]), mtls_link: link, ..Default::default() }; @@ -1723,10 +1722,10 @@ mod tests { let (dir, key) = key_dir(); let policy = Policy { keys: Some(Keys::Dir(dir.path().into())), - mtls: rules(&["**"], &["**"]), + mtls: rules(&[""], &[""]), ..Default::default() }; - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); for request in [ with_peer(with_token(request("/demo"), &jwt), None), with_peer(with_token(request("/demo"), "garbage"), None), @@ -1741,41 +1740,42 @@ mod tests { #[tokio::test] async fn anonymous_gets_the_public_rules() { let policy = Policy { - public: rules(&[], &["anon/**"]), + public: rules(&[], &["anon"]), ..Default::default() }; let grant = policy.decide(&request("/")).await.unwrap(); - assert_eq!(grant.subscribe.path, patterns(&["anon/**"])); + assert_eq!(grant.subscribe.path, prefixes(&["anon"])); assert!(grant.publish.path.is_empty()); // Never re-checked or closed by default, like 0.14. assert_eq!(grant.expires, None); assert_eq!(grant.revalidate, None); } - /// The rules are rooted at `/`, not at the dialed path: `anon/**` scopes a session - /// dialed anywhere to `anon/`, and refuses one dialed outside it. Bare `**` reads - /// the same either way, which is how rooting them at the dialed path went unnoticed. + /// The rules are rooted at `/`, not at the dialed path: `anon` scopes a session + /// dialed anywhere to `anon/`, and refuses one dialed outside it. The empty prefix + /// reads the same either way, which is how rooting them at the dialed path went + /// unnoticed. #[tokio::test] async fn public_and_mtls_rules_are_rooted_at_slash() { let policy = Policy { - public: rules(&["anon/**"], &["anon/**", "*/chat"]), - mtls: rules(&["origin/*"], &["origin/*"]), + public: rules(&["anon"], &["anon", "lobby/chat"]), + mtls: rules(&["origin"], &["origin"]), ..Default::default() }; for (path, publish, subscribe) in [ - ("/", &["anon/**"][..], &["anon/**", "*/chat"][..]), - ("/anon", &["**"], &["**", "chat"]), - ("/anon/room", &["**"], &["**"]), - ("/rooms", &[], &["chat"]), + ("/", &["anon"][..], &["anon", "lobby/chat"][..]), + ("/anon", &[""], &[""]), + ("/anon/room", &[""], &[""]), + ("/lobby", &[], &["chat"]), ] { let grant = policy.decide(&request(path)).await.unwrap(); - assert_eq!(grant.publish.path, patterns(publish), "{path}"); - assert_eq!(grant.subscribe.path, patterns(subscribe), "{path}"); + assert_eq!(grant.publish.path, prefixes(publish), "{path}"); + assert_eq!(grant.subscribe.path, prefixes(subscribe), "{path}"); assert_eq!(grant.root, None, "{path}"); } - // Before, `/rooms/123` got `rooms/123/anon/**`: into any room, anonymously. + // Before, `/rooms/123` got `rooms/123/anon`: into any room, anonymously. for path in ["/rooms/123", "/other/room", "/anonymous/room"] { assert_eq!( policy.decide(&request(path)).await.unwrap_err(), @@ -1787,7 +1787,7 @@ mod tests { } let grant = policy.decide(&with_peer(request("/origin/edge0"), None)).await.unwrap(); - assert_eq!(grant.publish.path, patterns(&[""])); + assert_eq!(grant.publish.path, prefixes(&[""])); let err = policy.decide(&with_peer(request("/anon"), None)).await.unwrap_err(); assert!(matches!(err, Denial::NoAccess { .. }), "{err}"); } @@ -1796,7 +1796,7 @@ mod tests { async fn limits_count_connect_end_and_aging() { tokio::time::pause(); let server = Server::new(Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), limits: Limits { token: None, remote: Some(2), @@ -1893,7 +1893,7 @@ mod tests { #[tokio::test] async fn a_bound_past_the_clock_is_refused() { let endless = Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), expires: Some(Duration::MAX), ..Default::default() }; @@ -1909,7 +1909,7 @@ mod tests { #[tokio::test] async fn a_cadence_past_the_clock_keeps_every_slot() { let server = Server::new(Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), limits: Limits { token: None, remote: Some(1), @@ -1930,7 +1930,7 @@ mod tests { #[tokio::test] async fn no_limits_keep_no_sessions() { let server = Server::new(Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }) .unwrap(); @@ -1952,7 +1952,7 @@ mod tests { ..limited() }) .unwrap(); - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); server.answer(&with_token(request("/demo"), &jwt)).await.unwrap(); let mut mapped = with_token(request("/demo"), &jwt); @@ -1967,7 +1967,7 @@ mod tests { let other = sign( &key, "demo", - &["**"], + &[""], &[], Some(kio::clock::wall() + Duration::from_secs(60)), ); @@ -2002,7 +2002,7 @@ mod tests { ..limited() }) .unwrap(); - let jwt = sign(&key, "demo", &["**"], &[], None); + let jwt = sign(&key, "demo", &[""], &[], None); let b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD; let (input, signature) = jwt.rsplit_once('.').unwrap(); @@ -2042,12 +2042,12 @@ mod tests { }) .unwrap(); let exp = Some(kio::clock::wall() + Duration::from_secs(60)); - let a = sign(&key, "demo", &["**"], &[], None); - let b = sign(&key, "demo", &["**"], &[], exp); + let a = sign(&key, "demo", &[""], &[], None); + let b = sign(&key, "demo", &[""], &[], exp); let c = sign( &key, "demo", - &["**"], + &[""], &[], Some(kio::clock::wall() + Duration::from_secs(61)), ); @@ -2100,7 +2100,7 @@ mod tests { #[tokio::test] async fn the_client_admits_and_reads_the_refusal() { let (client, ended) = serve(Policy { - public: rules(&["**"], &[]), + public: rules(&[""], &[]), limits: Limits { token: None, remote: Some(1), @@ -2110,7 +2110,7 @@ mod tests { .await; let consumer = client.connect(request("/demo")).await.unwrap(); - assert_eq!(consumer.grant().publish.path, patterns(&["**"])); + assert_eq!(consumer.grant().publish.path, prefixes(&[""])); let err = client.connect(request("/demo")).await.unwrap_err(); assert!(matches!(err, Error::Refused(_)), "{err}"); @@ -2134,9 +2134,9 @@ mod tests { ..Default::default() }) .await; - let expired_jwt = sign(&key, "tenant", &["media/**"], &[], expired); - let valid_jwt = sign(&key, "tenant", &["media/**"], &[], None); - let forged_jwt = sign(&stranger, "forged", &["**"], &[], expired); + let expired_jwt = sign(&key, "tenant", &["media"], &[], expired); + let valid_jwt = sign(&key, "tenant", &["media"], &[], None); + let forged_jwt = sign(&stranger, "forged", &[""], &[], expired); for (path, token, reason, root) in [ ( "/attacker/path", @@ -2181,8 +2181,8 @@ mod tests { .clone() .with_scope(crate::Scope { root: "tenant".into(), - publish: patterns(&["allowed/**"]), - subscribe: Patterns::default(), + publish: prefixes(&["allowed"]), + subscribe: Prefixes::new(), }) .unwrap(); scoped.to_file(dir.path().join("kid1.jwk")).unwrap(); @@ -2194,7 +2194,7 @@ mod tests { let token = sign( &key, "attacker", - &["**"], + &[""], &[], Some(kio::clock::Wall::UNIX_EPOCH + Duration::from_secs(1)), ); @@ -2215,7 +2215,7 @@ mod tests { let path = dir.path().join("auth.sock"); let listener = tokio::net::UnixListener::bind(&path).unwrap(); let server = Server::new(Policy { - public: rules(&["**"], &["**"]), + public: rules(&[""], &[""]), ..Default::default() }) .unwrap(); @@ -2225,6 +2225,6 @@ mod tests { let url = format!("unix://{}", url.path()).parse().unwrap(); let client = Client::new(url, None).unwrap(); let consumer = client.connect(request("/")).await.unwrap(); - assert_eq!(consumer.grant().subscribe.path, patterns(&["**"])); + assert_eq!(consumer.grant().subscribe.path, prefixes(&[""])); } } diff --git a/rs/moq-auth/src/set.rs b/rs/moq-auth/src/set.rs index db50e148bce..be78ccf3462 100644 --- a/rs/moq-auth/src/set.rs +++ b/rs/moq-auth/src/set.rs @@ -153,7 +153,7 @@ impl KeySet { mod tests { use super::*; - fn patterns(texts: &[&str]) -> crate::Patterns { + fn prefixes(texts: &[&str]) -> crate::Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } use crate::Algorithm; @@ -163,8 +163,8 @@ mod tests { fn create_test_claims() -> Claims { Claims { root: "test-path".to_string(), - publish: patterns(&["test-pub/**"]).into(), - subscribe: patterns(&["test-sub/**"]).into(), + publish: prefixes(&["test-pub"]).into(), + subscribe: prefixes(&["test-sub"]).into(), expires: Some(kio::clock::wall() + Duration::from_secs(3600)), issued: Some(kio::clock::wall()), not_before: None, diff --git a/rs/moq-auth/src/wire.rs b/rs/moq-auth/src/wire.rs index 09222a449e0..43a3174af21 100644 --- a/rs/moq-auth/src/wire.rs +++ b/rs/moq-auth/src/wire.rs @@ -1,14 +1,17 @@ //! The JSON encoding of the grants in [`Claims`](crate::Claims) and [`Scope`](crate::Scope). //! -//! Grants are written as `publish` and `subscribe` pattern lists. They were once prefix -//! lists named `put` and `get`, which are still read: a prefix `p` means exactly the -//! pattern `p/**` (and `""` means `**`). One document never mixes the two encodings. +//! Grants are written as `publish` and `subscribe` prefix lists. They were once named +//! `put` and `get`, which are still read as the same prefixes, with their slashes +//! normalized as they always were. One document never mixes the two encodings. //! //! A role with a bitrate cap is written as an object, `{"path": [...], "rate": bps}`, //! which every older reader refuses rather than drop the cap. +//! +//! Until the version migrations [`alias`] names, a prefix is written and read as the +//! subtree pattern releases before 0.22 spell it: `foo/**`, and `**` for the empty prefix. use crate::{Access, Link}; -use moq_pattern::{Pattern, Patterns}; +use moq_pattern::{InvalidPrefix, Pattern, Prefix, Prefixes}; use serde::{Deserialize, Deserializer, Serialize}; use serde_with::{DurationSeconds, serde_as}; use std::collections::BTreeMap; @@ -24,13 +27,13 @@ pub(crate) struct Claims { root: String, // The legacy grants, read and never written. #[serde(deserialize_with = "present", skip_serializing)] - put: Option, + put: Option, #[serde(deserialize_with = "present", skip_serializing)] - get: Option, + get: Option, #[serde(deserialize_with = "present")] - publish: Option, + publish: Option, #[serde(deserialize_with = "present")] - subscribe: Option, + subscribe: Option, #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] exp: Option, #[serde(with = "seconds", skip_serializing_if = "Option::is_none")] @@ -58,8 +61,8 @@ impl From for Claims { root: claims.root, put: None, get: None, - publish: written(claims.publish), - subscribe: written(claims.subscribe), + publish: written(claims.publish).map(alias::Role), + subscribe: written(claims.subscribe).map(alias::Role), exp: claims.expires, iat: claims.issued, nbf: claims.not_before, @@ -75,10 +78,10 @@ impl TryFrom for crate::Claims { fn try_from(wire: Claims) -> Result { let grants = Grants { - put: wire.put.map(Prefixes::into_vec), - get: wire.get.map(Prefixes::into_vec), - publish: wire.publish, - subscribe: wire.subscribe, + put: wire.put.map(Legacy::into_vec), + get: wire.get.map(Legacy::into_vec), + publish: wire.publish.map(|role| role.0), + subscribe: wire.subscribe.map(|role| role.0), }; let (publish, subscribe) = grants.decode()?; Ok(Self { @@ -106,14 +109,14 @@ pub(crate) struct Scope { #[serde(deserialize_with = "present", skip_serializing)] get: Option>, #[serde(deserialize_with = "present")] - publish: Option, + publish: Option, #[serde(deserialize_with = "present")] - subscribe: Option, + subscribe: Option, } impl From for Scope { fn from(scope: crate::Scope) -> Self { - let paths = |patterns: Patterns| (!patterns.is_empty()).then_some(patterns); + let paths = |prefixes: Prefixes| (!prefixes.is_empty()).then_some(alias::List(prefixes)); Self { root: scope.root, put: None, @@ -132,8 +135,8 @@ impl TryFrom for crate::Scope { let grants = Grants { put: wire.put, get: wire.get, - publish: wire.publish.map(Access::new), - subscribe: wire.subscribe.map(Access::new), + publish: wire.publish.map(|list| Access::new(list.0)), + subscribe: wire.subscribe.map(|list| Access::new(list.0)), }; let (publish, subscribe) = grants.decode()?; Ok(Self { @@ -147,12 +150,12 @@ impl TryFrom for crate::Scope { /// Legacy claims wrote a single prefix as a bare string. #[derive(Deserialize)] #[serde(untagged)] -enum Prefixes { +enum Legacy { One(String), Many(Vec), } -impl Prefixes { +impl Legacy { fn into_vec(self) -> Vec { match self { Self::One(prefix) => vec![prefix], @@ -295,15 +298,20 @@ impl Grants { return Err("mixes the legacy put/get fields with publish/subscribe".to_string()); } - let subtrees = |prefixes: Option>| -> Result { - let path: Patterns = prefixes + // A legacy prefix had its slashes trimmed and collapsed, as a broadcast path does. + let legacy = |prefix: &String| -> Result { + let normalized = Pattern::literal(prefix).map_err(|err| format!("legacy prefix {prefix:?}: {err}"))?; + Prefix::try_from(normalized.as_str()).map_err(|err| format!("legacy prefix {prefix:?}: {err}")) + }; + let prefixes = |prefixes: Option>| -> Result { + let path: Prefixes = prefixes .unwrap_or_default() .iter() - .map(|prefix| Pattern::subtree(prefix).map_err(|err| format!("legacy prefix {prefix:?}: {err}"))) + .map(legacy) .collect::>()?; Ok(Access::new(path)) }; - Ok((subtrees(self.put)?, subtrees(self.get)?)) + Ok((prefixes(self.put)?, prefixes(self.get)?)) } } @@ -314,10 +322,10 @@ impl Grants { #[derive(Serialize, Deserialize, Default)] #[serde(default, deny_unknown_fields)] pub(crate) struct Grant { - #[serde(skip_serializing_if = "Access::is_unset")] - publish: Access, - #[serde(skip_serializing_if = "Access::is_unset")] - subscribe: Access, + #[serde(skip_serializing_if = "alias::Role::is_unset")] + publish: alias::Role, + #[serde(skip_serializing_if = "alias::Role::is_unset")] + subscribe: alias::Role, #[serde(deserialize_with = "present")] root: Option, #[serde(skip_serializing_if = "BTreeMap::is_empty")] @@ -341,8 +349,8 @@ pub(crate) struct Grant { impl From for Grant { fn from(grant: crate::Grant) -> Self { Self { - publish: grant.publish, - subscribe: grant.subscribe, + publish: alias::Role(grant.publish), + subscribe: alias::Role(grant.subscribe), root: grant.root, mounts: grant.mounts, expires: grant.expires, @@ -368,8 +376,8 @@ impl TryFrom for crate::Grant { (Some(link), Some(false)) => return Err(format!("`link` is {link} but `peer` is false")), }; Ok(Self { - publish: wire.publish, - subscribe: wire.subscribe, + publish: wire.publish.0, + subscribe: wire.subscribe.0, root: wire.root, mounts: wire.mounts, expires: wire.expires, @@ -380,3 +388,148 @@ impl TryFrom for crate::Grant { }) } } + +/// TEMPORARY MIGRATION: grant lists as releases before 0.22 spell them. +/// +/// Those releases read a member as a pattern: a bare `foo` is the one path `foo`, and `""` +/// the root alone. So every list one of them reads, in a token, a key scope, or an auth +/// server's grant, is written as the same subtrees in pattern form, `foo/**` and `**`, and +/// a member spelled that way reads back as its prefix. Any other wildcard is still +/// refused, and nothing else takes the alias: configs, flags, origin scopes, and AUTH_OK +/// stay strict. `@moq/auth`'s `wire.ts` mirrors this. +/// +/// Deleted in two steps (`quest/m4/grant-alias-delete.md`): the first +/// version migration after 0.22 writes bare prefixes, and the one after it deletes the +/// read, once tokens and key scopes minted with `/**` have expired or been re-minted. +pub(crate) mod alias { + use super::*; + + /// The prefix a member names: `foo/**` is `foo`, `**` the empty prefix, and anything + /// else is parsed as a prefix. + pub(crate) fn read(text: &str) -> Result { + let head = match text.strip_suffix("/**") { + Some(head) if !head.is_empty() => head, + _ if text == "**" => "", + _ => text, + }; + // A wildcard left in the head names something other than one subtree. + if head.contains('*') { + return Err(InvalidPrefix::Wildcard(text.to_string())); + } + head.parse() + } + + /// Prefixes spelled as the subtree patterns they cover. A prefix as deep as a path may + /// be is its literal, which a release reads as that one path: nothing lies beneath it. + struct Written<'a>(&'a Prefixes); + + impl Serialize for Written<'_> { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_seq(self.0.iter().map(Prefix::pattern)) + } + } + + /// A key scope's prefix list. + pub(crate) struct List(pub(crate) Prefixes); + + impl From for Prefixes { + fn from(list: List) -> Self { + list.0 + } + } + + impl Serialize for List { + fn serialize(&self, serializer: S) -> Result { + Written(&self.0).serialize(serializer) + } + } + + impl<'de> Deserialize<'de> for List { + fn deserialize>(deserializer: D) -> Result { + let texts = Vec::::deserialize(deserializer)?; + let prefixes = texts.iter().map(|text| read(text)).collect::>(); + prefixes.map(List).map_err(serde::de::Error::custom) + } + } + + /// A token's or grant's role. + #[derive(Default)] + pub(crate) struct Role(pub(crate) Access); + + impl Role { + pub(crate) fn is_unset(&self) -> bool { + self.0.is_unset() + } + } + + impl Serialize for Role { + fn serialize(&self, serializer: S) -> Result { + self.0.serialize_as(Written(&self.0.path), serializer) + } + } + + impl<'de> Deserialize<'de> for Role { + fn deserialize>(deserializer: D) -> Result { + Access::deserialize_as::(deserializer).map(Role) + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn reads_subtree_patterns_as_their_prefix() { + for (text, prefix) in [ + ("**", ""), + ("foo/**", "foo"), + ("foo/bar/**", "foo/bar"), + (r"a\/b/**", r"a\/b"), + (r"a\\/**", r"a\\"), + ("foo", "foo"), + ("", ""), + ] { + assert_eq!(read(text).unwrap().as_str(), prefix, "{text}"); + } + } + + #[test] + fn refuses_every_other_wildcard() { + for text in [ + "/**", + "*", + "*/chat", + "foo/*", + "foo/*/**", + "**/foo", + "foo/**/**", + "cam-*.hang", + r"a\/**", + ] { + assert!(read(text).is_err(), "{text}"); + } + // The rest is still a prefix, so its slashes are checked. + for text in ["/foo/**", "foo//**", "foo/", "/foo"] { + assert!(read(text).is_err(), "{text}"); + } + } + + #[test] + fn writes_the_subtree_each_prefix_covers() { + let prefixes: Prefixes = ["", "foo"].into_iter().map(|text| text.parse().unwrap()).collect(); + assert_eq!(serde_json::to_value(List(prefixes)).unwrap(), serde_json::json!(["**"])); + let prefixes: Prefixes = ["foo", r"a\/b"].into_iter().map(|text| text.parse().unwrap()).collect(); + assert_eq!( + serde_json::to_value(List(prefixes)).unwrap(), + serde_json::json!([r"a\/b/**", "foo/**"]) + ); + // Nothing lies beneath a prefix of the most segments a path holds, and a pattern + // one segment longer is refused, so it is written as the literal. + let deepest = vec!["a"; Pattern::MAX_SEGMENTS].join("/"); + let list = List(Prefixes::from(deepest.parse::().unwrap())); + assert_eq!(serde_json::to_value(&list).unwrap(), serde_json::json!([deepest])); + let read: List = serde_json::from_value(serde_json::to_value(&list).unwrap()).unwrap(); + assert_eq!(read.0, list.0); + } + } +} diff --git a/rs/moq-auth/tests/corpus.json b/rs/moq-auth/tests/corpus.json index 2d589f019bf..c0b98dec1b0 100644 --- a/rs/moq-auth/tests/corpus.json +++ b/rs/moq-auth/tests/corpus.json @@ -12,7 +12,7 @@ "key_ops": ["verify"], "kty": "OKP", "crv": "Ed25519", - "x": "S9n3tEG677fSNpa0Ftf46OPBte5y1pTwvm4fPkEw_Z8", + "x": "qu5t7m2T44BXaiUVEQrtZoUNGMLQqPwyTus-H8JagFA", "kid": "dfab9113ea85dc27" }, "js_hs256": { @@ -39,33 +39,39 @@ "kty": "oct", "k": "pLDMYsYrp6vZLQbefVzHXN6P4wc1ADTLn4L7xzi1Qm4", "kid": "e396f70014db9fe9", - "scope": { "root": "project", "publish": ["live/**"], "subscribe": ["watch/**"] } + "scope": { "root": "project", "publish": ["live"], "subscribe": ["watch"] } } }, "tokens": [ { "name": "rust_hs256", "key": "hs256", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UiXSwic3Vic2NyaWJlIjpbImJvYiJdfQ.yKTcU2z7euqevlly1CiC4EZEQ3MM1BwkKPrnEZXWfkc", + "claims": { "root": "demo", "publish": ["alice"], "subscribe": ["bob"] } + }, + { + "name": "minted_with_subtree_patterns", + "key": "hs256", "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdY", - "claims": { "root": "demo", "publish": ["alice/**"], "subscribe": ["bob/**"] } + "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/**"] } + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJwdWJsaXNoIjpbImFsaWNlIl0sInN1YnNjcmliZSI6WyJib2IiXX0.PZPVb8pZfDRHWQyEaJRSmYUOipw6AQNRG5rJm-Klax0", + "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/**"] } + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMSIsInN0cmVhbTIiXSwic3Vic2NyaWJlIjpbImZlZWQxIl19.g2aBeFIrej9jfZz4pQUPwX3GjZVROoTuvWdFSb4l55i5FLL9xfdEf2vUH83hdYo4og1i2OT8Wm0vnPGdwyhsCw", + "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/**"] } + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImUzOTZmNzAwMTRkYjlmZTkifQ.eyJyb290IjoicHJvamVjdCIsInB1Ymxpc2giOlsibGl2ZS9yb29tIl0sInN1YnNjcmliZSI6WyJ3YXRjaCJdfQ.2neixYP5xthkSZPqsu4EU72GemV4oom3yGAXTs0iMYY", + "claims": { "root": "project", "publish": ["live/room"], "subscribe": ["watch"] } }, { "name": "js_hs256", @@ -79,65 +85,65 @@ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1dCI6WyJjYW1lcmExIl0sImdldCI6WyJjYW1lcmExIiwiY2FtZXJhMiJdLCJpYXQiOjE3NzUxNzY3NTR9.tHNQtHh_HCIKxXOexDCM7AkjqWzbULLZzjEckfOGRfY", "claims": { "root": "live", - "publish": ["camera1/**"], - "subscribe": ["camera1/**", "camera2/**"], + "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 } + "token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsImtpZCI6ImpzLWVkZHNhLWtleSJ9.eyJyb290Ijoic3RyZWFtIiwicHVibGlzaCI6WyJ2aWRlbyJdLCJpYXQiOjE3NzUxNzY3NTZ9.F1q_MKWxwliM6mdM9f00zf4ekb3UmkEGzkbNyimF4hFcEkGrmvclVhHCKZnPpzSpgau9HZrtWPjub-_imZh0AA", + "claims": { "root": "stream", "publish": ["video"], "subscribe": [], "iat": 1775176756 } }, { "name": "eddsa_token_under_an_hs256_key", "key": "hs256", - "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMS8qKiIsInN0cmVhbTIvKioiXSwic3Vic2NyaWJlIjpbImZlZWQxLyoqIl19.dpZ_p-eyKxaw6bRZp7sfKjmao04MwPuPqbu744bucPjiQVNfGZodfeyEk-fsY79fa9YSbjM1m6DPw3Wg7pzRBA", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6ImRmYWI5MTEzZWE4NWRjMjcifQ.eyJyb290Ijoicm9vbSIsInB1Ymxpc2giOlsic3RyZWFtMSIsInN0cmVhbTIiXSwic3Vic2NyaWJlIjpbImZlZWQxIl19.g2aBeFIrej9jfZz4pQUPwX3GjZVROoTuvWdFSb4l55i5FLL9xfdEf2vUH83hdYo4og1i2OT8Wm0vnPGdwyhsCw", "claims": null }, { "name": "tampered_signature", "key": "hs256", - "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UvKioiXSwic3Vic2NyaWJlIjpbImJvYi8qKiJdfQ.OuaQY4hHKpBLvTK-9ZUv2oxiqXTGisng-cg2GcqrgdA", + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImQ1NWIxZjEzZTZiZGEyODEifQ.eyJyb290IjoiZGVtbyIsInB1Ymxpc2giOlsiYWxpY2UiXSwic3Vic2NyaWJlIjpbImJvYiJdfQ.yKTcU2z7euqevlly1CiC4EZEQ3MM1BwkKPrnEZXWfkA", "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 } + "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": "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 }, + "payload": { "publish": [""], "exp": 4102444800.5 }, "claims": null }, - { "name": "string_exp", "key": "hs256", "payload": { "publish": ["**"], "exp": "4102444800" }, "claims": null }, + { "name": "string_exp", "key": "hs256", "payload": { "publish": [""], "exp": "4102444800" }, "claims": null }, { "name": "exp_past_the_clock", "key": "hs256", - "payload": { "publish": ["**"], "exp": 18446744074 }, + "payload": { "publish": [""], "exp": 18446744074 }, "claims": null }, - { "name": "expired", "key": "hs256", "payload": { "publish": ["**"], "exp": 1 }, "claims": null }, + { "name": "expired", "key": "hs256", "payload": { "publish": [""], "exp": 1 }, "claims": null }, { "name": "not_yet_valid", "key": "hs256", - "payload": { "publish": ["**"], "nbf": 4102444800 }, + "payload": { "publish": [""], "nbf": 4102444800 }, "claims": null }, { "name": "legacy_prefixes", "key": "hs256", "payload": { "root": "demo", "put": "alice", "get": ["", "bob"] }, - "claims": { "root": "demo", "publish": ["alice/**"], "subscribe": ["**"] } + "claims": { "root": "demo", "publish": ["alice"], "subscribe": [""] } }, { "name": "legacy_prefix_with_a_wildcard", @@ -148,31 +154,31 @@ { "name": "mixed_encodings", "key": "hs256", - "payload": { "put": ["alice"], "subscribe": ["**"] }, + "payload": { "put": ["alice"], "subscribe": [""] }, "claims": null }, { "name": "registered_claims_are_ignored", "key": "hs256", - "payload": { "publish": ["**"], "iss": "issuer", "sub": 7 }, - "claims": { "root": "", "publish": ["**"], "subscribe": [] } + "payload": { "publish": [""], "iss": "issuer", "sub": 7 }, + "claims": { "root": "", "publish": [""], "subscribe": [] } }, { "name": "token_id", "key": "hs256", - "payload": { "publish": ["**"], "jti": "550e8400-e29b-41d4-a716-446655440000" }, - "claims": { "root": "", "publish": ["**"], "subscribe": [], "jti": "550e8400-e29b-41d4-a716-446655440000" } + "payload": { "publish": [""], "jti": "550e8400-e29b-41d4-a716-446655440000" }, + "claims": { "root": "", "publish": [""], "subscribe": [], "jti": "550e8400-e29b-41d4-a716-446655440000" } }, { "name": "token_id_at_the_length_bound", "key": "hs256", "payload": { - "publish": ["**"], + "publish": [""], "jti": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" }, "claims": { "root": "", - "publish": ["**"], + "publish": [""], "subscribe": [], "jti": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" } @@ -181,7 +187,7 @@ "name": "token_id_past_the_length_bound", "key": "hs256", "payload": { - "publish": ["**"], + "publish": [""], "jti": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }, "claims": null @@ -189,62 +195,92 @@ { "name": "token_id_not_a_string", "key": "hs256", - "payload": { "publish": ["**"], "jti": ["x"] }, + "payload": { "publish": [""], "jti": ["x"] }, "claims": null }, - { "name": "null_token_id", "key": "hs256", "payload": { "publish": ["**"], "jti": null }, "claims": null }, - { "name": "empty_token_id", "key": "hs256", "payload": { "publish": ["**"], "jti": "" }, "claims": null }, + { "name": "null_token_id", "key": "hs256", "payload": { "publish": [""], "jti": null }, "claims": null }, + { "name": "empty_token_id", "key": "hs256", "payload": { "publish": [""], "jti": "" }, "claims": null }, { "name": "token_id_with_a_space", "key": "hs256", - "payload": { "publish": ["**"], "jti": "a b" }, + "payload": { "publish": [""], "jti": "a b" }, "claims": null }, { "name": "token_id_beyond_ascii", "key": "hs256", - "payload": { "publish": ["**"], "jti": "caf\u00e9" }, + "payload": { "publish": [""], "jti": "caf\u00e9" }, "claims": null }, - { "name": "unknown_claim", "key": "hs256", "payload": { "publish": ["**"], "aud": "relay" }, "claims": null }, + { "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": "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": { "path": ["alice/**"], "rate": 1000 }, "subscribe": ["**"] } + "payload": { "publish": { "path": ["alice"], "rate": 1000 }, "subscribe": [""] }, + "claims": { "root": "", "publish": { "path": ["alice"], "rate": 1000 }, "subscribe": [""] } }, { "name": "misspelled_cap", "key": "hs256", - "payload": { "publish": { "path": ["alice/**"], "rat": 1000 } }, + "payload": { "publish": { "path": ["alice"], "rat": 1000 } }, "claims": null }, { "name": "zero_rate", "key": "hs256", - "payload": { "publish": { "path": ["**"], "rate": 0 } }, + "payload": { "publish": { "path": [""], "rate": 0 } }, "claims": null }, - { "name": "invalid_pattern", "key": "hs256", "payload": { "publish": ["**/a/**"] }, "claims": null }, + { + "name": "subtree_pattern", + "key": "hs256", + "payload": { "publish": ["alice/**"] }, + "claims": { "root": "", "publish": ["alice"], "subscribe": [] } + }, + { + "name": "everything_pattern", + "key": "hs256", + "payload": { "subscribe": ["**"] }, + "claims": { "root": "", "publish": [], "subscribe": [""] } + }, + { + "name": "subtree_pattern_beside_an_exact_path", + "key": "hs256", + "payload": { "publish": ["alice/**", "bob", "alice/cam"] }, + "claims": { "root": "", "publish": ["alice", "bob"], "subscribe": [] } + }, + { + "name": "subtree_pattern_in_a_capped_role", + "key": "hs256", + "payload": { "publish": { "path": ["**"], "rate": 1000 } }, + "claims": { "root": "", "publish": { "path": [""], "rate": 1000 }, "subscribe": [] } + }, + { "name": "wildcard_segment", "key": "hs256", "payload": { "subscribe": ["*/chat"] }, "claims": null }, + { "name": "wildcard_in_a_segment", "key": "hs256", "payload": { "publish": ["cam-*.hang"] }, "claims": null }, + { "name": "wildcard_before_a_subtree", "key": "hs256", "payload": { "publish": ["a/*/**"] }, "claims": null }, + { "name": "globstar_before_a_path", "key": "hs256", "payload": { "publish": ["**/a"] }, "claims": null }, + { "name": "subtree_of_a_leading_slash", "key": "hs256", "payload": { "publish": ["/**"] }, "claims": null }, + { "name": "subtree_of_an_escape", "key": "hs256", "payload": { "publish": ["a\\/**"] }, "claims": null }, + { "name": "malformed_prefix", "key": "hs256", "payload": { "publish": ["alice/"] }, "claims": null }, { "name": "minted_root_with_boundary_slashes", "key": "hs256", - "payload": { "root": "/demo/", "publish": ["**"] }, - "claims": { "root": "/demo/", "publish": ["**"], "subscribe": [] } + "payload": { "root": "/demo/", "publish": [""] }, + "claims": { "root": "/demo/", "publish": [""], "subscribe": [] } }, { "name": "outside_the_key_scope", "key": "scoped", - "payload": { "root": "project", "publish": ["other/**"] }, + "payload": { "root": "project", "publish": ["other"] }, "claims": null }, { "name": "root_a_signer_now_refuses", "key": "hs256", - "payload": { "root": "a\\//b", "publish": ["**"] }, - "claims": { "root": "a\\//b", "publish": ["**"], "subscribe": [] } + "payload": { "root": "a\\//b", "publish": [""] }, + "claims": { "root": "a\\//b", "publish": [""], "subscribe": [] } } ], "jwk": [ @@ -258,13 +294,33 @@ }, "valid": true }, + { + "name": "scope_with_subtree_patterns", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "demo", "publish": ["room/**"], "subscribe": ["**"] } + }, + "valid": true + }, + { + "name": "scope_with_a_wildcard", + "jwk": { + "kty": "oct", + "alg": "HS256", + "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", + "scope": { "root": "demo", "publish": ["*/room"] } + }, + "valid": false + }, { "name": "scope_root_with_a_leading_slash", "jwk": { "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "root": "/demo", "publish": ["**"] } + "scope": { "root": "/demo", "publish": [""] } }, "valid": false }, @@ -274,7 +330,7 @@ "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "root": "a//b", "publish": ["**"] } + "scope": { "root": "a//b", "publish": [""] } }, "valid": false }, @@ -284,7 +340,7 @@ "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "root": "a\\//b", "publish": ["**"] } + "scope": { "root": "a\\//b", "publish": [""] } }, "valid": true }, @@ -294,7 +350,7 @@ "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "root": "a\\b", "publish": ["**"] } + "scope": { "root": "a\\b", "publish": [""] } }, "valid": false }, @@ -314,7 +370,7 @@ "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "publish": { "path": ["**"], "rate": 1000 } } + "scope": { "publish": { "path": [""], "rate": 1000 } } }, "valid": false }, @@ -344,7 +400,7 @@ "kty": "oct", "alg": "HS256", "k": "Fp8kipWUJeUFqeSqWym_tRC_tyI8z-QpqopIGrbrD68", - "scope": { "root": null, "publish": ["**"] } + "scope": { "root": null, "publish": [""] } }, "valid": false }, @@ -420,7 +476,7 @@ "alg": "ES256", "key_ops": ["verify"], "crv": "Ed25519", - "x": "S9n3tEG677fSNpa0Ftf46OPBte5y1pTwvm4fPkEw_Z8" + "x": "qu5t7m2T44BXaiUVEQrtZoUNGMLQqPwyTus-H8JagFA" }, "valid": false }, @@ -488,7 +544,7 @@ }, { "name": "a_repeated_scope_member", - "jwk": "{\"scope\":{\"root\":\"a\",\"root\":\"b\",\"publish\":[\"**\"]},\"kty\":\"oct\",\"alg\":\"HS256\",\"k\":\"jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA\"}", + "jwk": "{\"scope\":{\"root\":\"a\",\"root\":\"b\",\"publish\":[\"\"]},\"kty\":\"oct\",\"alg\":\"HS256\",\"k\":\"jvS2B_SKkvhtObaiKg9n2vq_Iqdus-nVCVVYBWf0BQA\"}", "valid": false }, { @@ -640,47 +696,58 @@ } ], "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": "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": "invalid_tier", "grant": { "publish": ["**"], "tier": "a b" }, "valid": false }, - { "name": "unknown_field", "grant": { "publish": ["**"], "future": true }, "valid": false }, - { "name": "flat_rate", "grant": { "publish": ["**"], "publish_rate": 8000 }, "valid": false }, + { "name": "invalid_tier", "grant": { "publish": [""], "tier": "a b" }, "valid": false }, + { "name": "unknown_field", "grant": { "publish": [""], "future": true }, "valid": false }, + { "name": "flat_rate", "grant": { "publish": [""], "publish_rate": 8000 }, "valid": false }, { "name": "legacy_upstream_flag", - "grant": { "publish": ["**"], "peer": true, "upstream": true }, + "grant": { "publish": [""], "peer": true, "upstream": true }, "valid": false }, { "name": "capped_role", - "grant": { "publish": { "path": ["alice/**"], "rate": 8000 }, "subscribe": ["**"] }, + "grant": { "publish": { "path": ["alice"], "rate": 8000 }, "subscribe": [""] }, "valid": true }, { "name": "cap_on_nothing", "grant": { "publish": { "path": [], "rate": 8000 } }, "valid": false }, - { "name": "misspelled_cap", "grant": { "publish": { "path": ["**"], "rat": 8000 } }, "valid": false }, - { "name": "link_peer", "grant": { "publish": ["**"], "link": "peer" }, "valid": true }, - { "name": "link_upstream", "grant": { "publish": ["**"], "link": "upstream", "peer": true }, "valid": true }, - { "name": "peer_alias", "grant": { "publish": ["**"], "peer": true }, "valid": true }, - { "name": "not_a_peer", "grant": { "publish": ["**"], "peer": false }, "valid": true }, + { "name": "misspelled_cap", "grant": { "publish": { "path": [""], "rat": 8000 } }, "valid": false }, + { "name": "link_peer", "grant": { "publish": [""], "link": "peer" }, "valid": true }, + { "name": "link_upstream", "grant": { "publish": [""], "link": "upstream", "peer": true }, "valid": true }, + { "name": "peer_alias", "grant": { "publish": [""], "peer": true }, "valid": true }, + { "name": "not_a_peer", "grant": { "publish": [""], "peer": false }, "valid": true }, { "name": "link_disagrees_with_peer", - "grant": { "publish": ["**"], "link": "upstream", "peer": false }, - "valid": false - }, - { "name": "unknown_link", "grant": { "publish": ["**"], "link": "downstream" }, "valid": false }, - { "name": "null_link", "grant": { "publish": ["**"], "link": null }, "valid": false }, - { "name": "null_root", "grant": { "publish": ["**"], "root": null }, "valid": false }, - { "name": "null_revalidate", "grant": { "publish": ["**"], "revalidate": null }, "valid": false }, - { "name": "null_tier", "grant": { "publish": ["**"], "tier": null }, "valid": false }, - { "name": "null_label", "grant": { "publish": ["**"], "label": null }, "valid": false }, - { "name": "null_mounts", "grant": { "publish": ["**"], "mounts": null }, "valid": false }, - { "name": "null_peer", "grant": { "publish": ["**"], "peer": null }, "valid": false }, - { "name": "positional", "grant": [["**"]], "valid": false }, + "grant": { "publish": [""], "link": "upstream", "peer": false }, + "valid": false + }, + { "name": "unknown_link", "grant": { "publish": [""], "link": "downstream" }, "valid": false }, + { "name": "null_link", "grant": { "publish": [""], "link": null }, "valid": false }, + { "name": "null_root", "grant": { "publish": [""], "root": null }, "valid": false }, + { "name": "null_revalidate", "grant": { "publish": [""], "revalidate": null }, "valid": false }, + { "name": "null_tier", "grant": { "publish": [""], "tier": null }, "valid": false }, + { "name": "null_label", "grant": { "publish": [""], "label": null }, "valid": false }, + { "name": "null_mounts", "grant": { "publish": [""], "mounts": null }, "valid": false }, + { "name": "null_peer", "grant": { "publish": [""], "peer": null }, "valid": false }, + { "name": "positional", "grant": [[""]], "valid": false }, + { + "name": "subtree_patterns", + "grant": { "publish": ["**"], "subscribe": ["room/**", "lobby"] }, + "valid": true + }, + { + "name": "subtree_pattern_in_a_capped_role", + "grant": { "publish": { "path": ["room/**"], "rate": 1000 } }, + "valid": true + }, + { "name": "wildcard_member", "grant": { "subscribe": ["room", "lobby/*"] }, "valid": false }, { "name": "null_grant", "grant": null, "valid": false } ], "roots": [ diff --git a/rs/moq-auth/tests/released_token.rs b/rs/moq-auth/tests/released_token.rs index e00aba1b2cf..5957a1c4922 100644 --- a/rs/moq-auth/tests/released_token.rs +++ b/rs/moq-auth/tests/released_token.rs @@ -1,15 +1,19 @@ //! The token and grant encodings released issuers and verifiers depend on. //! -//! Tokens are written in the documented shape, `root`, `publish`, and `subscribe`, which -//! every moq-auth release reads. Tokens minted by `moq-token`, the format before -//! patterns, are still read: each `put`/`get` prefix `p` grants the subtree `p/**`. +//! Tokens are written in the documented shape, `root`, `publish`, and `subscribe`. Releases +//! before 0.22 read each member as a pattern, so a bare `p` would be the one path `p`: until +//! the version migrations `wire::alias` names, each prefix is written as the subtree pattern +//! `p/**`, and the empty prefix as `**`, which every release reads as the same subtree. What +//! released issuers write is read back alike: the `put`/`get` prefixes they write when every +//! member is a subtree, and the `publish`/`subscribe` patterns they write otherwise, whose +//! `p/**` and `**` members read as prefixes. //! Published moq-auth 0.2.1 refuses a role written as an object, so a capped role is a //! shape it refuses in full rather than misread. use moq_auth::{Algorithm, Claims, Key}; use serde_json::{Value, json}; -fn patterns(texts: &[&str]) -> moq_auth::Patterns { +fn prefixes(texts: &[&str]) -> moq_auth::Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } @@ -32,28 +36,29 @@ fn grant(claims: &Claims) -> Value { } #[test] -fn grants_are_written_as_patterns() { +fn grants_are_written_as_subtree_patterns() { for (claims, expected) in [ ( Claims::default() .with_root("room") - .with_publish(patterns(&["alice/**"])) - .with_subscribe(patterns(&["**"])), + .with_publish(prefixes(&["alice"])) + .with_subscribe(prefixes(&[""])), json!({"root": "room", "publish": ["alice/**"], "subscribe": ["**"]}), ), ( - Claims::default().with_root("room").with_publish(patterns(&["alice"])), - json!({"root": "room", "publish": ["alice"]}), + Claims::default().with_root("room").with_publish(prefixes(&["alice"])), + json!({"root": "room", "publish": ["alice/**"]}), ), ( - Claims::default().with_subscribe(patterns(&["*/chat"])), - json!({"subscribe": ["*/chat"]}), + Claims::default().with_subscribe(prefixes(&["room/chat", "lobby/chat"])), + json!({"subscribe": ["lobby/chat/**", "room/chat/**"]}), ), ( + // A member beneath another adds nothing, so it is never written. Claims::default() .with_root("room") - .with_publish(patterns(&["alice", "*/chat"])), - json!({"root": "room", "publish": ["*/chat", "alice"]}), + .with_publish(prefixes(&["bob", "alice/cam", "alice"])), + json!({"root": "room", "publish": ["alice/**", "bob/**"]}), ), ] { assert_eq!(wire(&claims), expected, "{claims:?}"); @@ -63,9 +68,69 @@ fn grants_are_written_as_patterns() { } } +/// Claims a released issuer wrote as patterns, since moq-dev/moq#3746, read as prefixes: +/// a subtree pattern as its prefix, and an exact path as the prefix it names, which widens +/// it to its subtree. `tests/corpus.json` holds a signed one. +#[test] +fn released_patterns_read_as_prefixes() { + for (payload, publish, subscribe) in [ + // What `moq auth sign --root compat --publish 'video/**' --publish bob` writes in + // moq 0.14.17, since one member is not a subtree. + ( + json!({"root": "compat", "publish": ["bob", "video/**"]}), + &["bob", "video"][..], + &[][..], + ), + (json!({"subscribe": ["**"]}), &[], &[""]), + ( + json!({"publish": {"path": ["alice/**"], "rate": 8_000_000}}), + &["alice"], + &[], + ), + ] { + let claims = serde_json::from_value::(payload.clone()).unwrap(); + assert_eq!(claims.publish.path, prefixes(publish), "{payload}"); + assert_eq!(claims.subscribe.path, prefixes(subscribe), "{payload}"); + } +} + +/// A pattern no prefix carries is refused loudly rather than read as a narrower or +/// wider grant. +#[test] +fn other_wildcards_are_refused() { + for payload in [ + json!({"subscribe": ["*/chat"]}), + json!({"publish": ["cam-*.hang"]}), + json!({"publish": ["room/*/**"]}), + json!({"publish": ["**/room"]}), + ] { + let err = serde_json::from_value::(payload.clone()).unwrap_err(); + assert!(err.to_string().contains("wildcard"), "{payload}: {err}"); + } +} + +/// What `moq auth generate` writes into a key's scope, which a released signer and verifier +/// read as the same subtrees, and what moq 0.14.17's writes, which reads back alike. +#[test] +fn key_scopes_are_written_as_subtree_patterns() { + let scope = moq_auth::Scope { + root: "compat".into(), + publish: prefixes(&["video"]), + subscribe: prefixes(&[""]), + }; + let wire = serde_json::to_value(&scope).unwrap(); + assert_eq!( + wire, + json!({"root": "compat", "publish": ["video/**"], "subscribe": ["**"]}) + ); + assert_eq!(serde_json::from_value::(wire).unwrap(), scope); + let released = json!({"root": "compat", "put": ["video"], "get": [""]}); + assert_eq!(serde_json::from_value::(released).unwrap(), scope); +} + /// A token `@moq/auth` minted in the `moq-token` prefix form, before tokens were patterns. #[test] -fn legacy_prefixes_verify_as_subtrees() { +fn legacy_prefixes_verify_as_prefixes() { let key: Key = r#"{"kty":"oct","alg":"HS256","k":"xm6xsSkfFqzPU3KfcbAcF2_h0OkStxQ_nNqVPYl0ync","kid":"js-test-key","key_ops":["sign","verify"]}"# .parse() .unwrap(); @@ -73,8 +138,8 @@ fn legacy_prefixes_verify_as_subtrees() { let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImpzLXRlc3Qta2V5In0.eyJyb290IjoibGl2ZSIsInB1dCI6WyJjYW1lcmExIl0sImdldCI6WyJjYW1lcmExIiwiY2FtZXJhMiJdLCJpYXQiOjE3NzUxNzY3NTR9.tHNQtHh_HCIKxXOexDCM7AkjqWzbULLZzjEckfOGRfY"; let claims = key.verify(token).unwrap(); - assert_eq!(claims.publish.path, patterns(&["camera1/**"])); - assert_eq!(claims.subscribe.path, patterns(&["camera1/**", "camera2/**"])); + assert_eq!(claims.publish.path, prefixes(&["camera1"])); + assert_eq!(claims.subscribe.path, prefixes(&["camera1", "camera2"])); // Signed again, it is written in the documented shape. assert_eq!( wire(&claims), @@ -88,8 +153,8 @@ fn legacy_prefixes_verify_as_subtrees() { fn capped_roles_are_written_as_objects() { let rate = moq_auth::Rate::from_bps(8_000_000).unwrap(); let claims = Claims::default() - .with_publish(patterns(&["alice/**"])) - .with_subscribe(patterns(&["**"])); + .with_publish(prefixes(&["alice"])) + .with_subscribe(prefixes(&[""])); for (claims, expected) in [ ( claims.clone().with_publish_rate(rate), diff --git a/rs/moq-bench/src/connection.rs b/rs/moq-bench/src/connection.rs index 2b2e9d99e9a..1f0a0820519 100644 --- a/rs/moq-bench/src/connection.rs +++ b/rs/moq-bench/src/connection.rs @@ -316,7 +316,7 @@ async fn produce( fn discover(consume: &moq_net::origin::Producer, name: &str) -> moq_net::origin::Consumer { consume .consume() - .scope(name, &moq_net::Patterns::from(moq_net::Pattern::all())) + .scope(name, &moq_net::Prefixes::all()) .expect("origin must permit the bench namespace") } diff --git a/rs/moq-boy/src/main.rs b/rs/moq-boy/src/main.rs index f975b372de4..e80be3134e2 100644 --- a/rs/moq-boy/src/main.rs +++ b/rs/moq-boy/src/main.rs @@ -278,7 +278,7 @@ async fn run(config: &Config) -> Result<()> { let consume_origin = moq_tokio::origin::spawn(); let viewers = consume_origin .consume() - .scope(&viewer_path, &moq_net::Patterns::from(moq_net::Pattern::all())) + .scope(&viewer_path, &moq_net::Prefixes::all()) .context("invalid viewer prefix")?; tracing::info!(url = %moq_tokio::RedactedUrl::new(&url), %name, broadcast = %broadcast_path, viewers = %viewer_path, "connecting to relay"); diff --git a/rs/moq-cli/README.md b/rs/moq-cli/README.md index 7b613552ef5..edf99e43ef8 100644 --- a/rs/moq-cli/README.md +++ b/rs/moq-cli/README.md @@ -60,7 +60,7 @@ Hosts a MoQ server and publishes a single broadcast read from stdin into it. Use ```bash ffmpeg -i input.mp4 -c copy -f mpegts -pes_payload_size 0 -muxdelay 0 - | \ - moq --listen '[::]:4443' --listen-tls-generate localhost --auth-public '**' --broadcast my-stream.hang import ts + moq --listen '[::]:4443' --listen-tls-generate localhost --auth-public '' --broadcast my-stream.hang import ts ``` ### Self-host: subscribe to an inbound broadcast @@ -68,7 +68,7 @@ ffmpeg -i input.mp4 -c copy -f mpegts -pes_payload_size 0 -muxdelay 0 - | \ Hosts a MoQ server and writes an incoming broadcast's media to stdout. The inverse of the above. ```bash -moq --listen '[::]:4443' --listen-tls-generate localhost --auth-public '**' --broadcast my-stream.hang export ts | ffplay - +moq --listen '[::]:4443' --listen-tls-generate localhost --auth-public '' --broadcast my-stream.hang export ts | ffplay - ``` ### Import formats diff --git a/rs/moq-cli/src/announced.rs b/rs/moq-cli/src/announced.rs index 8ec9b04b934..22c5f77cd85 100644 --- a/rs/moq-cli/src/announced.rs +++ b/rs/moq-cli/src/announced.rs @@ -65,9 +65,9 @@ async fn follow( let prefix = prefix.unwrap_or_default(); // Scoped to the prefix, so the session only asks the relay for what is followed. - let pattern = moq_net::Pattern::subtree(prefix).with_context(|| format!("invalid prefix `{prefix}`"))?; + let scope = moq_net::Prefix::try_from(prefix).with_context(|| format!("invalid prefix `{prefix}`"))?; let origin = moq_tokio::origin::spawn() - .scope("", &moq_net::Patterns::from(pattern)) + .scope("", &moq_net::Prefixes::from(scope)) .with_context(|| format!("failed to scope to `{prefix}`"))?; // Subscribe-only: this session reads and never publishes. diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index 89e4f963558..74edd0efb97 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -2215,7 +2215,7 @@ mod tests { "--listen-tcp-bind", "127.0.0.1:0", "--auth-public", - "**", + "", "import", "ts", ]) @@ -2233,7 +2233,7 @@ mod tests { "--listen-unix-bind", "/tmp/moq-cli.sock", "--auth-public", - "**", + "", "export", "ts", ]) @@ -2256,7 +2256,7 @@ mod tests { argv.extend(["import", "ts"]); Invocation::try_parse_from(argv).expect("parse") }; - let err = parse(["--auth-public", "**"]).moq.validate().unwrap_err().to_string(); + let err = parse(["--auth-public", ""]).moq.validate().unwrap_err().to_string(); assert!(err.contains("--auth-public ignores"), "{err}"); assert!(parse(["--auth-url", "http://127.0.0.1:4440/"]).moq.validate().is_ok()); } @@ -2268,7 +2268,7 @@ mod tests { "--listen-tcp-bind", "127.0.0.1:0", "--auth-public", - "**", + "", "--drain-timeout", "500ms", "import", @@ -2302,7 +2302,7 @@ mod tests { "--listen-tcp-bind", "127.0.0.1:0", "--auth-public", - "**", + "", "--drain-timeout", "500ms", "play", @@ -2327,7 +2327,7 @@ mod tests { "--auth-url", "http://127.0.0.1:4440/", "--auth-public", - "**", + "", "import", "ts", ]) @@ -2347,7 +2347,7 @@ mod tests { assert!(cli.moq.validate().is_ok()); // A local verb refuses the flag like every other MoQ-side flag. - let cli = Invocation::try_parse_from(["moq", "--auth-public", "**", "auth", "generate"]).expect("parse"); + let cli = Invocation::try_parse_from(["moq", "--auth-public", "", "auth", "generate"]).expect("parse"); assert!(cli.reject("auth").unwrap_err().to_string().contains("--auth-public")); } @@ -2851,7 +2851,7 @@ mod tests { &["--listen-lb-id", "01"], &["--listen-tcp-bind", "127.0.0.1:0"], &["--cluster-node", "https://self.example"], - &["--auth-public", "**"], + &["--auth-public", ""], &["--drain-timeout", "500ms"], ]; let dial: &[&[&str]] = &[ @@ -2909,7 +2909,7 @@ mod tests { &["--listen-tls-cert", "cert.pem"], &["--listen-tcp-tls"], &["--listen-version", "moq-lite-02"], - &["--auth-public", "**"], + &["--auth-public", ""], ] { let err = parse(flag).unserved().unwrap_err().to_string(); assert!(err.contains(flag[0]), "{flag:?}: {err}"); diff --git a/rs/moq-cli/src/auth.rs b/rs/moq-cli/src/auth.rs index c08b60926b7..ccfa26e161e 100644 --- a/rs/moq-cli/src/auth.rs +++ b/rs/moq-cli/src/auth.rs @@ -7,7 +7,7 @@ use std::net::SocketAddr; use std::{io, path::PathBuf}; use moq_auth::serve::{Keys, Policy}; -use moq_auth::{Algorithm, Pattern, Permissions}; +use moq_auth::{Algorithm, Permissions, Prefix}; /// Generate, sign, and verify the JWT tokens a relay authenticates with. #[derive(usage_rs::Args, Clone, Debug)] @@ -174,13 +174,13 @@ enum Command { #[usage(long, default = "")] root: String, - /// Publish patterns the key may grant (repeatable); `foo/**` for a subtree. + /// Publish prefixes the key may grant (repeatable); each covers its path and everything beneath it, `""` everything. #[usage(long)] - publish: Vec, + publish: Vec, - /// Subscribe patterns the key may grant (repeatable); `foo/**` for a subtree. + /// Subscribe prefixes the key may grant (repeatable); each covers its path and everything beneath it, `""` everything. #[usage(long)] - subscribe: Vec, + subscribe: Vec, }, /// Sign a token, writing it to stdout. @@ -193,13 +193,13 @@ enum Command { #[usage(long, default = "")] root: String, - /// Patterns the user can publish (repeatable); `foo/**` for a subtree. + /// Prefixes the user can publish (repeatable); each covers its path and everything beneath it, `""` everything. #[usage(long)] - publish: Vec, + publish: Vec, - /// Patterns the user can subscribe to (repeatable); `foo/**` for a subtree. + /// Prefixes the user can subscribe to (repeatable); each covers its path and everything beneath it, `""` everything. #[usage(long)] - subscribe: Vec, + subscribe: Vec, /// Expiration time as a unix timestamp, instead of `--ttl` from now. #[usage(long, conflicts("--ttl", "--unbounded"))] @@ -458,13 +458,13 @@ pub struct Serve { #[usage(unknown_flags = "error", args_override_self = false)] #[serde(deny_unknown_fields, default)] struct PublicRules { - /// Patterns an anonymous session may publish, rooted at `/` (repeatable); `foo/**` for a subtree. + /// Prefixes an anonymous session may publish, rooted at `/` (repeatable); each covers its path and everything beneath it. #[usage(name = "public-publish", long = "public-publish", setting = "public.publish")] - publish: Vec, + publish: Vec, - /// Patterns an anonymous session may subscribe to, rooted at `/` (repeatable). + /// Prefixes an anonymous session may subscribe to, rooted at `/` (repeatable). #[usage(name = "public-subscribe", long = "public-subscribe", setting = "public.subscribe")] - subscribe: Vec, + subscribe: Vec, } /// What a session presenting a verified certificate may do. @@ -472,13 +472,13 @@ struct PublicRules { #[usage(unknown_flags = "error", args_override_self = false)] #[serde(deny_unknown_fields, default)] struct MtlsRules { - /// Patterns a session with a verified certificate may publish, rooted at `/` (repeatable). Empty refuses certificates no rule names. + /// Prefixes a session with a verified certificate may publish, rooted at `/` (repeatable). Empty refuses certificates no rule names. #[usage(name = "mtls-publish", long = "mtls-publish", setting = "mtls.publish")] - publish: Vec, + publish: Vec, - /// Patterns a session with a verified certificate may subscribe to, rooted at `/` (repeatable). + /// Prefixes a session with a verified certificate may subscribe to, rooted at `/` (repeatable). #[usage(name = "mtls-subscribe", long = "mtls-subscribe", setting = "mtls.subscribe")] - subscribe: Vec, + subscribe: Vec, /// The role verified certificates have on cluster links: peer or upstream. #[usage( @@ -641,10 +641,10 @@ impl Serve { anyhow::bail!("--key, --key-dir, and --key-set conflict; pick one"); } - let rules = |publish: &[Pattern], subscribe: &[Pattern]| { + let rules = |publish: &[Prefix], subscribe: &[Prefix]| { Permissions::new( - publish.iter().cloned().collect::(), - subscribe.iter().cloned().collect::(), + publish.iter().cloned().collect::(), + subscribe.iter().cloned().collect::(), ) }; let mut policy = Policy::default(); @@ -1038,11 +1038,11 @@ mod tests { "--key-dir", "/keys", "--public-subscribe", - "anon/**", + "anon", "--mtls-publish", - "**", + "", "--mtls-subscribe", - "**", + "", "--mtls-link", "upstream", "--tier", @@ -1059,12 +1059,9 @@ mod tests { .policy() .unwrap(); assert!(matches!(&policy.keys, Some(Keys::Dir(dir)) if dir == std::path::Path::new("/keys"))); - assert_eq!( - policy.public.subscribe, - ["anon/**".parse().unwrap()].into_iter().collect() - ); + assert_eq!(policy.public.subscribe, ["anon".parse().unwrap()].into_iter().collect()); assert!(policy.public.publish.is_empty()); - assert_eq!(policy.mtls.publish, ["**".parse().unwrap()].into_iter().collect()); + assert_eq!(policy.mtls.publish, ["".parse().unwrap()].into_iter().collect()); assert_eq!(policy.mtls_link, Some(moq_auth::Link::Upstream)); assert_eq!(policy.tier.as_deref(), Some("internal")); assert_eq!(policy.revalidate, Some(std::time::Duration::from_secs(30))); @@ -1131,11 +1128,11 @@ revalidate = "30s" expires = "2h" [mtls] -subscribe = ["**"] +subscribe = [""] link = "peer" [mtls.certificates.{leaf}] -publish = {{ path = ["alice/**"], rate = 1000000 }} +publish = {{ path = ["alice"], rate = 1000000 }} link = "none" "# ), @@ -1152,23 +1149,23 @@ link = "none" let policy = serve.policy().unwrap(); assert_eq!(policy.tier.as_deref(), Some("from-flag")); assert_eq!(policy.revalidate, Some(std::time::Duration::from_secs(30))); - assert_eq!(policy.mtls.subscribe, ["**".parse().unwrap()].into_iter().collect()); + assert_eq!(policy.mtls.subscribe, ["".parse().unwrap()].into_iter().collect()); assert_eq!(policy.mtls_link, Some(moq_auth::Link::Peer)); let rule = &policy.certificates[&leaf.parse().unwrap()]; let publish = rule.publish.as_ref().expect("the rule names publish"); - assert_eq!(publish.path, ["alice/**".parse().unwrap()].into_iter().collect()); + assert_eq!(publish.path, ["alice".parse().unwrap()].into_iter().collect()); assert_eq!(publish.rate, moq_auth::Rate::from_bps(1_000_000).ok()); assert_eq!(rule.subscribe, None); assert_eq!(rule.link, Some(moq_auth::serve::RuleLink::None)); // A flag replaces the file's list rather than adding to it. - let (serve, _) = merged(&["moq", "auth", "serve", path, "--mtls-subscribe", "room/**"]).unwrap(); - assert_eq!(serve.mtls.subscribe, vec!["room/**".parse::().unwrap()]); + let (serve, _) = merged(&["moq", "auth", "serve", path, "--mtls-subscribe", "room"]).unwrap(); + assert_eq!(serve.mtls.subscribe, vec!["room".parse::().unwrap()]); for (body, needs) in [ ("lisen = \"127.0.0.1:1\"", "lisen"), - ("[mtls.certificates.nothex]\npublish = [\"**\"]", "SHA-256"), - (&format!("[mtls.certificates.{leaf}]\npublsh = [\"**\"]"), "publsh"), + ("[mtls.certificates.nothex]\npublish = [\"\"]", "SHA-256"), + (&format!("[mtls.certificates.{leaf}]\npublsh = [\"\"]"), "publsh"), (&format!("[mtls.certificates.{leaf}]\nlink = \"sideways\""), "sideways"), ] { std::fs::write(&file, body).unwrap(); @@ -1197,7 +1194,7 @@ link = "none" let file = dir.path().join("auth.toml"); let leaf = "ab".repeat(32); let write = |body: &str| std::fs::write(&file, body).unwrap(); - write(&format!("[mtls.certificates.{leaf}]\npublish = [\"**\"]")); + write(&format!("[mtls.certificates.{leaf}]\npublish = [\"\"]")); // Reloads start from the command line, never from a merge that already holds the file. let (serve, cli) = parsed(&["moq", "auth", "serve", file.to_str().unwrap()]); let env = usage_rs::config::EnvLayer::new([]); @@ -1214,18 +1211,18 @@ link = "none" let publish = |grant: Option| grant.unwrap().publish.path; assert_eq!( publish(server.answer(&session).await.unwrap()), - ["**".parse().unwrap()].into_iter().collect() + ["".parse().unwrap()].into_iter().collect() ); session.event = moq_auth::Event::Revalidate; - write(&format!("[mtls.certificates.{leaf}]\npublish = [\"alice/**\"]")); + write(&format!("[mtls.certificates.{leaf}]\npublish = [\"alice\"]")); serve.apply(&server, &cli, &env).unwrap(); - let narrowed = ["alice/**".parse().unwrap()].into_iter().collect(); + let narrowed = ["alice".parse().unwrap()].into_iter().collect(); assert_eq!(publish(server.answer(&session).await.unwrap()), narrowed); for broken in [ - format!("[mtls.certificates.{leaf}]\npublish = [\"alice/**\""), - format!("[mtls.certificates.{leaf}]\npublish = [\"alice/**\"]\ntier = \"not a tier\""), + format!("[mtls.certificates.{leaf}]\npublish = [\"alice\""), + format!("[mtls.certificates.{leaf}]\npublish = [\"alice\"]\ntier = \"not a tier\""), "revalidate = \"1m\"".to_string(), ] { write(&broken); @@ -1238,9 +1235,9 @@ link = "none" assert!(server.answer(&session).await.is_err(), "deleting the rule revokes it"); } - /// A rule without a wildcard names exactly one broadcast. + /// A rule is a prefix, covering its path and everything beneath it. #[test] - fn serve_accepts_a_rule_without_a_wildcard() { + fn serve_accepts_prefix_rules() { for (flag, rule) in [ ("--public-publish", "anon"), ("--public-subscribe", "event/cam1.hang"), @@ -1254,10 +1251,21 @@ link = "none" .unwrap(); assert_eq!( policy.public.publish, - ["anon".parse::().unwrap()].into_iter().collect() + ["anon".parse::().unwrap()].into_iter().collect() ); } + /// A wildcard rule is refused where it is parsed, naming the prefix to write instead. + #[test] + fn serve_refuses_a_wildcard_rule() { + for rule in ["anon/**", "**", "*", "*/chat", "cam-*.hang"] { + let err = parse(&["moq", "auth", "serve", "--public-publish", rule]).unwrap_err(); + assert!(format!("{err:#}").contains("wildcard"), "{rule}: {err:#}"); + } + let err = parse(&["moq", "auth", "serve", "--public-publish", "anon/**"]).unwrap_err(); + assert!(format!("{err:#}").contains("`anon` covers `anon`"), "{err:#}"); + } + #[cfg(unix)] #[test] fn unlink_socket_leaves_anything_but_a_socket_alone() { @@ -1309,7 +1317,7 @@ link = "none" "--root", "demo", "--publish", - "alice/**", + "alice", "--issued", "1700000000", "--expires", @@ -1326,7 +1334,7 @@ link = "none" .sign( &moq_auth::Claims::default() .with_root("demo") - .with_publish(["alice/**".parse().unwrap()]), + .with_publish(["alice".parse().unwrap()]), ) .unwrap(); let path = dir.path().join("alice.jwt"); diff --git a/rs/moq-cli/src/fetch.rs b/rs/moq-cli/src/fetch.rs index 90ec1c3b9c5..803cab53c8a 100644 --- a/rs/moq-cli/src/fetch.rs +++ b/rs/moq-cli/src/fetch.rs @@ -63,9 +63,10 @@ async fn fetch( // Scoped to the broadcast, so the relay announces it by name even when a hidden // segment such as `.stats` would keep it out of an unscoped listing. - let pattern = moq_net::Pattern::subtree(&broadcast).with_context(|| format!("invalid broadcast `{broadcast}`"))?; + let scope = + moq_net::Prefix::try_from(broadcast.as_str()).with_context(|| format!("invalid broadcast `{broadcast}`"))?; let origin = moq_tokio::origin::spawn() - .scope("", &moq_net::Patterns::from(pattern)) + .scope("", &moq_net::Prefixes::from(scope)) .with_context(|| format!("failed to scope to `{broadcast}`"))?; // Subscribe-only: this session reads and never publishes. diff --git a/rs/moq-cli/src/hls.rs b/rs/moq-cli/src/hls.rs index 4ecdc804fae..e9a8b9325e1 100644 --- a/rs/moq-cli/src/hls.rs +++ b/rs/moq-cli/src/hls.rs @@ -273,8 +273,8 @@ fn playlist_url(playlist: &str) -> anyhow::Result { /// `//master.m3u8` and `//manifest.mpd`); other broadcasts in the /// Origin are not served. pub async fn export(origin: moq_net::origin::Consumer, args: ExportArgs, name: String) -> anyhow::Result<()> { - let scope = moq_net::Patterns::from( - moq_net::Pattern::subtree(&name).with_context(|| format!("invalid broadcast name `{name}`"))?, + let scope = moq_net::Prefixes::from( + moq_net::Prefix::try_from(name.as_str()).with_context(|| format!("invalid broadcast name `{name}`"))?, ); let scoped = origin .scope("", &scope) diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index b356abdd22c..323af4ea399 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -1222,7 +1222,7 @@ mod tests { let (trigger, observer) = moq_relay::shutdown::Observer::new(window); let (auth, mut admissions) = moq_relay::auth::Auth::embedded("test"); - let all = moq_auth::Pattern::all; + let all = moq_auth::Prefixes::all; let (producer, consumer) = moq_auth::lease::Producer::new(moq_auth::Grant::new(all(), all())); let (lease, ()) = tokio::join!(auth.admit(auth.request(moq_auth::Transport::Tcp, "/room")), async { admissions.next().await.unwrap().grant(consumer); @@ -1396,8 +1396,8 @@ mod tests { "{timeout}: decided before the grant" ); admission.grant(moq_auth::lease::Consumer::fixed(moq_auth::Grant::new( - moq_auth::Pattern::all(), - moq_auth::Pattern::all(), + moq_auth::Prefixes::all(), + moq_auth::Prefixes::all(), ))); tokio::time::resume(); let connection = kio::clock::timeout(TIMEOUT, listening.connection.clone().established()) @@ -1573,7 +1573,7 @@ mod tests { "--listen-tcp-bind", &addr, "--auth-public", - "**", + "", "import", "ts", ]) @@ -1635,7 +1635,7 @@ mod tests { "--listen-tcp-bind", "127.0.0.1:0", "--auth-public", - "**", + "", "import", "ts", ]) diff --git a/rs/moq-cli/src/webrtc.rs b/rs/moq-cli/src/webrtc.rs index 2950eb28bc0..a9a1ac2ee8a 100644 --- a/rs/moq-cli/src/webrtc.rs +++ b/rs/moq-cli/src/webrtc.rs @@ -64,8 +64,8 @@ pub async fn listen_import(target: ImportTarget, listen: Listen) -> anyhow::Resu /// WHEP server: serve WebRTC plays of `name` from the Origin (export). pub async fn listen_export(origin: moq_net::origin::Consumer, name: String, listen: Listen) -> anyhow::Result<()> { - let scope = moq_net::Patterns::from( - moq_net::Pattern::subtree(&name).with_context(|| format!("invalid broadcast name `{name}`"))?, + let scope = moq_net::Prefixes::from( + moq_net::Prefix::try_from(name.as_str()).with_context(|| format!("invalid broadcast name `{name}`"))?, ); let subscriber = origin .scope("", &scope) @@ -76,8 +76,8 @@ pub async fn listen_export(origin: moq_net::origin::Consumer, name: String, list /// Restrict a producer to the single broadcast `name` so a WHIP peer can only publish it. fn scope_producer(origin: &moq_net::origin::Producer, name: &str) -> anyhow::Result { - let scope = moq_net::Patterns::from( - moq_net::Pattern::subtree(name).with_context(|| format!("invalid broadcast name `{name}`"))?, + let scope = moq_net::Prefixes::from( + moq_net::Prefix::try_from(name).with_context(|| format!("invalid broadcast name `{name}`"))?, ); origin .scope("", &scope) diff --git a/rs/moq-cli/tests/integration/export.rs b/rs/moq-cli/tests/integration/export.rs index 6f4f64aedb9..67ee40de3de 100644 --- a/rs/moq-cli/tests/integration/export.rs +++ b/rs/moq-cli/tests/integration/export.rs @@ -98,7 +98,7 @@ async fn relay() -> Relay { config.listen.version = std::iter::once(LITE_07.parse().unwrap()) .chain(moq_net::Versions::all().iter().copied()) .collect(); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = moq_relay::Relay::load(config).await.expect("test relay"); let url = format!("https://{}/", relay.quic_addr().expect("QUIC address")); let ready = relay.ready(); @@ -1084,8 +1084,8 @@ async fn next_instance(announced: &mut moq_net::origin::Routes) -> moq_net::Inst #[ignore = "real-time process test: just rs cli-export"] async fn a_passthrough_export_ends_with_its_redundant_importer() { let relay = relay().await; - let demo = moq_net::Patterns::from(moq_net::Pattern::literal("demo").unwrap()); - let mut announced = relay.origin.scope("", &demo).unwrap().routes(); + let demo = moq_net::Pattern::literal("demo").unwrap(); + let mut announced = relay.origin.filter(&demo).unwrap().routes(); let (feed, feeding) = redundant_feed(); let mut first = redundant_import(&relay, &feed, &["--passthrough"]); let incumbent = next_instance(&mut announced).await; diff --git a/rs/moq-cli/tests/integration/instances.rs b/rs/moq-cli/tests/integration/instances.rs index 16fd3f4cd72..7dcdfe69bd4 100644 --- a/rs/moq-cli/tests/integration/instances.rs +++ b/rs/moq-cli/tests/integration/instances.rs @@ -30,7 +30,7 @@ async fn relay() -> Relay { config.listen.version = std::iter::once(LITE_07.parse().unwrap()) .chain(moq_net::Versions::all().iter().copied()) .collect(); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = moq_relay::Relay::load(config).await.unwrap(); let url = format!("https://{}/", relay.quic_addr().unwrap()).parse().unwrap(); let ready = relay.ready(); @@ -151,8 +151,8 @@ async fn replacement(version: &str) { let viewer_origin = moq_tokio::origin::spawn(); let viewer = viewer_origin.consume(); let watch = |path| { - let path = moq_net::Patterns::from(moq_net::Pattern::literal(path).unwrap()); - viewer.scope("", &path).unwrap().routes() + let path = moq_net::Pattern::literal(path).unwrap(); + viewer.filter(&path).unwrap().routes() }; let mut catalog_follow = watch("live/catalog.hang"); let mut media_follow = watch("live/media"); diff --git a/rs/moq-cli/tests/integration/listen.rs b/rs/moq-cli/tests/integration/listen.rs index 7803650ff11..1b6f435a329 100644 --- a/rs/moq-cli/tests/integration/listen.rs +++ b/rs/moq-cli/tests/integration/listen.rs @@ -33,7 +33,7 @@ async fn listen(stage: &str, flags: &[&str]) -> Listening { "--listen-unix-bind", socket.to_str().unwrap(), "--auth-public", - "**", + "", "--broadcast", "demo.hang", ]) diff --git a/rs/moq-ffi/src/auth.rs b/rs/moq-ffi/src/auth.rs index 78282b6ef16..dfaa0a44614 100644 --- a/rs/moq-ffi/src/auth.rs +++ b/rs/moq-ffi/src/auth.rs @@ -5,12 +5,13 @@ use crate::ffi::Polled; /// What the peer lets this side do, in this side's own paths. /// -/// An empty pattern list grants nothing in that direction; `**` grants everything. +/// Each entry is a path prefix covering that path and everything beneath it. An empty list +/// grants nothing in that direction; the empty prefix `""` grants everything. #[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)] pub struct MoqGrant { - /// The broadcast patterns this side may publish to the peer. + /// The broadcast prefixes this side may publish to the peer. pub publish: Vec, - /// The broadcast patterns this side may subscribe to from the peer. + /// The broadcast prefixes this side may subscribe to from the peer. pub subscribe: Vec, /// How long until the grant lapses, measured when it was read, or `None` for never. pub expires: Option, @@ -18,10 +19,10 @@ pub struct MoqGrant { impl From for MoqGrant { fn from(grant: moq_net::auth::Grant) -> Self { - let patterns = |patterns: &moq_net::Patterns| patterns.iter().map(ToString::to_string).collect(); + let prefixes = |prefixes: &moq_net::Prefixes| prefixes.iter().map(ToString::to_string).collect(); Self { - publish: patterns(&grant.publish), - subscribe: patterns(&grant.subscribe), + publish: prefixes(&grant.publish), + subscribe: prefixes(&grant.subscribe), expires: grant .expires .map(|expires| crate::ffi::duration(expires.saturating_duration_since(moq_net::time::Instant::now()))), diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index 1333d0a3b3b..02a15e17ba0 100644 --- a/rs/moq-ffi/src/origin.rs +++ b/rs/moq-ffi/src/origin.rs @@ -358,7 +358,7 @@ impl MoqOriginProducer { #[uniffi::export] impl MoqOriginConsumer { - /// Subscribe to routes whose names match a pattern scope; updates stay relative to the origin. + /// Subscribe to routes whose names match a discovery filter; updates stay relative to the origin. pub fn announced(&self, config: MoqAnnounceConfig) -> Result, MoqError> { let _guard = crate::ffi::enter(); self.driver.check()?; @@ -367,10 +367,7 @@ impl MoqOriginConsumer { None => moq_net::Pattern::all(), }; let filter = filter.rooted(&config.prefix)?; - let origin = self - .inner - .scope("", &moq_net::Patterns::from(filter))? - .with_hidden(config.hidden); + let origin = self.inner.filter(&filter)?.with_hidden(config.hidden); Ok(Arc::new(MoqAnnounceConsumer { reads: Polled::new(origin.announced()).supervised(self.driver.clone()), })) diff --git a/rs/moq-ffi/src/server.rs b/rs/moq-ffi/src/server.rs index 7c33990bd68..943e52b4fcf 100644 --- a/rs/moq-ffi/src/server.rs +++ b/rs/moq-ffi/src/server.rs @@ -676,8 +676,8 @@ mod lifecycle_tests { let peer = pollster::block_on(request.accept(None, None)).unwrap(); let session = pollster::block_on(connect).unwrap().unwrap(); let everything = crate::auth::MoqGrant { - publish: vec!["**".into()], - subscribe: vec!["**".into()], + publish: vec!["".into()], + subscribe: vec!["".into()], expires: None, }; for grants in [peer.grants(), session.grants()] { diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 281228906e9..7b5f04072ae 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -9373,8 +9373,8 @@ async fn auth_reads_the_peer_grant() { .expect("pair"); // Each side's origins span the root, so its peer grants it everything. - await_grant(&client, grant(&["**"], &["**"])).await; - await_grant(&accepted, grant(&["**"], &["**"])).await; + await_grant(&client, grant(&[""], &[""])).await; + await_grant(&accepted, grant(&[""], &[""])).await; assert!(accepted.tokens().is_none(), "an accepted session presents no tokens"); // The live session was dialed without a credential, so this one rides AUTH. @@ -9386,7 +9386,7 @@ async fn auth_reads_the_peer_grant() { assert!(matches!(err, MoqError::Unsupported), "refusal: {err:?}"); assert_eq!( client.grants().grant(), - Some(grant(&["**"], &["**"])), + Some(grant(&[""], &[""])), "a refusal leaves the union" ); @@ -9425,9 +9425,9 @@ async fn auth_tokens_join_and_leave_the_union() { let mut listener = config.init(Default::default()).unwrap().listen().await.unwrap(); let addr = listener.tcp_local_addr().unwrap(); - let publish = |pattern: &str| { + let publish = |prefix: &str| { let mut grant = moq_net::auth::Grant::default(); - grant.publish = pattern.parse::().unwrap().into(); + grant.publish = prefix.parse::().unwrap().into(); grant }; let serving = tokio::spawn(async move { @@ -9437,8 +9437,8 @@ async fn auth_tokens_join_and_leave_the_union() { let mut issued = Vec::new(); while let Some(request) = requests.next().await { match request.token().as_ref() { - b"" => issued.push(request.accept(publish("setup/**"))), - b"extra" => issued.push(request.accept(publish("extra/**"))), + b"" => issued.push(request.accept(publish("setup"))), + b"extra" => issued.push(request.accept(publish("extra"))), _ => request.reject(moq_net::SessionError::Unauthorized, "unknown token"), } } @@ -9456,7 +9456,7 @@ async fn auth_tokens_join_and_leave_the_union() { .await .expect("connect timed out") .unwrap(); - await_grant(&session, grant(&["setup/**"], &[])).await; + await_grant(&session, grant(&["setup"], &[])).await; // A second consumer watches on its own and starts from the grant it was made with. let watcher = session.grants(); // The session was dialed without a credential, so the empty set is already answered. @@ -9465,13 +9465,13 @@ async fn auth_tokens_join_and_leave_the_union() { let tokens = session.tokens().unwrap(); tokens.set(vec!["extra".into()]).unwrap(); answered(&session).await.expect("the extra token was refused"); - // Patterns are kept sorted. - await_grant(&session, grant(&["extra/**", "setup/**"], &[])).await; + // Prefixes are kept sorted. + await_grant(&session, grant(&["extra", "setup"], &[])).await; let seen = kio::clock::timeout(TIMEOUT, watcher.changed()) .await .expect("the second consumer sees the change") .unwrap(); - assert_eq!(seen, Some(grant(&["extra/**", "setup/**"], &[]))); + assert_eq!(seen, Some(grant(&["extra", "setup"], &[]))); // A refused replacement still withdraws the token it replaces. tokens.set(vec!["bogus".into()]).unwrap(); @@ -9481,7 +9481,7 @@ async fn auth_tokens_join_and_leave_the_union() { crate::error::MoqErrorScope::Session, MoqProtocolKind::Unauthorized, ); - await_grant(&session, grant(&["setup/**"], &[])).await; + await_grant(&session, grant(&["setup"], &[])).await; session.cancel(0); kio::clock::timeout(TIMEOUT, serving) diff --git a/rs/moq-gst/src/source/imp.rs b/rs/moq-gst/src/source/imp.rs index ba6ae1dde74..17c83430f5d 100644 --- a/rs/moq-gst/src/source/imp.rs +++ b/rs/moq-gst/src/source/imp.rs @@ -4380,8 +4380,8 @@ mod session_tests { /// The announcements of the routes covering `path` on `origin`. fn watch(origin: &moq_net::origin::Producer, path: &str) -> moq_net::origin::Routes { - let path = moq_net::Patterns::from(moq_net::Pattern::literal(path).unwrap()); - origin.consume().scope("", &path).unwrap().routes() + let path = moq_net::Pattern::literal(path).unwrap(); + origin.consume().filter(&path).unwrap().routes() } /// Follow `room` on `origin` past its start. diff --git a/rs/moq-hls/src/server/include_tests.rs b/rs/moq-hls/src/server/include_tests.rs index 366686d0658..c08e32f7ce2 100644 --- a/rs/moq-hls/src/server/include_tests.rs +++ b/rs/moq-hls/src/server/include_tests.rs @@ -561,8 +561,8 @@ async fn includes_take_the_roots_grid_and_denied_ones_are_not_advertised() { let include = publish(&origin, "room/ladder", &child, &value); let server = Server::new(origin.consume(), Default::default()).unwrap(); master(&server, "include/", true).await; - let patterns = ["room/live".parse::().unwrap()].into_iter().collect(); - let denied = Server::new(origin.consume().scope("", &patterns).unwrap(), Default::default()).unwrap(); + let prefixes = moq_net::Prefixes::from("room/live".parse::().unwrap()); + let denied = Server::new(origin.consume().scope("", &prefixes).unwrap(), Default::default()).unwrap(); master(&denied, "include/", false).await; for resource in ["media.m3u8", "init.abcd.mp4", "seg/abcd.0.m4s"] { assert_eq!( diff --git a/rs/moq-hls/src/server/routes.rs b/rs/moq-hls/src/server/routes.rs index a9b9bdbeaa9..9f9c6a69211 100644 --- a/rs/moq-hls/src/server/routes.rs +++ b/rs/moq-hls/src/server/routes.rs @@ -477,7 +477,7 @@ mod tests { .consume() .scope( "", - &moq_net::Patterns::from("room/live".parse::().unwrap()), + &moq_net::Prefixes::from("room/live".parse::().unwrap()), ) .unwrap(); let app = Server::new(restricted, Default::default()).unwrap().router(); @@ -772,11 +772,11 @@ mod tests { write_three_gops(&mut allowed); write_three_gops(&mut denied); - let patterns = ["room/live", "room/source"] + let prefixes = ["room/live", "room/source"] .into_iter() - .map(|path| path.parse::().unwrap()) + .map(|path| path.parse::().unwrap()) .collect(); - let restricted = origin.consume().scope("", &patterns).unwrap(); + let restricted = origin.consume().scope("", &prefixes).unwrap(); let server = Server::new(restricted, Default::default()).unwrap(); let broadcaster = server.broadcaster("room/live").await.unwrap(); broadcaster.ready().await; @@ -842,7 +842,7 @@ mod tests { .consume() .scope( "", - &moq_net::Patterns::from("room/live".parse::().unwrap()), + &moq_net::Prefixes::from("room/live".parse::().unwrap()), ) .unwrap(); let all_denied = Server::new(only_catalog, Default::default()).unwrap(); diff --git a/rs/moq-net-tests/tests/announce_allocs.rs b/rs/moq-net-tests/tests/announce_allocs.rs index 4ec45e0f09c..354f4ea9b13 100644 --- a/rs/moq-net-tests/tests/announce_allocs.rs +++ b/rs/moq-net-tests/tests/announce_allocs.rs @@ -14,7 +14,7 @@ use std::alloc::{GlobalAlloc, Layout, System}; use std::cell::Cell; use moq_net::{ - Client, Path, Pattern, Patterns, Server, Version, + Client, Path, Prefixes, Server, Version, fuzz::{decode_path, decode_subscribe, encode_path, encode_subscribe}, origin, time::{Driver, Instant}, @@ -91,7 +91,7 @@ fn peer_route(producer: &origin::Producer, path: &'static str, peer: u64, cost: fn churn_allocs(root: &str, cursors: usize) -> usize { let (producer, _driver) = origin::Producer::new(origin::Config::default()); let _winner = peer_route(&producer, "room/a", 1, 1); - let all = Patterns::from(Pattern::all()); + let all = Prefixes::all(); let mut cursors: Vec<_> = (0..cursors) .map(|_| producer.consume().scope(root, &all).unwrap().routes()) .collect(); diff --git a/rs/moq-net/benches/interests.rs b/rs/moq-net/benches/interests.rs index 89cef731b4c..05e5dede4bd 100644 --- a/rs/moq-net/benches/interests.rs +++ b/rs/moq-net/benches/interests.rs @@ -15,7 +15,7 @@ //! Run with `cargo bench -p moq-net --features fuzz --bench interests`. use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; -use moq_net::{Pattern, Patterns, fuzz, origin}; +use moq_net::{Prefixes, fuzz, origin}; /// `(watches, sessions)` shapes. const SHAPES: [(usize, usize); 4] = [(10, 10), (1_000, 10), (10, 1_000), (1_000, 1_000)]; @@ -49,10 +49,7 @@ fn table(watches: usize, sessions: usize) -> Table { } fn scoped(producer: &origin::Producer, prefix: &str) -> origin::Consumer { - producer - .consume() - .scope(prefix, &Patterns::from(Pattern::all())) - .unwrap() + producer.consume().scope(prefix, &Prefixes::all()).unwrap() } fn bench_uncovered(c: &mut Criterion) { diff --git a/rs/moq-net/benches/origin.rs b/rs/moq-net/benches/origin.rs index 1fac9133935..b9dda946431 100644 --- a/rs/moq-net/benches/origin.rs +++ b/rs/moq-net/benches/origin.rs @@ -50,7 +50,7 @@ use std::time::Duration; use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::FutureExt; -use moq_net::{Instance, Pattern, Patterns, Timestamp, auth, broadcast, origin, track}; +use moq_net::{Instance, Pattern, Prefix, Prefixes, Timestamp, auth, broadcast, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; /// A millisecond track: the frames written here carry timestamps. @@ -152,7 +152,7 @@ fn bench_announce_covering(c: &mut Criterion) { .collect(); let consumer = producer .consume() - .scope("", &Patterns::from("**/*.chat".parse::().unwrap())) + .filter(&"**/*.chat".parse::().unwrap()) .unwrap(); let consumer = if covering { consumer.covering() } else { consumer }; let mut cursors: Vec<_> = (0..readers).map(|_| consumer.routes()).collect(); @@ -400,7 +400,7 @@ fn bench_announce_mounted(c: &mut Criterion) { producer .mount(format!("p{project}/.svc"), ".svc/p0") .unwrap() - .scope(format!("p{project}"), &Patterns::from(Pattern::all())) + .scope(format!("p{project}"), &Prefixes::all()) .unwrap() .consume() .with_hidden(true) @@ -453,10 +453,8 @@ fn bench_announce_fleet(c: &mut Criterion) { .collect(); let _cursors: Vec<_> = (0..peers) .map(|peer| { - let patterns: Patterns = [Pattern::subtree(&format!(".stats/p{}", peer % projects)).unwrap()] - .into_iter() - .collect(); - consumer.scope("", &patterns).unwrap().routes() + let prefixes = Prefixes::from(Prefix::try_from(format!(".stats/p{}", peer % projects)).unwrap()); + consumer.scope("", &prefixes).unwrap().routes() }) .collect(); b.iter(|| { @@ -548,7 +546,7 @@ fn bench_reprice_duplicate(c: &mut Criterion) { let scoped = producer .scope( "", - &Patterns::from(Pattern::subtree(&format!("{PATH}/p{}", (peer - 1) % 2)).unwrap()), + &Prefixes::from(Prefix::try_from(format!("{PATH}/p{}", (peer - 1) % 2)).unwrap()), ) .unwrap(); moq_net::fuzz::requests_route( @@ -572,7 +570,7 @@ fn bench_reprice_duplicate(c: &mut Criterion) { .consume() .scope( "", - &Patterns::from(Pattern::subtree(&format!("{PATH}/p{}", peer % 2)).unwrap()), + &Prefixes::from(Prefix::try_from(format!("{PATH}/p{}", peer % 2)).unwrap()), ) .unwrap() .with_hidden(true) @@ -1254,7 +1252,7 @@ fn runtime() -> tokio::runtime::Runtime { /// Everything under `room/`, which leaves out only the `muted` subscription. fn room() -> auth::Grant { let mut grant = auth::Grant::all(); - grant.subscribe = Pattern::subtree("room").unwrap().into(); + grant.subscribe = Prefix::try_from("room").unwrap().into(); grant } @@ -1274,7 +1272,7 @@ fn bench_narrow_delivery(c: &mut Criterion) { if narrowed { // Covers every subscription but the muted one, which is not subscribed here. let mut grant = room(); - grant.subscribe.insert(Pattern::literal("muted").unwrap()); + grant.subscribe.insert(Prefix::try_from("muted").unwrap()); watching.pair.server.auth().authorize(&grant); } b.iter(|| rt.block_on(watching.round())); diff --git a/rs/moq-net/benches/path.rs b/rs/moq-net/benches/path.rs index 3515c80658d..6943af7420e 100644 --- a/rs/moq-net/benches/path.rs +++ b/rs/moq-net/benches/path.rs @@ -14,7 +14,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use futures::FutureExt; use moq_net::{ - Path, Pattern, Patterns, + Path, Prefixes, fuzz::{Announced, Messages, decode_announces, decode_namespaces, encode_announces, encode_namespaces}, origin, }; @@ -160,7 +160,7 @@ fn requests() { // A rooted handle joins its root onto every path it is given. let (root, rest) = s.split_once('/').unwrap(); - let rooted = consumer.scope(root, &Patterns::from(Pattern::all())).unwrap(); + let rooted = consumer.scope(root, &Prefixes::all()).unwrap(); let rooted_target = format!("{rest}/0"); allocs(&format!("origin/request_rooted_str/{name}"), 2_000, 1, || { local(&|| rooted.request(rooted_target.as_str(), None)) diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index a0b856247e9..a57f5847b0d 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -807,7 +807,7 @@ impl Viewer { let mut announced = self .origin .consume() - .scope(path, &moq_net::Patterns::from(moq_net::Pattern::all())) + .scope(path, &moq_net::Prefixes::all()) .unwrap() .routes(); let event = announced.next().await.expect("announced"); diff --git a/rs/moq-net/benches/synced.rs b/rs/moq-net/benches/synced.rs index 86a5af13253..3fe8cdf6051 100644 --- a/rs/moq-net/benches/synced.rs +++ b/rs/moq-net/benches/synced.rs @@ -14,7 +14,7 @@ //! Run with `cargo bench -p moq-net --features fuzz --bench synced`. use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; -use moq_net::{Pattern, Patterns, fuzz, origin}; +use moq_net::{Prefixes, fuzz, origin}; /// `(streams, consumers)` shapes. const SHAPES: [(usize, usize); 4] = [(10, 10), (1_000, 10), (10, 1_000), (1_000, 1_000)]; @@ -45,10 +45,7 @@ fn table(streams: usize, consumers: usize) -> Table { } fn scoped(producer: &origin::Producer, prefix: &str) -> origin::Consumer { - producer - .consume() - .scope(prefix, &Patterns::from(Pattern::all())) - .unwrap() + producer.consume().scope(prefix, &Prefixes::all()).unwrap() } fn bench_land(c: &mut Criterion) { diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index ebfd0088c94..db909f82c79 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -31,7 +31,7 @@ //! Older moq-lite versions and moq-transport carry no AUTH exchange: there the grant stays //! `None`. On moq-transport a [`Credential`] is still answered, with a grant that stays //! unknown, since a request that names it carries its token; on moq-lite without AUTH its -//! answer is [`Error::Unsupported`]. moq-lite carries a grant's patterns as they are, and refuses one +//! answer is [`Error::Unsupported`]. moq-lite carries a grant's prefixes as they are, and refuses one //! too large for a single AUTH_OK rather than trim it. use std::{ @@ -42,7 +42,7 @@ use std::{ use bytes::Bytes; -use crate::{Error, Patterns, Result, SessionError, time::Instant}; +use crate::{Error, Prefixes, Result, SessionError, time::Instant}; mod tokens; @@ -83,15 +83,16 @@ pub(crate) enum Answer { /// What a peer lets this side do, in this side's own paths. /// /// The paths are relative to the session's root, the same paths the session -/// announces and subscribes with. `Grant::default()` grants nothing; set the -/// fields on it to grant more. +/// announces and subscribes with. Each is a set of prefixes, each covering its whole +/// subtree: the empty prefix grants everything. `Grant::default()` grants nothing; set +/// the fields on it to grant more. #[derive(Clone, Debug, Default, PartialEq, Eq)] #[non_exhaustive] pub struct Grant { /// The broadcasts this side may publish to the peer. - pub publish: Patterns, + pub publish: Prefixes, /// The broadcasts this side may subscribe to from the peer. - pub subscribe: Patterns, + pub subscribe: Prefixes, /// When the grant lapses, or `None` for never. pub expires: Option, } @@ -100,8 +101,8 @@ impl Grant { /// A grant of everything that never expires. pub fn all() -> Self { Self { - publish: crate::Pattern::all().into(), - subscribe: crate::Pattern::all().into(), + publish: Prefixes::all(), + subscribe: Prefixes::all(), expires: None, } } @@ -125,18 +126,10 @@ impl Grant { } /// The paths both grants allow, lapsing at the earlier expiry. - /// - /// Fails closed: an intersection too large to hold grants nothing, never more. fn intersect(&self, other: &Self) -> Self { - let both = |a: &Patterns, b: &Patterns| { - a.intersect(b).unwrap_or_else(|err| { - tracing::warn!(%err, "grant intersection too large; granting nothing"); - Patterns::new() - }) - }; Self { - publish: both(&self.publish, &other.publish), - subscribe: both(&self.subscribe, &other.subscribe), + publish: self.publish.intersect(&other.publish), + subscribe: self.subscribe.intersect(&other.subscribe), expires: earliest(self.expires, other.expires), } } @@ -436,9 +429,9 @@ impl Attached { } } - /// The union's and the scope's patterns for `direction`, each `None` while + /// The union's and the scope's prefixes for `direction`, each `None` while /// unrestricted. - fn parts(&self, direction: Direction) -> (Option<&Patterns>, Option<&Patterns>) { + fn parts(&self, direction: Direction) -> (Option<&Prefixes>, Option<&Prefixes>) { // The peer's grant names what we may do; the scope names what the peer may, // so what we send is what the peer may subscribe to, and the other way around. match direction { @@ -462,7 +455,7 @@ impl Attached { } } - /// Whether `direction` allows `path` right now, without copying the patterns. + /// Whether `direction` allows `path` right now, without copying the prefixes. fn allows(&self, direction: Direction, path: &str) -> bool { let (granted, limit) = self.parts(direction); granted.is_none_or(|granted| granted.matches(path)) && limit.is_none_or(|limit| limit.matches(path)) @@ -569,14 +562,14 @@ impl State { /// while unrestricted. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct Permit { - granted: Option, - limit: Option, + granted: Option, + limit: Option, } impl Permit { /// A permit limiting the peer to `limit`, with no grant of its own. #[cfg(test)] - pub(crate) fn limited(limit: Patterns) -> Self { + pub(crate) fn limited(limit: Prefixes) -> Self { Self { granted: None, limit: Some(limit), @@ -585,30 +578,25 @@ impl Permit { /// A permit the peer granted `granted`, with no limit of our own. #[cfg(test)] - pub(crate) fn granting(granted: Patterns) -> Self { + pub(crate) fn granting(granted: Prefixes) -> Self { Self { granted: Some(granted), limit: None, } } - /// Whether both allow `path`. + /// Whether both allow `path`, and so every path beneath it. pub(crate) fn matches(&self, path: &str) -> bool { self.granted(path) && self.within_limit(path) } - /// The paths at or beneath `prefix` both allow, which a claim there covers: under - /// `a/*`, those beneath `a`, though not `a` itself. An intersection too large to spell - /// allows nothing, never more. - pub(crate) fn beneath(&self, prefix: &str) -> Patterns { - let Ok(subtree) = moq_pattern::Pattern::subtree(prefix) else { - return Patterns::new(); - }; - let mut beneath = Patterns::from(subtree); - for patterns in [&self.granted, &self.limit].into_iter().flatten() { - beneath = beneath.intersect(patterns).unwrap_or_default(); - } - beneath + /// The paths `scope` holds that both allow too. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn within(&self, scope: Prefixes) -> Prefixes { + [&self.granted, &self.limit] + .into_iter() + .flatten() + .fold(scope, |scope, prefixes| scope.intersect(prefixes)) } /// Whether the peer's grant alone allows `path`. @@ -622,17 +610,6 @@ impl Permit { self.limit.as_ref().is_none_or(|limit| limit.matches(path)) } - /// Whether this allows every path at or beneath `prefix`, judged pattern by - /// pattern, so a `false` may be spurious but a `true` never is. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn covers_subtree(&self, prefix: &str) -> bool { - let Ok(subtree) = moq_pattern::Pattern::subtree(prefix) else { - return false; - }; - let covers = |patterns: &Option| patterns.as_ref().is_none_or(|patterns| patterns.contains(&subtree)); - covers(&self.granted) && covers(&self.limit) - } - /// The prefixes the peer's grant confines this direction to, or `None` while it /// grants everything: asking for one of them, rather than a prefix above it, is a /// request the peer's floors can certify. @@ -645,50 +622,41 @@ impl Permit { /// while neither does: every path both allow lies within one of them. #[cfg(feature = "async")] pub(crate) fn regions(&self) -> Option> { - let granted = self.granted.as_ref().and_then(confining); - let limit = self.limit.as_ref().and_then(confining); - let (granted, limit) = match (granted, limit) { - (None, None) => return None, - (Some(one), None) | (None, Some(one)) => return Some(one), - (Some(granted), Some(limit)) => (granted, limit), - }; - // Of two regions one lies within the other, or they share nothing. - let mut both: Vec = (granted.iter()) - .flat_map(|a| limit.iter().map(move |b| (a, b))) - .filter_map(|(a, b)| match () { - _ if a.has_prefix(b) => Some(a.clone()), - _ if b.has_prefix(a) => Some(b.clone()), - _ => None, - }) - .collect(); - both.sort(); - both.dedup(); - Some(both) + // Neither confining anything leaves every path; otherwise what both allow can never + // be every path. + fn confines(prefixes: &Option) -> Option<&Prefixes> { + prefixes.as_ref().filter(|prefixes| !prefixes.matches("")) + } + match (confines(&self.granted), confines(&self.limit)) { + (None, None) => None, + (Some(one), None) | (None, Some(one)) => confining(one), + (Some(granted), Some(limit)) => confining(&granted.intersect(limit)), + } } /// What the peer granted, or `None` when it grants everything. #[cfg(feature = "async")] - pub(crate) fn grant(&self) -> Option<&Patterns> { + pub(crate) fn grant(&self) -> Option<&Prefixes> { self.granted.as_ref() } - /// The patterns of the grant that `covered` does not contain, judged pattern by - /// pattern, so one may be spurious but none is missed, each with the prefix holding + /// The prefixes of the grant that `covered` does not cover, each as the path holding /// what it shows. `None` covers everything. #[cfg(feature = "async")] - pub(crate) fn beyond(&self, covered: Option<&Patterns>) -> Vec<(moq_pattern::Pattern, crate::Path)> { + pub(crate) fn beyond(&self, covered: Option<&Prefixes>) -> Vec<(crate::Prefix, crate::Path)> { let Some(covered) = covered else { return Vec::new(); }; let granted = match &self.granted { Some(granted) => granted.clone(), - None => Patterns::from(moq_pattern::Pattern::all()), + None => Prefixes::all(), }; - (granted.iter()) - .filter(|pattern| !covered.contains(pattern)) - .filter_map(|pattern| { - let head = crate::model::interest_prefixes(&Patterns::from(pattern.clone())); - Some((pattern.clone(), head.into_iter().next()?)) + (granted.into_iter()) + .filter(|prefix| !covered.matches(prefix.as_str())) + .filter_map(|prefix| { + // A prefix too long for a path covers nothing a peer could name. + let region = crate::Path::new(prefix.as_str()).ok()?; + Some((prefix, region)) }) .collect() } @@ -705,10 +673,9 @@ impl Permit { self.limit == other.limit } - /// Whether this allows everything `other` does, judged part by part, so a `false` - /// may be spurious but a `true` never is. + /// Whether this allows everything `other` does. pub(crate) fn covers(&self, other: &Self) -> bool { - let covers = |this: &Option, other: &Option| match (this, other) { + let covers = |this: &Option, other: &Option| match (this, other) { (None, _) => true, (Some(_), None) => false, (Some(this), Some(other)) => this.covers(other), @@ -717,13 +684,18 @@ impl Permit { } } -/// The prefixes `patterns` confine paths to, or `None` when they allow every path. +/// The paths `prefixes` confine every path to, or `None` when they allow every path. A +/// prefix too long for a path confines to nothing a peer could name, so it drops. #[cfg(feature = "async")] -fn confining(patterns: &Patterns) -> Option> { - let everything = moq_pattern::Pattern::subtree("").ok()?; - match patterns.contains(&everything) { +fn confining(prefixes: &Prefixes) -> Option> { + match prefixes.matches("") { true => None, - false => Some(crate::model::interest_prefixes(patterns)), + false => Some( + prefixes + .iter() + .filter_map(|prefix| crate::Path::new(prefix.as_str()).ok()) + .collect(), + ), } } @@ -1172,7 +1144,7 @@ impl Link { pub(crate) fn granted(&self, id: u64, grant: Grant) { // One parseable line per AUTH_OK, so the grant the peer actually sent is observable // without an API; the interop harness checks it against the token it minted. - let list = |patterns: &Patterns| format!("{:?}", patterns.iter().map(|p| p.to_string()).collect::>()); + let list = |prefixes: &Prefixes| format!("{:?}", prefixes.iter().map(|p| p.to_string()).collect::>()); tracing::debug!( publish = %list(&grant.publish), subscribe = %list(&grant.subscribe), @@ -2103,31 +2075,29 @@ fn refuse(issue: &kio::Shared, code: SessionError, reason: String) { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(crate) enum RequestKind { /// The peer reads a track: a SUBSCRIBE, a FETCH, or a TRACK_STATUS. Covered by the - /// grant's `subscribe` patterns. + /// grant's `subscribe` prefixes. Subscribe, /// A PUBLISH_NAMESPACE: the peer announces a namespace it will publish under. Its - /// `publish` patterns must cover the whole namespace. + /// `publish` prefixes must cover the whole namespace. PublishNamespace, /// A SUBSCRIBE_NAMESPACE: the peer discovers only the broadcasts the token allows /// beneath the requested prefix. Discover, - /// A PUBLISH: the peer offers a track. Covered by the grant's `publish` patterns. + /// A PUBLISH: the peer offers a track. Covered by the grant's `publish` prefixes. Publish, } impl RequestKind { /// Whether `grant`, issued to the peer that presented the token, covers this request at /// `path`. A grant names what its holder may do, so a request to read is covered by its - /// `subscribe` patterns and a request to announce by its `publish` patterns. An announce - /// routes everything beneath its namespace, so the grant must cover that whole subtree. + /// `subscribe` prefixes and a request to announce by its `publish` prefixes. An announce + /// routes everything beneath its namespace, which a prefix covering the namespace covers + /// too. pub(crate) fn covers(self, grant: &Grant, path: &str) -> bool { match self { Self::Subscribe => grant.subscribe.matches(path), Self::Discover => !grant.subscribe.is_empty(), - Self::Publish => grant.publish.matches(path), - Self::PublishNamespace => { - crate::Pattern::subtree(path).is_ok_and(|subtree| grant.publish.contains(&subtree)) - } + Self::Publish | Self::PublishNamespace => grant.publish.matches(path), } } } @@ -2719,7 +2689,7 @@ impl Gate { } let mut permit = permit.expect("a session epoch is never u64::MAX"); permit.limit = Some(if ended { - Patterns::new() + Prefixes::new() } else { match self.direction { Direction::Publish => token.grant.subscribe.clone(), @@ -2860,7 +2830,7 @@ pub(crate) fn unauthorized_reason(path: &crate::Path) -> String { #[derive(Default)] pub(crate) struct Enforce { epoch: u64, - permit: Option, + permit: Option, /// Every broadcast admitted so far, still announced. live: std::collections::HashSet, setup: bool, @@ -3072,7 +3042,7 @@ mod tests { { let issue = serving.issue.read(); assert_eq!(issue.outbox.len(), 1, "grants piled up"); - assert!(matches!(issue.outbox.front(), Some(Reply::Grant(grant)) if grant.publish == patterns(&["99"]))); + assert!(matches!(issue.outbox.front(), Some(Reply::Grant(grant)) if grant.publish == prefixes(&["99"]))); } issued.revoke(SessionError::Unauthorized, "revoked"); let issue = serving.issue.read(); @@ -3150,17 +3120,17 @@ mod tests { assert!(requests.poll_next(&kio::Waiter::noop()).is_pending()); } - fn patterns(prefixes: &[&str]) -> Patterns { - prefixes + fn prefixes(texts: &[&str]) -> Prefixes { + texts .iter() - .map(|prefix| crate::Pattern::subtree(prefix).unwrap()) + .map(|text| crate::Prefix::try_from(*text).unwrap()) .collect() } fn grant(publish: &[&str], subscribe: &[&str]) -> Grant { Grant { - publish: patterns(publish), - subscribe: patterns(subscribe), + publish: prefixes(publish), + subscribe: prefixes(subscribe), expires: None, } } @@ -3170,8 +3140,8 @@ mod tests { #[test] fn the_granted_regions_follow_the_grant_alone() { let permit = |granted: Option<&[&str]>, limit: Option<&[&str]>| Permit { - granted: granted.map(patterns), - limit: limit.map(patterns), + granted: granted.map(prefixes), + limit: limit.map(prefixes), }; let path = crate::Path::from_static; assert_eq!(permit(None, None).granted_regions(), None); @@ -3189,14 +3159,13 @@ mod tests { } /// What a publisher shows its peer lies within the regions of the grant and the limit - /// together, the deeper of any two nested; and two grants with the same regions can - /// still differ, as `a/*` and `a/**` do. + /// together, the deeper of any two nested. #[cfg(feature = "async")] #[test] fn the_regions_follow_the_grant_and_the_limit() { let permit = |granted: Option<&[&str]>, limit: Option<&[&str]>| Permit { - granted: granted.map(patterns), - limit: limit.map(patterns), + granted: granted.map(prefixes), + limit: limit.map(prefixes), }; let path = crate::Path::from_static; assert_eq!(permit(None, None).regions(), None); @@ -3206,31 +3175,31 @@ mod tests { permit(Some(&["a", "c/d"]), Some(&["a/b", "c", "e"])).regions(), Some(vec![path("a/b"), path("c/d")]) ); - - let one = |pattern: &str| Permit { - granted: Some(Patterns::from(pattern.parse::().unwrap())), - limit: None, - }; - assert_eq!(one("a/*").granted_regions(), one("a/**").granted_regions()); - assert!(!one("a/*").same_grant(&one("a/**"))); + assert_eq!(permit(Some(&[""]), Some(&[""])).regions(), None, "neither confines"); + assert_eq!( + permit(Some(&["a"]), Some(&["b"])).regions(), + Some(vec![]), + "nothing in both" + ); } - /// A permit allows what both allow beneath a path, the same paths: beneath `a` under - /// `a/*`, but nothing beneath `a/b` under `a/*/x` granted and `a/*/y` limited. + /// A permit allows within a scope what the scope, the grant, and the limit all allow: + /// the deepest of any nested prefixes. #[test] - fn a_permit_allows_what_both_allow_beneath() { - let permit = |granted: Option<&str>, limit: Option<&str>| Permit { - granted: granted.map(|pattern| Patterns::from(pattern.parse::().unwrap())), - limit: limit.map(|pattern| Patterns::from(pattern.parse::().unwrap())), + fn a_permit_allows_what_all_three_allow() { + let permit = |granted: Option<&[&str]>, limit: Option<&[&str]>| Permit { + granted: granted.map(prefixes), + limit: limit.map(prefixes), }; - let reaches = |permit: Permit, prefix: &str| !permit.beneath(prefix).is_empty(); - assert!(reaches(permit(Some("a/*"), None), "a")); - assert!(reaches(permit(None, Some("a/*")), "")); - assert!(!reaches(permit(Some("a/*"), None), "a/b/c")); - assert!(!reaches(permit(None, Some("a/*")), "b")); - assert!(reaches(permit(Some("a/*/x"), Some("a/b/*")), "a")); - assert!(!reaches(permit(Some("a/*/x"), Some("a/*/y")), "a/b")); - assert!(reaches(permit(None, None), "a")); + let within = |permit: Permit, scope: &[&str]| permit.within(prefixes(scope)); + assert_eq!(within(permit(Some(&["a/b"]), None), &[""]), prefixes(&["a/b"])); + assert_eq!(within(permit(None, Some(&["a"])), &["a/b", "c"]), prefixes(&["a/b"])); + assert_eq!( + within(permit(Some(&["a"]), Some(&["a/b/c"])), &["a/b"]), + prefixes(&["a/b/c"]) + ); + assert!(within(permit(Some(&["a/x"]), Some(&["a/y"])), &["a"]).is_empty()); + assert_eq!(within(permit(None, None), &["a"]), prefixes(&["a"])); } /// A limit replaces the last one, narrower or wider, and applies to a version @@ -4030,8 +3999,8 @@ mod request_token_tests { fn subscribe_only(prefix: &str) -> Grant { Grant { - publish: Patterns::new(), - subscribe: crate::Pattern::subtree(prefix).unwrap().into(), + publish: Prefixes::new(), + subscribe: crate::Prefix::try_from(prefix).unwrap().into(), expires: None, } } @@ -4123,8 +4092,8 @@ mod request_token_tests { fn grant_in(runtime: &crate::time::Deadlines, secs: u64) -> Grant { Grant { - publish: Patterns::new(), - subscribe: crate::Pattern::all().into(), + publish: Prefixes::new(), + subscribe: Prefixes::all(), expires: runtime.now().checked_add(Duration::from_secs(secs)), } } @@ -4428,13 +4397,13 @@ mod request_token_tests { #[test] fn request_coverage_is_from_the_presenters_side() { let read_only = Grant { - publish: Patterns::new(), - subscribe: crate::Pattern::all().into(), + publish: Prefixes::new(), + subscribe: Prefixes::all(), expires: None, }; let write_only = Grant { - publish: crate::Pattern::all().into(), - subscribe: Patterns::new(), + publish: Prefixes::all(), + subscribe: Prefixes::new(), expires: None, }; assert!(RequestKind::Subscribe.covers(&read_only, "room")); @@ -4443,31 +4412,22 @@ mod request_token_tests { assert!(!RequestKind::PublishNamespace.covers(&read_only, "room")); } - /// An announce routes its whole namespace, so a grant naming only the namespace itself - /// does not cover it, while one naming its subtree does. + /// An announce routes its whole namespace, which the prefix naming it covers, and a + /// namespace beneath it too, but not one above it or beside it. #[test] fn an_announce_needs_its_whole_subtree() { - let exact = Grant { - publish: crate::Pattern::literal("room").unwrap().into(), - subscribe: Patterns::new(), - expires: None, - }; - let subtree = Grant { - publish: crate::Pattern::subtree("room").unwrap().into(), - subscribe: Patterns::new(), - expires: None, - }; - assert!(!RequestKind::PublishNamespace.covers(&exact, "room")); - assert!(RequestKind::PublishNamespace.covers(&subtree, "room")); - assert!(RequestKind::PublishNamespace.covers(&subtree, "room/cam")); - assert!(!RequestKind::PublishNamespace.covers(&subtree, "other")); - // A SUBSCRIBE names one broadcast, which the exact grant's twin covers. - let read = Grant { - publish: Patterns::new(), - subscribe: crate::Pattern::literal("room").unwrap().into(), + let room = Grant { + publish: crate::Prefix::try_from("room").unwrap().into(), + subscribe: Prefixes::new(), expires: None, }; - assert!(RequestKind::Subscribe.covers(&read, "room")); + assert!(RequestKind::PublishNamespace.covers(&room, "room")); + assert!(RequestKind::PublishNamespace.covers(&room, "room/cam")); + assert!(!RequestKind::PublishNamespace.covers(&room, "")); + assert!(!RequestKind::PublishNamespace.covers(&room, "other")); + assert!(!RequestKind::PublishNamespace.covers(&room, "roomy")); + // A path no prefix can spell is still covered by the prefix above it. + assert!(RequestKind::PublishNamespace.covers(&room, "room/cam-*")); } /// An acceptor-side revoke ends the request before the deadline, and does not close the diff --git a/rs/moq-net/src/discovery.rs b/rs/moq-net/src/discovery.rs index fa0440b157f..4d1dedc88d3 100644 --- a/rs/moq-net/src/discovery.rs +++ b/rs/moq-net/src/discovery.rs @@ -90,11 +90,15 @@ impl Discovery { }; match client && !origin.relays() { true => discovery.interests = Some(origin.interests()), - // The wire speaks announce interest by prefix: ask for each granted pattern's - // literal head and let the origin's scope filter what arrives. Each snapshot is - // taken now, so a reader never reads as synced before the request goes out. + // The wire speaks announce interest by prefix: ask for each granted prefix. Each + // snapshot is taken now, so a reader never reads as synced before the request + // goes out. A prefix too long for a path names nothing to ask about. false => { - for prefix in crate::model::interest_prefixes(&origin.allowed()) { + let granted = origin.allowed(); + for prefix in granted + .iter() + .filter_map(|prefix| crate::Path::new(prefix.as_str()).ok()) + { let snapshot = origin.snapshot(&prefix); discovery.requests.insert(prefix.clone(), Request::default()); discovery.changes.push_back(Change::Open(prefix, snapshot)); diff --git a/rs/moq-net/src/fuzz/session.rs b/rs/moq-net/src/fuzz/session.rs index d2fa1865c39..1e4e5aba873 100644 --- a/rs/moq-net/src/fuzz/session.rs +++ b/rs/moq-net/src/fuzz/session.rs @@ -28,7 +28,7 @@ use bytes::Bytes; use kio::clock::sim::{self, JoinHandle}; use crate::{ - Client, Pattern, Patterns, Server, Session, Version, broadcast, cache, + Client, Prefixes, Server, Session, Version, broadcast, cache, coding::{Encoder, Form}, origin, track, transport::{ @@ -602,10 +602,7 @@ impl Relay { /// Accept the peer's session as a relay would: it may publish only beneath [`PEER`], and /// subscribe to anything. fn serve(&self, versions: &[Version], transport: memory::Session) -> Victim { - let publish = self - .origin - .scope(PEER, &Patterns::from(Pattern::all())) - .expect("the peer's scope"); + let publish = self.origin.scope(PEER, &Prefixes::all()).expect("the peer's scope"); let server = Server::new() .with_versions(versions.to_vec().into()) .with_publisher(self.origin.consume()) diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index ea8b3809cf9..545837ceff5 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -4264,12 +4264,9 @@ impl Publisher { } ns.watched.clear(); *prefix = next; - let scope = crate::Pattern::subtree(prefix.as_str()) - .map(|p| p.rebase(prefix.as_str())) - .unwrap_or_default(); let origin = self .origin - .scope(&*prefix, &scope) + .scope(&*prefix, &crate::Prefixes::all()) .unwrap_or_else(|_| self.origin.empty()); *announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).routes(); true @@ -6570,17 +6567,14 @@ impl NamespaceOpen { // A prefix outside our scope (empty origin, or a token that doesn't grant it) // just means we have nothing to announce; respond with an empty set rather // than erroring, which would look fatal to the peer. - // The wire prefix decodes as a literal path; convert it explicitly to its - // subtree grant, refusing anything that cannot be a subtree. + // The wire prefix decodes as a literal path, scoped as its whole subtree; a + // prefix no grant can spell (holding a `*`) reaches nothing. // The cursor is rooted at the prefix so every update arrives named as its // wire suffix, a route covering the prefix included: that one presents at // the root, as the empty suffix. - let scope = crate::Pattern::subtree(prefix.as_str()) - .map(|subtree| subtree.rebase(prefix.as_str())) - .unwrap_or_default(); let origin = publisher .origin - .scope(prefix, &scope) + .scope(prefix, &crate::Prefixes::all()) .unwrap_or_else(|_| publisher.origin.empty()); // Register the split-horizon peer on the announce cursor too. The origin @@ -11498,7 +11492,7 @@ mod serve_tests { let request = taken_token(&mut running, &mut requests).await; issued.update(crate::auth::Grant { - subscribe: crate::Pattern::subtree("other").unwrap().into(), + subscribe: crate::Prefix::try_from("other").unwrap().into(), ..crate::auth::Grant::default() }); assert!(futures::poll!(running.as_mut()).is_pending()); @@ -12307,7 +12301,7 @@ mod serve_tests { let scoped = h ._origin .consume() - .scope(root.as_str(), &crate::Patterns::from(crate::Pattern::all())) + .scope(root.as_str(), &crate::Prefixes::all()) .unwrap(); let peer_setup = peer::PeerSetup::default(); peer_setup.set(peer::Peer::default()); @@ -17298,8 +17292,8 @@ mod serve_tests { /// A grant to subscribe to everything, lapsing in `secs` (or never), on the publisher's clock. fn grant_all_expiring(runtime: &crate::time::Deadlines, secs: Option) -> crate::auth::Grant { crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::all().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefixes::all(), expires: secs.map(|s| runtime.now().checked_add(Duration::from_secs(s)).unwrap()), } } @@ -17314,8 +17308,8 @@ mod serve_tests { // The peer lets us publish everything, and we let it read only "other". auth.grant(0, crate::auth::Grant::all()); auth.handle().authorize(&crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::subtree("other").unwrap().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from("other").unwrap().into(), expires: None, }); assert!( @@ -18385,13 +18379,13 @@ mod serve_tests { #[kio::test] async fn a_subscribe_token_needs_a_subscribe_grant() { let read_only = crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::all().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefixes::all(), expires: None, }; let write_only = crate::auth::Grant { - publish: crate::Pattern::all().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefixes::all(), + subscribe: crate::Prefixes::new(), expires: None, }; let nothing = Some(crate::auth::Grant::default()); @@ -18412,8 +18406,8 @@ mod serve_tests { "a limit of nothing defers to the token" ); auth.handle().authorize(&crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::subtree("other").unwrap().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from("other").unwrap().into(), expires: None, }); assert!( @@ -18468,8 +18462,8 @@ mod serve_tests { #[kio::test] async fn a_covering_limit_still_verifies_the_token() { let limit = |subtree: &str| crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::subtree(subtree).unwrap().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from(subtree).unwrap().into(), expires: None, }; let (_auth, _h, mut serving) = serve_token_subscribe(crate::auth::Grant::default(), Some(limit("room"))).await; @@ -18728,8 +18722,8 @@ mod serve_tests { auth.grant( 0, crate::auth::Grant { - publish: crate::Pattern::subtree("other").unwrap().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefix::try_from("other").unwrap().into(), + subscribe: crate::Prefixes::new(), expires: None, }, ); @@ -19528,7 +19522,7 @@ mod tests { let consume = match scoped { true => origin .consume() - .scope("", &crate::Pattern::subtree(".stats").unwrap().into()) + .scope("", &crate::Prefix::try_from(".stats").unwrap().into()) .unwrap(), false => origin.consume(), }; diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 69255d023b3..3eb23be2508 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -1980,9 +1980,9 @@ mod tests { #[kio::test] async fn every_permitted_prefix_gets_its_own_subscribe_namespace() { let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let scope: crate::Patterns = ["cam", "mic"] + let scope: crate::Prefixes = ["cam", "mic"] .into_iter() - .map(|prefix| crate::Pattern::subtree(prefix).unwrap()) + .map(|prefix| crate::Prefix::try_from(prefix).unwrap()) .collect(); let scoped = origin .scope("rootns", &scope) @@ -2054,7 +2054,7 @@ mod tests { fn watch(origin: &crate::origin::Producer, prefix: &str) -> crate::origin::Routes { origin .consume() - .scope(prefix, &crate::Patterns::from(crate::Pattern::all())) + .scope(prefix, &crate::Prefixes::all()) .unwrap() .routes() } diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 0d06b32cb61..dd8bc45400d 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -8741,7 +8741,7 @@ mod tests { #[kio::test] async fn a_rooted_subscriber_asks_for_its_scope_not_its_root() { let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let scope = crate::Patterns::from(crate::Pattern::subtree("cam").unwrap()); + let scope = crate::Prefixes::from(crate::Prefix::try_from("cam").unwrap()); let scoped = origin.scope("rootns", &scope).expect("scope the origin"); let gate = kio::Producer::new(true); @@ -8766,7 +8766,9 @@ mod tests { Default::default(), ); - let mut namespaces = crate::model::interest_prefixes(&subscriber.origin.allowed()); + let mut namespaces = (subscriber.origin.allowed().iter()) + .map(|prefix| crate::Path::new(prefix.as_str()).unwrap()) + .collect::>(); assert_eq!( namespaces, vec![crate::Path::from_static("cam")], @@ -8882,7 +8884,7 @@ mod tests { let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); let consumer = origin.consume(); - let scope = crate::Patterns::from(crate::Pattern::subtree("cam").unwrap()); + let scope = crate::Prefixes::from(crate::Prefix::try_from("cam").unwrap()); let scoped = origin.scope("rootns", &scope).expect("scope the origin"); let session = crate::lite::test_transport::ScriptedSession::new(namespace_response(VERSION, "x.hang").await); @@ -8905,7 +8907,9 @@ mod tests { Default::default(), ); - let prefix = crate::model::interest_prefixes(&subscriber.origin.allowed()) + let prefix = (subscriber.origin.allowed().iter()) + .map(|prefix| crate::Path::new(prefix.as_str()).unwrap()) + .collect::>() .pop() .expect("one prefix"); let (_live, live) = live(); @@ -9004,7 +9008,9 @@ mod tests { ); subscriber.announces = crate::session::Slots::new(1); - let prefix = crate::model::interest_prefixes(&subscriber.origin.allowed()) + let prefix = (subscriber.origin.allowed().iter()) + .map(|prefix| crate::Path::new(prefix.as_str()).unwrap()) + .collect::>() .pop() .expect("one prefix"); let (_live, live) = live(); @@ -12648,15 +12654,15 @@ mod tests { auth.grant( 0, crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::subtree("other").unwrap().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from("other").unwrap().into(), expires: None, }, ); // We let the peer announce only "other". auth.handle().authorize(&crate::auth::Grant { - publish: crate::Pattern::subtree("other").unwrap().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefix::try_from("other").unwrap().into(), + subscribe: crate::Prefixes::new(), expires: None, }); assert!( @@ -12687,8 +12693,8 @@ mod tests { /// what an announcing peer's token must carry. fn publish_grant_expiring(runtime: &crate::time::Deadlines, secs: Option) -> crate::auth::Grant { crate::auth::Grant { - publish: crate::Pattern::all().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefixes::all(), + subscribe: crate::Prefixes::new(), expires: secs.map(|s| runtime.now().checked_add(std::time::Duration::from_secs(s)).unwrap()), } } @@ -12969,8 +12975,8 @@ mod tests { async fn a_covering_limit_still_verifies_the_token() { const VERSION: Version = Version::Draft18; let limit = |subtree: &str| crate::auth::Grant { - publish: crate::Pattern::subtree(subtree).unwrap().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefix::try_from(subtree).unwrap().into(), + subscribe: crate::Prefixes::new(), expires: None, }; let auth = crate::auth::Link::new(false); @@ -13061,8 +13067,8 @@ mod tests { assert!(routed, "the token admits the announce outside the limit"); auth.handle().authorize(&crate::auth::Grant { - publish: crate::Pattern::subtree("other").unwrap().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefix::try_from("other").unwrap().into(), + subscribe: crate::Prefixes::new(), expires: None, }); for _ in 0..50 { @@ -13617,8 +13623,8 @@ mod tests { // Nothing was held for the limit to bring back. auth.handle().authorize(&crate::auth::Grant { - publish: crate::Pattern::subtree("other").unwrap().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefix::try_from("other").unwrap().into(), + subscribe: crate::Prefixes::new(), expires: None, }); settle().await; @@ -14411,13 +14417,13 @@ mod tests { async fn an_announce_token_needs_a_publish_grant() { const VERSION: Version = Version::Draft18; let read_only = crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Pattern::all().into(), + publish: crate::Prefixes::new(), + subscribe: crate::Prefixes::all(), expires: None, }; let write_only = crate::auth::Grant { - publish: crate::Pattern::all().into(), - subscribe: crate::Patterns::new(), + publish: crate::Prefixes::all(), + subscribe: crate::Prefixes::new(), expires: None, }; for (grant, admitted) in [(read_only, false), (write_only, true)] { diff --git a/rs/moq-net/src/lib.rs b/rs/moq-net/src/lib.rs index b9f9565a26a..23b153487c2 100644 --- a/rs/moq-net/src/lib.rs +++ b/rs/moq-net/src/lib.rs @@ -147,7 +147,7 @@ pub use driver::Driver; pub use error::*; pub use instance::{Instance, InvalidInstance}; pub use model::*; -pub use path::{InvalidPattern, Path, Pattern, Patterns}; +pub use path::{InvalidPattern, InvalidPrefix, Path, Pattern, Patterns, Prefix, Prefixes}; #[cfg(feature = "async")] pub use reconnect::Reconnect; #[cfg(feature = "async")] diff --git a/rs/moq-net/src/lite/auth.rs b/rs/moq-net/src/lite/auth.rs index e6599260ea2..a27ca63e38c 100644 --- a/rs/moq-net/src/lite/auth.rs +++ b/rs/moq-net/src/lite/auth.rs @@ -2,7 +2,7 @@ use std::time::Duration; use bytes::Bytes; -use crate::Patterns; +use crate::Prefixes; use crate::coding::*; use super::{Message, Version}; @@ -45,9 +45,9 @@ impl<'a> Message<'a> for Auth { #[derive(Clone, Debug, PartialEq, Eq)] pub struct AuthOk { /// What the opener may publish to the acceptor. - pub publish: Patterns, + pub publish: Prefixes, /// What the opener may subscribe to from the acceptor. - pub subscribe: Patterns, + pub subscribe: Prefixes, /// How long until the grant lapses, or `None` for never. pub expires: Option, } @@ -61,8 +61,8 @@ impl<'a> Message<'a> for AuthOk { if !version.has_auth() { return Err(DecodeError::Version); } - let publish = Patterns::decode(r, version)?; - let subscribe = Patterns::decode(r, version)?; + let publish = Prefixes::decode(r, version)?; + let subscribe = Prefixes::decode(r, version)?; let expires = match r.varint()? { 0 => None, ms if ms <= MAX_EXPIRES_MS => Some(Duration::from_millis(ms)), @@ -190,10 +190,10 @@ impl<'a> Decode<'a, Version> for AuthReply { #[cfg(test)] mod tests { use super::*; - use crate::Pattern; + use crate::Prefix; - fn patterns(texts: &[&str]) -> Patterns { - texts.iter().map(|text| Pattern::try_from(*text).unwrap()).collect() + fn prefixes(texts: &[&str]) -> Prefixes { + texts.iter().map(|text| Prefix::try_from(*text).unwrap()).collect() } fn encode>(msg: &T, version: Version) -> Result, EncodeError> { @@ -218,16 +218,16 @@ mod tests { } } - /// `**` grants everything, the empty pattern only the root, and the empty list - /// nothing; literals and wildcards travel exactly, never widened to a prefix. + /// The empty prefix grants everything and the empty list nothing; each prefix travels + /// as written, covering its subtree. #[test] fn auth_ok_round_trips() { for (publish, subscribe, expires) in [ - (patterns(&["**"]), patterns(&[]), None), - (patterns(&[""]), patterns(&["room/**"]), None), + (prefixes(&[""]), prefixes(&[]), None), + (prefixes(&["room"]), prefixes(&["lobby", r"a\/b"]), None), ( - patterns(&["room/alice", "room/*/cam", "**/demo.hang"]), - patterns(&["room/cam-*.hang", "lobby/**"]), + prefixes(&["room/alice", "room-b", "demo.hang"]), + prefixes(&["room/cam", "lobby"]), Some(Duration::from_secs(60)), ), ] { @@ -244,29 +244,40 @@ mod tests { #[test] fn auth_ok_golden() { let msg = AuthReply::Ok(AuthOk { - publish: patterns(&["room/*/cam", "**/b.hang"]), - subscribe: patterns(&[""]), + publish: prefixes(&["room-b", "room/a"]), + subscribe: prefixes(&[""]), expires: Some(Duration::from_millis(1000)), }); let buf = encode(&msg, Version::Lite07).unwrap(); #[rustfmt::skip] let want: &[u8] = &[ 0x00, // AUTH_OK - 0x1a, // length - 0x02, // publish count, in canonical order - 0x09, b'*', b'*', b'/', b'b', b'.', b'h', b'a', b'n', b'g', - 0x0a, b'r', b'o', b'o', b'm', b'/', b'*', b'/', b'c', b'a', b'm', + 0x13, // length + 0x02, // publish count, in segment order: a subtree right after its prefix + 0x06, b'r', b'o', b'o', b'm', b'/', b'a', + 0x06, b'r', b'o', b'o', b'm', b'-', b'b', 0x01, // subscribe count - 0x00, // the empty pattern: the root alone + 0x00, // the empty prefix: everything 0x83, 0xe8, // expires: 1000ms, as a leading-ones varint ]; assert_eq!(&buf[..], want); } - /// Only valid, canonical text decodes: each pattern has exactly one encoding. + /// Only a valid prefix decodes: a wildcard, from a peer still writing patterns, is + /// refused like any other malformed prefix. #[test] - fn invalid_patterns_are_refused() { - for text in ["*/**", "/room", "room/", "room//a", "a*b*c", "**/**", "a**"] { + fn invalid_prefixes_are_refused() { + for text in [ + "**", + "room/**", + "*", + "room/*/cam", + "cam-*.hang", + "/room", + "room/", + "room//a", + r"a\b", + ] { let mut buf = Vec::new(); let mut w = Encoder::new(&mut buf, Version::Lite07.into()); w.varint(AUTH_OK).unwrap(); @@ -299,8 +310,8 @@ mod tests { #[test] fn zero_expiry_rounds_up() { let msg = AuthReply::Ok(AuthOk { - publish: Patterns::new(), - subscribe: Patterns::new(), + publish: Prefixes::new(), + subscribe: Prefixes::new(), expires: Some(Duration::ZERO), }); let AuthReply::Ok(got) = round_trip(&msg) else { @@ -312,8 +323,8 @@ mod tests { #[test] fn maximum_expiry_round_trips() { let msg = AuthReply::Ok(AuthOk { - publish: Patterns::new(), - subscribe: Patterns::new(), + publish: Prefixes::new(), + subscribe: Prefixes::new(), expires: Some(Duration::from_millis(MAX_EXPIRES_MS)), }); assert_eq!(round_trip(&msg), msg); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 068eaa8a12d..c6f670ef393 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1061,18 +1061,15 @@ impl AnnounceServe { // that doesn't grant it), we simply have nothing to announce. Respond with an // empty set and keep the stream open (the subscriber treats a FIN here as a // fatal stream close), rather than erroring, which would reset the stream. - // The wire prefix decodes as a literal path; convert it explicitly to its - // subtree grant, refusing anything that cannot be a subtree. + // The wire prefix decodes as a literal path, scoped as its whole subtree; a prefix + // no grant can spell (holding a `*`) reaches nothing. // The cursor is rooted at the prefix so every update arrives named as its // wire suffix, a route covering the prefix included: that one presents at // the root, as the empty suffix. - let scope = crate::Pattern::subtree(prefix.as_str()) - .map(|subtree| subtree.rebase(prefix.as_str())) - .unwrap_or_default(); let origin = self .shared .origin - .scope(&prefix, &scope) + .scope(&prefix, &crate::Prefixes::all()) .unwrap_or_else(|_| self.shared.origin.empty()); // Register the split-horizon peer on the announce cursor too. The origin // model uses this exposure to park a reflected copy before it can replace @@ -3663,10 +3660,7 @@ mod announce_test { let origin = Hop::new(1).unwrap().produce(); let root = "r".repeat(100); let _route = origin.announce(format!("{root}/x"), origin::RouteInfo::new()).unwrap(); - let consumer = origin - .consume() - .scope(root.as_str(), &crate::Patterns::from(crate::Pattern::all())) - .unwrap(); + let consumer = origin.consume().scope(root.as_str(), &crate::Prefixes::all()).unwrap(); let prefix = crate::Path::new(&"p".repeat(crate::Path::MAX_BYTES - 50)).unwrap(); let mut answer = super::routed::Answer::alone(consumer); let log = Log::default(); @@ -3787,10 +3781,10 @@ mod announce_test { #[kio::test] async fn a_lite07_prefix_wider_than_the_grant_is_answered_region_by_region() { let path = |path: &str| crate::Path::new(path).unwrap(); - let subtrees = |paths: &[&str]| -> crate::Patterns { + let subtrees = |paths: &[&str]| -> crate::Prefixes { paths .iter() - .map(|path| crate::Pattern::subtree(path).unwrap()) + .map(|path| crate::Prefix::try_from(*path).unwrap()) .collect() }; let origin = Hop::new(1).unwrap().produce(); @@ -3800,12 +3794,12 @@ mod announce_test { let _outside = origin.announce("d/z", origin::RouteInfo::new()).unwrap(); let _cam = origin.announce("src/cam", origin::RouteInfo::new()).unwrap(); let consumer = (origin.mount("e/m", "src").unwrap()) - .scope("", &crate::Patterns::from(crate::Pattern::all())) + .scope("", &crate::Prefixes::all()) .unwrap() .consume(); let auth = crate::auth::Link::new(false); let limit = |paths: &[&str]| crate::auth::Grant { - publish: crate::Patterns::default(), + publish: crate::Prefixes::new(), subscribe: subtrees(paths), expires: None, }; @@ -3897,7 +3891,7 @@ mod announce_test { } /// Grant changes queued before the answer drains each move the reads they queued, in - /// order: one that keeps the regions keeps a mount and its claim, though a later one + /// order: one that keeps a region keeps its mount and claim, though a later one /// replaces that mount, and the claim every grant shows is never ended. #[kio::test] async fn lite07_grant_changes_queued_back_to_back_apply_in_order() { @@ -3905,16 +3899,19 @@ mod announce_test { let origin = Hop::new(1).unwrap().produce(); let _cam = origin.announce("src/m", origin::RouteInfo::new()).unwrap(); let consumer = (origin.mount("a", "src").unwrap()) - .scope("", &crate::Patterns::from(crate::Pattern::all())) + .scope("", &crate::Prefixes::all()) .unwrap() .consume(); let auth = crate::auth::Link::new(false); - let limit = |pattern: &str| crate::auth::Grant { - publish: crate::Patterns::default(), - subscribe: crate::Patterns::from(pattern.parse::().unwrap()), + let limit = |prefixes: &[&str]| crate::auth::Grant { + publish: crate::Prefixes::new(), + subscribe: prefixes + .iter() + .map(|prefix| crate::Prefix::try_from(*prefix).unwrap()) + .collect(), expires: None, }; - auth.handle().authorize(&limit("a/*")); + auth.handle().authorize(&limit(&["a", "x"])); let mut answer = super::routed::Answer::alone_on(consumer, auth.clone()); let log = Log::default(); let mut wire = Wire { @@ -3940,10 +3937,10 @@ mod announce_test { .filter(|msg| matches!(msg, lite::AnnounceBroadcast::Start { .. })); assert_eq!(starts.count(), 1, "{first:?}"); - // `a/**` keeps the region `a`; `a/m/**` then reads `a/m` in place of `a`. - auth.handle().authorize(&limit("a/**")); + // `a` alone keeps the region `a`; `a/m` then reads `a/m` in place of `a`. + auth.handle().authorize(&limit(&["a"])); answer.poll_permit(&waiter).unwrap(); - auth.handle().authorize(&limit("a/m/**")); + auth.handle().authorize(&limit(&["a/m"])); answer.poll_permit(&waiter).unwrap(); let changed = sent(&mut answer); let ends = changed @@ -3960,16 +3957,16 @@ mod announce_test { let origin = Hop::new(1).unwrap().produce(); let _cam = origin.announce("src/m/cam", origin::RouteInfo::new()).unwrap(); let consumer = (origin.mount("a", "src").unwrap()) - .scope("", &crate::Patterns::from(crate::Pattern::all())) + .scope("", &crate::Prefixes::all()) .unwrap() .consume(); let auth = crate::auth::Link::new(false); - let limit = |pattern: &str| crate::auth::Grant { - publish: crate::Patterns::default(), - subscribe: crate::Patterns::from(pattern.parse::().unwrap()), + let limit = |prefix: &str| crate::auth::Grant { + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from(prefix).unwrap().into(), expires: None, }; - auth.handle().authorize(&limit("a/**")); + auth.handle().authorize(&limit("a")); let mut answer = super::routed::Answer::alone_on(consumer, auth.clone()); let log = Log::default(); let mut wire = Wire { @@ -3985,7 +3982,7 @@ mod announce_test { answer.request(false, Vec::new(), vec![(path(""), true)]).unwrap(); // Narrowed before the mount at `a` sent its initial set: `a/m` is read in its place. - auth.handle().authorize(&limit("a/m/**")); + auth.handle().authorize(&limit("a/m")); answer.poll_permit(&waiter).unwrap(); answer.drain(&mut writer).unwrap(); assert!(writer.poll_flush(&waiter).is_ready()); @@ -3999,10 +3996,9 @@ mod announce_test { assert!(start.is_some() && start < synced, "{msgs:?}"); } - /// A claim at a path the scope does not match, but above paths it does, is shown where - /// it is presented: `a` under `a/*`, at the region and at its own path. One beneath - /// every path the scope matches stays hidden, and so does `a` from a handle scoped to - /// `a/*/x`, which shares no path with the scope. + /// A claim at a path above the scope is shown where it is presented: `a` under `a/x`, at + /// the region one segment beneath it. One within the region is shown at its own path, + /// and nothing is shown to a handle scoped to `a/y`, which shares no path with the grant. #[kio::test] async fn a_lite07_claim_above_what_the_scope_matches_is_shown() { let path = |path: &str| crate::Path::new(path).unwrap(); @@ -4012,15 +4008,15 @@ mod announce_test { let _b = origin.announce("b", origin::RouteInfo::new()).unwrap(); let auth = crate::auth::Link::new(false); auth.handle().authorize(&crate::auth::Grant { - publish: crate::Patterns::default(), - subscribe: crate::Patterns::from("a/*".parse::().unwrap()), + publish: crate::Prefixes::new(), + subscribe: crate::Prefix::try_from("a/x").unwrap().into(), expires: None, }); let waiter = kio::Waiter::noop(); let disjoint = (origin.consume()) - .scope("", &crate::Patterns::from("a/*/x".parse::().unwrap())) + .scope("", &crate::Prefix::try_from("a/y").unwrap().into()) .unwrap(); - let shown = [("a".to_string(), 0)]; + let shown = [("a/x".to_string(), 1), ("a/x/y".to_string(), 0)]; for (consumer, asked, expected) in [ (origin.consume(), "", &shown[..]), (origin.consume(), "a", &shown[..]), @@ -4042,7 +4038,7 @@ mod announce_test { assert!(writer.poll_flush(&waiter).is_ready()); let msgs = wire.take_announces(); let mut decoder = lite::AnnounceDecoder::default(); - let told: Vec<(String, u64)> = (msgs.iter()) + let mut told: Vec<(String, u64)> = (msgs.iter()) .filter_map(|msg| match msg { lite::AnnounceBroadcast::Start { suffix, claim, .. } => { let path = decoder.start(Some(suffix.clone())).unwrap().unwrap(); @@ -4051,6 +4047,7 @@ mod announce_test { _ => None, }) .collect(); + told.sort(); assert_eq!(told, expected, "{asked:?}: {msgs:?}"); assert_eq!(msgs.last(), Some(&lite::AnnounceBroadcast::Synced { prefix: 0 })); } @@ -4066,7 +4063,7 @@ mod announce_test { .announce("tenant-a/public/cam", origin::RouteInfo::new()) .unwrap(); let consumer = (origin.mount("tenant-b/shared", "tenant-a/public").unwrap()) - .scope("tenant-b", &crate::Patterns::from(crate::Pattern::all())) + .scope("tenant-b", &crate::Prefixes::all()) .unwrap() .consume(); let mut answer = super::routed::Answer::alone(consumer); @@ -4130,7 +4127,7 @@ mod announce_test { .announce("tenant-a/public/cam", origin::RouteInfo::new()) .unwrap(); let consumer = (origin.mount("tenant-b/shared", "tenant-a/public").unwrap()) - .scope("tenant-b", &crate::Patterns::from(crate::Pattern::all())) + .scope("tenant-b", &crate::Prefixes::all()) .unwrap() .consume(); let mut answer = super::routed::Answer::alone(consumer); @@ -11827,10 +11824,7 @@ mod tests { for version in [Version::Lite05, Version::Lite06, Version::Lite07] { let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); let root = "r".repeat(crate::Path::MAX_BYTES - 2); - let scoped = origin - .consume() - .scope(root.as_str(), &crate::Patterns::from(crate::Pattern::all())) - .unwrap(); + let scoped = origin.consume().scope(root.as_str(), &crate::Prefixes::all()).unwrap(); let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); peer_setup.set(crate::lite::Setup::default()).unwrap(); diff --git a/rs/moq-net/src/lite/publisher/routed.rs b/rs/moq-net/src/lite/publisher/routed.rs index c58a46b7747..5127a6e727d 100644 --- a/rs/moq-net/src/lite/publisher/routed.rs +++ b/rs/moq-net/src/lite/publisher/routed.rs @@ -451,7 +451,7 @@ impl Answer { /// Whether the grant and this handle show the peer the whole tree, as a routing peer /// must be. pub(in crate::lite) fn whole(&self) -> bool { - self.origin.root().is_empty() && self.origin.covers(&Path::empty()) && self.permit.covers_subtree("") + self.origin.root().is_empty() && self.origin.covers(&Path::empty()) && self.permit.matches("") } /// Ready once the peer reads its stream too slowly for what the node queued. @@ -895,7 +895,8 @@ impl Answer { } /// Whether the peer is shown the claim at the absolute `path`: the permit and this - /// handle allow some path it covers, the same one. + /// handle allow some path it covers, the same one, which prefixes always meet as one + /// whole subtree. fn shows(&self, path: &Path) -> bool { let Some((_, prefix)) = self.region(path) else { return false; @@ -904,7 +905,9 @@ impl Answer { let (Some(suffix), Some(relative)) = (within, path.strip_prefix(self.origin.root())) else { return false; }; - (prefix.hidden || !suffix.is_hidden()) && self.origin.reaches_any(path, &self.permit.beneath(relative.as_str())) + (prefix.hidden || !suffix.is_hidden()) + && self.origin.reaches(path) + && self.permit.within(self.origin.allowed()).overlaps(relative.as_str()) } /// Bring the peer's claim `key` in line with what the node told and the answer diff --git a/rs/moq-net/src/lite/publisher/routed/tests.rs b/rs/moq-net/src/lite/publisher/routed/tests.rs index 8fe5a262f52..5818a28d7eb 100644 --- a/rs/moq-net/src/lite/publisher/routed/tests.rs +++ b/rs/moq-net/src/lite/publisher/routed/tests.rs @@ -24,25 +24,25 @@ use super::Answer; use crate::coding::Decode; use crate::lite::{self, AnnounceBroadcast, Version, subscriber::route_stream::Writer, test_transport::*}; use crate::model::ProduceTest; -use crate::{Hop, Instance, Path, Pattern, Patterns, origin}; +use crate::{Hop, Instance, Path, Prefix, Prefixes, origin}; /// Where claims are announced: some above what grants and prefixes name, some deeper than a -/// grant's `*` shows, one hidden, and some beneath the mount target `s`. +/// grant reaches, one hidden, and some beneath the mount target `s`. const PATHS: &[&str] = &["a", "a/x", "a/x/y", "a/.h", "a/m/x", "b", "b/c", "b/c/d", "s", "s/x"]; /// The scopes claims are announced through, which bound what each may serve. -const SCOPES: &[&[&str]] = &[&["**"], &["*/x"]]; +const SCOPES: &[&[&str]] = &[&[""], &["a/x", "b/x"]]; /// The grants the peer gives: widen, narrow, move, and revoke between them. const GRANTS: &[&[&str]] = &[ - &["**"], - &["a/**"], - &["b/**"], - &["a/x/**"], - &["a/*"], - &["a/m/**"], - &["a/**", "b/**"], - &["a/**", "b/*"], + &[""], + &["a"], + &["b"], + &["a/x"], + &["a/x/y"], + &["a/m"], + &["a", "b"], + &["a", "b/c"], &[], ]; @@ -53,23 +53,23 @@ const PREFIXES: &[&str] = &["", "a", "a/x", "b", "a/m", "s"]; const MOUNTS: &[Option<(&str, &str)>] = &[None, Some(("a", "s")), Some(("a/m", "s"))]; /// The handle's own scope. -const HANDLES: &[&[&str]] = &[&["**"], &["a/**", "b/**", "s/**"]]; +const HANDLES: &[&[&str]] = &[&[""], &["a", "b", "s"]]; fn path(path: &str) -> Path { Path::new(path).unwrap() } -fn patterns(members: &[&str]) -> Patterns { +fn prefixes(members: &[&str]) -> Prefixes { members .iter() - .map(|member| member.parse::().unwrap()) + .map(|member| Prefix::try_from(*member).unwrap()) .collect() } fn grant(index: usize) -> crate::auth::Grant { crate::auth::Grant { - publish: Patterns::default(), - subscribe: patterns(GRANTS[index]), + publish: Prefixes::new(), + subscribe: prefixes(GRANTS[index]), expires: None, } } @@ -323,7 +323,7 @@ struct Sim { impl Sim { fn new(case: &Case) -> Self { let origin = Hop::new(1).unwrap().produce(); - let handle = patterns(HANDLES[case.handle]); + let handle = prefixes(HANDLES[case.handle]); let consumer = match MOUNTS[case.mount] { Some((at, target)) => origin.mount(at, target).unwrap().scope("", &handle).unwrap().consume(), None => origin.consume().scope("", &handle).unwrap(), @@ -387,7 +387,7 @@ impl Sim { if self.claims.contains_key(index) { return Ok(()); } - let Ok(producer) = self.origin.scope("", &patterns(SCOPES[*scope])) else { + let Ok(producer) = self.origin.scope("", &prefixes(SCOPES[*scope])) else { return Ok(()); }; self.next_instance += 1; @@ -648,8 +648,8 @@ fn sent(answer: &mut Answer, writer: &mut Writer, peer: &mut Peer) -> Vec, /// What of the grant the peer's answers already cover, `None` for everything: what /// a grant change shows beyond it may already be in the peer's answer, unheard here. - covered: Option, + covered: Option, /// What the grant shows beyond `covered` within the wants, held until an answer after - /// it is in: one per want and pattern of the grant, however often it changes. + /// it is in: one per want and prefix of the grant, however often it changes. gained: Vec, /// The grant showed more since the last edit: the next is sent even when it changes /// nothing, since its ANNOUNCE_OK follows what the change shows under a wider prefix. @@ -211,9 +211,9 @@ pub(in crate::lite) struct Routed { /// answer after it. struct Gain { want: Path, - /// The pattern of the grant that showed it. - pattern: crate::Pattern, - /// The deeper of the want and what the pattern shows, relative to the root. + /// The prefix of the grant that showed it. + prefix: crate::Prefix, + /// The deeper of the want and the prefix, relative to the root. held: Path, /// Holds the readers until dropped. _snapshot: crate::model::Snapshot, @@ -460,16 +460,16 @@ impl Routed { .into_iter() .partition(|gain| answered(&gain.held)); self.gained = held; - // The peer's answers now cover what each released pattern shows, and once nothing - // is held, the whole grant. + // The peer's answers now cover each released prefix, and once nothing is held, the + // whole grant. if self.gained.is_empty() { if self.covered.as_ref() != self.permit.grant() { self.covered = self.permit.grant().cloned(); } } else if let Some(covered) = self.covered.as_mut() { for gain in released { - if !self.gained.iter().any(|held| held.pattern == gain.pattern) { - covered.insert(gain.pattern); + if !self.gained.iter().any(|held| held.prefix == gain.prefix) { + covered.insert(gain.prefix); } } } @@ -836,17 +836,16 @@ impl Routed { // for it. Elsewhere, and on a narrowing, nothing new can arrive, and a reader stays // synced. if !permit.same_grant(&self.permit) { - // The peer's answers cover only what the new grant still shows of what they did; - // past what one intersection can hold, take them to cover nothing, holding more. + // The peer's answers cover only what the new grant still shows of what they did. self.covered = match (self.covered.take(), permit.grant()) { (None, granted) => granted.cloned(), (Some(covered), None) => Some(covered), - (Some(covered), Some(granted)) => Some(covered.intersect(granted).unwrap_or_default()), + (Some(covered), Some(granted)) => Some(covered.intersect(granted)), }; let overlaps = |a: &Path, b: &Path| a.has_prefix(b) || b.has_prefix(a); let asking = self.stream.asking(); let mut before = std::mem::take(&mut self.gained); - for (pattern, region) in permit.beyond(self.covered.as_ref()) { + for (granted, region) in permit.beyond(self.covered.as_ref()) { // Too deep to lie beneath the root, it shows nothing here. let Ok(absolute) = self.root.join(®ion) else { continue; @@ -857,7 +856,7 @@ impl Routed { for want in self.wants.keys().filter(|want| overlaps(want, ®ion)) { self.regranted = true; // Held already: keep its snapshot, so its readers wait on unbroken. - let kept = (before.iter()).position(|gain| gain.want == *want && gain.pattern == pattern); + let kept = (before.iter()).position(|gain| gain.want == *want && gain.prefix == granted); if let Some(kept) = kept { self.gained.push(before.swap_remove(kept)); continue; @@ -868,7 +867,7 @@ impl Routed { }; self.gained.push(Gain { want: want.clone(), - pattern: pattern.clone(), + prefix: granted.clone(), _snapshot: self.origin.snapshot(held.clone()), held, }); @@ -1062,9 +1061,9 @@ fn admits(origin: &crate::origin::Producer, root: &Path, permit: &crate::auth::P } /// Whether `permit` trusts the peer with the whole tree: neither grant confines it, -/// whether by setting no patterns or by one that covers everything. +/// whether by setting no prefixes or by the empty one, which covers everything. pub(in crate::lite) fn whole(permit: &crate::auth::Permit) -> bool { - permit.covers_subtree("") + permit.matches("") } #[cfg(test)] diff --git a/rs/moq-net/src/lite/subscriber/routed/tests.rs b/rs/moq-net/src/lite/subscriber/routed/tests.rs index 16f3e04f99e..e501ad2c068 100644 --- a/rs/moq-net/src/lite/subscriber/routed/tests.rs +++ b/rs/moq-net/src/lite/subscriber/routed/tests.rs @@ -71,8 +71,8 @@ fn start(path: &'static str) -> Wire { } } -fn limited(pattern: &str) -> Permit { - Permit::limited([pattern.parse::().unwrap()].into_iter().collect()) +fn limited(prefix: &str) -> Permit { + Permit::limited(crate::Prefix::try_from(prefix).unwrap().into()) } /// Send the peer what the node asks of it, which the peer answers at once, then serve @@ -254,8 +254,8 @@ async fn claims_never_pass_the_announce_cap() { #[kio::test] async fn the_limit_admits_each_claim() { let (subscriber, _consumer) = subscriber(); - let public = limited("live/*/public/**"); - let everything = limited("live/**"); + let public = limited("live/a/public"); + let everything = limited("live"); let mut routed = asking(&subscriber, &public); for msg in [start("live/a/public/x"), start("live/a/private/y")] { @@ -283,11 +283,11 @@ async fn a_grant_change_unsyncs_only_the_readers_it_can_show_more() { Permit::granting( paths .iter() - .map(|path| crate::Pattern::subtree(path).unwrap()) + .map(|path| crate::Prefix::try_from(*path).unwrap()) .collect(), ) }; - let all = crate::Patterns::from(crate::Pattern::all()); + let all = crate::Prefixes::all(); let everything = consumer.routes(); let a = consumer.scope("a", &all).unwrap().routes(); let synced = |routes: &crate::origin::Routes| routes.poll_synced(&kio::Waiter::noop()).is_ready(); @@ -338,16 +338,18 @@ async fn a_grant_change_unsyncs_only_the_readers_it_can_show_more() { #[kio::test] async fn a_grant_change_under_a_coarsened_ask_holds_the_readers_that_arrive() { let (subscriber, consumer) = subscriber(); - let granting = |patterns: &[&str]| { + let wants = prefixes("a", 17, 4000); + let granting = |more: &[&str]| { Permit::granting( - (patterns.iter()) - .map(|pattern| pattern.parse::().unwrap()) + (wants.iter().map(|want| crate::Prefix::try_from(want.as_str()).unwrap())) + .chain(more.iter().map(|prefix| crate::Prefix::try_from(*prefix).unwrap())) .collect(), ) }; - let mut routed = Routed::new(&subscriber, 1, &granting(&["a/*"]), None, None).unwrap(); + // The grant covers each want and nothing else under `a`. + let mut routed = Routed::new(&subscriber, 1, &granting(&[]), None, None).unwrap(); // Too many to ask for one by one: the session asks for `a`. - for want in prefixes("a", 17, 4000) { + for want in wants.iter().cloned() { routed.want(want.clone(), subscriber.origin.snapshot(want)); } pump(&mut routed, &subscriber); @@ -356,9 +358,9 @@ async fn a_grant_change_under_a_coarsened_ask_holds_the_readers_that_arrive() { routed.receive(Wire::Synced { prefix: asked[0] }).unwrap(); routed.release(); - routed.limit(&granting(&["a/*", "a/new/**"])).unwrap(); + routed.limit(&granting(&["a/new"])).unwrap(); let fresh = Path::from_static("a/new/x"); - let reader = consumer.scope(fresh.as_str(), &crate::Patterns::from(crate::Pattern::all())); + let reader = consumer.scope(fresh.as_str(), &crate::Prefixes::all()); let reader = reader.unwrap().routes(); routed.want(fresh.clone(), subscriber.origin.snapshot(fresh)); routed.release(); @@ -370,23 +372,20 @@ async fn a_grant_change_under_a_coarsened_ask_holds_the_readers_that_arrive() { } /// A peer that leaves an edit unanswered while its grant keeps changing holds one prefix -/// per want and pattern it still grants, however many changes it sends; and a region too +/// per want and prefix it still grants, however many changes it sends; and a region too /// deep to lie beneath the session's root shows nothing there, holding no reader back. #[kio::test] async fn grant_changes_hold_only_what_the_grant_still_shows() { let (mut subscriber, _consumer) = subscriber(); - subscriber.origin = subscriber - .origin - .scope("r", &crate::Patterns::from(crate::Pattern::all())) - .unwrap(); - let granting = |patterns: &[String]| { + subscriber.origin = subscriber.origin.scope("r", &crate::Prefixes::all()).unwrap(); + let granting = |prefixes: &[String]| { Permit::granting( - (patterns.iter()) - .map(|pattern| pattern.parse::().unwrap()) + (prefixes.iter()) + .map(|prefix| crate::Prefix::try_from(prefix.as_str()).unwrap()) .collect(), ) }; - let held = ["a/**".to_string(), "c/**".to_string()]; + let held = ["a".to_string(), "c".to_string()]; let mut routed = Routed::new(&subscriber, 1, &granting(&held[..1]), None, None).unwrap(); routed.want(Path::empty(), subscriber.origin.snapshot("")); routed.want(Path::from_static("a"), subscriber.origin.snapshot("a")); @@ -398,14 +397,14 @@ async fn grant_changes_hold_only_what_the_grant_still_shows() { // Each gain the grant takes back again lets go of what it held. for i in 0..64 { let mut more = held.to_vec(); - more.push(format!("b{i}/*/**")); + more.push(format!("b{i}")); routed.limit(&granting(&more)).unwrap(); routed.limit(&granting(&held)).unwrap(); } assert_eq!(routed.gained.len(), 1, "only `c` is held, for the whole tree"); let mut deep = held.to_vec(); - deep.push(format!("{}/**", "x".repeat(crate::Path::MAX_BYTES))); + deep.push("x".repeat(crate::Path::MAX_BYTES)); routed.limit(&granting(&deep)).unwrap(); assert_eq!(routed.gained.len(), 1); } @@ -545,8 +544,8 @@ async fn a_routing_neighbour_keeps_a_whole_permit() { let mut routed = routing(&subscriber); routed.limit(&Permit::default()).unwrap(); // A grant that names everything confines nothing, as a relay's token for a peer does. - routed.limit(&limited("**")).unwrap(); - let narrowed = routed.limit(&limited("live/**")); + routed.limit(&limited("")).unwrap(); + let narrowed = routed.limit(&limited("live")); assert!(matches!(narrowed, Err(Error::Unauthorized)), "{narrowed:?}"); } @@ -812,13 +811,13 @@ fn routes(registry: &crate::stats::Registry) -> crate::stats::Routes { async fn a_claim_outside_the_limit_counts_once() { let (mut subscriber, _consumer) = subscriber(); let registry = counted(&mut subscriber); - let public = limited("live/*/public/**"); + let public = limited("live/a/public"); let mut routed = asking(&subscriber, &public); for msg in [start("live/a/public/x"), start("live/a/private/y")] { routed.receive(msg).unwrap(); } routed.receive(Wire::Change { id: 1, claim: claim(1) }).unwrap(); - routed.limit(&limited("live/**")).unwrap(); + routed.limit(&limited("live")).unwrap(); routed.limit(&public).unwrap(); pump(&mut routed, &subscriber); let refused = routes(®istry).refused; diff --git a/rs/moq-net/src/model/follow.rs b/rs/moq-net/src/model/follow.rs index a2379c0ae53..587a37a767d 100644 --- a/rs/moq-net/src/model/follow.rs +++ b/rs/moq-net/src/model/follow.rs @@ -454,7 +454,7 @@ mod tests { use super::super::ProduceTest; use super::*; use crate::{ - Error, Hop, Instance, Pattern, Patterns, + Error, Hop, Instance, Prefix, Prefixes, origin::{Cost, RouteInfo, RouteOptions}, }; @@ -613,7 +613,7 @@ mod tests { #[kio::test] async fn follow_ignores_a_route_scoped_beneath_the_path() { let origin = Hop::new(1).unwrap().produce(); - let chat = Patterns::from("room/*/chat".parse::().unwrap()); + let chat = Prefixes::from(Prefix::try_from("room/alice/chat").unwrap()); let _chat = origin .scope("", &chat) .unwrap() @@ -958,7 +958,7 @@ mod tests { #[test] fn follow_refuses_a_path_outside_the_scope() { let origin = Hop::new(1).unwrap().produce(); - let scope = Patterns::from(Pattern::subtree("pool/job/cam").unwrap()); + let scope = Prefixes::from(Prefix::try_from("pool/job/cam").unwrap()); let scoped = origin.consume().scope("", &scope).unwrap(); assert!(matches!(scoped.path_routes("pool/job"), Err(Error::Unauthorized))); } diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index a92fb45ab44..6698a525a74 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -84,10 +84,6 @@ pub use origin_impl::Consume; // plain ids. pub(crate) use origin_impl::{Hop, Hops, InvalidHop}; -// The announce-interest prefixes a scope needs on a prefix-shaped wire. -#[cfg(feature = "async")] -pub(crate) use origin_impl::interest_prefixes; - // Held by a session while its peer's initial announcements arrive. #[cfg(feature = "async")] pub(crate) use origin_impl::Snapshot; diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 2db491f070e..9ef678355bb 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -20,7 +20,7 @@ use super::{ front::{Action, Candidate, Event, Front, Refusal}, }; use crate::{ - Error, Instance, InvalidPattern, Path, Pattern, Patterns, + Error, Instance, InvalidPattern, Path, Pattern, Patterns, Prefix, Prefixes, coding::{BoundsExceeded, Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, varint}, path::{self, Segment}, time::{Deadlines, Instant}, @@ -1434,9 +1434,9 @@ struct RouteEntry { /// Which incarnation of the instance; see [`RouteInfo::epoch`]. epoch: u64, prefix: Path, - /// The absolute patterns the announcing producer may serve. The prefix is + /// The absolute prefixes the announcing producer may serve. The prefix is /// only the wire-visible covering claim; this scope remains authoritative. - scope: Patterns, + scope: Prefixes, /// The absolute subtrees the announcing producer may not serve, even beneath /// a covering prefix. excluded: Arc<[Path]>, @@ -1491,9 +1491,9 @@ struct RouteEntry { /// and building one allocates a segment vector and a canonical string. Holding /// it makes that visit a comparison. claim: Pattern, - /// Each [`Self::scope`] member intersected with [`Self::claim`], built once at - /// announce time for the same reason: the paths beneath the prefix the route - /// may serve. A member whose intersection is too large to spell reaches nothing. + /// The subtrees beneath the prefix the route may serve, built once at announce time + /// for the same reason: [`Self::claim`] when the scope covers the prefix, else each + /// [`Self::scope`] member beneath it. reach: Vec, /// Whether the job routes answering this prefix route take its instance, which it /// minted rather than was given: their readers continue on the job's route. It moves @@ -1593,13 +1593,16 @@ fn publish_path(path: &Path) -> Result<(), Error> { Ok(()) } -/// What `scope` lets a route at `claim` serve: see [`RouteEntry::reach`]. -fn reach(scope: &Patterns, claim: &Pattern) -> Vec { - scope - .iter() - .filter_map(|scope| scope.intersect(claim).ok()) - .flatten() - .collect() +/// What `scope` lets a route at `prefix`, claiming `claim`, serve: see [`RouteEntry::reach`]. +fn reach(scope: &Prefixes, prefix: &Path, claim: &Pattern) -> Vec { + if scope.matches(prefix.as_str()) { + return vec![claim.clone()]; + } + // The claim spells its prefix, so the prefix holds no wildcard. + let Ok(prefix) = Prefix::try_from(prefix.as_str()) else { + return Vec::new(); + }; + scope.intersect(&prefix.into()).iter().map(Prefix::pattern).collect() } /// The paths a prefix can cover, using an exact pattern at the path depth limit. @@ -1826,7 +1829,7 @@ struct TableCursor { impl TableCursor { /// Where `prefix` presents on this cursor, named relative to the cursor root. - /// The prefix stays a prefix; the pattern scope only decides visibility. + /// The prefix stays a prefix; the scope and filter only decide visibility. /// `claim` is the prefix's [`prefix_claim`], which the caller already holds: /// building one allocates, and the sweeps below ask this per route per /// cursor. @@ -1977,13 +1980,13 @@ impl TableCursor { } } -/// A handle's view of an origin: the absolute patterns it may reach, and the +/// A handle's view of an origin: the absolute prefixes it may reach, and the /// subtrees it reads from elsewhere on the origin. #[derive(Clone)] struct OriginScope { // The paths this handle may reach, absolute, named as the handle sees them: // a path under a mount is authorized here, before it is resolved. - allowed: Patterns, + allowed: Prefixes, // The subtrees that resolve elsewhere; see [`Producer::mount`]. Disjoint. mounts: Arc<[Mount]>, // The subtrees this handle never reaches despite `allowed`, absolute and @@ -2090,15 +2093,15 @@ impl OriginScope { #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn empty() -> Self { Self { - allowed: Patterns::new(), + allowed: Prefixes::new(), mounts: Arc::from([]), excluded: Arc::from([]), } } - /// This view narrowed to the absolute `patterns`: the paths in both. - fn narrow(&self, patterns: &Patterns) -> Option { - let allowed = self.allowed.intersect(patterns).ok()?; + /// This view narrowed to the absolute `prefixes`: the paths in both. + fn narrow(&self, prefixes: &Prefixes) -> Option { + let allowed = self.allowed.intersect(prefixes); if allowed.is_empty() { None } else { @@ -2154,7 +2157,7 @@ impl OriginScope { } /// What this view reaches, named from `root`. - fn relative(&self, root: &Path) -> Patterns { + fn relative(&self, root: &Path) -> Prefixes { self.allowed.rebase(root.as_str()) } } @@ -2162,14 +2165,14 @@ impl OriginScope { impl Default for OriginScope { fn default() -> Self { Self { - allowed: Patterns::from(Pattern::all()), + allowed: Prefixes::all(), mounts: Arc::from([]), excluded: Arc::from([]), } } } -/// The announce-interest prefixes that cover a pattern scope on a prefix-only +/// The announce-interest prefixes that cover what a cursor lists on a prefix-only /// wire: each member's literal head, minus heads another already covers. /// /// A pattern's literal segments are spelled like a path's, so a head fails only when it @@ -2561,8 +2564,7 @@ impl Producer { /// serving the permitted descendants), and no mount or excluded subtree covers it. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn publishable(&self, full: &Path) -> bool { - Pattern::subtree(full.as_str()).is_ok_and(|subtree| self.scope.allowed.overlaps(&subtree)) - && self.scope.publishes(full) + self.scope.allowed.overlaps(full.as_str()) && self.scope.publishes(full) } /// An exact route at the absolute `full` path, answering `request` on its first @@ -2734,7 +2736,9 @@ impl Producer { let listener = Listener { id, root: self.root.clone(), - heads: interest_prefixes(&self.allowed()), + heads: (self.allowed().iter()) + .filter_map(|prefix| Path::new(prefix.as_str()).ok()) + .collect(), allowed: self.scope.allowed.clone(), link: self.link, queue: queue.clone(), @@ -2801,8 +2805,8 @@ impl Producer { /// The claim is priced, credentialed and shown like an exact route. It is visible /// for as long as the returned [`Claim`] (and every clone) lives, unless withdrawn. /// - /// The prefix must overlap this producer's pattern scope. Individual requests - /// remain authoritative and are refused when they do not match the scope. + /// The prefix must overlap this producer's scope. Individual requests remain + /// authoritative and are refused when the scope does not cover them. pub fn claim(&self, prefix: impl TryInto>, options: RouteOptions) -> Result { // A minted instance lives in the table, which moves it on as its jobs end; see // [`OriginState::advance`]. @@ -2871,15 +2875,16 @@ impl Producer { }) } - /// Returns a producer rooted at `root` and restricted to matching `patterns`. + /// Returns a producer rooted at `root` and restricted to `prefixes`, each covering + /// its whole subtree. /// - /// `root` is relative to this producer's root, and `patterns` are relative to + /// `root` is relative to this producer's root, and `prefixes` are relative to /// the new root. Returns [`Error::Unauthorized`] when the requested scope has /// no overlap with this producer's scope, or [`Error::BoundsExceeded`] when - /// rooting the patterns would exceed the path limit. - pub fn scope(&self, root: impl TryInto>, patterns: &Patterns) -> Result { + /// rooting the prefixes would exceed the path limit or the root holds a `*`. + pub fn scope(&self, root: impl TryInto>, prefixes: &Prefixes) -> Result { let root = self.root.join(&path::convert(root)?)?; - let rooted = patterns.rooted(root.as_str()).map_err(|_| BoundsExceeded)?; + let rooted = prefixes.rooted(root.as_str()).map_err(|_| BoundsExceeded)?; let scope = self.scope.narrow(&rooted).ok_or(Error::Unauthorized)?; Ok(Producer { node: self.node, @@ -2939,11 +2944,7 @@ impl Producer { } // A mount point no pattern can spell could never be announced or authorized. Pattern::literal(at.as_str())?; - if !self - .scope - .allowed - .covers(&Patterns::from(Pattern::subtree(target.as_str())?)) - { + if !self.scope.allowed.matches(target.as_str()) { return Err(Error::Unauthorized); } // Symmetric, so the order mounts are added never changes which sets are accepted: @@ -2990,8 +2991,8 @@ impl Producer { &self.root } - /// The patterns this producer may publish under, relative to its root. - pub fn allowed(&self) -> Patterns { + /// The prefixes this producer may publish under, relative to its root. + pub fn allowed(&self) -> Prefixes { self.scope.relative(&self.root) } @@ -3009,12 +3010,12 @@ struct Announcing { shared: SendCell>, /// The absolute advertised prefix, which also keys the ingress announce counters. requested: Path, - /// The prefix inserted into the table, with its [`prefix_claim`]. Pattern - /// scopes decide visibility and request authorization without changing the - /// route's prefix shape. + /// The prefix inserted into the table, with its [`prefix_claim`]. The scope + /// decides visibility and request authorization without changing the route's + /// prefix shape. prefixes: Vec<(Path, Pattern)>, /// The absolute paths the producer is authorized to serve. - scope: Patterns, + scope: Prefixes, /// The absolute subtrees the producer may not serve. excluded: Arc<[Path]>, local: bool, @@ -3034,7 +3035,7 @@ impl Announcing { return Err(BoundsExceeded.into()); } let claim = prefix_claim(&requested)?; - if !producer.scope.allowed.overlaps(&claim) || !producer.scope.publishes(&requested) { + if !producer.scope.allowed.overlaps(requested.as_str()) || !producer.scope.publishes(&requested) { return Err(Error::Unauthorized); } Ok(Self { @@ -3108,7 +3109,7 @@ impl Announcing { stale, retired: false, claim: claim.clone(), - reach: reach(&self.scope, claim), + reach: reach(&self.scope, prefix, claim), lends: serving.lends, lent: None, }); @@ -3649,7 +3650,7 @@ enum Retract { pub(crate) struct AnnounceProducer { shared: SendCell>, /// The table entries this advertisement created, by prefix and id. A prefix - /// remains unchanged; pattern scopes only filter its visibility and requests. + /// remains unchanged; scopes only filter its visibility and requests. entries: Vec<(Path, u64)>, /// The ingress counters an advertised interval's announce guard comes from. ingress: stats::Scope, @@ -5991,8 +5992,8 @@ struct Listener { root: Path, /// The broadest prefixes the session may ask about, relative to `root`. heads: Vec, - /// The absolute patterns the session may publish under. - allowed: Patterns, + /// The absolute prefixes the session may publish under. + allowed: Prefixes, /// The link the session's routes arrive on, which its snapshots wait for. link: Link, queue: kio::Queue, @@ -6014,8 +6015,7 @@ impl Listener { if head.has_prefix(&relative) { out.push(head.clone()); } else if relative.has_prefix(head) { - let reaches = Pattern::subtree(prefix.as_str()).is_ok_and(|subtree| self.allowed.overlaps(&subtree)); - return match reaches { + return match self.allowed.overlaps(prefix.as_str()) { true => vec![relative], false => Vec::new(), }; @@ -7617,6 +7617,10 @@ pub struct Consumer { max_hops: NonZeroUsize, scope: OriginScope, + // What `announced` and `routes` list, absolute: the scope's subtrees, narrowed by + // each `filter`. Discovery only; requests answer to the scope alone. + listed: Patterns, + // A prefix that is automatically stripped from all paths. root: Path, @@ -7672,6 +7676,7 @@ impl Consumer { node: producer.node, max_hops: producer.max_hops, scope: producer.scope.clone(), + listed: producer.scope.allowed.patterns(), root: producer.root.clone(), shared: producer.shared.clone(), stats, @@ -7716,10 +7721,7 @@ impl Consumer { #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn node_id(&self, subscribe: &Producer) -> Option { let whole = |root: &Path, scope: &OriginScope| { - root.is_empty() - && Pattern::subtree("").is_ok_and(|all| scope.allowed.contains(&all)) - && scope.excluded.is_empty() - && scope.mounts.is_empty() + root.is_empty() && scope.allowed.matches("") && scope.excluded.is_empty() && scope.mounts.is_empty() }; let routing = self.proxy && subscribe.link() != Link::Local @@ -7755,6 +7757,7 @@ impl Consumer { node: self.node, max_hops: self.max_hops, scope: OriginScope::default(), + listed: Patterns::from(Pattern::all()), root: Path::default(), shared: self.shared.clone(), stats: stats::Session::default(), @@ -7834,25 +7837,11 @@ impl Consumer { /// there, never one the origin holds at `path` itself (see [`Self::mounted`]). #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn reaches(&self, path: &Path) -> bool { - let Ok(subtree) = Pattern::subtree(path.as_str()) else { - return false; - }; - self.scope.allowed.overlaps(&subtree) + self.scope.allowed.overlaps(path.as_str()) && !self.scope.excluded.iter().any(|excluded| path.has_prefix(excluded)) && self.scope.mount(path).is_none_or(|mount| mount.resolve(path).is_some()) } - /// [`Self::reaches`], and some path `allowed`, relative to this handle's root, holds - /// is in its scope too. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn reaches_any(&self, path: &Path, allowed: &Patterns) -> bool { - if !self.reaches(path) { - return false; - } - let ours = self.scope.allowed.rebase(self.root().as_str()); - allowed.iter().any(|pattern| ours.overlaps(pattern)) - } - /// Whether the absolute `path` lies beneath one of this handle's mounts, where the /// origin's own claims at that path are hidden from it. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -7875,15 +7864,11 @@ impl Consumer { .collect() } - /// Whether this handle reaches every path at or beneath the absolute `prefix`, - /// judged pattern by pattern, so a `false` may be spurious but a `true` never is. + /// Whether this handle reaches every path at or beneath the absolute `prefix`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn covers(&self, prefix: &Path) -> bool { - let Ok(subtree) = Pattern::subtree(prefix.as_str()) else { - return false; - }; let overlaps = |other: &Path| other.has_prefix(prefix) || prefix.has_prefix(other); - self.scope.allowed.contains(&subtree) + self.scope.allowed.matches(prefix.as_str()) && !self.scope.excluded.iter().any(overlaps) && !self.scope.mounts.iter().any(|mount| overlaps(&mount.at)) } @@ -8016,6 +8001,7 @@ impl Consumer { pub(crate) fn empty(&self) -> Self { Self { scope: OriginScope::empty(), + listed: Patterns::new(), ..self.clone() } } @@ -8035,11 +8021,11 @@ impl Consumer { /// /// Allocates a per-cursor coalescing buffer and replays the currently /// announced routes as initial updates. Routes stay prefixes and are named - /// relative to this consumer's root; its patterns only filter visibility. - /// Only routes whose literal name matches those patterns are reported, unless + /// relative to this consumer's root; its scope and [`filter`](Self::filter)s only + /// decide visibility. Only routes whose literal name both admit are reported, unless /// [`covering`](Self::covering) asks for every route that could serve them. - /// Routes with a segment starting with `.` below the literal head of those - /// patterns are hidden unless [`with_hidden`](Self::with_hidden) opted in. + /// Routes with a segment starting with `.` below the literal head of what is listed + /// are hidden unless [`with_hidden`](Self::with_hidden) opted in. /// The stream also watches its scope: a client session fed by this origin asks /// its peer about the scope while any stream watches it. /// Drop the returned [`Routes`] to unregister. @@ -8057,7 +8043,7 @@ impl Consumer { state: state.clone(), under, holes, - named: mount.map(|mount| (mount.clone(), self.scope.allowed.clone())), + named: mount.map(|mount| (mount.clone(), self.listed.clone())), interested: self.interested, current: HashMap::new(), } @@ -8080,7 +8066,7 @@ impl Consumer { }; let cursors = vec![cursor( root, - mount.translate(&self.scope.allowed), + mount.translate(&self.listed), self.hidden.translate(mount), Some(mount), Path::default(), @@ -8098,7 +8084,7 @@ impl Consumer { // Otherwise the consumer's own cursor, minus the mounted and excluded subtrees, // plus one cursor per mount beneath the root it may discover, presenting under it. - let heads = interest_prefixes(&self.scope.allowed); + let heads = interest_prefixes(&self.listed); let mut holes = self.scope.holes(None).unwrap_or_default(); let mut cursors = Vec::new(); for mount in self.scope.mounts.iter() { @@ -8109,7 +8095,7 @@ impl Consumer { let Some(mount_holes) = self.scope.holes(Some(mount)) else { continue; }; - let allowed = mount.translate(&self.scope.allowed); + let allowed = mount.translate(&self.listed); // Hidden at the mount point means hidden throughout: the dot segment is above // everything the mount holds. A top-up feed's rule moves onto the target. if allowed.is_empty() @@ -8133,7 +8119,7 @@ impl Consumer { 0, cursor( self.root.clone(), - self.scope.allowed.clone(), + self.listed.clone(), self.hidden.clone(), None, Path::default(), @@ -8182,18 +8168,27 @@ impl Consumer { pub(crate) fn path_routes(&self, path: impl TryInto>) -> Result { let path = path::convert(path)?; - // Scope down to the path itself, which still sees every route whose claim covers it: - // the rest of the origin never wakes it, and a route claiming only paths beneath it - // (a scoped dynamic at the same prefix) never masks the one that serves it, just as a - // request skips it. - let consumer = self.scope("", &Patterns::from(Pattern::literal(path.as_str())?))?; - - // `scope` keeps narrower permissions intact: on a consumer limited to - // `foo/specific`, no route can ever cover `foo`. - if !consumer.scope.permits(&consumer.root.join(&path)?) { + // On a consumer limited to `foo/specific`, no route can ever cover `foo`. + if !self.scope.permits(&self.root.join(&path)?) { return Err(Error::Unauthorized); } + // List the path itself, which still sees every route whose claim covers it: the + // rest of the origin never wakes it, and a route claiming only paths beneath it (a + // scoped dynamic at the same prefix) never masks the one that serves it, just as a + // request skips it. A lookup answers to the scope alone, so it starts from the + // scope's subtrees rather than this handle's discovery filters. + let rooted = Pattern::literal(path.as_str())? + .rooted(self.root.as_str()) + .map_err(|_| BoundsExceeded)?; + let listed = self + .scope + .allowed + .patterns() + .intersect(&rooted.into()) + .map_err(|_| Error::Unauthorized)?; + let consumer = Consumer { listed, ..self.clone() }; + // Untagged: this is a lookup, not egress announce forwarding, so it must not drive // the announce guards. Hiding narrows discovery, not lookup, so a hidden path // follows like any other. @@ -8265,23 +8260,45 @@ impl Consumer { Ok(Following::new(self.clone(), path, instance.into(), routes)) } - /// Returns a consumer rooted at `root` and restricted to matching `patterns`. + /// Returns a consumer rooted at `root` and restricted to `prefixes`, each covering + /// its whole subtree. /// - /// `root` is relative to this consumer's root, and `patterns` are relative to + /// `root` is relative to this consumer's root, and `prefixes` are relative to /// the new root. Returns [`Error::Unauthorized`] when the requested scope has /// no overlap with this consumer's scope, or [`Error::BoundsExceeded`] when - /// rooting the patterns would exceed the path limit. - pub fn scope(&self, root: impl TryInto>, patterns: &Patterns) -> Result { + /// rooting the prefixes would exceed the path limit or the root holds a `*`. + pub fn scope(&self, root: impl TryInto>, prefixes: &Prefixes) -> Result { let root = self.root.join(&path::convert(root)?)?; - let rooted = patterns.rooted(root.as_str()).map_err(|_| BoundsExceeded)?; + let rooted = prefixes.rooted(root.as_str()).map_err(|_| BoundsExceeded)?; let scope = self.scope.narrow(&rooted).ok_or(Error::Unauthorized)?; + // A filter too intricate to narrow with the scope lists nothing it can name; refuse + // it rather than list less than asked. + let listed = self + .listed + .intersect(&rooted.patterns()) + .map_err(|_| Error::Unauthorized)?; Ok(Consumer { scope, + listed, root, ..self.clone() }) } + /// A clone whose [`announced`](Self::announced) and [`routes`](Self::routes) list only + /// the routes `pattern`, relative to this root, matches, and report what its wildcards + /// captured ([`Announce::captures`]). Filters narrow each other. A discovery filter, not + /// a scope: requests are unaffected. + /// + /// Returns [`Error::BoundsExceeded`] when rooting the pattern would exceed the path + /// limit or the root holds a `*`, and [`Error::Unauthorized`] when the filter is too + /// intricate to narrow what this handle lists. + pub fn filter(&self, pattern: &Pattern) -> Result { + let rooted = pattern.rooted(self.root.as_str()).map_err(|_| BoundsExceeded)?; + let listed = self.listed.intersect(&rooted.into()).map_err(|_| Error::Unauthorized)?; + Ok(Consumer { listed, ..self.clone() }) + } + /// Resolve a broadcast by exact path, optionally pinning its [`Instance`]. /// /// Returns a [`kio::Pending`] future, mirroring @@ -8500,8 +8517,8 @@ impl Consumer { &self.root } - /// The patterns this consumer may reach, relative to its root. - pub fn allowed(&self) -> Patterns { + /// The prefixes this consumer may reach, relative to its root. + pub fn allowed(&self) -> Prefixes { self.scope.relative(&self.root) } @@ -9177,7 +9194,7 @@ mod tests { ..Default::default() }; let (producer, _driver) = Producer::new(config); - let scoped = producer.scope("room", &Patterns::from(Pattern::all())).unwrap(); + let scoped = producer.scope("room", &Prefixes::all()).unwrap(); assert_eq!(scoped.config().max_hops.get(), effective); assert_eq!( scoped.consume().forwardable().horizon.max_hops.map(NonZeroUsize::get), @@ -9249,10 +9266,10 @@ mod tests { } /// The scope granting these prefixes: each spelled as its subtree pattern. - fn scopes(prefixes: &[&str]) -> Patterns { + fn scopes(prefixes: &[&str]) -> Prefixes { prefixes .iter() - .map(|prefix| Pattern::subtree(prefix).unwrap()) + .map(|prefix| Prefix::try_from(*prefix).unwrap()) .collect() } @@ -9572,10 +9589,7 @@ mod tests { #[test] fn only_overlapping_snapshots_hold() { let producer = origin(1).produce(); - let room = producer - .consume() - .scope("room", &Patterns::from(Pattern::all())) - .unwrap(); + let room = producer.consume().scope("room", &Prefixes::all()).unwrap(); let other = producer.snapshot("other"); assert!(room.routes().is_synced(), "a disjoint prefix"); @@ -9598,7 +9612,7 @@ mod tests { #[test] fn a_shadowed_snapshot_never_holds() { let producer = origin(1).produce(); - let project = mounted(&producer, "p1", &["**"]).with_hidden(true); + let project = mounted(&producer, "p1", &[""]).with_hidden(true); let announced = project.routes(); assert!(announced.is_synced()); @@ -9694,10 +9708,7 @@ mod tests { announced.assert_next_wait(); // Naming the dot segment lists what is under it, by root or by pattern. - let mut announced = consumer - .scope(".stats", &Patterns::from(Pattern::all())) - .unwrap() - .routes(); + let mut announced = consumer.scope(".stats", &Prefixes::all()).unwrap().routes(); announced.expect_next_active("node").await; announced.assert_next_wait(); let mut announced = consumer.scope("", &scopes(&["room/.internal"])).unwrap().routes(); @@ -9731,7 +9742,7 @@ mod tests { let _overlay = producer.route("p1/.pro/cam", RouteOptions::default()).unwrap(); let consumer = producer .consume() - .scope("p1", &Patterns::from(Pattern::all())) + .scope("p1", &Prefixes::all()) .unwrap() .with_hidden(true) .exclude(".pro") @@ -9765,7 +9776,7 @@ mod tests { let _foo = producer.route(".svc/p1/foo", RouteOptions::default()).unwrap(); let _bar = producer.route(".svc/p1/bar", RouteOptions::default()).unwrap(); let _cam = producer.route("p1/cam", RouteOptions::default()).unwrap(); - let project = mounted(&producer, "p1", &["**"]).with_hidden(true); + let project = mounted(&producer, "p1", &[""]).with_hidden(true); let mut announced = project.exclude(".svc/foo").unwrap().routes(); announced.expect_next_active("cam").await; @@ -9778,7 +9789,7 @@ mod tests { // Rooted inside the mount, the exclusion moves onto the target. let mut inside = project - .scope(".svc", &Patterns::from(Pattern::all())) + .scope(".svc", &Prefixes::all()) .unwrap() .exclude("foo") .unwrap() @@ -9788,12 +9799,11 @@ mod tests { } /// A project-rooted handle reading `.svc` from the fleet-wide `.svc/`. - fn mounted(producer: &Producer, pid: &str, patterns: &[&str]) -> Consumer { - let patterns: Patterns = patterns.iter().map(|pattern| pattern.parse().unwrap()).collect(); + fn mounted(producer: &Producer, pid: &str, prefixes: &[&str]) -> Consumer { producer .mount(format!("{pid}/.svc"), format!(".svc/{pid}")) .unwrap() - .scope(pid, &patterns) + .scope(pid, &scopes(prefixes)) .unwrap() .consume() } @@ -9805,7 +9815,7 @@ mod tests { async fn mount_resolves_on_the_target_front() { let producer = origin(1).produce(); let server = producer.forward(".svc", RouteInfo::new()).unwrap(); - let project = mounted(&producer, "p1", &["**"]); + let project = mounted(&producer, "p1", &[""]); let through = project.request(".svc/foo", None); let direct = producer.consume().request(".svc/p1/foo", None); @@ -9834,7 +9844,7 @@ mod tests { let _other = producer.route(".svc/p2/bar", RouteOptions::default()).unwrap(); let _cam = producer.route("p1/cam", RouteOptions::default()).unwrap(); let _shadowed = producer.route("p1/.svc/forged", RouteOptions::default()).unwrap(); - let project = mounted(&producer, "p1", &["**"]); + let project = mounted(&producer, "p1", &[""]); // A dot segment hides the mount like any other. let mut announced = project.routes(); @@ -9848,11 +9858,7 @@ mod tests { announced.assert_next_wait(); // Rooted inside the mount, the claim covers the root itself. - let mut inside = project - .scope(".svc", &Patterns::from(Pattern::all())) - .unwrap() - .covering() - .routes(); + let mut inside = project.scope(".svc", &Prefixes::all()).unwrap().covering().routes(); inside.expect_next_active("").await; inside.expect_next_active("foo").await; inside.assert_next_wait(); @@ -9896,7 +9902,7 @@ mod tests { narrower.clone().with_hidden(true).routes().assert_next_wait(); // Another project's mount reaches its own target, never this one's. - let other = mounted(&producer, "p2", &["**"]); + let other = mounted(&producer, "p2", &[""]); other.clone().with_hidden(true).routes().assert_next_wait(); let err = other.request(".svc/foo", None).await.err().unwrap(); assert!(matches!(err, Error::Unroutable)); @@ -9908,7 +9914,7 @@ mod tests { async fn mount_captures_the_named_path() { let producer = origin(1).produce(); let _foo = producer.route(".svc/p1/foo", RouteOptions::default()).unwrap(); - let project = mounted(&producer, "p1", &["**"]).with_hidden(true); + let project = mounted(&producer, "p1", &[""]).with_hidden(true); let Some(RouteEvent { kind: RouteKind::Start, @@ -9920,7 +9926,7 @@ mod tests { assert_eq!(update.prefix.as_str(), ".svc/foo"); assert_eq!(update.captures, Some(vec![".svc/foo".parse::().unwrap()])); - let inside = project.scope(".svc", &Patterns::from(Pattern::all())).unwrap(); + let inside = project.scope(".svc", &Prefixes::all()).unwrap(); let Some(RouteEvent { kind: RouteKind::Start, announce: update, @@ -9932,8 +9938,8 @@ mod tests { assert_eq!(update.captures, Some(vec!["foo".parse::().unwrap()])); } - /// A target at the maximum depth still presents its exact path: the `**` a - /// wildcard scope carries past it can only match nothing there. + /// A target at the maximum depth still presents its exact path: nothing a scope + /// covers beneath it fits there. #[kio::test] #[cfg(feature = "async")] async fn mount_keeps_a_max_depth_target() { @@ -9943,7 +9949,7 @@ mod tests { let project = producer .mount("p1/.svc", deep.as_str()) .unwrap() - .scope("p1", &Patterns::from(Pattern::all())) + .scope("p1", &Prefixes::all()) .unwrap() .consume() .with_hidden(true); @@ -9955,7 +9961,7 @@ mod tests { // Beneath the mount the target has no room: refused, never handed to a route. let err = project.request(".svc/x", None).await.err().unwrap(); assert!(matches!(err, Error::BoundsExceeded(_)), "{err:?}"); - let inside = project.scope(".svc/x", &Patterns::from(Pattern::all())).unwrap(); + let inside = project.scope(".svc/x", &Prefixes::all()).unwrap(); inside.routes().assert_next_wait(); } @@ -9970,7 +9976,7 @@ mod tests { let project = producer .mount("p1/a/b", "t") .unwrap() - .scope("p1", &Patterns::from(Pattern::all())) + .scope("p1", &Prefixes::all()) .unwrap() .consume(); @@ -9992,7 +9998,7 @@ mod tests { let project = producer .mount("p1/.svc", ".svc/p1") .unwrap() - .scope("p1", &Patterns::from(Pattern::all())) + .scope("p1", &Prefixes::all()) .unwrap(); assert!(matches!( project.route(".svc/foo", RouteOptions::hidden()), @@ -10057,7 +10063,7 @@ mod tests { let registry = stats::Registry::new(stats::Config::new()); let producer = origin(1).produce(); let _foo = producer.route(".svc/p1/foo", RouteOptions::default()).unwrap(); - let project = mounted(&producer, "p1", &["**"]) + let project = mounted(&producer, "p1", &[""]) .with_stats( registry .tier(stats::Tier::default()) @@ -10392,10 +10398,7 @@ mod tests { let producer = origin(1).produce(); let _a = producer.announce("room/alice", RouteInfo::new()).unwrap(); - let consumer = producer - .consume() - .scope("room", &Patterns::from(Pattern::all())) - .unwrap(); + let consumer = producer.consume().scope("room", &Prefixes::all()).unwrap(); let mut announced = consumer.routes(); announced.expect_next_active("alice").await; } @@ -11267,10 +11270,7 @@ mod tests { let _broad = producer.announce("room", RouteInfo::new().with_cost(1)).unwrap(); let _narrow = producer.announce("room/alice", RouteInfo::new().with_cost(9)).unwrap(); - let consumer = producer - .consume() - .scope("room/alice", &Patterns::from(Pattern::all())) - .unwrap(); + let consumer = producer.consume().scope("room/alice", &Prefixes::all()).unwrap(); let mut announced = consumer.routes(); let route = announced.expect_next_active("").await; assert_eq!(route.cost, Cost::new(9)); @@ -11285,9 +11285,9 @@ mod tests { let exact = producer.announce("room/alice", RouteInfo::new()).unwrap(); let consumer = producer .consume() - .scope("", &Patterns::from("room/*".parse::().unwrap())) + .filter(&"room/*".parse::().unwrap()) .unwrap() - .scope("room/alice", &Patterns::from(Pattern::all())) + .scope("room/alice", &Prefixes::all()) .unwrap() .covering(); let mut announced = consumer.routes(); @@ -11332,7 +11332,7 @@ mod tests { let exact = producer.forward("room/alice", RouteInfo::new()).unwrap(); let mut announced = producer .consume() - .scope("room/alice", &Patterns::from(Pattern::all())) + .scope("room/alice", &Prefixes::all()) .unwrap() .covering() .routes(); @@ -11351,7 +11351,7 @@ mod tests { let exact = producer.forward("room/alice", RouteInfo::new()).unwrap(); let mut announced = producer .consume() - .scope("room/alice", &Patterns::from(Pattern::all())) + .scope("room/alice", &Prefixes::all()) .unwrap() .covering() .routes(); @@ -11450,6 +11450,32 @@ mod tests { drop(broadcast); } + /// A discovery filter narrows what a handle lists, never what it resolves: a path the + /// filter leaves out still follows and requests through the scope alone. + #[kio::test] + #[cfg(feature = "async")] + async fn a_filter_never_narrows_a_lookup() { + let producer = origin(1).produce(); + let broadcast = producer.route("b", RouteOptions::hidden()).unwrap(); + broadcast.announce().unwrap(); + let consumer = producer.consume().filter(&"a".parse().unwrap()).unwrap(); + consumer.routes().assert_next_wait(); + + let mut following = consumer.follow("b", None).unwrap(); + let followed = kio::clock::timeout(Duration::from_secs(1), following.available()) + .await + .expect("follows a path the filter leaves out") + .unwrap() + .expect("the followed broadcast"); + assert_eq!(followed.info().path.as_str(), "b"); + let requested = kio::clock::timeout(Duration::from_secs(1), consumer.request("b", None)) + .await + .expect("requests a path the filter leaves out") + .unwrap(); + assert_eq!(requested.info().path.as_str(), "b"); + drop(broadcast); + } + /// A local broadcast competes on its announced cost: a cheaper route at the /// same path wins, for cursors and requests alike. #[kio::test] @@ -11757,7 +11783,7 @@ mod tests { fn the_route_node_answers_with_the_table_as_it_stands() { let producer = origin(1).produce(); (producer.clone().with_link(Link::Upstream).with_proxy(true).consume()) - .scope("elsewhere", &Patterns::from(Pattern::all())) + .scope("elsewhere", &Prefixes::all()) .unwrap() .with_hidden(false) .route_node(); @@ -12712,7 +12738,7 @@ mod tests { )); // The byte limit counts the root the producer joins on. - let rooted = producer.scope("r", &Patterns::from(Pattern::all())).unwrap(); + let rooted = producer.scope("r", &Prefixes::all()).unwrap(); let longest = "b".repeat(Path::MAX_BYTES - 2); let _long = rooted .route(longest.as_str(), RouteOptions::hidden()) @@ -12773,8 +12799,7 @@ mod tests { #[cfg(feature = "async")] async fn forward_keeps_its_producer_scope() { let producer = origin(1).produce(); - let scope = Patterns::from("*/chat".parse::().unwrap()); - let scoped = producer.scope("", &scope).unwrap(); + let scoped = producer.scope("", &scopes(&["room/chat"])).unwrap(); let dynamic = scoped.forward("", RouteInfo::new()).unwrap(); let mut matching = producer @@ -12808,13 +12833,9 @@ mod tests { #[kio::test] async fn scoped_cursor_selects_among_the_routes_it_can_see() { let producer = origin(1).produce(); - let scoped = |pattern: &str| { - producer - .scope("", &Patterns::from(pattern.parse::().unwrap())) - .unwrap() - }; - let _chat = scoped("*/chat").forward("", RouteInfo::new().with_cost(1)).unwrap(); - let _video = scoped("*/video").forward("", RouteInfo::new().with_cost(5)).unwrap(); + let scoped = |prefix: &str| producer.scope("", &scopes(&[prefix])).unwrap(); + let _chat = scoped("room/chat").forward("", RouteInfo::new().with_cost(1)).unwrap(); + let _video = scoped("room/video").forward("", RouteInfo::new().with_cost(5)).unwrap(); let mut video = producer .consume() @@ -12838,10 +12859,7 @@ mod tests { .unwrap() .covering() .routes(); - let mut rooted = consumer - .scope("room/live", &Patterns::from(Pattern::all())) - .unwrap() - .routes(); + let mut rooted = consumer.scope("room/live", &Prefixes::all()).unwrap().routes(); let chat = producer .scope("", &scopes(&["room/live/chat"])) @@ -12885,7 +12903,7 @@ mod tests { let narrow = producer.forward("room/live", RouteInfo::new().with_cost(9)).unwrap(); let mut announced = producer .consume() - .scope("room/live/video", &Patterns::from(Pattern::all())) + .scope("room/live/video", &Prefixes::all()) .unwrap() .covering() .routes(); @@ -13163,7 +13181,7 @@ mod tests { ); // The mark survives narrowing the handle. - let scoped = peer.scope("room", &Patterns::from(Pattern::all())).unwrap(); + let scoped = peer.scope("room", &Prefixes::all()).unwrap(); let _nested = scoped.forward("x", RouteInfo::new().with_via(origin(8))).unwrap(); assert_eq!(announced.expect_next_active("room/x").await.source(), Source::Peer(8)); } @@ -16862,18 +16880,23 @@ mod tests { } #[kio::test] - async fn scope_accepts_every_pattern_union() { + async fn scope_takes_prefixes_each_covering_its_subtree() { let producer = origin(1).produce(); - // The root grant is `**`, the old empty prefix. - let root = producer.scope("", &Patterns::from(Pattern::all())).unwrap(); - assert_eq!(root.allowed(), Patterns::from(Pattern::all())); + // The empty prefix grants everything. + let root = producer.scope("", &Prefixes::all()).unwrap(); + assert_eq!(root.allowed(), Prefixes::all()); - // `foo/**` keeps the old `foo` prefix meaning. let scoped = producer.scope("", &scopes(&["room"])).unwrap(); assert_eq!(scoped.allowed(), scopes(&["room"])); + assert!(scoped.route("room", RouteOptions::default()).is_ok()); + assert!(scoped.route("room/alice", RouteOptions::default()).is_ok()); + assert!(matches!( + scoped.route("roomy", RouteOptions::default()), + Err(Error::Unauthorized) + )); - // Multiple prefixes round-trip, with overlap collapsed. + // Multiple prefixes round-trip, with nested ones collapsed. let multi = producer.scope("", &scopes(&["room", "room/chat", "anon"])).unwrap(); assert_eq!(multi.allowed(), scopes(&["room", "anon"])); @@ -16881,20 +16904,11 @@ mod tests { let consumer = producer.consume().scope("", &scopes(&["room"])).unwrap(); assert_eq!(consumer.allowed(), scopes(&["room"])); - for text in ["room", "", "*room", "room/*", "*", "**/room", "room/**/chat", "*.hang"] { - let union = Patterns::from(text.parse::().unwrap()); - assert_eq!(producer.scope("", &union).expect(text).allowed(), union, "{text}"); - assert_eq!( - producer.consume().scope("", &union).expect(text).allowed(), - union, - "{text}" - ); - } - - let mixed: Patterns = ["room/**".parse().unwrap(), "other".parse().unwrap()] - .into_iter() - .collect(); - assert_eq!(producer.scope("", &mixed).unwrap().allowed(), mixed); + // A root no prefix can spell is refused, not read as a pattern. + assert!(matches!( + producer.scope("room/*", &Prefixes::all()), + Err(Error::BoundsExceeded(_)) + )); } #[kio::test] @@ -17234,9 +17248,7 @@ mod tests { assert!(producer.route(path, RouteOptions::hidden()).is_err(), "created {path}"); assert!(producer.announce(path, RouteInfo::new()).is_err(), "announced {path}"); } - let scoped = producer - .scope("room/..", &Patterns::from_iter([Pattern::all()])) - .unwrap(); + let scoped = producer.scope("room/..", &Prefixes::all()).unwrap(); assert!(scoped.route("cam", RouteOptions::hidden()).is_err()); for path in ["room/.pro/cam", "room/a..b", "room/cam"] { let _route = producer.route(path, RouteOptions::hidden()).unwrap(); @@ -17264,10 +17276,10 @@ mod tests { async fn scope_empty_union_grants_nothing() { let producer = origin(1).produce(); - // An empty union grants nothing: scoping is refused, like a disjoint prefix. - assert!(matches!(producer.scope("", &Patterns::new()), Err(Error::Unauthorized))); + // An empty set grants nothing: scoping is refused, like a disjoint prefix. + assert!(matches!(producer.scope("", &Prefixes::new()), Err(Error::Unauthorized))); assert!(matches!( - producer.consume().scope("", &Patterns::new()), + producer.consume().scope("", &Prefixes::new()), Err(Error::Unauthorized) )); } @@ -17288,7 +17300,7 @@ mod tests { )); // A literal root rebases the grant without changing its meaning. - let rooted = nested.scope("room/chat", &Patterns::from(Pattern::all())).unwrap(); + let rooted = nested.scope("room/chat", &Prefixes::all()).unwrap(); assert_eq!(rooted.allowed(), scopes(&[""])); // Publishing through the nested view lands where the root says. @@ -17298,42 +17310,36 @@ mod tests { } #[kio::test] - async fn scope_intersects_and_rebases_arbitrary_grants() { + async fn scope_intersects_and_rebases_prefixes() { let producer = origin(1).produce(); - let rooms = producer - .scope("", &Patterns::from("room/*".parse::().unwrap())) - .unwrap(); - let chats = rooms - .scope("", &Patterns::from("*/chat".parse::().unwrap())) - .unwrap(); - assert_eq!(chats.allowed(), Patterns::from("room/chat".parse::().unwrap())); - - let exact = producer - .scope("", &Patterns::from("room/alice".parse::().unwrap())) - .unwrap(); - let rooted = exact.scope("room", &Patterns::from(Pattern::all())).unwrap(); - assert_eq!(rooted.allowed(), Patterns::from("alice".parse::().unwrap())); + // Narrowing keeps the deeper of two nested prefixes, whichever side holds it. + let rooms = producer.scope("", &scopes(&["room", "lobby/a"])).unwrap(); + let both = rooms.scope("", &scopes(&["room/chat", "lobby"])).unwrap(); + assert_eq!(both.allowed(), scopes(&["room/chat", "lobby/a"])); + + let alice = producer.scope("", &scopes(&["room/alice"])).unwrap(); + let rooted = alice.scope("room", &Prefixes::all()).unwrap(); + assert_eq!(rooted.allowed(), scopes(&["alice"])); assert!(matches!( - exact.scope("room/bob", &Patterns::from(Pattern::all())), + alice.scope("room/bob", &Prefixes::all()), Err(Error::Unauthorized) )); - let broadcast = exact.route("room/alice", RouteOptions::hidden()).unwrap(); + // A prefix covers its whole subtree, so what lies beneath publishes too. + let broadcast = alice.route("room/alice", RouteOptions::hidden()).unwrap(); + let cam = alice.route("room/alice/cam", RouteOptions::hidden()).unwrap(); assert!(matches!( - exact.route("room/alice/cam", RouteOptions::hidden()), + alice.route("room/bob", RouteOptions::hidden()), Err(Error::Unauthorized) )); assert!(producer.consume().get_broadcast("room/alice").is_some()); - drop(broadcast); + drop((broadcast, cam)); } #[kio::test] - async fn wildcard_scope_filters_announcements_and_reports_captures() { + async fn a_filter_lists_its_matches_and_reports_captures() { let producer = origin(1).produce(); - let consumer = producer - .consume() - .scope("", &Patterns::from("room/*/chat".parse::().unwrap())) - .unwrap(); + let consumer = producer.consume().filter(&"room/*/chat".parse().unwrap()).unwrap(); let mut listed = consumer.routes(); let mut announced = consumer.covering().routes(); @@ -17374,20 +17380,20 @@ mod tests { #[kio::test] async fn announced_lists_full_names_in_replay_and_live_changes() { let producer = origin(1).produce(); - let patterns: Patterns = ["*.hang", "**/*.chat"] - .into_iter() - .map(|p| p.parse::().unwrap()) - .collect(); - let consumer = producer.consume().scope("project", &patterns).unwrap(); + let consumer = (producer.consume().scope("project", &Prefixes::all()).unwrap()) + .filter(&"**/*.chat".parse().unwrap()) + .unwrap(); let cam = producer.announce("project/cam.hang", RouteInfo::new()).unwrap(); let overlap = producer.announce("project/room/mic.hang", RouteInfo::new()).unwrap(); let chat = producer.announce("project/lobby.chat", RouteInfo::new()).unwrap(); + let _other = producer.announce("elsewhere/lobby.chat", RouteInfo::new()).unwrap(); + // Every route whose subtree could hold a match, within the scope. let mut routing = consumer.clone().covering().routes(); routing.expect_next_active("cam.hang").await; routing.expect_next_active("lobby.chat").await; routing.expect_next_active("room/mic.hang").await; + routing.assert_next_wait(); let mut selected = consumer.routes(); - selected.expect_next_active("cam.hang").await; selected.expect_next_active("lobby.chat").await; selected.assert_next_wait(); @@ -17405,10 +17411,7 @@ mod tests { #[kio::test] async fn announced_keeps_lifecycle_and_filters_before_rerooting() { let producer = origin(1).produce(); - let consumer = producer - .consume() - .scope("room/alice", &Patterns::from(Pattern::all())) - .unwrap(); + let consumer = producer.consume().scope("room/alice", &Prefixes::all()).unwrap(); let broad = producer.announce("room", RouteInfo::new()).unwrap(); let mut selected = consumer.routes(); selected.assert_next_wait(); @@ -17466,14 +17469,16 @@ mod tests { let _excluded = producer .announce(".svc/p1/private/lobby.chat", RouteInfo::new()) .unwrap(); - let project = mounted(&producer, "p1", &["**/*.chat"]) + let project = mounted(&producer, "p1", &[""]) + .filter(&"**/*.chat".parse().unwrap()) + .unwrap() .with_hidden(true) .exclude(".svc/private") .unwrap(); let mut selected = project.routes(); selected.expect_next_active(".svc/.private/lobby.chat").await; selected.assert_next_wait(); - let mut inside = project.scope(".svc", &Patterns::from(Pattern::all())).unwrap().routes(); + let mut inside = project.scope(".svc", &Prefixes::all()).unwrap().routes(); inside.expect_next_active(".private/lobby.chat").await; inside.assert_next_wait(); let live = producer.announce(".svc/p1/lobby.chat", RouteInfo::new()).unwrap(); @@ -17491,9 +17496,8 @@ mod tests { let producer = origin(1).produce(); let local = producer.route("project/cam.chat", RouteOptions::default()).unwrap(); let overlap = producer.announce("project/room", RouteInfo::new()).unwrap(); - let consumer = producer - .consume() - .scope("project", &Patterns::from("**/*.chat".parse::().unwrap())) + let consumer = (producer.consume().scope("project", &Prefixes::all()).unwrap()) + .filter(&"**/*.chat".parse().unwrap()) .unwrap() .local() .with_stats( diff --git a/rs/moq-net/src/path/mod.rs b/rs/moq-net/src/path/mod.rs index c5bb565b4ba..7cadd2781c6 100644 --- a/rs/moq-net/src/path/mod.rs +++ b/rs/moq-net/src/path/mod.rs @@ -1,14 +1,17 @@ -//! Broadcast paths and the patterns that match them. +//! Broadcast paths, the prefixes that grant them, and the patterns that filter them. //! //! [`Path`] is a literal coordinate: `/`-separated non-empty segments, with //! segment-aware prefix operations. A `\` escapes the `\` or `/` after it, so `a\/b` is one -//! segment holding a `/`; [`parts`] is the one walk every segment reading goes through. [`Pattern`] describes a set of paths with -//! wildcards, and [`Patterns`] is a union of them reduced by containment. The -//! grammar and algebra live in [`moq-pattern`](moq_pattern); this module -//! re-exports them beside [`Path`] so grants, origin scopes, and announce -//! interests can share one dialect. +//! segment holding a `/`; [`parts`] is the one walk every segment reading goes through. +//! [`Prefixes`] is a grant or an origin scope: a set of [`Prefix`]es, each covering its +//! whole subtree. [`Pattern`] describes a set of paths with wildcards, for discovery +//! filters, and [`Patterns`] is a union of them reduced by containment. All of these live +//! in [`moq-pattern`](moq_pattern); this module re-exports them beside [`Path`]. -pub use moq_pattern::{InvalidPattern, Parts, Pattern, Patterns, Segment, Specificity, escape, parts, unescape}; +pub use moq_pattern::{ + InvalidPattern, InvalidPrefix, Parts, Pattern, Patterns, Prefix, Prefixes, Segment, Specificity, escape, parts, + unescape, +}; use std::fmt::{self, Display}; use std::sync::Arc; @@ -699,35 +702,30 @@ impl Encode for Path { } } -/// A grant's patterns as a count and each one's canonical text, the one encoding both -/// wires' AUTH_OK share. -impl Encode for Patterns { +/// A grant's prefixes as a count and each one's text, as AUTH_OK carries them. +impl Encode for Prefixes { fn encode(&self, w: &mut Encoder<'_>, _: V) -> Result<(), EncodeError> { w.varint(self.len() as u64)?; - for pattern in self { - w.string(pattern.as_str())?; + for prefix in self { + w.string(prefix.as_str())?; } Ok(()) } } -impl<'a, V> Decode<'a, V> for Patterns { +impl<'a, V> Decode<'a, V> for Prefixes { fn decode(r: &mut Decoder<'a>, _: V) -> Result { let count = r.varint()?; // No preallocation: the count is peer-controlled, and the message size limit - // is what bounds how many patterns actually fit. - let mut patterns = Vec::new(); + // is what bounds how many prefixes actually fit. + let mut prefixes = Vec::new(); for _ in 0..count { - let text = r.string()?; - let pattern = Pattern::try_from(text.as_str()).map_err(|_| DecodeError::InvalidValue)?; - // Only the canonical spelling is valid, so each pattern has one encoding. - if pattern.as_str() != text { - return Err(DecodeError::InvalidValue); - } - patterns.push(pattern); + // A wildcard is refused like any other malformed prefix. + let prefix = Prefix::try_from(r.string()?).map_err(|_| DecodeError::InvalidValue)?; + prefixes.push(prefix); } - // Reduce once, so a peer's count never turns into quadratic work. - Ok(patterns.into_iter().collect()) + // Reduce once, in one sort, so a peer's count never turns into quadratic work. + Ok(prefixes.into_iter().collect()) } } diff --git a/rs/moq-net/src/test_interop.rs b/rs/moq-net/src/test_interop.rs index 9d904febfaf..32bffeeae6b 100644 --- a/rs/moq-net/src/test_interop.rs +++ b/rs/moq-net/src/test_interop.rs @@ -488,8 +488,8 @@ fn ietf_datagram_interop() { fn lite_auth_unsupported_interop() { let version = lite::Version::Lite07; let grant = lite::AuthReply::Ok(lite::AuthOk { - publish: crate::Patterns::from(crate::Pattern::subtree("room").unwrap()), - subscribe: crate::Patterns::new(), + publish: crate::Prefixes::from(crate::Prefix::try_from("room").unwrap()), + subscribe: crate::Prefixes::new(), expires: None, }); let refused = lite::AuthReply::Error(lite::AuthError { diff --git a/rs/moq-net/tests/integration/announce_covering_route.rs b/rs/moq-net/tests/integration/announce_covering_route.rs index 1670b135478..fe2b1df4377 100644 --- a/rs/moq-net/tests/integration/announce_covering_route.rs +++ b/rs/moq-net/tests/integration/announce_covering_route.rs @@ -11,7 +11,7 @@ use crate::support; use std::time::Duration; -use moq_net::{Pattern, Patterns, Version, origin}; +use moq_net::{Pattern, Prefix, Prefixes, Version, origin}; use support::harness::{MockConnectOptions, connect_mock}; /// Long enough, in virtual time, for anything in flight to reach the far side. @@ -40,13 +40,13 @@ fn drain(announced: &mut moq_net::origin::Routes) -> Vec { /// subscriber whose announce interest is that prefix. async fn covering(version: Option<&str>) -> Vec { let publisher = produce_origin(1); - let everything = Patterns::from(Pattern::all()); + let everything = Prefixes::all(); let (observer, pair) = match version { None => (publisher.consume(), None), Some(version) => { let subscriber = produce_origin(2); - let interest = Patterns::from(Pattern::subtree(REQUESTED).unwrap()); + let interest = Prefixes::from(Prefix::try_from(REQUESTED).unwrap()); let scoped = subscriber.scope("", &interest).unwrap(); let mut options = MockConnectOptions::new(version.parse::().unwrap()); options.server_publish = Some(publisher.consume()); @@ -110,12 +110,18 @@ async fn remote_ietf_peer_hears_the_covering_route() { } } -/// A relay forwards a claim at `room` through a `**/*.chat` scope, since a path beneath it -/// could match, so a request for `room/lobby.chat` is served across both hops. Listing that -/// scope at the far end shows only literal matches, so the claim stays out of it. +/// A relay forwards a claim at `room` through a scope granting `room/lobby.chat`, since the +/// claim covers it, so a request for `room/lobby.chat` is served across both hops. A +/// `**/*.chat` listing at the far end shows only literal matches, so the claim stays out of it. +/// lite-07 tells a claim at the scope's region instead, here `room/lobby.chat` itself, which the +/// listing then matches. #[kio::test] async fn relays_route_a_covering_claim_that_listings_omit() { - let chat = Patterns::from("**/*.chat".parse::().unwrap()); + let scope: Prefixes = ["lobby.chat", "room/lobby.chat"] + .into_iter() + .map(|prefix| Prefix::try_from(prefix).unwrap()) + .collect(); + let chat = "**/*.chat".parse::().unwrap(); for version in [ "moq-lite-03", "moq-lite-06", @@ -123,6 +129,14 @@ async fn relays_route_a_covering_claim_that_listings_omit() { "moq-transport-18", "moq-transport-22", ] { + // lite-07 tells the claim at the scope's region, which the listing matches. + let (expect_listed, expect_covering): (&[&str], &[&str]) = match version { + "moq-lite-07-wip" => ( + &["Start lobby.chat", "Start room/lobby.chat"], + &["Start lobby.chat", "Start room/lobby.chat"], + ), + _ => (&["Start lobby.chat"], &["Start lobby.chat", "Start room"]), + }; let version: Version = version.parse().unwrap(); let publisher = produce_origin(1); let claim = publisher @@ -141,18 +155,20 @@ async fn relays_route_a_covering_claim_that_listings_omit() { let client = produce_origin(3); let mut options = MockConnectOptions::new(version); - options.server_publish = Some(relay.consume().scope("", &chat).unwrap()); + options.server_publish = Some(relay.consume().scope("", &scope).unwrap()); options.client_subscribe = Some(client.clone()); let _downstream = connect_mock(options).await; - let scoped = client.consume().scope("", &chat).unwrap(); + let scoped = client.consume().filter(&chat).unwrap(); let mut listed = scoped.routes(); let mut covering = scoped.clone().covering().routes(); kio::clock::sleep(SETTLE).await; - assert_eq!(drain(&mut listed), ["Start lobby.chat"], "{version}"); + let mut routes = drain(&mut listed); + routes.sort(); + assert_eq!(routes, expect_listed, "{version}"); let mut routes = drain(&mut covering); routes.sort(); - assert_eq!(routes, ["Start lobby.chat", "Start room"], "{version}"); + assert_eq!(routes, expect_covering, "{version}"); // The claim is asked once something subscribes beneath it. let broadcast = kio::clock::timeout(SETTLE, scoped.request("room/lobby.chat", None)) diff --git a/rs/moq-net/tests/integration/auth.rs b/rs/moq-net/tests/integration/auth.rs index 48a399c618a..f30d2280db8 100644 --- a/rs/moq-net/tests/integration/auth.rs +++ b/rs/moq-net/tests/integration/auth.rs @@ -11,7 +11,7 @@ use std::time::Duration; use futures::StreamExt; use moq_net::{ - Client, Error, Pattern, Patterns, Server, Session, SessionError, StreamError, Version, + Client, Error, Prefix, Prefixes, Server, Session, SessionError, StreamError, Version, auth::{self, Grant}, origin, }; @@ -71,9 +71,9 @@ cases!( a_token_queued_before_our_goaway_rides_only_the_successor, a_broadcast_received_from_the_peer_is_not_ours_to_check, a_broadcast_relayed_from_another_client_is_not_ours_to_check, - exact_and_subtree_grants_admit_their_broadcast, - pattern_grants_arrive_exactly, - a_wildcard_grant_is_enforced, + a_prefix_grant_admits_its_broadcast, + prefix_grants_arrive_exactly, + a_prefix_grant_is_enforced_by_segment, ); #[kio::test] @@ -331,19 +331,19 @@ fn produce_origin(hop: u64) -> origin::Producer { producer.with_proxy(true) } -fn patterns(prefixes: &[&str]) -> Patterns { +fn prefixes(prefixes: &[&str]) -> Prefixes { prefixes .iter() - .map(|prefix| Pattern::subtree(prefix).unwrap()) + .map(|prefix| Prefix::try_from(*prefix).unwrap()) .collect() } fn grant(publish: &[&str], subscribe: &[&str]) -> Grant { - grant_of(patterns(publish), patterns(subscribe)) + grant_of(prefixes(publish), prefixes(subscribe)) } /// A grant of exactly these paths that never expires. -fn grant_of(publish: Patterns, subscribe: Patterns) -> Grant { +fn grant_of(publish: Prefixes, subscribe: Prefixes) -> Grant { let mut grant = Grant::default(); grant.publish = publish; grant.subscribe = subscribe; @@ -535,8 +535,8 @@ async fn both_sides_learn_their_grant_from_scoped_origins(version: &'static str) version: Some(version), client_publish: Some(produce_origin(2)), client_subscribe: Some(produce_origin(3)), - server_publish: Some(relay.scope("", &patterns(&["room"])).unwrap()), - server_subscribe: Some(relay.scope("", &patterns(&["room/alice"])).unwrap()), + server_publish: Some(relay.scope("", &prefixes(&["room"])).unwrap()), + server_subscribe: Some(relay.scope("", &prefixes(&["room/alice"])).unwrap()), ..Default::default() }) .await; @@ -578,7 +578,7 @@ async fn an_out_of_scope_announce_aborts_with_the_path(version: &'static str) { let pair = connect(Options { version: Some(version), client_publish: Some(publisher.clone()), - server_subscribe: Some(relay.scope("", &patterns(&["baz"])).unwrap()), + server_subscribe: Some(relay.scope("", &prefixes(&["baz"])).unwrap()), ..Default::default() }) .await; @@ -620,7 +620,7 @@ async fn a_broadcast_published_before_the_grant_is_checked(version: &'static str let pair = connect(Options { version: Some(version), client_publish: Some(publisher.clone()), - server_subscribe: Some(produce_origin(1).scope("", &patterns(&["baz"])).unwrap()), + server_subscribe: Some(produce_origin(1).scope("", &prefixes(&["baz"])).unwrap()), ..Default::default() }) .await; @@ -1222,10 +1222,10 @@ async fn older_versions_have_no_grant(version: &'static str) { .expect("timed out"); } -/// An exact path and a subtree are both admitted, and told as issued wherever AUTH runs: -/// a grant is never refused for the shape of its patterns. -async fn exact_and_subtree_grants_admit_their_broadcast(version: &'static str) { - for scope in ["ch1", "ch1/**"] { +/// A prefix naming the broadcast and the empty prefix both admit it, and are told as +/// issued wherever AUTH runs. +async fn a_prefix_grant_admits_its_broadcast(version: &'static str) { + for scope in ["ch1", ""] { within(async { let relay = produce_origin(1); let broadcast = relay.route("ch1", moq_net::origin::RouteOptions::hidden()).unwrap(); @@ -1233,7 +1233,7 @@ async fn exact_and_subtree_grants_admit_their_broadcast(version: &'static str) { broadcast.announce().unwrap(); let received = produce_origin(2); - let scope = pattern_set(&[scope]); + let scope = prefixes(&[scope]); let pair = connect(Options { version: Some(version), client_subscribe: Some(received.clone()), @@ -1545,10 +1545,10 @@ async fn a_grant_update_too_large_for_one_message_is_unsupported(version: &'stat let token = added(&pair.client.auth(), "update").await.unwrap(); let (_, updated) = issued.next().await.unwrap(); assert_eq!(granted(&pair.client).await, grant(&["a", "b"], &[])); - let huge: Patterns = (0..20) - .map(|i| Pattern::subtree(&format!("{i}{}", "x".repeat(4000))).unwrap()) + let huge: Prefixes = (0..20) + .map(|i| Prefix::try_from(format!("{i}{}", "x".repeat(4000))).unwrap()) .collect(); - updated.update(grant_of(huge, Patterns::new())); + updated.update(grant_of(huge, Prefixes::new())); let err = token.closed().await; assert!(matches!(err, Error::Unsupported), "{err:?}"); assert_eq!(token.grant().peek(), None); @@ -1559,13 +1559,9 @@ async fn a_grant_update_too_large_for_one_message_is_unsupported(version: &'stat .expect("timed out"); } -fn pattern_set(texts: &[&str]) -> Patterns { - texts.iter().map(|text| Pattern::try_from(*text).unwrap()).collect() -} - -/// Literal, wildcard, and mixed grants reach the presenter exactly as issued, never +/// Single, nested, sibling, and root grants reach the presenter exactly as issued, never /// widened to a covering prefix. -async fn pattern_grants_arrive_exactly(version: &'static str) { +async fn prefix_grants_arrive_exactly(version: &'static str) { within(async { let mut pair = connect(Options { version: Some(version), @@ -1577,11 +1573,11 @@ async fn pattern_grants_arrive_exactly(version: &'static str) { .await; let table = |token: &[u8]| -> Option { let (publish, subscribe) = match token { - b"" => (pattern_set(&["a/**"]), pattern_set(&[])), - b"exact" => (pattern_set(&["room/alice"]), pattern_set(&[])), - b"wildcard" => (pattern_set(&["room/*/cam"]), pattern_set(&["**/demo.hang"])), - b"mixed" => (pattern_set(&["room/**", "lobby", "cam-*.hang"]), pattern_set(&[])), - b"root" => (pattern_set(&[""]), pattern_set(&["**"])), + b"" => (prefixes(&["a"]), prefixes(&[])), + b"single" => (prefixes(&["room/alice"]), prefixes(&[])), + b"nested" => (prefixes(&["room/a/cam", "room/b"]), prefixes(&["demo.hang"])), + b"siblings" => (prefixes(&["room", "lobby", "cam.hang"]), prefixes(&[])), + b"root" => (prefixes(&[""]), prefixes(&[""])), _ => return None, }; Some(grant_of(publish, subscribe)) @@ -1591,7 +1587,7 @@ async fn pattern_grants_arrive_exactly(version: &'static str) { assert_eq!(granted(&pair.client).await, table(b"").unwrap()); let mut held = Vec::new(); - for token in ["exact", "wildcard", "mixed", "root"] { + for token in ["single", "nested", "siblings", "root"] { let added = added(&pair.client.auth(), token).await.expect(token); assert_eq!(added.grant().peek(), table(token.as_bytes()), "{token}"); held.push(added); @@ -1602,13 +1598,13 @@ async fn pattern_grants_arrive_exactly(version: &'static str) { .expect("timed out"); } -/// A wildcard grant admits what it matches, including a leading `**` matching zero -/// segments, and a publish outside it still aborts naming the path. -async fn a_wildcard_grant_is_enforced(version: &'static str) { +/// A prefix grant admits its path and every path beneath it, and a publish that shares +/// only its bytes, not its segments, still aborts naming the path. +async fn a_prefix_grant_is_enforced_by_segment(version: &'static str) { within(async { let publisher = produce_origin(2); let relay = produce_origin(1); - let scope = pattern_set(&["room/*/cam", "**/b.hang"]); + let scope = prefixes(&["room/alice", "b.hang"]); let pair = connect(Options { version: Some(version), client_publish: Some(publisher.clone()), @@ -1619,7 +1615,7 @@ async fn a_wildcard_grant_is_enforced(version: &'static str) { assert_eq!(granted(&pair.client).await.publish, scope); let mut held = Vec::new(); - for path in ["room/alice/cam", "b.hang", "deep/x/b.hang"] { + for path in ["room/alice", "room/alice/cam", "b.hang"] { let broadcast = publisher.route(path, moq_net::origin::RouteOptions::hidden()).unwrap(); broadcast.announce().unwrap(); wait_announced(&relay.consume(), path, true).await; @@ -1628,7 +1624,7 @@ async fn a_wildcard_grant_is_enforced(version: &'static str) { assert_eq!(pair.client_transport.close_reason(), None); let bad = publisher - .route("room/alice/mic", moq_net::origin::RouteOptions::hidden()) + .route("room/alice-mic", moq_net::origin::RouteOptions::hidden()) .unwrap(); bad.announce().unwrap(); assert!(matches!( @@ -1637,7 +1633,7 @@ async fn a_wildcard_grant_is_enforced(version: &'static str) { )); assert_eq!( pair.client_transport.close_reason().map(|(_, reason)| reason), - Some("unauthorized: room/alice/mic".to_string()) + Some("unauthorized: room/alice-mic".to_string()) ); }) .await @@ -1702,7 +1698,7 @@ async fn a_broadcast_received_from_the_peer_is_not_ours_to_check(version: &'stat client_publish: Some(local.clone()), client_subscribe: Some(local.clone()), server_publish: Some(relay.clone()), - server_subscribe: Some(relay.scope("", &patterns(&["local"])).unwrap()), + server_subscribe: Some(relay.scope("", &prefixes(&["local"])).unwrap()), ..Default::default() }) .await; @@ -1814,7 +1810,7 @@ async fn a_narrowing_deafens_one_path(version: &'static str) { let pair = connect(Options { version: Some(version), client_subscribe: Some(received.clone()), - server_publish: Some(relay.scope("", &patterns(&["room"])).unwrap()), + server_publish: Some(relay.scope("", &prefixes(&["room"])).unwrap()), ..Default::default() }) .await; @@ -1871,7 +1867,7 @@ async fn a_narrowing_deafens_one_path(version: &'static str) { let narrowed = wait_for(pair.client.auth().grant(), |grant| { grant .as_ref() - .is_some_and(|grant| grant.subscribe == patterns(&["room/alice/video"])) + .is_some_and(|grant| grant.subscribe == prefixes(&["room/alice/video"])) }) .await; assert_eq!(narrowed, Some(grant(&[], &["room/alice/video"]))); @@ -2047,7 +2043,7 @@ async fn a_widening_brings_back_a_deafened_path(version: &'static str) { let pair = connect(Options { version: Some(version), client_subscribe: Some(received.clone()), - server_publish: Some(relay.scope("", &patterns(&["room"])).unwrap()), + server_publish: Some(relay.scope("", &prefixes(&["room"])).unwrap()), ..Default::default() }) .await; @@ -2091,7 +2087,7 @@ async fn a_widening_brings_back_a_deafened_path(version: &'static str) { let widened = wait_for(pair.client.auth().grant(), |grant| { grant .as_ref() - .is_some_and(|grant| grant.subscribe == patterns(&["room"])) + .is_some_and(|grant| grant.subscribe == prefixes(&["room"])) }) .await; assert_eq!(widened, Some(grant(&[], &["room"]))); @@ -2401,7 +2397,7 @@ async fn a_broadcast_relayed_from_another_client_is_not_ours_to_check(version: & client_publish: Some(local.clone()), client_subscribe: Some(local.clone()), server_publish: Some(relay.clone()), - server_subscribe: Some(relay.scope("", &patterns(&["local"])).unwrap()), + server_subscribe: Some(relay.scope("", &prefixes(&["local"])).unwrap()), ..Default::default() }) .await; @@ -2474,8 +2470,8 @@ async fn lite_07_a_grant_too_wide_to_ask_is_answered_region_by_region() { published.push(route); } let subscribe = |regions: &[String]| { - let subtrees = regions.iter().map(|region| Pattern::subtree(region).unwrap()); - grant_of(Patterns::default(), subtrees.collect()) + let subtrees = regions.iter().map(|region| Prefix::try_from(region.as_str()).unwrap()); + grant_of(Prefixes::default(), subtrees.collect()) }; let client = produce_origin(9); let mut pair = connect(Options { @@ -2506,7 +2502,7 @@ async fn lite_07_a_grant_too_wide_to_ask_is_answered_region_by_region() { .await; let mut everything = client.consume().routes(); - let mut first = (client.consume().scope(®ions[0], &Patterns::from(Pattern::all()))) + let mut first = (client.consume().scope(®ions[0], &Prefixes::all())) .unwrap() .routes(); kio::clock::sleep(Duration::from_secs(1)).await; diff --git a/rs/moq-net/tests/integration/lazy_discovery.rs b/rs/moq-net/tests/integration/lazy_discovery.rs index 907acc38113..7ad1a8c0909 100644 --- a/rs/moq-net/tests/integration/lazy_discovery.rs +++ b/rs/moq-net/tests/integration/lazy_discovery.rs @@ -7,7 +7,7 @@ use std::time::Duration; -use moq_net::{Error, Pattern, Patterns, Version, origin}; +use moq_net::{Error, Pattern, Prefixes, Version, origin}; use crate::support; use support::harness::{MockConnectOptions, MockPair, connect_mock}; @@ -121,7 +121,7 @@ impl Lazy { fn watch(&self, prefix: &str) -> origin::Routes { self.subscriber .consume() - .scope(prefix, &Patterns::from(Pattern::all())) + .scope(prefix, &Prefixes::all()) .unwrap() .routes() } @@ -131,7 +131,7 @@ impl Lazy { let consumer = self .subscriber .consume() - .scope("", &Patterns::from(Pattern::literal(path).unwrap())) + .filter(&Pattern::literal(path).unwrap()) .unwrap(); let mut announced = consumer.routes(); matches!( @@ -198,11 +198,7 @@ async fn asks_only_about_the_watched_prefix() { #[kio::test] async fn a_new_session_asks_about_existing_watches() { let subscriber = produce_origin(2); - let mut watch = subscriber - .consume() - .scope("b", &Patterns::from(Pattern::all())) - .unwrap() - .routes(); + let mut watch = subscriber.consume().scope("b", &Prefixes::all()).unwrap().routes(); let lazy = Lazy::with("moq-lite-07-wip", subscriber).await; assert_eq!(synced(&mut watch).await, ["y"]); assert_eq!(lazy.requests(), 1); diff --git a/rs/moq-net/tests/integration/mount.rs b/rs/moq-net/tests/integration/mount.rs index 97ca30a3272..f760548a8a1 100644 --- a/rs/moq-net/tests/integration/mount.rs +++ b/rs/moq-net/tests/integration/mount.rs @@ -10,7 +10,7 @@ use support::AtStart; use std::time::Duration; -use moq_net::{Pattern, Patterns, Version, origin, track}; +use moq_net::{Prefixes, Version, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; const TEST_TIMEOUT: Duration = Duration::from_secs(600); @@ -50,7 +50,7 @@ fn tenant(origin: &origin::Producer, root: &str) -> origin::Consumer { origin .mount("tenant-b/shared", "tenant-a/public") .unwrap() - .scope(root, &Patterns::from(Pattern::all())) + .scope(root, &Prefixes::all()) .unwrap() .with_proxy(true) .consume() @@ -247,7 +247,7 @@ async fn a_claim_above_the_target_outranks_a_claim_above_the_mount_point() { let handle = relay .mount("tenant-b/x/shared", "tenant-a/public") .unwrap() - .scope("", &Patterns::from(Pattern::all())) + .scope("", &Prefixes::all()) .unwrap() .with_proxy(true) .consume(); diff --git a/rs/moq-net/tests/integration/nested_claim.rs b/rs/moq-net/tests/integration/nested_claim.rs index f49b1ce74ae..f2c31079c7b 100644 --- a/rs/moq-net/tests/integration/nested_claim.rs +++ b/rs/moq-net/tests/integration/nested_claim.rs @@ -16,7 +16,7 @@ use support::AtStart; use std::time::Duration; -use moq_net::{Pattern, Patterns, Version, origin, track}; +use moq_net::{Prefix, Prefixes, Version, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; fn produce_origin(hop: u64) -> origin::Producer { @@ -40,7 +40,7 @@ async fn link(from: &origin::Producer, to: &origin::Producer) -> MockPair { /// Have `to` pull only what `from` publishes at or below `prefix`, as a viewer following /// that one path asks. async fn follow(from: &origin::Producer, to: &origin::Producer, prefix: &str) -> MockPair { - let interest = Patterns::from(Pattern::subtree(prefix).unwrap()); + let interest = Prefixes::from(Prefix::try_from(prefix).unwrap()); let mut options = MockConnectOptions::new(lite07()); options.server_publish = Some(from.consume()); options.client_subscribe = Some(to.scope("", &interest).unwrap()); @@ -121,12 +121,7 @@ async fn a_claim_above_a_rooted_export_keeps_the_session() { let _published = publish(&source, "", "root"); let mut options = MockConnectOptions::new(lite07()); - options.server_publish = Some( - source - .consume() - .scope("tenant", &Patterns::from(Pattern::all())) - .unwrap(), - ); + options.server_publish = Some(source.consume().scope("tenant", &Prefixes::all()).unwrap()); options.client_subscribe = Some(relay.clone()); let pair = connect_mock(options).await; settle().await; diff --git a/rs/moq-net/tests/integration/path_routes.rs b/rs/moq-net/tests/integration/path_routes.rs index 085d8377c84..335369fdb52 100644 --- a/rs/moq-net/tests/integration/path_routes.rs +++ b/rs/moq-net/tests/integration/path_routes.rs @@ -14,7 +14,7 @@ use support::{AppendNext, AtStart}; use std::time::Duration; use futures::channel::mpsc; -use moq_net::{Error, Pattern, Patterns, Version, auth::Grant, origin}; +use moq_net::{Error, Prefix, Prefixes, Version, auth::Grant, origin}; use support::harness::{MockConnectOptions, MockPair, connect_mock, peer}; /// How long a relay lets an announce stream stall before closing its session. @@ -364,7 +364,7 @@ async fn a_scoped_peers_dead_leaves_other_sessions_routes_alone() { options.server_subscribe = Some(w.clone()); let s_w = connect_mock(options).await; let mut grant = Grant::default(); - grant.publish = [Pattern::subtree("a").unwrap()].into_iter().collect::(); + grant.publish = Prefixes::from(Prefix::try_from("a").unwrap()); s_w.server.auth().authorize(&grant); quiesce().await; diff --git a/rs/moq-net/tests/integration/route_cluster.rs b/rs/moq-net/tests/integration/route_cluster.rs index 7b6d3aa6811..ce5a8e4bbfe 100644 --- a/rs/moq-net/tests/integration/route_cluster.rs +++ b/rs/moq-net/tests/integration/route_cluster.rs @@ -19,7 +19,7 @@ use std::time::Duration; use futures::{StreamExt, channel::mpsc}; use moq_net::stats::{Message, Registry, SessionInfo, Tier}; -use moq_net::{Error, Instance, Pattern, Patterns, Timestamp, auth, origin, track}; +use moq_net::{Error, Instance, Prefix, Prefixes, Timestamp, auth, origin, track}; use support::Kind; use support::harness::{MockConnectOptions, MockPair, connect_mock, now, spawn}; @@ -765,16 +765,12 @@ async fn tokened_uplink(dialer: &origin::Producer, acceptor: &origin::Producer) (pair, requests) } -/// A grant of the `publish` subtrees, or of everything for `**`, subscribing to -/// everything and lapsing at `expires`. +/// A grant of the `publish` prefixes, subscribing to everything and lapsing at `expires`. fn grant(publish: &[&str], expires: Option) -> auth::Grant { let mut grant = auth::Grant::all(); grant.publish = (publish.iter()) - .map(|prefix| match *prefix { - "**" => Pattern::all(), - prefix => Pattern::subtree(prefix).unwrap(), - }) - .collect::(); + .map(|prefix| Prefix::try_from(*prefix).unwrap()) + .collect::(); grant.expires = expires; grant } @@ -810,7 +806,7 @@ async fn a_grant_union_crosses_the_scoped_boundary_each_way() { while let Some(request) = requests.next().await { match request.token().as_ref() { b"wide" => { - let issued = request.accept(grant(&["**"], Some(wide_expires))); + let issued = request.accept(grant(&[""], Some(wide_expires))); // Revoking a token's grant once it expires is the acceptor's job. kio::clock::sim::spawn(async move { kio::clock::sleep_until(wide_expires).await; @@ -934,9 +930,7 @@ struct Listing { impl Listing { async fn new(node: &origin::Producer, prefix: &str) -> Self { - let routes = (node.consume().scope(prefix, &Patterns::from(Pattern::all()))) - .unwrap() - .routes(); + let routes = (node.consume().scope(prefix, &Prefixes::all())).unwrap().routes(); let listing = Self { routes }; // The request, its answer, and the initial set. kio::clock::sleep(DEBOUNCE).await; @@ -1039,7 +1033,7 @@ async fn an_origin_leaving_the_interest_ends_its_route_by_its_floor() { let l = Origin::new(2, &["live/a", "vod/l"], &relay).await; if scoped { let mut grant = auth::Grant::default(); - grant.publish = [Pattern::subtree("live/a").unwrap(), Pattern::subtree("vod").unwrap()] + grant.publish = [Prefix::try_from("live/a").unwrap(), Prefix::try_from("vod").unwrap()] .into_iter() .collect(); l.link.server.auth().authorize(&grant); diff --git a/rs/moq-net/tests/integration/route_failover.rs b/rs/moq-net/tests/integration/route_failover.rs index 4665dbcdf78..efb28c069b0 100644 --- a/rs/moq-net/tests/integration/route_failover.rs +++ b/rs/moq-net/tests/integration/route_failover.rs @@ -21,7 +21,7 @@ use std::collections::BTreeMap; use std::time::Duration; use futures::channel::mpsc; -use moq_net::{Error, Pattern, Patterns, Timestamp, group, origin, stats, track}; +use moq_net::{Error, Prefixes, Timestamp, group, origin, stats, track}; use support::Kind; use support::harness::{MockConnectOptions, MockPair, connect_mock, peer_with_latency}; @@ -470,11 +470,7 @@ async fn a_narrower_next_hop_fails_over_to_a_wider_route() { let mut options = links.options("p-c", &p, &c); options.client_discovers_all = false; let _p_c = connect_mock(options).await; - let _c_reads_other = c - .consume() - .scope("other", &Patterns::from(Pattern::all())) - .unwrap() - .routes(); + let _c_reads_other = c.consume().scope("other", &Prefixes::all()).unwrap().routes(); kio::clock::sleep(Duration::from_secs(1)).await; let mut watched = Vec::new(); diff --git a/rs/moq-net/tests/integration/route_race.rs b/rs/moq-net/tests/integration/route_race.rs index 30815d9dff0..d6b362d860e 100644 --- a/rs/moq-net/tests/integration/route_race.rs +++ b/rs/moq-net/tests/integration/route_race.rs @@ -37,7 +37,7 @@ use std::rc::Rc; use std::time::Duration; use moq_net::{ - Client, Error, Pattern, Patterns, Server, Version, + Client, Error, Prefix, Prefixes, Server, Version, auth::Grant, origin::{self, RouteKind}, track, @@ -208,7 +208,7 @@ fn publish(origin: &origin::Producer, path: &str) -> Live { fn grant(subscribe: &str) -> Grant { let mut grant = Grant::default(); - grant.subscribe = [Pattern::subtree(subscribe).unwrap()].into_iter().collect::(); + grant.subscribe = Prefixes::from(Prefix::try_from(subscribe).unwrap()); grant } diff --git a/rs/moq-pattern/benches/matching.rs b/rs/moq-pattern/benches/matching.rs index 5677acf620d..5f4d3c6bffc 100644 --- a/rs/moq-pattern/benches/matching.rs +++ b/rs/moq-pattern/benches/matching.rs @@ -1,6 +1,6 @@ //! Matching a pattern union, swept over union size and path depth. use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; -use moq_pattern::{Pattern, Patterns}; +use moq_pattern::{Pattern, Patterns, Prefix, Prefixes}; fn matching(c: &mut Criterion) { let mut group = c.benchmark_group("pattern-matching"); @@ -142,9 +142,52 @@ fn algebra(c: &mut Criterion) { }); } } + // The grant `AUTH_OK` carries: prefixes reduce in one sort and one sweep on any shape. + for bytes in [4096, 16384, MAX_MESSAGE] { + let (message, previous) = prefix_grant(bytes); + let size = wire(&message); + assert!( + size <= bytes && size * 16 > bytes * 15, + "prefix fills {size} of {bytes} bytes" + ); + let message: Vec = message.iter().map(|text| text.parse().unwrap()).collect(); + let reduced: Prefixes = message.iter().cloned().collect(); + assert_eq!(reduced.len(), message.len(), "prefix members are disjoint"); + assert!(previous.covers(&reduced)); + group.throughput(Throughput::Bytes(size as u64)); + group.bench_function(BenchmarkId::new("collect-prefix", bytes), |b| { + b.iter(|| std::hint::black_box(message.iter().cloned().collect::().len())); + }); + group.bench_function(BenchmarkId::new("covers-prefix", bytes), |b| { + b.iter(|| std::hint::black_box(previous.covers(&reduced))); + }); + group.bench_function(BenchmarkId::new("intersect-prefix", bytes), |b| { + b.iter(|| std::hint::black_box(previous.intersect(&reduced).len())); + }); + } group.finish(); } +/// Distinct prefix members two segments beneath a shared parent, filling at most `bytes` +/// encoded and given in reverse order so collecting sorts them, with a previous grant of +/// their parents. Mirrors the prefix rows of `js/pattern/bench/algebra.ts`. +fn prefix_grant(bytes: usize) -> (Vec, Prefixes) { + let (mut texts, mut parents) = (Vec::new(), Vec::new()); + let mut size = 0; + for index in 0.. { + let parent = format!("room/{index:05}"); + let text = format!("{parent}/cam"); + size += varint(text.len()) + text.len(); + if varint(texts.len() + 1) + size > bytes { + break; + } + texts.push(text); + parents.push(parent.parse::().unwrap()); + } + texts.reverse(); + (texts, parents.into_iter().collect()) +} + /// The largest moq-lite control message, and so the largest `AUTH_OK`. const MAX_MESSAGE: usize = u16::MAX as usize; @@ -162,7 +205,8 @@ fn wire(texts: &[String]) -> usize { varint(texts.len()) + texts.iter().map(|text| varint(text.len()) + text.len()).sum::() } -/// A peer grant shaped against the union index, filling one message. +/// A pattern union shaped against the union index, filling one message. No peer sends a +/// pattern union since `AUTH_OK` carries prefixes, so these measure local discovery filters. /// /// Containment between patterns with wildcards reduces to Orthogonal Vectors, so some shape /// costs more than linear time against any index; these are the two worst known against diff --git a/rs/moq-pattern/src/lib.rs b/rs/moq-pattern/src/lib.rs index 9736ddf22d6..b442275c793 100644 --- a/rs/moq-pattern/src/lib.rs +++ b/rs/moq-pattern/src/lib.rs @@ -1,8 +1,13 @@ -//! Exact path patterns for Media over QUIC. +//! Path patterns and prefix grants for Media over QUIC. //! -//! A [`Pattern`] describes a set of broadcast paths. [`Patterns`] is an unordered union -//! reduced by exact containment. Matching is linear. [`Pattern::literal`] rejects `*` -//! because it is reserved for pattern syntax. +//! A [`Pattern`] describes a set of broadcast paths, for discovery filters that pick which +//! announcements a reader looks at. [`Patterns`] is an unordered union reduced by exact +//! containment. Matching is linear. [`Pattern::literal`] rejects `*` because it is +//! reserved for pattern syntax. +//! +//! A [`Prefix`] is a grant member: a path covering itself and every path beneath it, the +//! empty prefix covering everything. [`Prefixes`] is a grant, a reduced set of them. A +//! grant takes no wildcard: parsing one fails with [`InvalidPrefix::Wildcard`]. //! //! `moq-net` and `moq-auth` re-export this crate. The TypeScript twin is `@moq/pattern`. //! @@ -74,7 +79,9 @@ mod index; mod parts; mod pattern; mod patterns; +mod prefix; pub use parts::{Parts, escape, parts, unescape}; pub use pattern::{IntersectionError, InvalidPattern, Pattern, Segment, Specificity}; pub use patterns::Patterns; +pub use prefix::{InvalidPrefix, Prefix, Prefixes}; diff --git a/rs/moq-pattern/src/pattern.rs b/rs/moq-pattern/src/pattern.rs index a8ed3dc5e6f..fdd22067b88 100644 --- a/rs/moq-pattern/src/pattern.rs +++ b/rs/moq-pattern/src/pattern.rs @@ -479,8 +479,8 @@ impl Pattern { /// Whether every path `other` matches, this pattern matches too. /// - /// This is the authorization check: a grant contains a request when the request - /// cannot name a path outside it. A pattern contains itself. + /// A filter contains another when the other cannot name a path outside it. A pattern + /// contains itself. pub fn contains(&self, other: &Self) -> bool { match (self.globstar, other.globstar) { (None, None) => { @@ -575,7 +575,7 @@ impl Pattern { /// The patterns that, relative to `root`, match exactly the paths this pattern /// matches beneath `root`. /// - /// This is how a grant or an advertisement is presented inside a rooted view. It is + /// This is how a filter or an advertisement is presented inside a rooted view. It is /// a set because `**` may consume the root or stop short of it: `**/a` rebased at `a` /// is both the empty pattern (the root itself) and `**/a` (deeper paths ending in /// `a`). Empty when nothing under `root` matches. The root is normalized like a @@ -625,8 +625,8 @@ impl Pattern { /// The patterns matching exactly the paths both patterns match. /// - /// This is how a claim is clamped to a scope: the covered paths inside the - /// grant, as patterns of their own. It is a set because two partial segments or + /// This is how a filter is clamped to a scope: the paths in both, as patterns of + /// their own. It is a set because two partial segments or /// two `**` runs can meet in more than one way: `ab*` and `*b` meet at `ab*b` and /// at `ab`, and `a/**` and `**/a` meet at `a/**/a` and at `a`. Empty when the /// two do not [overlap](Self::overlaps). diff --git a/rs/moq-pattern/src/patterns.rs b/rs/moq-pattern/src/patterns.rs index af5bb0bac1a..3268b2b3a3a 100644 --- a/rs/moq-pattern/src/patterns.rs +++ b/rs/moq-pattern/src/patterns.rs @@ -2,14 +2,14 @@ use super::{IntersectionError, InvalidPattern, Pattern, index::Index}; /// A union of patterns, reduced so no member is contained by another. /// -/// This is the shape of a grant (the paths a token may publish) and of a rebased -/// pattern (see [`Pattern::rebase`]). Order is canonical, so two unions describing the -/// same reduced set compare equal. +/// This is the shape of a discovery filter and of a rebased pattern (see +/// [`Pattern::rebase`]). Order is canonical, so two unions describing the same reduced set +/// compare equal. /// /// Containment is per member: [`contains`](Self::contains) holds when one pattern in the /// union contains the candidate. A candidate covered only jointly by several members /// (`a/**` against `a`, `a/*`, and `a/*/**`) is refused, which keeps the check linear -/// and its answer easy to predict. A grant that means a subtree writes `a/**`. +/// and its answer easy to predict. A filter that means a subtree writes `a/**`. /// /// Operations over a whole union (collecting, extending, [`covers`](Self::covers), and /// [`intersect`](Self::intersect)) index one side by segment first, so their cost diff --git a/rs/moq-pattern/src/prefix.rs b/rs/moq-pattern/src/prefix.rs new file mode 100644 index 00000000000..fdad926d546 --- /dev/null +++ b/rs/moq-pattern/src/prefix.rs @@ -0,0 +1,660 @@ +use std::cmp::Ordering; +use std::fmt; +use std::str::FromStr; + +use crate::parts::{is_segment, parts}; +use crate::{InvalidPattern, Pattern, Patterns}; + +/// Why a string is not a valid [`Prefix`]. +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum InvalidPrefix { + /// The text holds a `*`. A grant is a set of prefixes, each covering its whole subtree, + /// so it takes no wildcard: `foo` already covers what `foo/**` meant. + Wildcard(String), + /// The text is not a path: an empty segment, a malformed escape, or too many segments. + Path(InvalidPattern), +} + +impl fmt::Display for InvalidPrefix { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Wildcard(text) if text == "**" => { + write!( + f, + "`**` is a wildcard: grants are prefixes, and the empty prefix covers every path" + ) + } + Self::Wildcard(text) => match text + .strip_suffix("/**") + .filter(|head| !head.is_empty() && !head.contains('*')) + { + Some(head) => write!( + f, + "`{text}` is a wildcard: grants are prefixes, and `{head}` covers `{head}` and every path beneath it" + ), + None => write!( + f, + "`{text}` holds a wildcard: grants are prefixes, each covering one path and every path beneath it" + ), + }, + Self::Path(err) => write!(f, "invalid prefix: {err}"), + } + } +} + +impl std::error::Error for InvalidPrefix {} + +impl From for InvalidPrefix { + fn from(err: InvalidPattern) -> Self { + Self::Path(err) + } +} + +/// One member of a grant: a path, covering itself and every path beneath it. +/// +/// The empty prefix covers every path. A prefix is spelled like a path: `/`-separated +/// segments, a `\` escaping the `\` or `/` after it, at most [`Pattern::MAX_SEGMENTS`] +/// segments, and no leading, trailing, or doubled `/`. It never holds a `*`: parsing one +/// fails loudly with [`InvalidPrefix::Wildcard`], since a grant never names a pattern. +/// +/// Ordered segment by segment, so a prefix sorts right before every path beneath it. +#[derive(Clone, Default, PartialEq, Eq, Hash)] +pub struct Prefix { + text: String, +} + +impl Prefix { + /// The empty prefix, covering every path. + pub fn all() -> Self { + Self::default() + } + + /// The canonical text. + pub fn as_str(&self) -> &str { + &self.text + } + + /// Whether this is the empty prefix, covering every path. + pub fn is_empty(&self) -> bool { + self.text.is_empty() + } + + /// Whether `path`, spelled as a broadcast path, is this prefix or beneath it. + pub fn matches(&self, path: &str) -> bool { + beneath(path, &self.text) + } + + /// Whether this prefix covers some path at or beneath the canonical `path`: one lies + /// beneath the other. + pub fn overlaps(&self, path: &str) -> bool { + beneath(path, &self.text) || beneath(&self.text, path) + } + + /// This prefix placed beneath the path `root`, normalized like a broadcast path (slashes + /// trimmed and collapsed). Fails when `root` holds a wildcard or a malformed escape, or + /// the result has too many segments. + pub fn rooted(&self, root: &str) -> Result { + let root: Prefix = normalize(root).parse()?; + match (root.is_empty(), self.is_empty()) { + (true, _) => Ok(self.clone()), + (false, true) => Ok(root), + (false, false) => { + if parts(&root.text).count() + parts(&self.text).count() > Pattern::MAX_SEGMENTS { + return Err(InvalidPattern::TooManySegments.into()); + } + Ok(Self { + text: format!("{}/{}", root.text, self.text), + }) + } + } + } + + /// This prefix as seen from the path `root`, normalized like a broadcast path: the rest + /// beneath it, or the empty prefix when this one covers `root` already. `None` when they + /// share no path. + pub fn rebase(&self, root: &str) -> Option { + // Segment by segment, so a root ending inside an escape matches no segment. + let mut root = parts(root).filter(|part| !part.is_empty()); + let mut mine = parts(&self.text); + let mut end = 0; + loop { + match (root.next(), mine.next()) { + // This prefix ends at or above the root, so it covers all of it. + (_, None) => return Some(Self::all()), + // The root ends above this prefix: the rest of it is what lies beneath. + (None, Some(_)) => { + return Some(Self { + text: self.text[end..].to_string(), + }); + } + (Some(theirs), Some(ours)) if theirs == ours => end += ours.len() + 1, + (Some(_), Some(_)) => return None, + } + } + } + + /// The paths this prefix covers, as a pattern: `prefix/**`, or the literal when it is + /// already as deep as a path may be. + pub fn pattern(&self) -> Pattern { + Pattern::subtree(&self.text).expect("a prefix is a valid literal") + } +} + +/// Whether the canonical path `path` is `prefix` or beneath it. +/// +/// A valid prefix never ends inside an escape, so the byte after it is a separator exactly +/// when it is a `/`. +fn beneath(path: &str, prefix: &str) -> bool { + prefix.is_empty() + || (path.starts_with(prefix) && (path.len() == prefix.len() || path.as_bytes()[prefix.len()] == b'/')) +} + +/// `path` with its slashes trimmed and collapsed, as a broadcast path is normalized. +fn normalize(path: &str) -> std::borrow::Cow<'_, str> { + if parts(path).all(|part| !part.is_empty()) { + return path.into(); + } + parts(path) + .filter(|part| !part.is_empty()) + .collect::>() + .join("/") + .into() +} + +/// Segment-by-segment order of two canonical paths. +fn order(a: &str, b: &str) -> Ordering { + parts(a).cmp(parts(b)) +} + +impl Ord for Prefix { + fn cmp(&self, other: &Self) -> Ordering { + order(&self.text, &other.text) + } +} + +impl PartialOrd for Prefix { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl FromStr for Prefix { + type Err = InvalidPrefix; + + /// Parse a prefix's text. A `*` anywhere is refused before anything else, so a pattern + /// written where a prefix belongs always says so. Slashes are not normalized: a + /// leading, trailing, or doubled `/` is an error, so a typo cannot silently widen a grant. + fn from_str(text: &str) -> Result { + if text.contains('*') { + return Err(InvalidPrefix::Wildcard(text.to_string())); + } + // One past the limit is enough to refuse, without splitting a peer's megabytes of + // `a/a/...` first. + let mut count = 0; + for part in parts(text).take(Pattern::MAX_SEGMENTS + 1) { + count += 1; + if part.is_empty() { + return Err(InvalidPattern::EmptySegment.into()); + } + if !is_segment(part) { + return Err(InvalidPattern::InvalidSegment(part.to_string()).into()); + } + } + if count > Pattern::MAX_SEGMENTS { + return Err(InvalidPattern::TooManySegments.into()); + } + Ok(Self { text: text.to_string() }) + } +} + +impl TryFrom<&str> for Prefix { + type Error = InvalidPrefix; + + fn try_from(text: &str) -> Result { + text.parse() + } +} + +impl TryFrom for Prefix { + type Error = InvalidPrefix; + + fn try_from(text: String) -> Result { + text.parse() + } +} + +impl fmt::Display for Prefix { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.text) + } +} + +impl fmt::Debug for Prefix { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.text, f) + } +} + +#[cfg(feature = "serde")] +impl serde::Serialize for Prefix { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(&self.text) + } +} + +#[cfg(feature = "serde")] +impl<'de> serde::Deserialize<'de> for Prefix { + fn deserialize>(deserializer: D) -> Result { + let text = >::deserialize(deserializer)?; + text.parse().map_err(serde::de::Error::custom) + } +} + +/// A grant: a set of prefixes, reduced so no member lies beneath another. +/// +/// Order is canonical (see [`Prefix`]), so two sets covering the same paths compare equal. +/// The empty set grants nothing; [`all`](Self::all) grants everything. +/// +/// Every operation costs near-linear time in the members it reads: collecting sorts once +/// and sweeps, and a query binary-searches each prefix above the path it asks about, of +/// which there are at most [`Pattern::MAX_SEGMENTS`] + 1. So a peer's grant costs +/// O(n log n) to read, whatever its shape. +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +pub struct Prefixes(Vec); + +impl Prefixes { + /// The empty set, which grants nothing. + pub fn new() -> Self { + Self::default() + } + + /// The set of the empty prefix, which grants every path. + pub fn all() -> Self { + Self(vec![Prefix::all()]) + } + + /// Add a prefix, dropping the members beneath it. Returns `false` when a member already + /// covers it, leaving the set unchanged. + pub fn insert(&mut self, prefix: Prefix) -> bool { + if self.matches(&prefix.text) { + return false; + } + // Everything beneath the new member sorts right after where it goes. + let at = self.0.partition_point(|member| member < &prefix); + let end = at + + self.0[at..] + .iter() + .take_while(|member| prefix.matches(&member.text)) + .count(); + self.0.splice(at..end, [prefix]); + true + } + + /// Whether some member covers the canonical `path`. + pub fn matches(&self, path: &str) -> bool { + self.covering(path).is_some() + } + + /// The member covering the canonical `path`, if any. + fn covering(&self, path: &str) -> Option<&Prefix> { + if self.0.is_empty() { + return None; + } + // Each prefix of the path at a segment boundary, the empty one first. + let mut end = 0; + let mut ancestors = std::iter::once(0).chain(parts(path).map(move |part| { + end += usize::from(end > 0) + part.len(); + end + })); + ancestors.find_map(|end| { + let ancestor = &path[..end]; + let index = self.0.binary_search_by(|member| order(&member.text, ancestor)).ok()?; + Some(&self.0[index]) + }) + } + + /// Whether every member of `other` is covered here: `other` grants nothing this set + /// does not. The empty set is covered by anything. + pub fn covers(&self, other: &Self) -> bool { + other.0.iter().all(|prefix| self.matches(&prefix.text)) + } + + /// Whether some member covers a path at or beneath the canonical `path`. + pub fn overlaps(&self, path: &str) -> bool { + if self.matches(path) { + return true; + } + // Members beneath it sort right after it. + let at = self + .0 + .partition_point(|member| order(&member.text, path) == Ordering::Less); + self.0.get(at).is_some_and(|member| beneath(&member.text, path)) + } + + /// The paths both sets grant. Each member of the result is a member of one side lying + /// within the other, so it never grows past both sets together. + pub fn intersect(&self, other: &Self) -> Self { + let ours = self.0.iter().filter(|prefix| other.matches(&prefix.text)); + let theirs = other.0.iter().filter(|prefix| self.matches(&prefix.text)); + ours.chain(theirs).cloned().collect() + } + + /// Every member as seen from the path `root`; see [`Prefix::rebase`]. + pub fn rebase(&self, root: &str) -> Self { + self.0.iter().filter_map(|prefix| prefix.rebase(root)).collect() + } + + /// Every member placed beneath the path `root`; see [`Prefix::rooted`]. + pub fn rooted(&self, root: &str) -> Result { + self.0.iter().map(|prefix| prefix.rooted(root)).collect() + } + + /// The paths this set covers, as patterns: each member's [`Prefix::pattern`]. + pub fn patterns(&self) -> Patterns { + self.0.iter().map(Prefix::pattern).collect() + } + + /// The members, in canonical order. + pub fn iter(&self) -> std::slice::Iter<'_, Prefix> { + self.0.iter() + } + + /// The number of members. + pub fn len(&self) -> usize { + self.0.len() + } + + /// Whether there are no members, so nothing is granted. + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl From for Prefixes { + fn from(prefix: Prefix) -> Self { + Self(vec![prefix]) + } +} + +impl FromIterator for Prefixes { + /// Reduce in one sort and one sweep: a member's subtree sorts right after it, so each + /// prefix is beneath a member kept already only if it is beneath the last one kept. + fn from_iter>(iter: I) -> Self { + let mut sorted: Vec = iter.into_iter().collect(); + sorted.sort_unstable(); + let mut kept: Vec = Vec::with_capacity(sorted.len()); + for prefix in sorted { + if kept.last().is_none_or(|last| !last.matches(&prefix.text)) { + kept.push(prefix); + } + } + kept.shrink_to_fit(); + Self(kept) + } +} + +impl Extend for Prefixes { + /// Reduce the members and the new prefixes together, in one pass. + fn extend>(&mut self, iter: I) { + let members = std::mem::take(&mut self.0); + *self = members.into_iter().chain(iter).collect(); + } +} + +impl IntoIterator for Prefixes { + type Item = Prefix; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl<'a> IntoIterator for &'a Prefixes { + type Item = &'a Prefix; + type IntoIter = std::slice::Iter<'a, Prefix>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +#[cfg(feature = "serde")] +impl serde::Serialize for Prefixes { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_seq(self.iter()) + } +} + +#[cfg(feature = "serde")] +impl<'de> serde::Deserialize<'de> for Prefixes { + /// Reads a list and reduces it, so a persisted grant is canonical after a round trip. + fn deserialize>(deserializer: D) -> Result { + Ok(Vec::::deserialize(deserializer)?.into_iter().collect()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn prefix(text: &str) -> Prefix { + text.parse().unwrap() + } + + fn prefixes(texts: &[&str]) -> Prefixes { + texts.iter().map(|text| prefix(text)).collect() + } + + #[test] + fn refuses_every_wildcard_loudly() { + for text in ["**", "*", "a/**", "a/*", "cam-*.hang", "a*", "*/chat", "**/a", r"a\*"] { + let err = text.parse::().unwrap_err(); + assert_eq!(err, InvalidPrefix::Wildcard(text.to_string()), "{text}"); + } + assert!( + InvalidPrefix::Wildcard("**".into()) + .to_string() + .contains("empty prefix") + ); + assert!( + InvalidPrefix::Wildcard("anon/**".into()) + .to_string() + .contains("`anon` covers") + ); + assert!( + InvalidPrefix::Wildcard("a/*/b".into()) + .to_string() + .contains("holds a wildcard") + ); + } + + #[test] + fn refuses_what_is_not_a_path() { + for text in ["/a", "a/", "a//b", r"a\", r"a\b"] { + assert!(matches!(text.parse::(), Err(InvalidPrefix::Path(_))), "{text}"); + } + let deep = ["a"; Pattern::MAX_SEGMENTS + 1].join("/"); + assert!(deep.parse::().is_err()); + assert!(["a"; Pattern::MAX_SEGMENTS].join("/").parse::().is_ok()); + assert!(prefix("").is_empty()); + assert_eq!(prefix(r"a\/b/c\\").as_str(), r"a\/b/c\\"); + } + + #[test] + fn covers_its_subtree_by_segment() { + let room = prefix("room"); + assert!(room.matches("room")); + assert!(room.matches("room/alice")); + assert!(!room.matches("roomy")); + assert!(!room.matches("room-b/x")); + assert!(!room.matches("")); + assert!(Prefix::all().matches("")); + assert!(Prefix::all().matches("anything/at/all")); + // An escaped `/` never splits a segment. + assert!(!prefix("a").matches(r"a\/b")); + assert!(prefix(r"a\/b").matches(r"a\/b/c")); + assert!(prefix(r"a\\").matches(r"a\\/b")); + assert!(!prefix(r"a\/").matches(r"a\/b")); + assert!(prefix("room/a").overlaps("room")); + assert!(room.overlaps("room/a")); + assert!(!prefix("a").overlaps("b")); + // A path no prefix can spell still has an answer. + assert!(room.overlaps("room/cam-*")); + } + + #[test] + fn sorts_a_subtree_right_after_its_prefix() { + let mut sorted = [prefix("a/b"), prefix("a-b"), prefix("a"), prefix("a/b/c"), prefix("")]; + sorted.sort(); + let texts: Vec<_> = sorted.iter().map(Prefix::as_str).collect(); + assert_eq!(texts, ["", "a", "a/b", "a/b/c", "a-b"]); + } + + #[test] + fn collecting_reduces_and_sorts() { + assert_eq!(prefixes(&["a/b", "a-b", "a", "c/d"]), prefixes(&["c/d", "a-b", "a"])); + assert_eq!(prefixes(&["a/b", "", "c"]), Prefixes::all()); + assert_eq!(prefixes(&["b", "a", "a"]).len(), 2); + let set = prefixes(&["b", "a/x", "a-b"]); + let texts: Vec<_> = set.iter().map(Prefix::as_str).collect(); + assert_eq!(texts, ["a/x", "a-b", "b"]); + } + + #[test] + fn insert_drops_what_it_covers() { + let mut set = prefixes(&["a/b", "a/c", "a-b", "b"]); + assert!(!set.insert(prefix("a/b/x"))); + assert!(set.insert(prefix("a"))); + assert_eq!(set, prefixes(&["a", "a-b", "b"])); + assert!(set.insert(prefix(""))); + assert_eq!(set, Prefixes::all()); + } + + #[test] + fn queries_any_member() { + let set = prefixes(&["a/b", "c"]); + assert!(set.matches("a/b/x")); + assert!(set.matches("c")); + assert!(!set.matches("a")); + assert!(!set.matches("a/bc")); + assert!(set.matches("c/d")); + assert!(set.overlaps("a")); + assert!(set.overlaps("a/b/c")); + assert!(set.overlaps("")); + assert!(!set.overlaps("a/c")); + assert!(!set.overlaps("b")); + assert!(Prefixes::all().overlaps("z")); + assert!(!Prefixes::new().matches("")); + assert!(!Prefixes::new().overlaps("")); + } + + #[test] + fn covers_and_intersects() { + let wide = prefixes(&["a", "b/c"]); + assert!(wide.covers(&prefixes(&["a/x", "b/c/d"]))); + assert!(!wide.covers(&prefixes(&["a/x", "b"]))); + assert!(wide.covers(&Prefixes::new())); + assert!(!Prefixes::new().covers(&wide)); + assert_eq!(wide.intersect(&prefixes(&["a/x", "b", "z"])), prefixes(&["a/x", "b/c"])); + assert_eq!(wide.intersect(&Prefixes::all()), wide); + assert!(wide.intersect(&prefixes(&["z"])).is_empty()); + } + + #[test] + fn rebases_and_roots() { + let set = prefixes(&["a/b", "c"]); + assert_eq!(set.rebase("a"), prefixes(&["b"])); + assert_eq!(set.rebase("c/d"), Prefixes::all()); + assert_eq!(set.rebase(""), set); + assert!(set.rebase("x").is_empty()); + assert_eq!(set.rooted("r").unwrap(), prefixes(&["r/a/b", "r/c"])); + assert_eq!(Prefixes::all().rooted("r").unwrap(), prefixes(&["r"])); + assert!(set.rooted("*").is_err()); + let deep = ["a"; Pattern::MAX_SEGMENTS].join("/"); + assert!(set.rooted(&deep).is_err()); + assert_eq!(set.rooted("r").unwrap().rebase("r"), set); + // A root is normalized like a broadcast path, as a pattern's is. + assert_eq!(set.rebase("/a//"), prefixes(&["b"])); + assert_eq!(set.rooted("/r//s/").unwrap(), prefixes(&["r/s/a/b", "r/s/c"])); + // A root ending inside an escape is no segment of a prefix. + let escaped = prefixes(&[r"a\/b"]); + assert!(escaped.rebase(r"a\").is_empty()); + assert_eq!(escaped.rebase(r"a\/b"), Prefixes::all()); + } + + #[test] + fn patterns_cover_the_same_paths() { + let set = prefixes(&["a", ""]); + assert_eq!(set.patterns(), Patterns::from(Pattern::all())); + assert_eq!( + prefixes(&["a/b"]).patterns(), + Patterns::from(Pattern::subtree("a/b").unwrap()) + ); + let deep = ["a"; Pattern::MAX_SEGMENTS].join("/"); + assert_eq!(prefix(&deep).pattern(), Pattern::literal(&deep).unwrap()); + } + + #[cfg(feature = "serde")] + #[test] + fn serde_round_trips_reduced_and_refuses_wildcards() { + let set: Prefixes = serde_json::from_str(r#"["a/b", "a", "c"]"#).unwrap(); + assert_eq!(serde_json::to_string(&set).unwrap(), r#"["a","c"]"#); + let err = serde_json::from_str::(r#"["a/**"]"#).unwrap_err().to_string(); + assert!(err.contains("`a` covers"), "{err}"); + } + + /// Every operation agrees with a brute-force reading over a small universe of paths. + #[test] + fn agrees_with_brute_force() { + let universe = ["", "a", "a/b", "a/b/c", "a/c", "a-b", "b", "b/a"]; + let paths: Vec<&str> = universe.to_vec(); + // Every subset of a few members. + let members = ["", "a", "a/b", "a-b", "b/a"]; + let sets: Vec = (0..1u32 << members.len()) + .map(|mask| { + (0..members.len()) + .filter(|bit| mask & (1 << bit) != 0) + .map(|bit| prefix(members[bit])) + .collect() + }) + .collect(); + let covered = + |set: &Prefixes| -> Vec<&str> { paths.iter().copied().filter(|path| set.matches(path)).collect() }; + let brute = |set: &Prefixes, path: &str| set.iter().any(|member| member.matches(path)); + for a in &sets { + for path in &paths { + assert_eq!(a.matches(path), brute(a, path), "{a:?} {path}"); + let p = prefix(path); + let beneath: Vec<_> = paths.iter().filter(|other| p.matches(other)).collect(); + // Covering a path covers its whole subtree. + assert_eq!( + a.matches(path), + beneath.iter().all(|other| brute(a, other)), + "{a:?} covers {path}" + ); + assert_eq!( + a.overlaps(path), + beneath.iter().any(|other| brute(a, other)), + "{a:?} overlaps {path}" + ); + } + for b in &sets { + let both = a.intersect(b); + let want: Vec<&str> = paths + .iter() + .copied() + .filter(|path| brute(a, path) && brute(b, path)) + .collect(); + assert_eq!(covered(&both), want, "{a:?} & {b:?}"); + let a_paths = covered(a); + assert_eq!( + a.covers(b), + covered(b).iter().all(|path| a_paths.contains(path)), + "{a:?} covers {b:?}" + ); + } + } + } +} diff --git a/rs/moq-relay/README.md b/rs/moq-relay/README.md index 258ef3a41ec..87b07ef459e 100644 --- a/rs/moq-relay/README.md +++ b/rs/moq-relay/README.md @@ -84,6 +84,6 @@ Quick example configuration in your `.toml` file: ```toml [auth] -url = "http://127.0.0.1:4440/" # moq auth serve --key root.jwk --public-subscribe 'anon/**' -# public = "anon/**" # or a static anonymous grant with no server +url = "http://127.0.0.1:4440/" # moq auth serve --key root.jwk --public-subscribe anon +# public = "anon" # or a static anonymous grant with no server ``` diff --git a/rs/moq-relay/benches/auth.rs b/rs/moq-relay/benches/auth.rs index 9caab6f436f..17953cf513f 100644 --- a/rs/moq-relay/benches/auth.rs +++ b/rs/moq-relay/benches/auth.rs @@ -8,10 +8,7 @@ use moq_relay::{Connection, auth, cluster}; use std::time::Duration; fn grant(prefix: &str) -> Grant { - Grant::new( - format!("{prefix}/**").parse::().unwrap(), - moq_auth::Patterns::new(), - ) + Grant::new(prefix.parse::().unwrap(), moq_auth::Prefixes::new()) } struct Fixture { diff --git a/rs/moq-relay/benches/http_auth.rs b/rs/moq-relay/benches/http_auth.rs index 97954763471..989ef4400bf 100644 --- a/rs/moq-relay/benches/http_auth.rs +++ b/rs/moq-relay/benches/http_auth.rs @@ -138,7 +138,7 @@ impl Fixture { } fn grant() -> Grant { - Grant::new(moq_auth::Patterns::new(), moq_auth::Pattern::all()) + Grant::new(moq_auth::Prefixes::new(), moq_auth::Prefix::all()) } async fn decide( diff --git a/rs/moq-relay/benches/session.rs b/rs/moq-relay/benches/session.rs index e209463787f..38231fb0471 100644 --- a/rs/moq-relay/benches/session.rs +++ b/rs/moq-relay/benches/session.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use futures::FutureExt; -use moq_auth::{Grant, Pattern, lease}; +use moq_auth::{Grant, Prefix, lease}; use moq_net::{cache, origin}; use moq_relay::{Accepted, Connection, auth, cluster, stats}; use session::{Backend, MockConnectOptions, Pair, mock::create_mock_session_pair}; @@ -90,7 +90,7 @@ impl Backend for Relay { let (auth, mut admissions) = auth::Auth::embedded(format!("bench-{id}")); let decider = tokio::spawn(async move { while let Some(admission) = admissions.next().await { - let mut grant = Grant::new(Pattern::all(), Pattern::all()); + let mut grant = Grant::new(Prefix::all(), Prefix::all()); grant.root = Some(String::new()); grant.link = (admission.request.path == "/peer").then_some(moq_auth::Link::Peer); admission.grant(lease::Consumer::fixed(grant)); diff --git a/rs/moq-relay/examples/embed.rs b/rs/moq-relay/examples/embed.rs index 06629de8e54..4f7cff79db1 100644 --- a/rs/moq-relay/examples/embed.rs +++ b/rs/moq-relay/examples/embed.rs @@ -29,7 +29,7 @@ async fn main() -> anyhow::Result<()> { config.listen.bind = Some("127.0.0.1:0".parse().unwrap()); config.listen.tls.generate = vec!["localhost".into()]; config.web.http.listen = Some("127.0.0.1:0".parse()?); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = Relay::load(config).await?; // Cloned before `run` consumes the relay. In-process workers publish into diff --git a/rs/moq-relay/src/auth.rs b/rs/moq-relay/src/auth.rs index 3e528881ee8..1cc1a92e2ed 100644 --- a/rs/moq-relay/src/auth.rs +++ b/rs/moq-relay/src/auth.rs @@ -14,7 +14,7 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; use axum::http; use moq_auth::{Bytes, Grant, Request, lease}; -use moq_auth::{Pattern, Patterns}; +use moq_auth::{Prefix, Prefixes}; use moq_net::{Path, stats::Tier}; use serde::{Deserialize, Serialize}; use serde_with::{OneOrMany, serde_as}; @@ -31,7 +31,7 @@ const ATTACH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); pub(crate) const RELAY: &str = concat!("moq-relay/", env!("CARGO_PKG_VERSION")); /// Where every session's grant comes from. Exactly one of `url` and the public -/// patterns is set; [`validate`](Self::validate) refuses anything else. +/// prefixes is set; [`validate`](Self::validate) refuses anything else. #[serde_as] #[derive(usage_rs::Args, Clone, Default, Serialize, Deserialize)] #[usage(unknown_flags = "error", args_override_self = false)] @@ -53,9 +53,10 @@ pub struct Config { #[serde_as(as = "OneOrMany<_>")] pub root: Vec, - /// Patterns an anonymous session may both publish and subscribe to, rooted at - /// `/`, such as `anon/**`. Repeatable or comma-separated. Sets a static grant - /// with no expiry and no server. + /// Prefixes an anonymous session may both publish and subscribe to, rooted at + /// `/`, such as `anon`, which covers `anon` and every path beneath it. The empty + /// prefix covers everything; a wildcard such as `anon/**` is refused. Repeatable or + /// comma-separated. Sets a static grant with no expiry and no server. #[usage( long = "auth-public", env = "MOQ_AUTH_PUBLIC", @@ -64,9 +65,9 @@ pub struct Config { )] #[serde(skip_serializing_if = "Vec::is_empty")] #[serde_as(as = "OneOrMany<_>")] - pub public: Vec, + pub public: Vec, - /// Patterns an anonymous session may subscribe to, rooted at `/`. Repeatable. + /// Prefixes an anonymous session may subscribe to, rooted at `/`. Repeatable. #[usage( long = "auth-public-subscribe", env = "MOQ_AUTH_PUBLIC_SUBSCRIBE", @@ -75,9 +76,9 @@ pub struct Config { )] #[serde(skip_serializing_if = "Vec::is_empty")] #[serde_as(as = "OneOrMany<_>")] - pub public_subscribe: Vec, + pub public_subscribe: Vec, - /// Patterns an anonymous session may publish, rooted at `/`. Repeatable. + /// Prefixes an anonymous session may publish, rooted at `/`. Repeatable. #[usage( long = "auth-public-publish", env = "MOQ_AUTH_PUBLIC_PUBLISH", @@ -86,7 +87,7 @@ pub struct Config { )] #[serde(skip_serializing_if = "Vec::is_empty")] #[serde_as(as = "OneOrMany<_>")] - pub public_publish: Vec, + pub public_publish: Vec, } impl std::fmt::Debug for Config { @@ -108,13 +109,13 @@ impl std::fmt::Debug for Config { const VERIFICATIONS: usize = 128; impl Config { - /// The static grant the public patterns name, or `None` when none is set. + /// The static grant the public prefixes name, or `None` when none is set. /// - /// Its patterns are rooted at `/`, not at a dialed path: each session is granted + /// Its prefixes are rooted at `/`, not at a dialed path: each session is granted /// what they reach from where it dialed, as a token with an empty root would be. pub fn public_grant(&self) -> Option { - let publish: Patterns = self.public.iter().chain(&self.public_publish).cloned().collect(); - let subscribe: Patterns = self.public.iter().chain(&self.public_subscribe).cloned().collect(); + let publish: Prefixes = self.public.iter().chain(&self.public_publish).cloned().collect(); + let subscribe: Prefixes = self.public.iter().chain(&self.public_subscribe).cloned().collect(); (!publish.is_empty() || !subscribe.is_empty()).then(|| Grant::new(publish, subscribe)) } @@ -286,9 +287,7 @@ pub struct Token { impl Token { /// Reduce `grant` for a session that dialed `path`. /// - /// The token keeps the patterns the grant yields: the origin scopes by any - /// pattern union, so `alice`, `*/chat`, and `alice/**` each mean exactly what - /// they say. + /// The token keeps the prefixes the grant yields, each covering its whole subtree. /// /// The dialed path is a URL path, whose one leading `/` is syntax rather than a /// segment. Fails with [`Error::Request`] if the rest of it is not a valid path, @@ -589,11 +588,12 @@ impl Lease { } } -/// Patterns as one comma-separated line, for logs. -fn join(patterns: &Patterns) -> String { - patterns +/// Prefixes as one comma-separated line of quoted strings for logs, so the empty +/// prefix, which covers everything, still shows. +fn join(prefixes: &Prefixes) -> String { + prefixes .iter() - .map(|pattern| pattern.as_str()) + .map(|prefix| format!("{:?}", prefix.as_str())) .collect::>() .join(",") } @@ -1293,7 +1293,7 @@ mod tests { assert!(admissions.next().await.is_none()); } - fn patterns(texts: &[&str]) -> moq_auth::Patterns { + fn prefixes(texts: &[&str]) -> moq_auth::Prefixes { texts.iter().map(|text| text.parse().unwrap()).collect() } @@ -1308,14 +1308,14 @@ 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()); + let mut tier = Grant::new(moq_net::Prefix::all(), moq_net::Prefix::all()); tier.tier = Some("a b".into()); assert!(Lease::new("/room", lease::Consumer::fixed(tier)).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()); + let grant = Grant::new(moq_net::Prefix::all(), moq_net::Prefix::all()); let (producer, consumer) = lease::Producer::new(grant.clone()); let mut lease = Lease::new("/room", consumer).unwrap(); let mut wildcard = grant; @@ -1374,7 +1374,7 @@ mod tests { let rate = moq_auth::Rate::from_bps(8000).unwrap(); for transport in [Quic, WebTransport, WebSocket, Http, Rtmp, Srt] { for publish in [false, true] { - let mut grant = Grant::new(Pattern::all(), Pattern::all()); + let mut grant = Grant::new(Prefix::all(), Prefix::all()); match publish { true => grant.publish.rate = Some(rate), false => grant.subscribe.rate = Some(rate), @@ -1419,7 +1419,7 @@ mod tests { async fn a_rechecked_cap_reaches_the_connection() { use futures::FutureExt; let rate = moq_auth::Rate::from_bps(8000).unwrap(); - let uncapped = Grant::new(Pattern::all(), Pattern::all()); + let uncapped = Grant::new(Prefix::all(), Prefix::all()); let mut capped = uncapped.clone(); capped.subscribe.rate = Some(rate); @@ -1450,17 +1450,17 @@ mod tests { #[test] fn exactly_one_source() { assert!(config(None, &[]).validate(false).is_err()); - assert!(config(Some("http://127.0.0.1:4440/"), &["**"]).validate(false).is_err()); + assert!(config(Some("http://127.0.0.1:4440/"), &[""]).validate(false).is_err()); assert!(config(Some("http://127.0.0.1:4440/"), &[]).validate(false).is_ok()); - assert!(config(None, &["anon/**"]).validate(false).is_ok()); + assert!(config(None, &["anon"]).validate(false).is_ok()); let split = Config { - public_subscribe: patterns(&["anon/**"]).into_iter().collect(), + public_subscribe: prefixes(&["anon"]).into_iter().collect(), ..Default::default() }; assert!(split.validate(false).is_ok()); let grant = split.public_grant().unwrap(); - assert_eq!(grant.subscribe.path, patterns(&["anon/**"])); + assert_eq!(grant.subscribe.path, prefixes(&["anon"])); assert!(grant.publish.path.is_empty()); } @@ -1475,7 +1475,7 @@ mod tests { /// needs an auth server, and `init` refuses it like `validate` does. #[test] fn a_client_ca_needs_an_auth_server() { - let public = config(None, &["anon/**"]); + let public = config(None, &["anon"]); let err = public.validate(true).unwrap_err().to_string(); assert!(err.contains("--auth-public ignores"), "{err}"); let Err(err) = public.init("relay-1", &moq_tokio::tls::Connect::default(), true) else { @@ -1485,29 +1485,29 @@ mod tests { assert!(config(Some("http://127.0.0.1:4440/"), &[]).validate(true).is_ok()); } - /// The public rules are rooted at `/`, like a token with an empty root. Bare `**` - /// reads the same either way, which is how rooting them at the dialed path went - /// unnoticed: `anon/**` at `/rooms/123` granted `rooms/123/anon/**`. + /// The public rules are rooted at `/`, like a token with an empty root. The empty + /// prefix reads the same either way, which is how rooting them at the dialed path went + /// unnoticed: `anon` at `/rooms/123` granted `rooms/123/anon`. #[tokio::test] async fn public_rules_are_rooted_at_slash() { let auth = Config { - public_publish: patterns(&["anon/**"]).into_iter().collect(), - public_subscribe: patterns(&["anon/**", "*/chat"]).into_iter().collect(), + public_publish: prefixes(&["anon"]).into_iter().collect(), + public_subscribe: prefixes(&["anon", "lobby/chat"]).into_iter().collect(), ..Default::default() } .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); for (path, root, publish, subscribe) in [ - ("/", "", &["anon/**"][..], &["anon/**", "*/chat"][..]), - ("/anon", "anon", &["**"], &["**", "chat"]), - ("/anon/room", "anon/room", &["**"], &["**"]), - ("/rooms", "rooms", &[], &["chat"]), + ("/", "", &["anon"][..], &["anon", "lobby/chat"][..]), + ("/anon", "anon", &[""], &[""]), + ("/anon/room", "anon/room", &[""], &[""]), + ("/lobby", "lobby", &[], &["chat"]), ] { let lease = auth.admit(auth.request(moq_auth::Transport::Quic, path)).await.unwrap(); assert_eq!(lease.token().root, Path::new(root).unwrap(), "{path}"); - assert_eq!(lease.token().publish.path, patterns(publish), "{path}"); - assert_eq!(lease.token().subscribe.path, patterns(subscribe), "{path}"); + assert_eq!(lease.token().publish.path, prefixes(publish), "{path}"); + assert_eq!(lease.token().subscribe.path, prefixes(subscribe), "{path}"); } // A path the rules don't reach is refused, never an outage the client retries. @@ -1523,7 +1523,7 @@ mod tests { /// Public rules verify nothing, so a token is refused rather than ignored. #[tokio::test] async fn public_rules_refuse_a_token() { - let auth = config(None, &["**"]) + let auth = config(None, &[""]) .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); let mut setup = auth.request(moq_auth::Transport::Quic, "/"); @@ -1545,7 +1545,7 @@ mod tests { /// A certificate is a fact the public rules ignore: it gets exactly what anyone does. #[tokio::test] async fn a_public_config_admits_anonymous_and_certificate_alike() { - let auth = config(None, &["anon/**"]) + let auth = config(None, &["anon"]) .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); let anonymous = auth.request(moq_auth::Transport::Quic, "/anon/room"); @@ -1560,7 +1560,7 @@ mod tests { for request in [anonymous, certificate] { let lease = auth.admit(request).await.unwrap(); assert_eq!(lease.token().root, Path::from_static("anon/room")); - assert_eq!(lease.token().subscribe.path, patterns(&["**"])); + assert_eq!(lease.token().subscribe.path, prefixes(&[""])); assert_eq!(lease.token().tier, Tier::default()); } } @@ -1569,7 +1569,7 @@ mod tests { /// sees it as a bare leaf, which would hide the intermediate a server scopes by. #[tokio::test] async fn an_untraced_certificate_chain_is_refused() { - let auth = config(None, &["anon/**"]) + let auth = config(None, &["anon"]) .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); let mut request = auth.request(moq_auth::Transport::Quic, "/anon/room"); @@ -1611,36 +1611,43 @@ mod tests { assert_eq!(peer.chain, vec![peer.fingerprint.clone(), root]); } - /// A public pattern without a wildcard names exactly one broadcast, never its subtree. + /// A public prefix covers its path and everything beneath it, but no sibling sharing + /// its bytes, and the empty prefix covers everything. #[tokio::test] - async fn a_literal_public_pattern_admits_exactly_that_broadcast() { + async fn a_public_prefix_admits_its_whole_subtree() { let auth = config(None, &["anon"]) .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); - // Dialed at the broadcast, the grant is the broadcast itself. - let lease = auth - .admit(auth.request(moq_auth::Transport::Quic, "/anon")) - .await - .unwrap(); - assert_eq!(lease.token().root, Path::new("anon").unwrap()); - assert_eq!(lease.token().publish, patterns(&[""]).into()); - assert_eq!(lease.token().subscribe, patterns(&[""]).into()); + // Dialed at or beneath the prefix, the grant is everything there. + for (path, root) in [("/anon", "anon"), ("/anon/room", "anon/room")] { + let lease = auth.admit(auth.request(moq_auth::Transport::Quic, path)).await.unwrap(); + assert_eq!(lease.token().root, Path::new(root).unwrap(), "{path}"); + assert_eq!(lease.token().publish, prefixes(&[""]).into(), "{path}"); + assert_eq!(lease.token().subscribe, prefixes(&[""]).into(), "{path}"); + } - // Dialed above it, the grant reaches only that name. + // Dialed above it, the grant reaches only that prefix. let lease = auth.admit(auth.request(moq_auth::Transport::Quic, "/")).await.unwrap(); - assert_eq!(lease.token().publish, patterns(&["anon"]).into()); + assert_eq!(lease.token().publish, prefixes(&["anon"]).into()); - // Nothing beneath it, and no other name, is reached. - for path in ["/anon/room", "/anonymous", "/other"] { + // A sibling, even one starting with the same bytes, is not reached. + for path in ["/anonymous", "/other"] { let Err(err) = auth.admit(auth.request(moq_auth::Transport::Quic, path)).await else { panic!("{path} was admitted"); }; assert!(matches!(err, Error::Refused(_)), "{path}: {err}"); } - // The empty pattern names the root broadcast. - assert!(config(None, &[""]).validate(false).is_ok()); + // The empty prefix covers every path. + let everything = config(None, &[""]) + .init("relay-1", &moq_tokio::tls::Connect::default(), false) + .unwrap(); + let lease = everything + .admit(everything.request(moq_auth::Transport::Quic, "/other/room")) + .await + .unwrap(); + assert_eq!(lease.token().publish, prefixes(&[""]).into()); } /// The embedder's answer is the session's verdict; an answer that never comes, @@ -1653,12 +1660,12 @@ mod tests { let decide = async { let admission = admissions.next().await.expect("an admission"); assert_eq!(admission.request.path, "/anon/room"); - admission.grant(lease::Consumer::fixed(Grant::new(patterns(&["**"]), patterns(&["**"])))); + admission.grant(lease::Consumer::fixed(Grant::new(prefixes(&[""]), prefixes(&[""])))); let admission = admissions.next().await.expect("an admission"); admission.refuse(Error::Refused(Default::default())); // A grant that admits nothing is the decider's mistake, refused like a server's. let admission = admissions.next().await.expect("an admission"); - admission.grant(lease::Consumer::fixed(Grant::new(Patterns::new(), Patterns::new()))); + admission.grant(lease::Consumer::fixed(Grant::new(Prefixes::new(), Prefixes::new()))); // Dropped without an answer. drop(admissions.next().await.expect("an admission")); admissions @@ -1702,7 +1709,7 @@ mod tests { use std::time::Duration; let start = kio::clock::now(); - let mut grant = Grant::new(patterns(&["**"]), patterns(&["**"])); + let mut grant = Grant::new(prefixes(&[""]), prefixes(&[""])); grant.expires = Some(kio::clock::wall() + Duration::from_secs(10)); let mut lease = Lease::new("/room", lease::Consumer::fixed(grant)).unwrap(); for _ in 0..9 { @@ -1724,7 +1731,7 @@ mod tests { async fn an_expired_grant_ends_at_once() { use std::time::Duration; - let mut grant = Grant::new(patterns(&["**"]), patterns(&["**"])); + let mut grant = Grant::new(prefixes(&[""]), prefixes(&[""])); 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(); @@ -1743,21 +1750,21 @@ mod tests { } #[test] - fn token_keeps_every_grant_pattern() { - let mut grant = Grant::new(patterns(&["alice/**"]), patterns(&["**"])); + fn token_keeps_every_grant_prefix() { + let mut grant = Grant::new(prefixes(&["alice"]), prefixes(&[""])); grant.root = Some("pid/room".into()); grant.tier = Some("gold".into()); grant.label = Some("project/key/123".into()); let token = Token::new("/vanity/room", &grant).unwrap(); assert_eq!(token.root, Path::from_static("pid/room")); - assert_eq!(token.publish.path, patterns(&["alice/**"])); - assert_eq!(token.subscribe.path, patterns(&["**"])); + assert_eq!(token.publish.path, prefixes(&["alice"])); + assert_eq!(token.subscribe.path, prefixes(&[""])); assert_eq!(token.tier, Tier::new(moq_net::Path::from_static("gold"))); assert_eq!(token.label, Path::from_static("project/key/123")); - let patterns = patterns(&["*/chat", "alice", ""]); - let token = Token::new("/", &Grant::new(patterns.clone(), Patterns::new())).unwrap(); - assert_eq!(token.publish.path, patterns); + let all = prefixes(&["lobby/chat", "alice", ""]); + let token = Token::new("/", &Grant::new(all.clone(), Prefixes::new())).unwrap(); + assert_eq!(token.publish.path, all); assert!( token.label.is_empty(), "an absent label is the empty label, not the root" @@ -1768,7 +1775,7 @@ mod tests { /// only contains dots is an ordinary segment. #[tokio::test] async fn admit_refuses_dot_segments() { - let auth = config(None, &["**"]) + let auth = config(None, &[""]) .init("relay-1", &moq_tokio::tls::Connect::default(), false) .unwrap(); for path in ["/tenant-a/../tenant-b", "/..", "/a/.", "/a/%2e%2E/b", "/a/.%2e", "/%2E"] { @@ -1786,7 +1793,7 @@ mod tests { #[test] fn token_refuses_empty_segments() { - let all = || Grant::new(patterns(&["**"]), patterns(&["**"])); + let all = || Grant::new(prefixes(&[""]), prefixes(&[""])); // The dialed URL path's one leading `/` is syntax; any other empty segment is the client's. assert_eq!(Token::new("/room", &all()).unwrap().root, Path::from_static("room")); for dialed in ["/room/", "//room", "/room//cam"] { @@ -1801,10 +1808,10 @@ mod tests { #[test] fn a_narrower_recheck_is_not_covered() { - let wide = Token::new("/room", &Grant::new(patterns(&["**"]), patterns(&["**"]))).unwrap(); - let narrow = Token::new("/room", &Grant::new(patterns(&["alice/**"]), patterns(&["**"]))).unwrap(); - let moved = Token::new("/other", &Grant::new(patterns(&["**"]), patterns(&["**"]))).unwrap(); - let mut mounted = Grant::new(patterns(&["**"]), patterns(&["**"])); + let wide = Token::new("/room", &Grant::new(prefixes(&[""]), prefixes(&[""]))).unwrap(); + let narrow = Token::new("/room", &Grant::new(prefixes(&["alice"]), prefixes(&[""]))).unwrap(); + let moved = Token::new("/other", &Grant::new(prefixes(&[""]), prefixes(&[""]))).unwrap(); + let mut mounted = Grant::new(prefixes(&[""]), prefixes(&[""])); mounted.mounts.insert(".svc".into(), ".svc/room".into()); let mounted = Token::new("/room", &mounted).unwrap(); assert_eq!( @@ -1824,7 +1831,7 @@ mod tests { /// flips it closes the session rather than misreport them. #[tokio::test] async fn a_recheck_that_flips_the_link_closes() { - let grant = Grant::new(patterns(&["**"]), patterns(&["**"])); + let grant = Grant::new(prefixes(&[""]), prefixes(&[""])); let (producer, consumer) = lease::Producer::new(grant.clone()); let mut lease = Lease::new("/", consumer).unwrap(); assert_eq!(lease.token().link, None); @@ -1846,7 +1853,7 @@ mod tests { #[test] fn a_recheck_resolves_a_dropped_root_to_the_dialed_path() { - let everything = || Grant::new(patterns(&["**"]), patterns(&["**"])); + let everything = || Grant::new(prefixes(&[""]), prefixes(&[""])); let mut aliased = everything(); aliased.root = Some("pid/room".into()); let token = Token::new("/vanity/room", &aliased).unwrap(); @@ -1880,7 +1887,7 @@ mod tests { for root in roots { let claims = moq_auth::Claims::default() .with_root(&root) - .with_publish([moq_auth::Pattern::all()]); + .with_publish([moq_auth::Prefix::all()]); assert_eq!(key.sign(&claims).is_ok(), Path::new(&root).is_ok(), "{root:?}"); } } diff --git a/rs/moq-relay/src/auth/admission_tests.rs b/rs/moq-relay/src/auth/admission_tests.rs index 99f5129c182..8f127bd9a47 100644 --- a/rs/moq-relay/src/auth/admission_tests.rs +++ b/rs/moq-relay/src/auth/admission_tests.rs @@ -1,17 +1,17 @@ //! Admission and lease behavior for a gateway embedding the relay. use crate::{auth, cluster}; -use moq_auth::{Grant, Pattern, Patterns, Role, Transport, lease}; +use moq_auth::{Grant, Prefix, Prefixes, Role, Transport, lease}; -fn patterns(texts: &[&str]) -> Patterns { - texts.iter().map(|text| text.parse::().unwrap()).collect() +fn prefixes(texts: &[&str]) -> Prefixes { + texts.iter().map(|text| text.parse::().unwrap()).collect() } #[tokio::test] async fn gateway_admission_scopes_origins_and_revoke_stops_work() { let cluster = cluster::Cluster::spawn(cluster::Options::default()).unwrap(); let (auth, mut admissions) = auth::Auth::embedded("edge"); - let mut grant = Grant::new(patterns(&["media/**"]), patterns(&["watch/**"])); + let mut grant = Grant::new(prefixes(&["media"]), prefixes(&["watch"])); grant.root = Some("canonical".into()); grant.tier = Some("gateway".into()); let decider = tokio::spawn(async move { @@ -26,7 +26,7 @@ async fn gateway_admission_scopes_origins_and_revoke_stops_work() { request.role = Some(Role::Publisher); let admitted = cluster.admit(&auth, request).await.unwrap(); assert_eq!(admitted.lease.token().root.as_str(), "canonical"); - assert_eq!(admitted.publisher.as_ref().unwrap().allowed(), patterns(&["media/**"])); + assert_eq!(admitted.publisher.as_ref().unwrap().allowed(), prefixes(&["media"])); assert!( admitted.subscriber.is_none(), "publisher-only gateway must not subscribe" @@ -47,8 +47,8 @@ async fn a_gateway_role_without_scope_is_forbidden() { tokio::spawn(async move { let admission = admissions.next().await.unwrap(); admission.grant(lease::Consumer::fixed(Grant::new( - patterns(&["media/**"]), - Patterns::new(), + prefixes(&["media"]), + Prefixes::new(), ))); }); let mut request = auth.request(Transport::WebRtc, "/room"); @@ -65,7 +65,7 @@ async fn completed_gateway_work_closes_its_lease() { let (auth, mut admissions) = auth::Auth::embedded("edge"); let decider = tokio::spawn(async move { let admission = admissions.next().await.unwrap(); - let (producer, consumer) = lease::Producer::new(Grant::new(patterns(&["media/**"]), Patterns::new())); + let (producer, consumer) = lease::Producer::new(Grant::new(prefixes(&["media"]), Prefixes::new())); admission.grant(consumer); producer }); @@ -84,7 +84,7 @@ async fn a_narrowed_gateway_grant_stops_work() { let (auth, mut admissions) = auth::Auth::embedded("edge"); let decider = tokio::spawn(async move { let admission = admissions.next().await.unwrap(); - let (producer, consumer) = lease::Producer::new(Grant::new(patterns(&["media/**"]), Patterns::new())); + let (producer, consumer) = lease::Producer::new(Grant::new(prefixes(&["media"]), Prefixes::new())); admission.grant(consumer); producer }); @@ -92,7 +92,7 @@ async fn a_narrowed_gateway_grant_stops_work() { request.role = Some(Role::Publisher); let admitted = cluster.admit(&auth, request).await.unwrap(); let producer = decider.await.unwrap(); - producer.update(Grant::new(patterns(&["media/video/**"]), Patterns::new())); + producer.update(Grant::new(prefixes(&["media/video"]), Prefixes::new())); assert_eq!( auth::hold(admitted.lease, std::future::pending::<()>()).await, Err(lease::Reason::Narrowed) @@ -116,7 +116,7 @@ async fn a_moved_gateway_tier_retags_its_stats() { }; let (auth, mut admissions) = auth::Auth::embedded("edge"); - let mut grant = Grant::new(patterns(&["media/**"]), Patterns::new()); + let mut grant = Grant::new(prefixes(&["media"]), Prefixes::new()); grant.tier = Some("gateway".into()); let decider = tokio::spawn({ let grant = grant.clone(); diff --git a/rs/moq-relay/src/auth/caps.rs b/rs/moq-relay/src/auth/caps.rs index 05af18e10b9..1e792fa72ea 100644 --- a/rs/moq-relay/src/auth/caps.rs +++ b/rs/moq-relay/src/auth/caps.rs @@ -102,13 +102,13 @@ mod tests { } fn grant(publish: &[&str], publish_rate: Option, subscribe: &[&str], subscribe_rate: Option) -> Grant { - let patterns = |texts: &[&str]| { + let prefixes = |texts: &[&str]| { texts .iter() .map(|text| text.parse().unwrap()) - .collect::() + .collect::() }; - let mut grant = Grant::new(patterns(publish), patterns(subscribe)); + let mut grant = Grant::new(prefixes(publish), prefixes(subscribe)); grant.publish.rate = publish_rate; grant.subscribe.rate = subscribe_rate; grant @@ -116,8 +116,8 @@ mod tests { #[test] fn a_union_takes_the_highest_cap_and_an_uncapped_grant_lifts_it() { - let low = grant(&["a/**"], rate(1_000), &["**"], rate(8_000)); - let high = grant(&["b/**"], rate(2_000), &["**"], rate(4_000)); + let low = grant(&["a"], rate(1_000), &[""], rate(8_000)); + let high = grant(&["b"], rate(2_000), &[""], rate(4_000)); let caps = Caps::union([&low, &high]); assert_eq!( caps, @@ -127,7 +127,7 @@ mod tests { } ); - let uncapped = grant(&["c/**"], None, &[], None); + let uncapped = grant(&["c"], None, &[], None); let caps = Caps::union([&low, &high, &uncapped]); assert_eq!( caps, diff --git a/rs/moq-relay/src/auth/refresh.rs b/rs/moq-relay/src/auth/refresh.rs index e3f3cd8bcf0..5b92688dbd3 100644 --- a/rs/moq-relay/src/auth/refresh.rs +++ b/rs/moq-relay/src/auth/refresh.rs @@ -511,7 +511,7 @@ mod tests { } fn grant(prefix: &str, seconds: u64) -> Grant { - let mut grant = Grant::new(prefix.parse::().unwrap(), moq_auth::Patterns::new()); + let mut grant = Grant::new(prefix.parse::().unwrap(), moq_auth::Prefixes::new()); grant.expires = Some(kio::clock::wall() + Duration::from_secs(seconds)); grant } @@ -634,13 +634,13 @@ mod tests { #[tokio::test(start_paused = true)] async fn an_in_band_recheck_cannot_apply_an_invalid_label() { let (auth, mut admissions) = super::super::Auth::embedded("test"); - let (producer, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (producer, consumer) = lease::Producer::new(grant("a", 3000)); let request = auth.request(moq_auth::Transport::Quic, "/room"); let (lease, ()) = tokio::join!(auth.admit(request), async { admissions.next().await.unwrap().grant(consumer); }); let (client, supervising) = sessions(lease.unwrap()).await; - let mut invalid = grant("a/**", 3000); + let mut invalid = grant("a", 3000); invalid.label = Some("project/*".into()); producer.update(invalid); assert!(supervising.await.unwrap().to_string().contains("invalid stats label")); @@ -690,7 +690,7 @@ mod tests { /// Admit a session, apply `change` to its grant before protocol setup, and check /// that setup refuses it as a failure. async fn refused_before_setup(change: impl FnOnce(&mut moq_auth::Grant), expected: SessionError) { - let initial = grant("a/**", 1); + let initial = grant("a", 1); let (producer, consumer) = lease::Producer::new(initial.clone()); let registry = moq_net::stats::Registry::new(Default::default()); let stats = registry @@ -748,10 +748,10 @@ mod tests { #[tokio::test(start_paused = true)] async fn an_invalid_attachment_is_refused_without_losing_the_existing_grant() { - let (lease, mut admissions) = admission(grant("a/**", 3000)).await; + let (lease, mut admissions) = admission(grant("a", 3000)).await; let (client, supervising) = sessions(lease).await; let invalid = present(&client, "invalid"); - let mut label = grant("a/**", 3000); + let mut label = grant("a", 3000); label.label = Some("project/*".into()); admissions.next().await.unwrap().grant(lease::Consumer::fixed(label)); // A grant the relay cannot use is the decider's fault, and asking again gets the @@ -769,7 +769,7 @@ mod tests { let (result, ()) = tokio::join!(added(&auth, "valid"), async { let admission = admissions.next().await.unwrap(); assert_eq!(presented(&admission), b"valid"); - admission.grant(lease::Consumer::fixed(grant("a/**", 3000))); + admission.grant(lease::Consumer::fixed(grant("a", 3000))); }); result.unwrap(); client.abort(moq_net::Error::Cancel); @@ -779,13 +779,13 @@ mod tests { #[tokio::test(start_paused = true)] async fn an_in_band_token_arrives_as_an_attachment() { - let (lease, mut admissions) = admission(grant("a/**", 3000)).await; + let (lease, mut admissions) = admission(grant("a", 3000)).await; let (client, supervising) = sessions(lease).await; let auth = client.auth(); let (token, ()) = tokio::join!(added(&auth, "replacement"), async { let admission = admissions.next().await.unwrap(); assert!(admission.attached(), "an in-band token arrived as a new session"); - admission.grant(lease::Consumer::fixed(grant("a/**", 3000))); + admission.grant(lease::Consumer::fixed(grant("a", 3000))); }); token.unwrap(); client.abort(moq_net::Error::Cancel); @@ -827,7 +827,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn the_union_of_credentials_paces_the_connection() { let (pacer, applied) = recorder(); - let initial = lease::Consumer::fixed(capped("a/**", Some(1_000), None)); + let initial = lease::Consumer::fixed(capped("a", Some(1_000), None)); let (lease, mut admissions) = paced(initial, Some(pacer)).await; assert_eq!( lease.caps(), @@ -840,18 +840,18 @@ mod tests { let auth = client.auth(); let (higher, ()) = tokio::join!(added(&auth, "higher"), async { - let higher = capped("a/**", Some(4_000), None); + let higher = capped("a", Some(4_000), None); admissions.next().await.unwrap().grant(lease::Consumer::fixed(higher)); }); // Held, since dropping a token withdraws its credential. let _higher = higher.unwrap(); - let (producer, uncapped) = lease::Producer::new(capped("a/**", None, None)); + let (producer, uncapped) = lease::Producer::new(capped("a", None, None)); let (lifted, ()) = tokio::join!(added(&auth, "uncapped"), async { admissions.next().await.unwrap().grant(uncapped); }); let _lifted = lifted.unwrap(); // The auth server caps the grant it had left uncapped. - producer.update(capped("a/**", Some(2_000), None)); + producer.update(capped("a", Some(2_000), None)); while applied.lock().unwrap().len() < 3 { kio::clock::sleep(Duration::from_millis(1)).await; } @@ -868,11 +868,11 @@ mod tests { /// Without a pacer, a capped in-band token is refused, and the session keeps its grant. #[tokio::test(start_paused = true)] async fn an_unpaced_session_refuses_a_capped_token() { - let (lease, mut admissions) = admission(grant("a/**", 3000)).await; + let (lease, mut admissions) = admission(grant("a", 3000)).await; let (client, supervising) = sessions(lease).await; let auth = client.auth(); let (result, ()) = tokio::join!(added(&auth, "capped"), async { - let capped = capped("a/**", None, Some(8_000)); + let capped = capped("a", None, Some(8_000)); admissions.next().await.unwrap().grant(lease::Consumer::fixed(capped)); }); assert!(result.is_err(), "the cap was discarded"); @@ -885,10 +885,10 @@ mod tests { /// Without a pacer, a re-check that adds a cap ends the session rather than drop it. #[tokio::test(start_paused = true)] async fn an_unpaced_session_ends_when_rechecked_into_a_cap() { - let (producer, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (producer, consumer) = lease::Producer::new(grant("a", 3000)); let (lease, _admissions) = paced(consumer, None).await; let (client, supervising) = sessions(lease).await; - producer.update(capped("a/**", Some(8_000), None)); + producer.update(capped("a", Some(8_000), None)); let reason = supervising.await.unwrap(); assert_eq!(reason.to_string(), caps::UNENFORCED); client.abort(moq_net::Error::Cancel); @@ -904,7 +904,7 @@ mod tests { (lease::Reason::Shutdown, None), (lease::Reason::Refused, Some(Failure::Application)), ] { - let (producer, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (producer, consumer) = lease::Producer::new(grant("a", 3000)); let registry = moq_net::stats::Registry::new(Default::default()); let stats = registry .tier(moq_net::stats::Tier::default()) @@ -930,7 +930,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_superseded_expiry_does_not_end_the_session() { let (auth, mut admissions) = super::super::Auth::embedded("test"); - let (producer, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (producer, consumer) = lease::Producer::new(grant("a", 3000)); let request = auth.request(moq_auth::Transport::Quic, "/room"); let (lease, ()) = tokio::join!(auth.admit(request), async { admissions.next().await.unwrap().grant(consumer); @@ -950,7 +950,7 @@ mod tests { .next() .await .unwrap() - .grant(lease::Consumer::fixed(grant("a/**", 3000))); + .grant(lease::Consumer::fixed(grant("a", 3000))); }); // Held, since dropping the handle withdraws the replacement. let _token = token.unwrap(); @@ -967,7 +967,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn an_outage_refuses_transiently_and_never_extends_the_expiry() { let clock = manual(); - let (lease, mut admissions) = admission(grant("a/**", 30)).await; + let (lease, mut admissions) = admission(grant("a", 30)).await; let (client, mut supervising) = sessions(lease).await; let mut adding = present(&client, "replacement"); for attempt in 0..3 { @@ -1006,7 +1006,7 @@ mod tests { /// and reaches the decider once one frees. #[tokio::test(start_paused = true)] async fn a_token_over_the_limit_waits_for_a_slot() { - let (lease, mut admissions) = admission_with(grant("a/**", 3000), 2).await; + let (lease, mut admissions) = admission_with(grant("a", 3000), 2).await; let (client, supervising) = sessions(lease).await; let first = present(&client, "first"); let mut held = vec![admissions.next().await.unwrap()]; @@ -1025,11 +1025,11 @@ mod tests { "a token past the limit was answered without a slot" ); - held.remove(0).grant(lease::Consumer::fixed(grant("a/**", 3000))); + held.remove(0).grant(lease::Consumer::fixed(grant("a", 3000))); first.await.unwrap().unwrap(); let admission = admissions.next().await.unwrap(); assert_eq!(presented(&admission), b"third"); - admission.grant(lease::Consumer::fixed(grant("a/**", 3000))); + admission.grant(lease::Consumer::fixed(grant("a", 3000))); third.await.unwrap().unwrap(); client.abort(moq_net::Error::Cancel); supervising.abort(); @@ -1041,7 +1041,7 @@ mod tests { /// refusal may pass, so the client keeps presenting both tokens. #[tokio::test(start_paused = true)] async fn a_waiting_token_times_out_without_reaching_the_decider() { - let (lease, mut admissions) = admission_with(grant("a/**", 3000), 1).await; + let (lease, mut admissions) = admission_with(grant("a", 3000), 1).await; let (client, supervising) = sessions(lease).await; let slow = present(&client, "slow"); let held = admissions.next().await.unwrap(); @@ -1075,13 +1075,13 @@ mod tests { #[tokio::test(start_paused = true)] async fn renewal_outlives_the_initial_credential_and_withdrawal_ends_only_its_grant() { let clock = manual(); - let (lease, mut admissions) = admission(grant("a/**", 3)).await; + let (lease, mut admissions) = admission(grant("a", 3)).await; let (client, mut supervising) = sessions(lease).await; let auth = client.auth(); let (token, ()) = tokio::join!(added(&auth, "replacement"), async { let admission = admissions.next().await.unwrap(); assert_eq!(presented(&admission), b"replacement"); - admission.grant(lease::Consumer::fixed(grant("a/**", 20))); + admission.grant(lease::Consumer::fixed(grant("a", 20))); }); let token = token.unwrap(); clock.advance(Duration::from_secs(4)); @@ -1109,7 +1109,7 @@ mod tests { moq_net::auth::Credential, ) { let (auth, mut admissions) = super::super::Auth::embedded("test"); - let (owner, consumer) = lease::Producer::new(grant("a/**", 3)); + let (owner, consumer) = lease::Producer::new(grant("a", 3)); let request = auth.request(moq_auth::Transport::Quic, "/room"); let (lease, ()) = tokio::join!(auth.admit(request), async { admissions.next().await.unwrap().grant(consumer); @@ -1146,7 +1146,7 @@ mod tests { // unbiased select picks between them at random, so repeat until the order is hit. for _ in 0..16 { let (owner, registry, client, session, lease, _token) = - renewed(&clock, lease::Consumer::fixed(grant("a/**", 3000))).await; + renewed(&clock, lease::Consumer::fixed(grant("a", 3000))).await; let supervising = tokio::spawn(crate::connection::supervise( session, lease, @@ -1178,7 +1178,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_revocation_pending_at_the_close_is_still_reported() { let clock = manual(); - let (replacement, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (replacement, consumer) = lease::Producer::new(grant("a", 3000)); let (owner, _registry, client, session, lease, _token) = renewed(&clock, consumer).await; replacement.revoke(lease::Reason::Refused); client.abort(moq_net::Error::Cancel); @@ -1191,18 +1191,18 @@ mod tests { #[tokio::test(start_paused = true)] async fn refused_and_different_root_tokens_leave_the_session_grant_unchanged() { - let (lease, mut admissions) = admission(grant("a/**", 30)).await; + let (lease, mut admissions) = admission(grant("a", 30)).await; let (client, supervising) = sessions(lease).await; let auth = client.auth(); for refusal in [0, 1, 2] { let (result, ()) = tokio::join!(added(&auth, "bad"), async { let admission = admissions.next().await.unwrap(); if refusal == 1 { - let mut other = grant("b/**", 30); + let mut other = grant("b", 30); other.root = Some("other".into()); admission.grant(lease::Consumer::fixed(other)); } else if refusal == 2 { - let mut other = grant("b/**", 30); + let mut other = grant("b", 30); other.subscribe = other.publish.clone(); other.publish = Default::default(); admission.grant(lease::Consumer::fixed(other)); @@ -1219,11 +1219,11 @@ mod tests { } #[tokio::test(start_paused = true)] async fn attached_grants_update_union_and_withdraw_without_widening_setup_role() { - let (lease, mut admissions) = admission(grant("a/**", 30)).await; + let (lease, mut admissions) = admission(grant("a", 30)).await; let (client, supervising) = sessions(lease).await; let auth = client.auth(); - let mut wider = grant("b/**", 30); - wider.subscribe.path = ["**".parse().unwrap()].into_iter().collect(); + let mut wider = grant("b", 30); + wider.subscribe.path = ["".parse().unwrap()].into_iter().collect(); let (producer, consumer) = lease::Producer::new(wider); let (token, ()) = tokio::join!(added(&auth, "replacement"), async { admissions.next().await.unwrap().grant(consumer); @@ -1237,7 +1237,7 @@ mod tests { let current = union.peek().unwrap(); assert!(current.publish.matches("a/cam")); assert!(current.publish.matches("b/cam")); - producer.update(grant("c/**", 30)); + producer.update(grant("c", 30)); let updated = union.changed().await.unwrap().unwrap(); assert!(updated.publish.matches("c/cam")); assert!(!updated.publish.matches("b/cam")); @@ -1255,7 +1255,7 @@ mod tests { async fn an_empty_token_is_refused_once_the_connection_credential_changes_root() { let (auth, mut admissions) = super::super::Auth::embedded("test"); let request = auth.request(moq_auth::Transport::Quic, "/room"); - let (owner, consumer) = lease::Producer::new(grant("a/**", 3000)); + let (owner, consumer) = lease::Producer::new(grant("a", 3000)); let (lease, ()) = tokio::join!(auth.admit(request), async { admissions.next().await.unwrap().grant(consumer); }); @@ -1266,10 +1266,10 @@ mod tests { .next() .await .unwrap() - .grant(lease::Consumer::fixed(grant("a/**", 3000))); + .grant(lease::Consumer::fixed(grant("a", 3000))); }); let _token = token.unwrap(); - let mut moved = grant("a/**", 3000); + let mut moved = grant("a", 3000); moved.root = Some("other".into()); owner.update(moved); tokio::task::yield_now().await; @@ -1289,7 +1289,7 @@ mod tests { /// while a sent check keeps its slot after its token is withdrawn. #[tokio::test(start_paused = true)] async fn a_withdrawn_waiting_token_never_reaches_the_decider() { - let (lease, mut admissions) = admission_with(grant("a/**", 3000), 1).await; + let (lease, mut admissions) = admission_with(grant("a", 3000), 1).await; let (client, supervising) = sessions(lease).await; let sent = present(&client, "sent"); let held = admissions.next().await.unwrap(); @@ -1308,10 +1308,10 @@ mod tests { "withdrawing a sent check released its slot" ); - held.grant(lease::Consumer::fixed(grant("a/**", 3000))); + held.grant(lease::Consumer::fixed(grant("a", 3000))); let admission = admissions.next().await.unwrap(); assert_eq!(presented(&admission), b"next"); - admission.grant(lease::Consumer::fixed(grant("a/**", 3000))); + admission.grant(lease::Consumer::fixed(grant("a", 3000))); next.await.unwrap().unwrap(); client.abort(moq_net::Error::Cancel); supervising.abort(); @@ -1321,7 +1321,7 @@ mod tests { /// A credential that is not UTF-8 reaches the decider unchanged. #[tokio::test(start_paused = true)] async fn a_binary_token_is_attached_unchanged() { - let (lease, mut admissions) = admission(grant("a/**", 3000)).await; + let (lease, mut admissions) = admission(grant("a", 3000)).await; let (client, supervising) = sessions(lease).await; let auth = client.auth(); let binary: &[u8] = &[0xff, 0x00, 0xfe, b'&', b'=', 0x80]; @@ -1329,7 +1329,7 @@ mod tests { let admission = admissions.next().await.unwrap(); assert!(admission.attached()); assert_eq!(presented(&admission), binary); - admission.grant(lease::Consumer::fixed(grant("a/**", 3000))); + admission.grant(lease::Consumer::fixed(grant("a", 3000))); }); token.unwrap(); client.abort(moq_net::Error::Cancel); @@ -1340,7 +1340,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn public_rule_sessions_refuse_tokens_as_unsupported() { let auth = super::super::Config { - public_publish: vec!["**".parse().unwrap()], + public_publish: vec!["".parse().unwrap()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -1375,7 +1375,7 @@ mod tests { .next() .await .unwrap() - .grant(lease::Consumer::fixed(grant("a/**", 30))); + .grant(lease::Consumer::fixed(grant("a", 30))); }); let (client, supervising) = sessions(lease.unwrap()).await; assert!(matches!( diff --git a/rs/moq-relay/src/cluster.rs b/rs/moq-relay/src/cluster.rs index a715f01bca4..bcd483d234e 100644 --- a/rs/moq-relay/src/cluster.rs +++ b/rs/moq-relay/src/cluster.rs @@ -1599,8 +1599,8 @@ impl Cluster { ) -> Result { let mut admitted = self.scope(lease, request)?; let mut bound = admitted.lease.token().clone(); - bound.publish.path = moq_net::Pattern::all().into(); - bound.subscribe.path = moq_net::Pattern::all().into(); + bound.publish.path = moq_net::Prefixes::all(); + bound.subscribe.path = moq_net::Prefixes::all(); if request.role != Some(moq_auth::Role::Subscriber) { admitted.publisher = self .publisher(&bound) @@ -1799,7 +1799,7 @@ impl Cluster { /// billed under `--cluster-tier`. The proof is a secret this relay minted for /// itself, so no auth server is asked. pub(crate) fn lan_peer_grant(&self) -> moq_auth::Grant { - let mut grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); grant.tier = self.config.tier.clone().filter(|tier| !tier.is_empty()); grant.link = Some(moq_auth::Link::Peer); grant @@ -2663,7 +2663,7 @@ mod tests { (Some(moq_auth::Link::Peer), 20), (Some(moq_auth::Link::Upstream), 20), ] { - let mut grant = moq_auth::Grant::new(moq_net::Pattern::all(), moq_net::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_net::Prefix::all(), moq_net::Prefix::all()); grant.link = link; let (_producer, consumer) = moq_auth::lease::Producer::new(grant); // A cluster-looking path alone never selects the peer policy. @@ -2692,7 +2692,7 @@ mod tests { }); for peer in [false, true] { for session in [false, true] { - let mut grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); grant.link = peer.then_some(moq_auth::Link::Peer); let lease = auth::Lease::new("/", moq_auth::lease::Consumer::fixed(grant)).unwrap(); let admitted = match session { @@ -2753,7 +2753,7 @@ mod tests { #[tokio::test] async fn session_handles_apply_grant_mounts() { let cluster = new_cluster(Config::default()).expect("cluster"); - let everything = || moq_net::Patterns::from(moq_net::Pattern::all()); + let everything = || moq_net::Prefixes::all(); let mut grant = moq_auth::Grant::new(everything(), everything()); grant.mounts.insert(".svc".into(), ".svc/pid".into()); let token = auth::Token::new("/pid", &grant).unwrap(); @@ -2792,7 +2792,7 @@ mod tests { #[tokio::test] async fn session_handles_refuse_invalid_grant_mounts() { let cluster = new_cluster(Config::default()).expect("cluster"); - let everything = || moq_net::Patterns::from(moq_net::Pattern::all()); + let everything = || moq_net::Prefixes::all(); let invalid: [&[(&str, &str)]; 2] = [&[(".a", "pid/.z"), (".z", "secret")], &[("*", ".svc/pid")]]; for mounts in invalid { let mut grant = moq_auth::Grant::new(everything(), everything()); @@ -2855,7 +2855,7 @@ mod tests { drop(cluster); } - /// Only cluster peers publish under the stats prefix: a root `**` client grant + /// Only cluster peers publish under the stats prefix: a client grant of everything /// can neither route nor announce there, yet still reads the relay's stats. #[kio::test] async fn only_peers_publish_under_the_stats_prefix() { @@ -2863,7 +2863,7 @@ mod tests { let options = || moq_net::origin::RouteOptions::default(); for link in [None, Some(moq_auth::Link::Peer), Some(moq_auth::Link::Upstream)] { let peer = link.is_some(); - let mut grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); grant.link = link; let token = auth::Lease::new("/", moq_auth::lease::Consumer::fixed(grant)).unwrap(); let publisher = cluster.publisher(token.token()).expect("publish grant"); @@ -2883,7 +2883,7 @@ mod tests { assert!(publisher.route(".statsish", options()).is_ok()); } - let grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + let grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); let token = auth::Lease::new("/", moq_auth::lease::Consumer::fixed(grant)).unwrap(); let consumer = cluster.subscriber(token.token()).expect("subscribe grant").consume(); let path = Path::from_static(".stats/node"); @@ -4741,7 +4741,7 @@ mod tests { #[tokio::test] async fn peer_grant_marks_the_session_publisher() { let cluster = new_cluster(Config::default()).expect("cluster"); - let all: moq_net::Patterns = [moq_net::Pattern::all()].into_iter().collect(); + let all = moq_net::Prefixes::all(); let client = auth::Token::new("/", &moq_auth::Grant::new(all.clone(), all.clone())).unwrap(); let mut grant = moq_auth::Grant::new(all.clone(), all); @@ -4777,7 +4777,7 @@ mod tests { #[tokio::test] async fn upstream_grant_hides_other_upstreams() { let cluster = new_cluster(Config::default()).expect("cluster"); - let all: moq_net::Patterns = [moq_net::Pattern::all()].into_iter().collect(); + let all = moq_net::Prefixes::all(); let client = auth::Token::new("/", &moq_auth::Grant::new(all.clone(), all.clone())).unwrap(); let mut grant = moq_auth::Grant::new(all.clone(), all); diff --git a/rs/moq-relay/src/config.rs b/rs/moq-relay/src/config.rs index 47cf577f210..94975cfdb36 100644 --- a/rs/moq-relay/src/config.rs +++ b/rs/moq-relay/src/config.rs @@ -909,7 +909,7 @@ max_streams = 64 if !test_env::Env::default().set("MOQ_AUTH_KEY", "root.jwk").isolate() { return; } - let err = Config::parse_and_merge(["moq-relay", "--auth-public", "anon/**"]) + let err = Config::parse_and_merge(["moq-relay", "--auth-public", "anon"]) .expect_err("must refuse") .to_string(); assert!(err.contains("MOQ_AUTH_KEY"), "{err}"); @@ -920,7 +920,7 @@ max_streams = 64 if !test_env::Env::without(&["MOQ_AUTH_KEY"]).isolate() { return; } - let err = Config::parse_and_merge(["moq-relay", "--auth-public", "anon/**", "--auth-key", "root.jwk"]) + let err = Config::parse_and_merge(["moq-relay", "--auth-public", "anon", "--auth-key", "root.jwk"]) .expect_err("must refuse") .to_string(); assert!(err.contains("--auth-key"), "{err}"); @@ -1135,6 +1135,9 @@ max_streams = 64 if path.file_name().unwrap() == "smoke-legacy.toml" { let error = result.expect_err("the published legacy fixture must be refused"); assert!(error.to_string().contains("[server]"), "{}: {error}", path.display()); + } else if path.file_name().unwrap() == "smoke-patterns.toml" { + let error = result.expect_err("the published pattern fixture must be refused"); + assert!(error.to_string().contains("wildcard"), "{}: {error}", path.display()); } else { result.unwrap_or_else(|error| panic!("{}: {error}", path.display())); } @@ -1749,7 +1752,7 @@ ws = false ]); } - /// The variables the public patterns could also be read from. + /// The variables the public prefixes could also be read from. const PUBLIC_ENV: &[&str] = &[ "MOQ_AUTH_PUBLIC", "MOQ_AUTH_PUBLIC_SUBSCRIBE", @@ -1757,10 +1760,9 @@ ws = false "MOQ_AUTH_URL", ]; - /// The public patterns arrive from the CLI and TOML alike, as patterns rather - /// than prefixes. + /// The public prefixes arrive from the CLI and TOML alike. #[test] - fn public_patterns_merge_from_every_source() { + fn public_prefixes_merge_from_every_source() { if !test_env::Env::without(PUBLIC_ENV).isolate() { return; } @@ -1768,9 +1770,9 @@ ws = false let config = Config::parse_and_merge([ "moq-relay", "--auth-public-subscribe", - "demo/**", + "demo", "--auth-public-publish", - "uploads/**", + "uploads", ]) .expect("config load"); @@ -1778,8 +1780,8 @@ ws = false config.auth.validate(false).is_ok(), "CLI public flags must admit anonymous sessions" ); - assert_eq!(config.auth.public_subscribe, vec!["demo/**".parse().unwrap()]); - assert_eq!(config.auth.public_publish, vec!["uploads/**".parse().unwrap()]); + assert_eq!(config.auth.public_subscribe, vec!["demo".parse().unwrap()]); + assert_eq!(config.auth.public_publish, vec!["uploads".parse().unwrap()]); assert!( config .source("auth.public_subscribe") @@ -1788,20 +1790,20 @@ ws = false config.source("auth.public_subscribe") ); - let toml = "[auth]\npublic = \"anon/**\"\n"; + let toml = "[auth]\npublic = \"anon\"\n"; let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("auth-public-toml.toml"); std::fs::write(&path, toml).unwrap(); let args = vec![std::ffi::OsString::from("moq-relay"), std::ffi::OsString::from(&path)]; let config = Config::parse_and_merge(args).expect("toml load"); - assert_eq!(config.auth.public, vec!["anon/**".parse().unwrap()]); + assert_eq!(config.auth.public, vec!["anon".parse().unwrap()]); } - /// The environment carries a list of public patterns, comma separated. + /// The environment carries a list of public prefixes, comma separated. #[test] - fn public_patterns_merge_from_the_environment() { + fn public_prefixes_merge_from_the_environment() { if !test_env::Env::without(PUBLIC_ENV) - .set("MOQ_AUTH_PUBLIC_SUBSCRIBE", "from-env/**,other/**") + .set("MOQ_AUTH_PUBLIC_SUBSCRIBE", "from-env,other") .isolate() { return; @@ -1809,7 +1811,76 @@ ws = false let config = Config::parse_and_merge(["moq-relay"]).expect("env load"); assert_eq!( config.auth.public_subscribe, - vec!["from-env/**".parse().unwrap(), "other/**".parse().unwrap()] + vec!["from-env".parse().unwrap(), "other".parse().unwrap()] + ); + } + + /// The public rule granting everything, as each source spells the empty prefix. + fn everything(config: &Config) -> Option<(moq_auth::Prefixes, moq_auth::Prefixes)> { + let grant = config.auth.public_grant()?; + Some((grant.publish.path, grant.subscribe.path)) + } + + /// "Anyone may do anything" is the empty prefix, which the CLI and TOML both spell. + #[test] + fn the_empty_public_prefix_covers_everything() { + if !test_env::Env::without(PUBLIC_ENV).isolate() { + return; + } + let all = Some((moq_auth::Prefixes::all(), moq_auth::Prefixes::all())); + + for args in [ + &["moq-relay", "--auth-public", ""][..], + &["moq-relay", "--auth-public="], + ] { + let config = Config::parse_and_merge(args.iter().copied()).expect("cli load"); + assert_eq!(everything(&config), all, "{args:?}"); + } + + let dir = tempfile::tempdir().unwrap(); + for (name, body) in [ + ("string.toml", "[auth]\npublic = \"\"\n"), + ("list.toml", "[auth]\npublic = [\"\"]\n"), + ] { + let path = dir.path().join(name); + std::fs::write(&path, body).unwrap(); + let args = [std::ffi::OsString::from("moq-relay"), std::ffi::OsString::from(&path)]; + let config = Config::parse_and_merge(args).expect("toml load"); + assert_eq!(everything(&config), all, "{body}"); + } + } + + /// The environment spells the empty prefix as a set but empty variable. + #[test] + fn the_empty_public_prefix_from_the_environment() { + if !test_env::Env::without(PUBLIC_ENV).set("MOQ_AUTH_PUBLIC", "").isolate() { + return; + } + let config = Config::parse_and_merge(["moq-relay"]).expect("env load"); + assert_eq!( + everything(&config), + Some((moq_auth::Prefixes::all(), moq_auth::Prefixes::all())) + ); + } + + /// A wildcard public rule is refused where it is read, from every source, naming the + /// prefix to write instead. + #[test] + fn a_wildcard_public_rule_is_refused_from_every_source() { + if !test_env::Env::without(PUBLIC_ENV).isolate() { + return; + } + let err = Config::parse_and_merge(["moq-relay", "--auth-public", "anon/**"]).unwrap_err(); + assert!(format!("{err:#}").contains("`anon` covers `anon`"), "{err:#}"); + + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("wildcard.toml"); + std::fs::write(&path, "[auth]\npublic = \"**\"\n").unwrap(); + let args = [std::ffi::OsString::from("moq-relay"), std::ffi::OsString::from(&path)]; + let err = Config::parse_and_merge(args).unwrap_err(); + assert!( + format!("{err:#}").contains("the empty prefix covers every path"), + "{err:#}" ); } diff --git a/rs/moq-relay/src/http_client.rs b/rs/moq-relay/src/http_client.rs index 37762658b8a..b214e91d147 100644 --- a/rs/moq-relay/src/http_client.rs +++ b/rs/moq-relay/src/http_client.rs @@ -156,7 +156,7 @@ mod tests { return None; }; let request = read_request(&mut stream).await; - let body = r#"{"publish":["**"],"subscribe":["**"]}"#; + let body = r#"{"publish":[""],"subscribe":[""]}"#; let response = format!( "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len() diff --git a/rs/moq-relay/src/session.rs b/rs/moq-relay/src/session.rs index 2bfaea4841a..10656ee5cce 100644 --- a/rs/moq-relay/src/session.rs +++ b/rs/moq-relay/src/session.rs @@ -13,7 +13,8 @@ use std::sync::{Arc, Mutex, OnceLock, PoisonError}; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; -use moq_auth::{Pattern, Peer, Request, Role, Transport}; +use moq_auth::{Peer, Request, Role, Transport}; +use moq_net::Pattern; use serde::{Deserialize, Serialize}; use serde_with::serde_as; diff --git a/rs/moq-relay/src/shutdown.rs b/rs/moq-relay/src/shutdown.rs index f85e385ecd2..f1c9c6ebc0a 100644 --- a/rs/moq-relay/src/shutdown.rs +++ b/rs/moq-relay/src/shutdown.rs @@ -585,7 +585,7 @@ mod tests { config.listen.tls.generate = vec!["localhost".into()]; config.web.ws = true; config.web.http.listen = Some("127.0.0.1:0".parse().unwrap()); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.drain_timeout = Some((window).into()); let relay = crate::Relay::load(config).await.unwrap().with_signals(false); let addr = relay.web_addrs().http.unwrap(); @@ -704,7 +704,7 @@ mod tests { }); let admission = admissions.next().await.unwrap(); trigger.start(); - let all = moq_auth::Pattern::all; + let all = moq_auth::Prefix::all; let (_lease, consumer) = moq_auth::lease::Producer::new(moq_auth::Grant::new(all(), all())); admission.grant(consumer); // The relay accepts the session and drains it at once, so the one-shot loop may already diff --git a/rs/moq-relay/src/test_support.rs b/rs/moq-relay/src/test_support.rs index 91de5f8704e..47b70f0f85b 100644 --- a/rs/moq-relay/src/test_support.rs +++ b/rs/moq-relay/src/test_support.rs @@ -33,7 +33,7 @@ pub async fn test_relay() -> anyhow::Result { .collect(); config.listen.tls.generate = vec!["localhost".into()]; config.web.http.listen = Some("127.0.0.1:0".parse()?); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = Relay::load(config).await?.with_signals(false); let quic = relay.quic_addr()?; diff --git a/rs/moq-relay/src/web.rs b/rs/moq-relay/src/web.rs index dba0efcbcf7..47fc5f6b2b2 100644 --- a/rs/moq-relay/src/web.rs +++ b/rs/moq-relay/src/web.rs @@ -1241,7 +1241,7 @@ mod tests { let (_, cert, _) = make_certs(&dir); let certificates = moq_tokio::tls::Certificates::from_pem(&std::fs::read(cert).unwrap()).unwrap(); let auth = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -1340,7 +1340,7 @@ mod tests { for publish_only in [false, true] { let auth = if publish_only { crate::auth::Config { - public_publish: vec![moq_auth::Pattern::all()], + public_publish: vec![moq_auth::Prefix::all()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -1472,7 +1472,7 @@ mod tests { for _ in 0..6 { let admission = admissions.next().await.unwrap(); assert_eq!(admission.request.transport, moq_auth::Transport::Http); - let mut grant = moq_auth::Grant::new(moq_auth::Patterns::new(), moq_auth::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_auth::Prefixes::new(), moq_auth::Prefix::all()); grant.tier = Some( admission .request @@ -1598,7 +1598,7 @@ mod tests { let (_, cert, _) = make_certs(&dir); let certificates = moq_tokio::tls::Certificates::from_pem(&std::fs::read(cert).unwrap()).unwrap(); let auth = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -1660,7 +1660,7 @@ mod tests { for _ in 0..2 { let admission = admissions.next().await.unwrap(); // Publish-only: admitted by auth, but forbidden from reading. - let grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Patterns::new()); + let grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefixes::new()); let (producer, consumer) = moq_auth::lease::Producer::new(grant); leases.push(producer); admission.grant(consumer); @@ -2008,7 +2008,7 @@ mod tests { // The probed route is the test's own, so auth never runs; it just has to be // configured with something for `Web` to build. let auth_config = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() }; let auth = auth_config @@ -2129,8 +2129,8 @@ mod tests { "WebSocket, tls={tls}" ); admission.grant(moq_auth::lease::Consumer::fixed(moq_auth::Grant::new( - moq_auth::Pattern::all(), - moq_auth::Pattern::all(), + moq_auth::Prefix::all(), + moq_auth::Prefix::all(), ))); drop(client.await.expect("the WebSocket session connects")); } @@ -2153,7 +2153,7 @@ mod tests { config.web.https.listen = Some("127.0.0.1:0".parse().unwrap()); config.web.https.cert = vec![cert]; config.web.https.key = vec![key]; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = crate::Relay::load(config).await.unwrap().with_signals(false); let https = relay.web_addrs().https.unwrap(); let ready = relay.ready(); @@ -2229,7 +2229,7 @@ mod tests { config.web.https.key = vec![key.clone()]; // A client CA needs a server to grant certificates; this one grants them everything. let mut policy = moq_auth::serve::Policy::default(); - policy.mtls = moq_auth::Permissions::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + policy.mtls = moq_auth::Permissions::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); let auth = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); config.auth.url = Some(format!("http://{}/", auth.local_addr().unwrap()).parse().unwrap()); let auth_server = moq_auth::serve::Server::new(policy).unwrap(); @@ -2325,7 +2325,7 @@ mod tests { config.https.cert = vec![cert.clone()]; config.https.key = vec![key.clone()]; let auth = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -2427,7 +2427,7 @@ mod tests { let mut config = Config::default(); config.http.listen = Some("127.0.0.1:0".parse().unwrap()); let auth = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() } .init("test", &moq_tokio::tls::Connect::default(), false) @@ -2486,7 +2486,7 @@ mod tests { #[tokio::test] async fn unreachable_timeout_waits_forever() { let auth_config = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() }; let auth = auth_config @@ -2512,7 +2512,7 @@ mod tests { let mut config = Config::default(); config.http.listen = Some("127.0.0.1:0".parse().unwrap()); let auth_config = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() }; let auth = auth_config @@ -2656,7 +2656,7 @@ mod tests { config.listen.tcp.bind = Some("127.0.0.1:0".parse().unwrap()); config.web.http.listen = Some("127.0.0.1:0".parse().unwrap()); config.web.ws = true; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.quic.receive_window = Some(3 << 20); config.quic.stream_receive_window = Some(384 << 10); config.quic.max_streams = Some(17); @@ -2724,7 +2724,7 @@ mod tests { config.http.listen = Some("127.0.0.1:0".parse().unwrap()); config.ws = true; let auth_config = crate::auth::Config { - public_subscribe: vec![moq_auth::Pattern::all()], + public_subscribe: vec![moq_auth::Prefix::all()], ..Default::default() }; let auth = auth_config @@ -2878,7 +2878,7 @@ mod tests { config.listen.tcp.bind = Some("127.0.0.1:0".parse().unwrap()); config.listen.timeout = Some((std::time::Duration::ZERO).into()); config.web.ws = true; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.drain_timeout = Some((DRAIN_WINDOW).into()); if tls { config.web.https.listen = Some("127.0.0.1:0".parse().unwrap()); diff --git a/rs/moq-relay/tests/drills.rs b/rs/moq-relay/tests/drills.rs index 9d1ffc2149a..c7f004add7e 100644 --- a/rs/moq-relay/tests/drills.rs +++ b/rs/moq-relay/tests/drills.rs @@ -285,7 +285,7 @@ fn relay_config() -> Config { let mut config = Config::default(); config.listen.bind = Some("127.0.0.1:0".parse().unwrap()); config.listen.tls.generate = vec!["localhost".into()]; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config } diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index cf8c18ec193..2d11f3b409d 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -21,7 +21,7 @@ use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use axum::routing::post; use axum::{Json, Router}; -use moq_auth::{Event, Grant, Pattern, Patterns, Request}; +use moq_auth::{Event, Grant, Prefixes, Request}; use moq_net::stats; use moq_relay::web; use moq_relay::{Config, Connection, Relay, auth, cluster}; @@ -168,8 +168,8 @@ impl Script { } } -fn all() -> Patterns { - [Pattern::all()].into_iter().collect() +fn all() -> Prefixes { + Prefixes::all() } /// How often [`grant`] asks to be re-checked. @@ -942,7 +942,7 @@ async fn a_rechecked_grant_resizes_live_sessions() { // Deafen alice's audio: everything else stays as admitted. let mut narrow = grant(Duration::from_secs(3600)); - narrow.subscribe.path = ["alice/video/**".parse().unwrap()].into_iter().collect(); + narrow.subscribe.path = ["alice/video".parse().unwrap()].into_iter().collect(); script.on_revalidate(Answer::Grant(narrow)); clock.advance(CADENCE); @@ -1376,10 +1376,10 @@ async fn a_certificate_admits_only_what_the_server_grants() { assert_refused(&format!("ws://127.0.0.1:{port}/room").parse().unwrap()).await; relay.abort(); - // A narrow grant: the certificate publishes under `mine/**` and nothing else. + // A narrow grant: the certificate publishes under `mine` and nothing else. let narrow = serve(policy(moq_auth::Permissions::new( - "mine/**".parse::().unwrap(), - Patterns::new(), + "mine".parse::().unwrap(), + Prefixes::new(), ))) .await; let cluster = crate::spawn_cluster(cluster::Options::default()); @@ -1915,7 +1915,7 @@ async fn quic_and_webtransport_report_distinct_auth_transports() { #[tokio::test] async fn in_band_tokens_widen_and_withdraw_live_prefixes() { for scheme in ["tcp", "ws"] { - let mut initial = Grant::new(all(), "a/**".parse::().unwrap()); + let mut initial = Grant::new(all(), "a".parse::().unwrap()); initial.expires = Some(kio::clock::wall() + Duration::from_secs(3600)); let script = Script::new(initial); let auth = build_auth(script.spawn().await); @@ -1965,7 +1965,7 @@ async fn in_band_tokens_widen_and_withdraw_live_prefixes() { .unwrap() .unwrap(); let mut a_sub = a_read.track("video").unwrap().subscribe(None).await.unwrap(); - let mut replacement = Grant::new(all(), "b/**".parse::().unwrap()); + let mut replacement = Grant::new(all(), "b".parse::().unwrap()); replacement.expires = Some(kio::clock::wall() + Duration::from_secs(3600)); script.on_connect(Answer::Grant(replacement)); let token = subscriber.auth().add("replacement").unwrap(); @@ -2248,7 +2248,7 @@ async fn websocket_refusal_is_in_band() { /// The role seen at SETUP reaches the shared admission gate on the WebSocket path. #[tokio::test] async fn websocket_publisher_role_is_refused_by_a_subscribe_only_grant() { - let script = Script::new(Grant::new(Patterns::new(), all())); + let script = Script::new(Grant::new(Prefixes::new(), all())); let (port, relay) = spawn_ws_relay(build_auth(script.spawn().await)).await; let url = room_url("ws", port); let origin = moq_tokio::origin::spawn(); diff --git a/rs/moq-relay/tests/integration/bitrate_caps.rs b/rs/moq-relay/tests/integration/bitrate_caps.rs index 6308ea184af..d42df03655e 100644 --- a/rs/moq-relay/tests/integration/bitrate_caps.rs +++ b/rs/moq-relay/tests/integration/bitrate_caps.rs @@ -25,7 +25,7 @@ const LEAST: Duration = Duration::from_millis(700); /// A grant of everything, with `publish` and `subscribe` caps. fn grant(publish: Option, subscribe: Option) -> moq_auth::Grant { - let all: moq_auth::Patterns = moq_auth::Pattern::all().into(); + let all = moq_auth::Prefixes::all(); let mut grant = moq_auth::Grant::new(all.clone(), all); grant.publish.rate = publish.map(|bps| moq_auth::Rate::from_bps(bps).unwrap()); grant.subscribe.rate = subscribe.map(|bps| moq_auth::Rate::from_bps(bps).unwrap()); diff --git a/rs/moq-relay/tests/integration/cluster_close.rs b/rs/moq-relay/tests/integration/cluster_close.rs index dbe17cc6de8..def71c73c5a 100644 --- a/rs/moq-relay/tests/integration/cluster_close.rs +++ b/rs/moq-relay/tests/integration/cluster_close.rs @@ -134,7 +134,7 @@ async fn a_relay_shutdown_closes_its_cluster_dials() { config.connect = connect; config.quic.idle_timeout = quic.idle_timeout; config.cluster.connect = vec![moq_relay::cluster::Peer::new(peer_url(&server))]; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let runtime = dialer_runtime(); let (trigger, running) = runtime diff --git a/rs/moq-relay/tests/integration/cluster_drain.rs b/rs/moq-relay/tests/integration/cluster_drain.rs index 40cb469e11e..a0af0a3bd3c 100644 --- a/rs/moq-relay/tests/integration/cluster_drain.rs +++ b/rs/moq-relay/tests/integration/cluster_drain.rs @@ -79,7 +79,7 @@ impl Relay { .route("b", moq_net::origin::RouteOptions::default()) .expect("create broadcast"); let (trigger, observer) = shutdown::Observer::new(DRAIN); - let _accepts = net.relay("b", &cluster, &memory::public(&["**"], &[], &[]), &observer); + let _accepts = net.relay("b", &cluster, &memory::public(&[""], &[], &[]), &observer); let net = net.clone(); let exits = owned.clone(); let exited = kio::clock::sim::spawn(async move { diff --git a/rs/moq-relay/tests/integration/cluster_unknown.rs b/rs/moq-relay/tests/integration/cluster_unknown.rs index 8e1c26ce908..f27e32f1da2 100644 --- a/rs/moq-relay/tests/integration/cluster_unknown.rs +++ b/rs/moq-relay/tests/integration/cluster_unknown.rs @@ -35,7 +35,7 @@ impl Mesh { /// version the relays dial each other with. async fn start(cluster_version: Option) -> Self { let net = Net::new(); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let mut relays = Vec::new(); for (name, peers) in [("a", &[][..]), ("b", &["a"]), ("c", &["a", "b"]), ("d", &["c"])] { let cluster = memory::spawn_cluster(net.options(name, peers)); diff --git a/rs/moq-relay/tests/integration/connection.rs b/rs/moq-relay/tests/integration/connection.rs index 8f18e54fb88..40351818f0c 100644 --- a/rs/moq-relay/tests/integration/connection.rs +++ b/rs/moq-relay/tests/integration/connection.rs @@ -17,7 +17,7 @@ const TIMEOUT: Duration = Duration::from_secs(60); async fn largest_quic_sequence_reaches_the_subscriber() { for alpn in ["moq-lite-02", "moq-lite-06", "moq-transport-22"] { let version = alpn.parse().unwrap(); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::cluster(); let publisher = sim::origin(); let broadcast = publisher @@ -153,7 +153,7 @@ async fn assert_refused(result: Result) { async fn a_session_declaring_the_relays_own_hop_is_refused() { for alpn in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { let version: moq_net::Version = alpn.parse().unwrap(); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::cluster(); if alpn != "moq-lite-07-wip" { let client = moq_net::Client::new().with_publisher(cluster.origin.consume()); @@ -193,7 +193,7 @@ async fn a_relay_dialing_itself_on_lite07_is_refused_inner() { use futures::StreamExt; let net = sim::Net::new(); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::spawn_cluster(net.options("self", &["self"])); let (_trigger, shutdown) = moq_relay::shutdown::Observer::new(TIMEOUT); let mut accepts = net.relay("self", &cluster, &auth, &shutdown); @@ -214,7 +214,7 @@ async fn a_relay_dialing_itself_on_lite07_is_refused_inner() { /// says it publishes. #[kio::test] async fn subscribe_only_public_rejects_publisher_role() { - let auth = memory::public(&[], &[], &["**"]); + let auth = memory::public(&[], &[], &[""]); let (origin, _broadcast, _track) = publish("cam"); let client = moq_net::Client::new().with_publisher(origin.consume()); assert_refused(connect(client, newest(), &memory::cluster(), &auth).await).await; @@ -223,7 +223,7 @@ async fn subscribe_only_public_rejects_publisher_role() { /// The same grant keeps a subscriber: the role gate refuses only the mismatched direction. #[kio::test] async fn subscribe_only_public_accepts_subscriber_role() { - let auth = memory::public(&[], &[], &["**"]); + let auth = memory::public(&[], &[], &[""]); let client = moq_net::Client::new().with_subscriber(sim::origin()); let session = connect(client, newest(), &memory::cluster(), &auth) .await @@ -237,7 +237,7 @@ async fn subscribe_only_public_accepts_subscriber_role() { /// The mirror: a subscriber whose grant covers only publishing is refused. #[kio::test] async fn publish_only_public_rejects_subscriber_role() { - let auth = memory::public(&[], &["**"], &[]); + let auth = memory::public(&[], &[""], &[]); let client = moq_net::Client::new().with_subscriber(sim::origin()); assert_refused(connect(client, newest(), &memory::cluster(), &auth).await).await; } @@ -246,7 +246,7 @@ async fn publish_only_public_rejects_subscriber_role() { /// neither tears the other down. #[kio::test] async fn two_publish_only_clients_coexist() { - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::cluster(); let (alpha, _alpha_broadcast, _alpha_track) = publish("alpha"); @@ -294,7 +294,7 @@ async fn the_setup_path_reaches_the_decider_and_roots_the_grant() { let mut seen = Vec::new(); while let Some(admission) = admissions.next().await { seen.push(admission.request.clone()); - let all: moq_auth::Patterns = [moq_auth::Pattern::all()].into_iter().collect(); + let all = moq_auth::Prefixes::all(); admission.grant(moq_auth::lease::Consumer::fixed(moq_auth::Grant::new(all.clone(), all))); if seen.len() == 2 { return seen; @@ -371,7 +371,7 @@ async fn stats_count_admitted_and_refused_sessions() { .find(|(tier, _)| *tier == stats::Tier::default()) .map_or(stats::Presence::default(), |(_, presence)| presence) }; - let auth = memory::public(&[], &[], &["**"]); + let auth = memory::public(&[], &[], &[""]); let client = moq_net::Client::new().with_subscriber(sim::origin()); let session = connect(client, newest(), &cluster, &auth) diff --git a/rs/moq-relay/tests/integration/drain.rs b/rs/moq-relay/tests/integration/drain.rs index 3d6867fd350..f9a611da31f 100644 --- a/rs/moq-relay/tests/integration/drain.rs +++ b/rs/moq-relay/tests/integration/drain.rs @@ -51,7 +51,7 @@ async fn admit(observer: &shutdown::Observer, version: moq_net::Version, stalled .await; })); - let all = moq_auth::Pattern::all; + let all = moq_auth::Prefix::all; let (lease, consumer) = lease::Producer::new(moq_auth::Grant::new(all(), all())); drop(kio::clock::sim::spawn(async move { admissions.next().await.expect("an admission").grant(consumer); diff --git a/rs/moq-relay/tests/integration/drain_dialed.rs b/rs/moq-relay/tests/integration/drain_dialed.rs index 5ff592d1a9a..0699889d10c 100644 --- a/rs/moq-relay/tests/integration/drain_dialed.rs +++ b/rs/moq-relay/tests/integration/drain_dialed.rs @@ -58,7 +58,7 @@ impl Node { tokio::spawn(async move { let mut leases = Vec::new(); while let Some(admission) = admissions.next().await { - let mut grant = moq_auth::Grant::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + let mut grant = moq_auth::Grant::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); grant.link = Some(moq_auth::Link::Peer); let (lease, consumer) = moq_auth::lease::Producer::new(grant); admission.grant(consumer); diff --git a/rs/moq-relay/tests/integration/embed.rs b/rs/moq-relay/tests/integration/embed.rs index 7dc134c59b8..e20c7178ae2 100644 --- a/rs/moq-relay/tests/integration/embed.rs +++ b/rs/moq-relay/tests/integration/embed.rs @@ -42,7 +42,7 @@ fn certificate(dir: &std::path::Path) -> (std::path::PathBuf, std::path::PathBuf } fn public_auth(config: &mut Config) { - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; } fn client() -> moq_tokio::Client { diff --git a/rs/moq-relay/tests/integration/goaway_cluster.rs b/rs/moq-relay/tests/integration/goaway_cluster.rs index 010ceb89e27..cbeb9fab6cb 100644 --- a/rs/moq-relay/tests/integration/goaway_cluster.rs +++ b/rs/moq-relay/tests/integration/goaway_cluster.rs @@ -368,7 +368,7 @@ async fn cluster_migrates_on_upstream_goaway_inner() { /// retracts under the subscriber. async fn cluster_diamond_goaway_seamless_failover_inner() { let net = Net::new(); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let mut lite07 = dialing(); lite07.version = vec![LITE_07.parse().unwrap()]; diff --git a/rs/moq-relay/tests/integration/handoff.rs b/rs/moq-relay/tests/integration/handoff.rs index 29b7b30511f..bd5fdca4fd9 100644 --- a/rs/moq-relay/tests/integration/handoff.rs +++ b/rs/moq-relay/tests/integration/handoff.rs @@ -53,7 +53,7 @@ fn config(dir: &Path, cert: &Path, key: &Path, backend: Backend) -> Config { config.internal.listen = Some("127.0.0.1:0".parse().unwrap()); config.runtime.workers = moq_relay::runtime::Workers::Count(std::num::NonZeroU16::new(2).unwrap()); config.runtime.pin = false; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.stats.enabled = true; config.stats.node = Some("test".into()); config.handoff.socket = Some(dir.join("handoff.sock")); @@ -410,7 +410,7 @@ fn stand_in_draining(dir: &Path, cert: &Path, key: &Path, notify: &Path, drain: .env("MOQ_LISTEN_TLS_CERT", cert) .env("MOQ_LISTEN_TLS_KEY", key) .env("MOQ_RUNTIME_WORKERS", "1") - .env("MOQ_AUTH_PUBLIC", "**") + .env("MOQ_AUTH_PUBLIC", "") .env("MOQ_HANDOFF_SOCKET", dir.join("handoff.sock")) .env("NOTIFY_SOCKET", notify) .env("MOQ_DRAIN_TIMEOUT", drain) @@ -472,7 +472,7 @@ fn relay(dir: &Path, cert: &Path, key: &Path, port: u16, notify: &Path) -> tokio .env("MOQ_QUIC_IDLE_TIMEOUT", "600s") .env("MOQ_DRAIN_TIMEOUT", "600s") .env("MOQ_RUNTIME_WORKERS", "2") - .env("MOQ_AUTH_PUBLIC", "**") + .env("MOQ_AUTH_PUBLIC", "") .env("MOQ_HANDOFF_SOCKET", dir.join("handoff.sock")) .env("NOTIFY_SOCKET", notify) .kill_on_drop(true) diff --git a/rs/moq-relay/tests/integration/implementation.rs b/rs/moq-relay/tests/integration/implementation.rs index 54ad586711a..6502b231fba 100644 --- a/rs/moq-relay/tests/integration/implementation.rs +++ b/rs/moq-relay/tests/integration/implementation.rs @@ -36,7 +36,7 @@ async fn admit_next(admissions: &mut moq_relay::auth::Admissions) -> Option /dev/null //! ``` diff --git a/rs/moq-relay/tests/integration/memory.rs b/rs/moq-relay/tests/integration/memory.rs index feca46f991f..cd1bc740abe 100644 --- a/rs/moq-relay/tests/integration/memory.rs +++ b/rs/moq-relay/tests/integration/memory.rs @@ -54,11 +54,11 @@ pub async fn connect( /// `public_subscribe` to anyone, as the `[auth]` config does. #[cfg(feature = "test-support")] pub fn public(public: &[&str], publish: &[&str], subscribe: &[&str]) -> auth::Auth { - let patterns = |texts: &[&str]| texts.iter().map(|text| text.parse().expect("pattern")).collect(); + let prefixes = |texts: &[&str]| texts.iter().map(|text| text.parse().expect("prefix")).collect(); let mut config = auth::Config::default(); - config.public = patterns(public); - config.public_publish = patterns(publish); - config.public_subscribe = patterns(subscribe); + config.public = prefixes(public); + config.public_publish = prefixes(publish); + config.public_subscribe = prefixes(subscribe); config .init("test", &moq_tokio::tls::Connect::default(), false) .expect("auth init") diff --git a/rs/moq-relay/tests/integration/peer_limits.rs b/rs/moq-relay/tests/integration/peer_limits.rs index 771c4e74cc0..479d8eaf2e4 100644 --- a/rs/moq-relay/tests/integration/peer_limits.rs +++ b/rs/moq-relay/tests/integration/peer_limits.rs @@ -126,7 +126,7 @@ async fn peer_widens_and_viewer_does_not(mut relay: Relay, port: u16) { .await .expect("admission timeout") .expect("admission"); - let all: moq_auth::Patterns = [moq_auth::Pattern::all()].into_iter().collect(); + let all = moq_auth::Prefixes::all(); let mut grant = moq_auth::Grant::new(all.clone(), all); grant.link = peer.then_some(moq_auth::Link::Peer); let (producer, consumer) = moq_auth::lease::Producer::new(grant); diff --git a/rs/moq-relay/tests/integration/public_rules.rs b/rs/moq-relay/tests/integration/public_rules.rs index 3d4f9457619..66c79cfe72c 100644 --- a/rs/moq-relay/tests/integration/public_rules.rs +++ b/rs/moq-relay/tests/integration/public_rules.rs @@ -2,10 +2,10 @@ //! relay: on the relay's own `--auth-public` and on `moq auth serve --public-*` //! behind `--auth-url`. //! -//! Every other test grants bare `**`, which reads the same whether rooted at `/` or -//! at the dialed path. These use `anon/**` and `event/**`, which do not: rooted at -//! the dialed path, `anon/**` at `/anon` meant `anon/anon/**`, and at `/rooms/123` -//! it let an anonymous client into `rooms/123/anon/**`. +//! Every other test grants the empty prefix, which reads the same whether rooted at `/` +//! or at the dialed path. These use `anon` and `event`, which do not: rooted at the +//! dialed path, `anon` at `/anon` meant `anon/anon`, and at `/rooms/123` it let an +//! anonymous client into `rooms/123/anon`. use std::time::Duration; @@ -162,7 +162,7 @@ async fn assert_refused(url: url::Url) { fn anon() -> auth::Config { let mut config = auth::Config::default(); - config.public = vec!["anon/**".parse().unwrap()]; + config.public = vec!["anon".parse().unwrap()]; config } @@ -174,23 +174,25 @@ async fn anon_rules_admit_under_anon_and_refuse_elsewhere() { assert_eq!(first_broadcast(url(port, "/")).await, "anon/test.hang"); assert_eq!(first_broadcast(url(port, "/anon")).await, "test.hang"); - // Rooted at the dialed path, this was `rooms/123/anon/**`. + // Rooted at the dialed path, this was `rooms/123/anon`. assert_refused(url(port, "/rooms/123")).await; assert_refused(url(port, "/other")).await; relay.abort(); } -#[tokio::test] -async fn a_leading_wildcard_does_not_reach_into_rooms() { - let mut config = auth::Config::default(); - config.public_subscribe = vec!["*".parse().unwrap()]; - let (port, relay) = spawn_relay(config).await; - - // Rooted at the dialed path, `*` at `/rooms/123` was every broadcast in the room. - assert_refused(url(port, "/rooms/123")).await; - - relay.abort(); +/// A public rule is a prefix: a wildcard is refused where it is parsed, naming the +/// prefix to write instead, rather than read as a narrower or wider grant. +#[test] +fn a_wildcard_public_rule_is_refused() { + for (rule, hint) in [ + ("anon/**", "`anon` covers"), + ("**", "the empty prefix"), + ("*", "wildcard"), + ] { + let err = rule.parse::().unwrap_err().to_string(); + assert!(err.contains(hint), "{rule}: {err}"); + } } #[tokio::test] @@ -223,9 +225,9 @@ async fn http_routes_follow_the_anon_rules() { relay.abort(); } -/// The reported upgrade: `moq auth serve --public-subscribe 'event/**'` behind +/// The reported upgrade: `moq auth serve --public-subscribe event` behind /// `--auth-url`, with a viewer at `/event` watching a camera published under a -/// `root=event` token. Rooted at the dialed path, the viewer saw `event/event/**`. +/// `root=event` token. Rooted at the dialed path, the viewer saw `event/event`. #[tokio::test] async fn auth_server_public_rules_are_rooted_at_slash() { let dir = tempfile::tempdir().expect("tempdir"); @@ -235,10 +237,7 @@ async fn auth_server_public_rules_are_rooted_at_slash() { let mut policy = moq_auth::serve::Policy::default(); policy.keys = Some(moq_auth::serve::Keys::File(key_path)); - policy.public = moq_auth::Permissions::new( - moq_auth::Patterns::new(), - "event/**".parse::().unwrap(), - ); + policy.public = moq_auth::Permissions::new(moq_auth::Prefixes::new(), "event".parse::().unwrap()); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let server_url: url::Url = format!("http://{}/", listener.local_addr().unwrap()).parse().unwrap(); let server = moq_auth::serve::Server::new(policy).unwrap(); @@ -250,7 +249,7 @@ async fn auth_server_public_rules_are_rooted_at_slash() { let claims = moq_auth::Claims::default() .with_root("event") - .with_publish(["**".parse().unwrap()]); + .with_publish(["".parse().unwrap()]); let jwt = key.sign(&claims).expect("sign"); let mut camera = url(port, "/event"); camera.query_pairs_mut().append_pair("jwt", &jwt); @@ -318,7 +317,7 @@ async fn an_issued_root_with_an_escaped_separator_is_dialable() { config.url = Some(server_url); let (port, relay) = spawn_relay(config).await; - let all = || ["**".parse::().unwrap()]; + let all = || ["".parse::().unwrap()]; let refused = moq_auth::Claims::default().with_root(r"cam\//1").with_publish(all()); assert!(key.sign(&refused).is_err(), "issued a root moq_net::Path::new refuses"); diff --git a/rs/moq-relay/tests/integration/quic_sim.rs b/rs/moq-relay/tests/integration/quic_sim.rs index f130593f1fc..9e0c81d7915 100644 --- a/rs/moq-relay/tests/integration/quic_sim.rs +++ b/rs/moq-relay/tests/integration/quic_sim.rs @@ -38,7 +38,7 @@ fn one_relay() { for seed in 0..2 { let network = Network::new(seed); let relay = SocketAddr::new(ip(1), PORT); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::start(memory::cluster(), moq_tokio::connect::Config::default()).await; let relay = serve(&network, relay, &cluster, &auth).await; for client in [PUBLISHER, SUBSCRIBER] { @@ -59,7 +59,7 @@ fn one_relay() { fn two_relays() { memory::run(|| async { let network = Network::new(1); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster_a = memory::start(memory::cluster(), moq_tokio::connect::Config::default()).await; let a = serve(&network, SocketAddr::new(ip(1), PORT), &cluster_a, &auth).await; @@ -238,7 +238,7 @@ const CHUNK: usize = 8 * 1024; fn a_group_carries_a_bulk_transfer_losslessly() { memory::run(|| async { let network = Network::new(0); - let auth = memory::public(&["**"], &[], &[]); + let auth = memory::public(&[""], &[], &[]); let cluster = memory::start(memory::cluster(), moq_tokio::connect::Config::default()).await; let relay = serve(&network, SocketAddr::new(ip(1), PORT), &cluster, &auth).await; diff --git a/rs/moq-relay/tests/integration/refusals.rs b/rs/moq-relay/tests/integration/refusals.rs index 45b78aafd57..0bd7473823b 100644 --- a/rs/moq-relay/tests/integration/refusals.rs +++ b/rs/moq-relay/tests/integration/refusals.rs @@ -140,7 +140,7 @@ async fn assert_refused(url: url::Url, publisher: bool) { async fn refused_sessions_are_counted() { let dir = tempfile::tempdir().expect("tempdir"); let mut config = config(dir.path(), false); - config.auth.public = vec!["anon/**".parse().unwrap()]; + config.auth.public = vec!["anon".parse().unwrap()]; let relay = start(config).await; assert_eq!(relay.refused("refused").await, 0); @@ -155,7 +155,7 @@ async fn refused_sessions_are_counted() { async fn websocket_refused_sessions_are_counted() { let dir = tempfile::tempdir().expect("tempdir"); let mut config = config(dir.path(), true); - config.auth.public = vec!["anon/**".parse().unwrap()]; + config.auth.public = vec!["anon".parse().unwrap()]; let relay = start(config).await; assert_refused(format!("ws://{}/rooms", relay.http).parse().unwrap(), false).await; @@ -168,7 +168,7 @@ async fn websocket_refused_sessions_are_counted() { async fn forbidden_sessions_are_counted() { let dir = tempfile::tempdir().expect("tempdir"); let mut config = config(dir.path(), false); - config.auth.public_subscribe = vec!["anon/**".parse().unwrap()]; + config.auth.public_subscribe = vec!["anon".parse().unwrap()]; let relay = start(config).await; assert_refused(format!("https://{}/anon", relay.quic).parse().unwrap(), true).await; @@ -183,7 +183,7 @@ async fn forbidden_sessions_are_counted() { async fn lan_refusals_are_counted() { let dir = tempfile::tempdir().expect("tempdir"); let mut config = config(dir.path(), false); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = start(config).await; assert_refused( @@ -236,7 +236,7 @@ async fn refused_only_stats_depth_one_has_root_totals_and_shared_embedder_metric use moq_net::stats::{RefusalKind, Tier}; let dir = tempfile::tempdir().unwrap(); let mut config = config(dir.path(), false); - config.auth.public = vec!["anon/**".parse().unwrap()]; + config.auth.public = vec!["anon".parse().unwrap()]; config.stats.enabled = true; config.stats.depth = 1; config.stats.node = Some("test".into()); @@ -303,7 +303,7 @@ async fn refused_only_stats_depth_one_has_root_totals_and_shared_embedder_metric async fn peer_zero_close_is_clean_and_application_close_counts_once() { let dir = tempfile::tempdir().unwrap(); let mut config = config(dir.path(), false); - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.stats.enabled = true; config.stats.node = Some("test".into()); let relay = start(config).await; @@ -383,7 +383,7 @@ async fn expired_verified_tokens_count_under_the_token_root_and_auth_tier() { key.to_file(&key_path).unwrap(); let mut claims = moq_auth::Claims::default() .with_root("verified/tenant") - .with_subscribe([moq_auth::Pattern::all()]); + .with_subscribe([moq_auth::Prefix::all()]); claims.expires = Some(kio::clock::Wall::UNIX_EPOCH + Duration::from_secs(1)); let token = key.sign(&claims).unwrap(); for tier in [None, Some("edge")] { diff --git a/rs/moq-relay/tests/integration/runtime_uring.rs b/rs/moq-relay/tests/integration/runtime_uring.rs index 15a477cbdc0..7408d7b92b0 100644 --- a/rs/moq-relay/tests/integration/runtime_uring.rs +++ b/rs/moq-relay/tests/integration/runtime_uring.rs @@ -106,7 +106,7 @@ fn uring_config(cert: &std::path::Path, key: &std::path::Path) -> Config { config.runtime.workers = moq_relay::runtime::Workers::Count(std::num::NonZeroU16::new(WORKERS).unwrap()); config.runtime.pin = false; config.runtime.io_uring = true; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config } @@ -437,7 +437,7 @@ async fn drain_on_the_trigger(version: Option) { let connecting = tokio::spawn(connect(client.clone(), url.clone())); let first = admissions.next().await.expect("first admission"); - let all = moq_auth::Pattern::all; + let all = moq_auth::Prefix::all; let (_first_lease, first_consumer) = moq_auth::lease::Producer::new(moq_auth::Grant::new(all(), all())); first.grant(first_consumer); let established = connecting.await.expect("client task"); @@ -623,7 +623,7 @@ async fn workers_share_a_generated_identity() { config.runtime.workers = moq_relay::runtime::Workers::Count(std::num::NonZeroU16::new(WORKERS).unwrap()); config.runtime.pin = false; config.runtime.io_uring = io_uring; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; let relay = Relay::load(config) .await .expect("generated worker identity") @@ -900,7 +900,7 @@ async fn uring_refusals_reach_metrics() { let dir = tempfile::tempdir().expect("tempdir"); let (cert, key) = certificate(dir.path()); let mut config = uring_config(&cert, &key); - config.auth.public = vec!["anon/**".parse().unwrap()]; + config.auth.public = vec!["anon".parse().unwrap()]; config.internal.listen = Some("127.0.0.1:0".parse().unwrap()); let relay = Relay::load(config).await.expect("load relay").with_signals(false); let port = relay.addr().expect("workers bound an address").port(); @@ -950,7 +950,7 @@ async fn uring_refusals_reach_metrics() { /// A policy admitting verified certificates and nobody else. fn mtls_only() -> moq_auth::serve::Policy { let mut policy = moq_auth::serve::Policy::default(); - policy.mtls = moq_auth::Permissions::new(moq_auth::Pattern::all(), moq_auth::Pattern::all()); + policy.mtls = moq_auth::Permissions::new(moq_auth::Prefix::all(), moq_auth::Prefix::all()); policy } @@ -1015,7 +1015,7 @@ async fn uring_forwards_the_setup_token() { assert_eq!(admission.request.path, "/"); assert!(admission.request.query.is_none()); } - let all: moq_auth::Patterns = [moq_auth::Pattern::all()].into_iter().collect(); + let all = moq_auth::Prefixes::all(); admission.grant(moq_auth::lease::Consumer::fixed(moq_auth::Grant::new(all.clone(), all))); } }); @@ -1102,7 +1102,7 @@ async fn uring_auth_reports_distinct_quic_and_webtransport() { .as_ref() .is_some_and(|alpn| alpn.starts_with("moq-lite-")) ); - let all: moq_auth::Patterns = [moq_auth::Pattern::all()].into_iter().collect(); + let all = moq_auth::Prefixes::all(); let grant = moq_auth::Grant::new(all.clone(), all); let (producer, consumer) = moq_auth::lease::Producer::new(grant); leases.push(producer); diff --git a/rs/moq-relay/tests/integration/runtime_workers.rs b/rs/moq-relay/tests/integration/runtime_workers.rs index 056d19bca63..7c691ee98b9 100644 --- a/rs/moq-relay/tests/integration/runtime_workers.rs +++ b/rs/moq-relay/tests/integration/runtime_workers.rs @@ -50,7 +50,7 @@ fn worker_config(cert: &std::path::Path, key: &std::path::Path, workers: u16) -> config.listen.tls.key = vec![key.to_path_buf()]; config.runtime.workers = moq_relay::runtime::Workers::Count(std::num::NonZeroU16::new(workers).unwrap()); config.runtime.pin = false; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config } diff --git a/rs/moq-relay/tests/integration/session_revalidate.rs b/rs/moq-relay/tests/integration/session_revalidate.rs index 1242ce789c7..148dc974996 100644 --- a/rs/moq-relay/tests/integration/session_revalidate.rs +++ b/rs/moq-relay/tests/integration/session_revalidate.rs @@ -13,7 +13,7 @@ use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use axum::routing::post; use axum::{Json, Router}; -use moq_auth::{Event, Grant, Pattern, Patterns, Request}; +use moq_auth::{Event, Grant, Prefixes, Request}; use moq_relay::session::{Filter, List, Nudged}; use moq_relay::{Connection, auth, cluster, internal, web}; @@ -123,8 +123,8 @@ impl Script { } } -fn all() -> Patterns { - [Pattern::all()].into_iter().collect() +fn all() -> Prefixes { + Prefixes::all() } /// A grant that lasts an hour and would not re-check on its own during a test. diff --git a/rs/moq-relay/tests/integration/shutdown_signal.rs b/rs/moq-relay/tests/integration/shutdown_signal.rs index a80b6a84e35..1ab6c4aa93c 100644 --- a/rs/moq-relay/tests/integration/shutdown_signal.rs +++ b/rs/moq-relay/tests/integration/shutdown_signal.rs @@ -547,7 +547,7 @@ async fn connect(client: &moq_tokio::Client, port: u16) -> moq_tokio::Connection fn relay_config() -> Config { // Fully public auth: any no-JWT stream client gets the whole root. let mut auth = auth::Config::default(); - auth.public = vec![moq_auth::Pattern::all()]; + auth.public = vec![moq_auth::Prefix::all()]; let mut config = Config::default(); config.listen.tcp.bind = Some("127.0.0.1:0".parse().expect("parse addr")); diff --git a/rs/moq-relay/tests/integration/smoke.rs b/rs/moq-relay/tests/integration/smoke.rs index 3208206a778..d2e2aed4a65 100644 --- a/rs/moq-relay/tests/integration/smoke.rs +++ b/rs/moq-relay/tests/integration/smoke.rs @@ -58,9 +58,9 @@ async fn build_web_with(web_config: web::Config, cluster: cluster::Cluster) -> w // no-ops, but the test binary may run before any other moq code does. let _ = rind_rustls::default_provider().unwrap().install_default(); - // A public grant of `**` lets any path through. + // A public grant of the empty prefix lets any path through. let mut auth_config = auth::Config::default(); - auth_config.public = vec![moq_auth::Pattern::all()]; + auth_config.public = vec![moq_auth::Prefix::all()]; let auth = auth_config .init("test", &moq_tokio::tls::Connect::default(), false) .expect("auth init"); @@ -85,14 +85,14 @@ async fn spawn_relay() -> (u16, tokio::task::JoinHandle<()>) { /// Stand up the assembled relay path with `--server-version` restricted. async fn spawn_versioned_relay(versions: Vec) -> (u16, tokio::task::JoinHandle<()>) { - spawn_public_relay(versions, vec![moq_auth::Pattern::all()]).await + spawn_public_relay(versions, vec![moq_auth::Prefix::all()]).await } /// Stand up the assembled relay path with `--server-version` restricted, granting anonymous /// sessions only `public`. async fn spawn_public_relay( versions: Vec, - public: Vec, + public: Vec, ) -> (u16, tokio::task::JoinHandle<()>) { let mut config = Config::default(); config.listen.bind = Some("127.0.0.1:0".parse().unwrap()); @@ -386,17 +386,21 @@ async fn hidden_broadcasts_need_a_lite07_opt_in() { } /// A moq-transport namespace field holding a `/` is one path segment all the way through a -/// relay: a public grant of `room/*` covers `room/a\/b`, which it could not if the escaped `/` -/// split the field, and each wire reads back the same broadcast the other published. +/// relay: a public grant of `room/a\/b` covers that broadcast, which it could not if the +/// escaped `/` split the field, and each wire reads back the same broadcast the other published. #[tokio::test] async fn an_escaped_separator_crosses_the_relay_as_one_segment() { kio::clock::host_time(); // a real relay on real sockets let lite: moq_net::Version = "moq-lite-07-wip".parse().unwrap(); let ietf: moq_net::Version = "moq-transport-22".parse().unwrap(); - let (port, web_handle) = spawn_public_relay(vec![lite, ietf], vec!["room/*".parse().unwrap()]).await; + let fields = [(ietf, lite), (lite, ietf)]; + let public = (fields.iter()) + .map(|(publisher, subscriber)| format!(r"room/{publisher}\/{subscriber}").parse().unwrap()) + .collect(); + let (port, web_handle) = spawn_public_relay(vec![lite, ietf], public).await; let url: url::Url = format!("ws://127.0.0.1:{port}/").parse().expect("parse url"); - for (publisher, subscriber) in [(ietf, lite), (lite, ietf)] { + for (publisher, subscriber) in fields { // The field `/`, escaped into one segment. let path = moq_net::Path::new(&format!(r"room/{publisher}\/{subscriber}")).unwrap(); assert_eq!(path.parts().count(), 2); @@ -667,7 +671,7 @@ async fn relay_websocket_root_path_upgrades() { /// Run the relay's accept loop over the given server config, the same path /// `main.rs` uses. Authenticates through the shared [`Auth`], here with fully -/// public access (`--auth-public "**"`) so no-JWT clients get the root. +/// public access (`--auth-public ""`) so no-JWT clients get the root. /// /// Returns the QUIC and TCP sockets the server bound, when it has them, so a /// caller that asked for an ephemeral port can dial it. @@ -716,9 +720,9 @@ async fn spawn_internal_relay() -> (u16, tokio::task::JoinHandle<()>) { let mut config = moq_tokio::listen::Config::default(); config.tcp.bind = Some("127.0.0.1:0".parse().expect("parse addr")); - // Public `**` lets any no-JWT stream client through at the root. + // The empty public prefix lets any no-JWT stream client through at the root. let mut auth_config = auth::Config::default(); - auth_config.public = vec![moq_auth::Pattern::all()]; + auth_config.public = vec![moq_auth::Prefix::all()]; let (_, tcp, handle) = spawn_accept_relay(config, auth_config).await; (tcp.expect("relay bound no TCP socket").port(), handle) @@ -824,9 +828,9 @@ async fn spawn_internal_unix_relay() -> (std::path::PathBuf, tokio::task::JoinHa let mut config = moq_tokio::listen::Config::default(); config.unix.bind = Some(path.clone()); - // Public `**` lets any no-JWT stream client through at the root. + // The empty public prefix lets any no-JWT stream client through at the root. let mut auth_config = auth::Config::default(); - auth_config.public = vec![moq_auth::Pattern::all()]; + auth_config.public = vec![moq_auth::Prefix::all()]; let (_, _, handle) = spawn_accept_relay(config, auth_config).await; (path, handle) @@ -1031,7 +1035,7 @@ async fn spawn_quic_relay() -> (std::net::SocketAddr, tokio::task::JoinHandle<() config.tls.generate = vec!["localhost".into()]; let mut auth_config = auth::Config::default(); - auth_config.public = vec![moq_auth::Pattern::all()]; + auth_config.public = vec![moq_auth::Prefix::all()]; let (quic, _, handle) = spawn_accept_relay(config, auth_config).await; (quic.expect("relay bound no QUIC socket"), handle) diff --git a/rs/moq-relay/tests/integration/stack.rs b/rs/moq-relay/tests/integration/stack.rs index d911aa163a3..4e51204162e 100644 --- a/rs/moq-relay/tests/integration/stack.rs +++ b/rs/moq-relay/tests/integration/stack.rs @@ -10,7 +10,7 @@ async fn load_and_run() { let mut config = Config::default(); config.listen.bind = Some("127.0.0.1:0".parse().unwrap()); config.listen.tls.generate = vec!["localhost".into()]; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.drain_timeout = Some((std::time::Duration::ZERO).into()); let relay = Relay::load(config).await.expect("load relay").with_signals(false); let ready = relay.ready(); diff --git a/rs/moq-relay/tests/integration/tenant_matrix.rs b/rs/moq-relay/tests/integration/tenant_matrix.rs index a302194f378..7af75dbbb47 100644 --- a/rs/moq-relay/tests/integration/tenant_matrix.rs +++ b/rs/moq-relay/tests/integration/tenant_matrix.rs @@ -17,7 +17,7 @@ use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use axum::routing::post; use axum::{Json, Router}; -use moq_auth::{Event, Grant, Pattern, Patterns, Request}; +use moq_auth::{Event, Grant, Prefixes, Request}; use moq_relay::Relay; use moq_tokio::quic::driver; @@ -33,7 +33,7 @@ fn timed() -> moq_net::track::Info { /// Publish and subscribe everything under `root`, for an hour, without a re-check /// of its own: a test pushes one through `/sessions/revalidate` when it wants it. fn rooted(root: &str) -> Grant { - let all: Patterns = [Pattern::all()].into_iter().collect(); + let all = Prefixes::all(); let mut grant = Grant::new(all.clone(), all); grant.root = Some(root.into()); grant.expires = Some(kio::clock::wall() + Duration::from_secs(3600)); diff --git a/rs/moq-relay/tests/integration/upstream_links.rs b/rs/moq-relay/tests/integration/upstream_links.rs index ef0c13a0033..9d3aee8f966 100644 --- a/rs/moq-relay/tests/integration/upstream_links.rs +++ b/rs/moq-relay/tests/integration/upstream_links.rs @@ -10,7 +10,7 @@ use std::collections::BTreeMap; use std::time::Duration; -use moq_auth::{Grant, Pattern, lease}; +use moq_auth::{Grant, lease}; use moq_net::origin; use moq_relay::{ Connection, auth, @@ -73,7 +73,7 @@ impl Node { /// What the embedded decider grants a dial at `path`: everything, as a peer, /// upstream when asked. fn grant(path: &str) -> Grant { - let all: moq_net::Patterns = [Pattern::all()].into_iter().collect(); + let all = moq_net::Prefixes::all(); let mut grant = Grant::new(all.clone(), all); grant.root = Some(String::new()); grant.link = Some(match path { diff --git a/rs/moq-relay/tests/integration/wire_stats.rs b/rs/moq-relay/tests/integration/wire_stats.rs index d9028e230c3..583c370c844 100644 --- a/rs/moq-relay/tests/integration/wire_stats.rs +++ b/rs/moq-relay/tests/integration/wire_stats.rs @@ -40,7 +40,7 @@ async fn wire_sources(io_uring: bool) { config.listen.bind = Some("127.0.0.1:0".parse().unwrap()); config.listen.tls.cert = vec![cert_path]; config.listen.tls.key = vec![key_path]; - config.auth.public = vec![moq_auth::Pattern::all()]; + config.auth.public = vec![moq_auth::Prefix::all()]; config.stats.enabled = true; config.internal.listen = Some("127.0.0.1:0".parse().unwrap()); config.stats.interval = Some(Duration::from_secs(3600).into()); diff --git a/rs/moq-tokio/examples/clock.rs b/rs/moq-tokio/examples/clock.rs index 40299478ea6..33a397c8151 100644 --- a/rs/moq-tokio/examples/clock.rs +++ b/rs/moq-tokio/examples/clock.rs @@ -100,7 +100,7 @@ async fn main() -> anyhow::Result<()> { let path = moq_net::Path::new(&config.broadcast)?; let scope = - moq_net::Patterns::from(moq_net::Pattern::subtree(path.as_str()).context("invalid broadcast name")?); + moq_net::Prefixes::from(moq_net::Prefix::try_from(path.as_str()).context("invalid broadcast name")?); let consumer = origin .scope("", &scope) .context("not allowed to consume broadcast")? diff --git a/rs/moq-tokio/tests/integration/broadcast.rs b/rs/moq-tokio/tests/integration/broadcast.rs index bd9272597de..28a4464e42c 100644 --- a/rs/moq-tokio/tests/integration/broadcast.rs +++ b/rs/moq-tokio/tests/integration/broadcast.rs @@ -2809,7 +2809,7 @@ async fn announce_interest_unauthorized_keeps_session_alive() { .consume() .scope( "", - &moq_net::Patterns::from(moq_net::Pattern::subtree("allowed").unwrap()), + &moq_net::Prefixes::from(moq_net::Prefix::try_from("allowed").unwrap()), ) .expect("failed to scope publish origin"); @@ -2823,8 +2823,8 @@ async fn announce_interest_unauthorized_keeps_session_alive() { "", &["allowed", "denied"] .into_iter() - .map(|prefix| moq_net::Pattern::subtree(prefix).unwrap()) - .collect::(), + .map(|prefix| moq_net::Prefix::try_from(prefix).unwrap()) + .collect::(), ) .expect("failed to scope consume origin"); let sub_consumer = consume.consume(); @@ -2871,12 +2871,12 @@ async fn announce_interest_unauthorized_keeps_session_alive() { ); } -/// A wildcard grant on both ends of a session. The subscriber asks for the grant's -/// literal head (`room`) and lands only what its scope admits; the server's grant -/// (`server_scope`) filters what it advertises and serves. Every wire still carries -/// prefix announcements; arbitrary patterns are local authorization and filtering. -async fn wildcard_scope_test(version: &str, server_scope: &str) { - let scope = |text: &str| moq_net::Patterns::from(text.parse::().unwrap()); +/// A prefix grant on both ends of a session, and a discovery filter on the subscriber. +/// The subscriber's scope (`room`) bounds what it may land and the server's grant +/// (`server_scope`) what it advertises and serves; the filter (`room/*/chat`) only picks +/// which announcements the subscriber lists, capturing the room. +async fn prefix_scope_test(version: &str, server_scope: &str) { + let scope = |text: &str| moq_net::Prefixes::from(text.parse::().unwrap()); // ── publisher (server): rooms with a chat and an audio track each ── let pub_origin = moq_tokio::origin::spawn(); @@ -2919,13 +2919,12 @@ async fn wildcard_scope_test(version: &str, server_scope: &str) { let mut server = server.listen().await.expect("failed to listen"); let addr = server.local_addr().expect("local addr"); - // ── subscriber (client): every room's chat ── + // ── subscriber (client): every room, listing each room's chat ── let sub_origin = moq_tokio::origin::spawn(); - let consume = sub_origin - .scope("", &scope("room/*/chat")) - .expect("scope consume origin"); + let consume = sub_origin.scope("", &scope("room")).expect("scope consume origin"); let sub_consumer = consume.consume(); - let mut announcements = sub_consumer.announced(); + let chats = "room/*/chat".parse::().unwrap(); + let mut announcements = sub_consumer.filter(&chats).expect("filter").announced(); let mut client_config = moq_tokio::connect::Config::default(); client_config.tls.insecure = Some(true); @@ -2947,8 +2946,8 @@ async fn wildcard_scope_test(version: &str, server_scope: &str) { .expect("client connect timed out") .expect("client connect failed"); - // Exactly alice's chat: bob's is outside the server's grant, alice's audio and the - // lobby are outside the client's. The match pins the room. + // Exactly alice's chat: bob's is outside the server's grant, the lobby is outside the + // client's, and alice's audio is outside the filter. The match pins the room. let (update, active) = next_announce(&mut announcements).await; assert_eq!(update.prefix.as_str(), "room/alice/chat"); assert_eq!( @@ -2975,16 +2974,11 @@ async fn wildcard_scope_test(version: &str, server_scope: &str) { .expect("group closed"); assert_eq!(frame.payload.as_ref(), b"room/alice/chat"); // The server grant excludes bob's chat, so that otherwise-authorized request is - // unroutable. Audio and lobby never pass the client's own scope and are unauthorized. + // unroutable. The lobby never passes the client's own scope and is unauthorized. let refused = sub_consumer.request("room/bob/chat", None).await.err(); assert!(matches!(refused, Some(moq_net::Error::Unroutable)), "{refused:?}"); - for path in ["room/alice/audio", "lobby/alice/chat"] { - let refused = sub_consumer.request(path, None).await.err(); - assert!( - matches!(refused, Some(moq_net::Error::Unauthorized)), - "{path}: {refused:?}" - ); - } + let refused = sub_consumer.request("lobby/alice/chat", None).await.err(); + assert!(matches!(refused, Some(moq_net::Error::Unauthorized)), "{refused:?}"); // Ending the session retracts alice's chat after every announcement the session // delivered, so anything outside the grants shows up before that retraction. @@ -3007,25 +3001,24 @@ async fn wildcard_scope_test(version: &str, server_scope: &str) { } #[tokio::test] -async fn broadcast_wildcard_scopes_lite_05() { - wildcard_scope_test("moq-lite-05", "room/alice/**").await; +async fn broadcast_prefix_scopes_lite_05() { + prefix_scope_test("moq-lite-05", "room/alice").await; } #[tokio::test] -async fn broadcast_wildcard_scopes_lite_06() { - wildcard_scope_test("moq-lite-06", "room/alice/**").await; +async fn broadcast_prefix_scopes_lite_06() { + prefix_scope_test("moq-lite-06", "room/alice").await; } -/// A leading-wildcard server grant still filters literal prefix announcements. +/// A server grant of exactly one broadcast's subtree. #[tokio::test] -async fn broadcast_wildcard_server_scope_lite_06() { - wildcard_scope_test("moq-lite-06", "*/alice/chat").await; +async fn broadcast_prefix_server_scope_lite_06() { + prefix_scope_test("moq-lite-06", "room/alice/chat").await; } -/// Older wires use the same literal-head interest and local filtering. #[tokio::test] -async fn broadcast_wildcard_server_scope_lite_05() { - wildcard_scope_test("moq-lite-05", "*/alice/chat").await; +async fn broadcast_prefix_server_scope_lite_05() { + prefix_scope_test("moq-lite-05", "room/alice/chat").await; } /// Reverse of the usual direction: a publish-only client (`with_publisher`, no `with_subscriber`) @@ -3041,8 +3034,8 @@ async fn publish_only_client_to_subscribe_only_server() { "", &["allowed", "denied"] .into_iter() - .map(|prefix| moq_net::Pattern::subtree(prefix).unwrap()) - .collect::(), + .map(|prefix| moq_net::Prefix::try_from(prefix).unwrap()) + .collect::(), ) .expect("failed to scope consume origin"); let sub_consumer = consume.consume(); @@ -3118,7 +3111,7 @@ async fn publish_only_client_to_subscribe_only_server() { .consume() .scope( "", - &moq_net::Patterns::from(moq_net::Pattern::subtree("allowed").unwrap()), + &moq_net::Prefixes::from(moq_net::Prefix::try_from("allowed").unwrap()), ) .expect("failed to scope publish origin"); diff --git a/rs/moq-tokio/tests/integration/migrate.rs b/rs/moq-tokio/tests/integration/migrate.rs index b8dfc1ae5f1..654eaf1996b 100644 --- a/rs/moq-tokio/tests/integration/migrate.rs +++ b/rs/moq-tokio/tests/integration/migrate.rs @@ -260,7 +260,7 @@ async fn a_predecessor_serves_until_its_last_subscription_leaves() { async fn a_replacement_does_not_check_what_its_predecessor_learned() { let versions = vec![LITE_07.parse().unwrap()]; let origin = moq_tokio::origin::spawn(); - let only: moq_net::Patterns = [moq_net::Pattern::subtree("up").unwrap()].into_iter().collect(); + let only = moq_net::Prefixes::from(moq_net::Prefix::try_from("up").unwrap()); let ingress = origin.scope("", &only).expect("scope"); let (port, mut sessions) = relay_into(&origin, ingress, versions.clone()).await; let local = moq_tokio::origin::spawn(); diff --git a/sh/demo/token.sh b/sh/demo/token.sh index 14f47b8c3b0..eec709d40c1 100755 --- a/sh/demo/token.sh +++ b/sh/demo/token.sh @@ -10,12 +10,12 @@ umask 077 if [ ! -f demo-web.jwt ]; then cargo run --quiet --bin moq -- auth sign --key root.jwk --unbounded \ - --root demo --subscribe '**' --publish 'me/**' \ + --root demo --subscribe '' --publish 'me' \ >demo-web.jwt fi if [ ! -f demo-cli.jwt ]; then cargo run --quiet --bin moq -- auth sign --key root.jwk --unbounded \ - --root demo --publish '**' \ + --root demo --publish '' \ >demo-cli.jwt fi diff --git a/swift/Sources/Moq/Auth.swift b/swift/Sources/Moq/Auth.swift index 6a7885b9a13..0c292fca1a7 100644 --- a/swift/Sources/Moq/Auth.swift +++ b/swift/Sources/Moq/Auth.swift @@ -2,8 +2,9 @@ import Foundation import MoqFFI /// What the peer lets this side do, in this side's own paths: the broadcast -/// patterns it may publish and subscribe to, and `expires` until it lapses. -/// An empty pattern list grants nothing in that direction; `**` grants everything. +/// prefixes it may publish and subscribe to, and `expires` until it lapses. Each +/// prefix covers its path and everything beneath it. An empty list grants nothing +/// in that direction; the empty prefix `""` grants everything. public typealias Grant = MoqFFI.MoqGrant /// Follows the grant a session's peer gave it. Iterate directly: diff --git a/swift/Tests/MoqTests/SmokeTests.swift b/swift/Tests/MoqTests/SmokeTests.swift index 17ed8b9c189..b4d974c5b3f 100644 --- a/swift/Tests/MoqTests/SmokeTests.swift +++ b/swift/Tests/MoqTests/SmokeTests.swift @@ -251,7 +251,7 @@ final class SmokeTests: XCTestCase { } defer { watchdog.cancel() } - let everything = Grant(publish: ["**"], subscribe: ["**"], expires: nil) + let everything = Grant(publish: [""], subscribe: [""], expires: nil) for side in [session, peer] { for try await grant in side.grants() where grant == everything { break diff --git a/test/audio-quality/relay.toml b/test/audio-quality/relay.toml index 5b8ed37b1d4..4ef52fb6aeb 100644 --- a/test/audio-quality/relay.toml +++ b/test/audio-quality/relay.toml @@ -19,4 +19,4 @@ listen = "127.0.0.1:4443" [auth] # Allow anonymous access to everything. -public = "**" +public = "" diff --git a/test/drain/relay.toml b/test/drain/relay.toml index dd3ebe57191..3fe934a19a3 100644 --- a/test/drain/relay.toml +++ b/test/drain/relay.toml @@ -31,4 +31,4 @@ listen = "127.0.0.1:4470" [auth] # Allow anonymous access to everything. -public = "**" +public = "" diff --git a/test/ietf-conformance/run.sh b/test/ietf-conformance/run.sh index 387071eb488..27c2018a261 100755 --- a/test/ietf-conformance/run.sh +++ b/test/ietf-conformance/run.sh @@ -63,7 +63,7 @@ tls.key = ["$HARNESS_RUN/key.pem"] [web.http] listen = "127.0.0.1:$port" [auth] -public = "**" +public = "" EOF if [[ -n "$seed" ]]; then cat >>"$config" <` and ``, and GStreamer through the `versions` property on `moqsrc`. diff --git a/test/interop/clients/js-native/server.ts b/test/interop/clients/js-native/server.ts index e299a6ff860..c96ff3c3992 100644 --- a/test/interop/clients/js-native/server.ts +++ b/test/interop/clients/js-native/server.ts @@ -119,7 +119,7 @@ async function accept(quic: WebTransport, url: URL, publish: Moq.Origin.Consumer }; } -const scoped = origin.scope(Moq.Path.empty(), new Moq.Path.Patterns([Moq.Path.Pattern.literal("handover")])); +const scoped = origin.scope(Moq.Path.empty(), Moq.Path.Prefixes.parse(["handover"])); const first = listen(scoped.consume()); const second = listen(origin.consume()); process.stdout.write(`${JSON.stringify([first.url, second.url])}\n`); diff --git a/test/interop/compat.sh b/test/interop/compat.sh index 5b08625cd46..89dab542e7b 100755 --- a/test/interop/compat.sh +++ b/test/interop/compat.sh @@ -65,26 +65,49 @@ PY (cd "$HARNESS_RUN/js" && bun install) cp "$HARNESS_RUN/js/bun.lock" "$HARNESS_RUN/resolved-js.lock" -# JWT verification compares normalized permissions, not just signature validity. -"$CURRENT" auth generate --out "$HARNESS_RUN/key.jwk" +# JWT verification compares normalized permissions, not just signature validity. Every source +# grants the `video` subtree for publishing and everything for subscribing, under a key whose +# scope grants exactly that, generated by each CLI and by the current library: a signer or +# verifier that read a scope or token narrower refuses it. The library's key is verified by the +# half `Key.public` publishes. Released issuers write the grant as `put`/`get` prefixes, and +# current ones write grants and scopes as the subtree patterns `video/**` and `**` (TEMPORARY +# MIGRATION, `wire::alias` in moq-auth and `wire.ts` in @moq/auth), which a released verifier +# reads as the same subtrees. +"$CURRENT" auth generate --out "$HARNESS_RUN/current.jwk" --root compat --publish video --subscribe '' +"$RELEASED" auth generate --out "$HARNESS_RUN/released.jwk" --root compat --publish 'video/**' --subscribe '**' +bun "$DIR/compat/client.ts" generate "$HARNESS_RUN/js-current.jwk" "$HARNESS_RUN/js-current.public.jwk" for source in current released js-current js-released; do - case "$source" in - current) "$CURRENT" auth sign --key "$HARNESS_RUN/key.jwk" --root compat --publish 'video/**' --subscribe '**' ;; - released) "$RELEASED" auth sign --key "$HARNESS_RUN/key.jwk" --root compat --publish 'video/**' --subscribe '**' ;; - js-current) bun "$DIR/compat/client.ts" sign "$HARNESS_RUN/key.jwk" ;; - js-released) bun "$HARNESS_RUN/js/client.ts" sign "$HARNESS_RUN/key.jwk" ;; - esac >"$HARNESS_RUN/$source.jwt" - for verifier in current released; do - if [[ "$verifier" == current ]]; then bin="$CURRENT"; else bin="$RELEASED"; fi - "$bin" auth verify --key "$HARNESS_RUN/key.jwk" --in "$HARNESS_RUN/$source.jwt" >"$HARNESS_RUN/$source.$verifier.claims" - # `auth verify` prints the claims' Rust Debug form, the CLI's only output. - bun "$DIR/compat/claims.ts" "$HARNESS_RUN/$source.$verifier.claims" + for issuer in current released js-current; do + key="$HARNESS_RUN/$issuer.jwk" token="$HARNESS_RUN/$source.$issuer.jwt" + public="$key" + [[ "$issuer" != js-current ]] || public="$HARNESS_RUN/js-current.public.jwk" + case "$source" in + current) "$CURRENT" auth sign --key "$key" --root compat --publish video --subscribe '' ;; + released) "$RELEASED" auth sign --key "$key" --root compat --publish 'video/**' --subscribe '**' ;; + js-current) bun "$DIR/compat/client.ts" sign "$key" video '' ;; + js-released) bun "$HARNESS_RUN/js/client.ts" sign "$key" 'video/**' '**' ;; + esac >"$token" + for verifier in current released; do + if [[ "$verifier" == current ]]; then bin="$CURRENT"; else bin="$RELEASED"; fi + "$bin" auth verify --key "$public" --in "$token" >"$token.$verifier.claims" + # `auth verify` prints the claims' Rust Debug form, the CLI's only output. + bun "$DIR/compat/claims.ts" "$token.$verifier.claims" + done + bun "$DIR/compat/client.ts" verify "$public" "$token" video '' + # The released package reads every grant as patterns. + bun "$HARNESS_RUN/js/client.ts" verify "$public" "$token" 'video/**' '**' done - bun "$DIR/compat/client.ts" verify "$HARNESS_RUN/key.jwk" "$HARNESS_RUN/$source.jwt" - bun "$HARNESS_RUN/js/client.ts" verify "$HARNESS_RUN/key.jwk" "$HARNESS_RUN/$source.jwt" echo "PASS token signed by $source" record pass case=token publisher="$source" done +# A released issuer writes `publish`/`subscribe` patterns when a member is not a subtree. Its +# subtree patterns read as their prefixes, and its exact path as the prefix it names. +"$CURRENT" auth generate --out "$HARNESS_RUN/open.jwk" +"$RELEASED" auth sign --key "$HARNESS_RUN/open.jwk" --root compat --publish 'video/**' --publish audio --subscribe '**' >"$HARNESS_RUN/patterns.jwt" +"$CURRENT" auth verify --key "$HARNESS_RUN/open.jwk" --in "$HARNESS_RUN/patterns.jwt" >"$HARNESS_RUN/patterns.claims" +bun "$DIR/compat/claims.ts" "$HARNESS_RUN/patterns.claims" audio,video '' +bun "$DIR/compat/client.ts" verify "$HARNESS_RUN/open.jwk" "$HARNESS_RUN/patterns.jwt" audio,video '' +echo "PASS released pattern token read as prefixes" # Exercise the actual hang library release, independently of the version bundled # into moq-cli. Compile one small, identical adapter against either source. diff --git a/test/interop/compat/claims.test.ts b/test/interop/compat/claims.test.ts index 895f4a658a9..d90773155d6 100644 --- a/test/interop/compat/claims.test.ts +++ b/test/interop/compat/claims.test.ts @@ -1,24 +1,65 @@ import { expect, test } from "bun:test"; import { verify } from "./claims"; +// The current CLI's prefixes, and a released CLI's patterns for the same grant. +const prefixes = + 'publish: Access { path: Prefixes(["video",],), rate: None, }, subscribe: Access { path: Prefixes(["",],), rate: None, },'; const patterns = 'publish: Patterns([Pattern("video/**"),]), subscribe: Patterns([Pattern("**"),]),'; const access = 'publish: Access { path: Patterns([Pattern("video/**"),]), rate: None, }, subscribe: Access { path: Patterns([Pattern("**"),]), rate: None, },'; +const claims = (permissions: string) => + `Claims { root: "compat", ${permissions} expires: None, issued: None, not_before: None, }`; -for (const permissions of [patterns, access]) { - const claims = `Claims { root: "compat", ${permissions} expires: None, issued: None, not_before: None, }`; - test(`verifies ${permissions.startsWith("publish: Access") ? "Access" : "Patterns"} claims`, () => { - expect(() => verify(claims)).not.toThrow(); - for (const [from, to] of [ - ["compat", "other"], +for (const [name, permissions, changes] of [ + [ + "Prefixes", + prefixes, + [ + ["video", "audio"], + ['[""', '["private"'], + ], + ], + [ + "Patterns", + patterns, + [ ["video/**", "audio/**"], ['Pattern("**")', 'Pattern("private/**")'], - ]) { - expect(() => verify(claims.replace(from, to))).toThrow("token scope changed"); + ], + ], + [ + "Access", + access, + [ + ["video/**", "audio/**"], + ['Pattern("**")', 'Pattern("private/**")'], + ], + ], +] as const) { + test(`verifies ${name} claims`, () => { + expect(() => verify(claims(permissions))).not.toThrow(); + for (const [from, to] of [["compat", "other"], ...changes]) { + expect(() => verify(claims(permissions).replace(from, to))).toThrow("token scope changed"); } }); } +test("a released verifier that read a prefix as one exact path fails", () => { + const exact = 'publish: Patterns([Pattern("video"),]), subscribe: Patterns([Pattern(""),]),'; + expect(() => verify(claims(exact))).toThrow("token scope changed"); +}); + +test("verifies a scope given as lists", () => { + const two = 'publish: Prefixes(["audio","video",],), subscribe: Prefixes(["",],),'; + expect(() => verify(claims(two), "audio,video", "")).not.toThrow(); + expect(() => verify(claims(two))).toThrow("token scope changed"); + expect(() => verify(claims(prefixes), "audio,video", "")).toThrow("token scope changed"); +}); + +test("refuses a wildcard it cannot read as a prefix", () => { + expect(() => verify(claims(patterns.replace("video/**", "*/video")))).toThrow("a wildcard grant"); +}); + test("refuses missing permission fields", () => { expect(() => verify('Claims { root: "compat" }')).toThrow("no publish"); }); diff --git a/test/interop/compat/claims.ts b/test/interop/compat/claims.ts index ec1e6d1a419..78790319009 100644 --- a/test/interop/compat/claims.ts +++ b/test/interop/compat/claims.ts @@ -1,18 +1,40 @@ import { readFileSync } from "node:fs"; -/** Verify the permission scope printed by either supported Rust CLI. */ -export function verify(text: string) { +/** + * The scope a supported Rust CLI printed, each grant as prefixes. A released CLI prints + * patterns: `p/**` is the prefix `p` and `**` the empty prefix, while a pattern without a + * wildcard names one exact path, written here as `=p`. + */ +export function scope(text: string): [string | undefined, string[], string[]] { // The CLI's internal Access wrapper exposes the same permission paths. const compact = text.replace(/\s+/g, ""); - const patterns = (field: string) => { - const block = compact.match(new RegExp(`${field}:(?:Access\\{path:)?Patterns\\(\\[(.*?)\\]`)); + const grant = (field: string) => { + const block = compact.match(new RegExp(`${field}:(?:Access\\{path:)?(Prefixes|Patterns)\\(\\[(.*?)\\]`)); if (!block) throw new Error(`no ${field} in ${text}`); - return [...block[1].matchAll(/Pattern\("([^"]*)"\)/g)].map((match) => match[1]); + if (block[1] === "Prefixes") return [...block[2].matchAll(/"([^"]*)"/g)].map((match) => match[1]); + return [...block[2].matchAll(/Pattern\("([^"]*)"\)/g)].map(([, pattern]) => { + if (pattern === "**") return ""; + if (pattern.endsWith("/**") && !pattern.slice(0, -3).includes("*")) return pattern.slice(0, -3); + if (pattern.includes("*")) throw new Error(`a wildcard grant: ${pattern}`); + return `=${pattern}`; + }); }; - const claims = [compact.match(/root:"([^"]*)"/)?.[1], patterns("publish"), patterns("subscribe")]; - if (JSON.stringify(claims) !== JSON.stringify(["compat", ["video/**"], ["**"]])) { + return [compact.match(/root:"([^"]*)"/)?.[1], grant("publish"), grant("subscribe")]; +} + +/** + * Verify the scope a compat token grants beneath `compat`: by default the `video` subtree to + * publish and everything to subscribe, else `publish` and `subscribe`, each a comma-separated + * list of prefixes. A verifier that read a prefix as one exact path fails it. + */ +export function verify(text: string, publish = "video", subscribe = "") { + const expected = ["compat", publish.split(","), subscribe.split(",")]; + const claims = scope(text); + if (JSON.stringify(claims) !== JSON.stringify(expected)) { throw new Error(`token scope changed: ${JSON.stringify(claims)}`); } } -if (import.meta.main) verify(readFileSync(process.argv[2], "utf8")); +if (import.meta.main) { + verify(readFileSync(process.argv[2], "utf8"), process.argv[3], process.argv[4]); +} diff --git a/test/interop/compat/client.ts b/test/interop/compat/client.ts index 5a77d770e97..e392710652c 100644 --- a/test/interop/compat/client.ts +++ b/test/interop/compat/client.ts @@ -278,15 +278,27 @@ async function subscribe(url: URL, broadcast: string, read: Read) { const [command, ...args] = process.argv.slice(2); if (command === "sign") { - const key = Auth.Key.parse(readFileSync(args[0], "utf8")); - console.log(await Auth.Key.sign(key, { root: "compat", publish: ["video/**"], subscribe: ["**"] })); + // The grant is spelled per release: prefixes now, the equivalent patterns before. + const [path, publish, subscribe] = args; + const key = Auth.Key.parse(readFileSync(path, "utf8")); + console.log(await Auth.Key.sign(key, { root: "compat", publish: [publish], subscribe: [subscribe] })); +} else if (command === "generate") { + // A scoped key as this library writes one, and the public half it publishes. Current only. + const [secret, published] = args; + const scope = { root: "compat", publish: ["video"], subscribe: [""] }; + const key = Auth.Key.parse(JSON.stringify({ ...(await Auth.Key.generate("ES256")), scope })); + writeFileSync(secret, JSON.stringify(key)); + writeFileSync(published, JSON.stringify(Auth.Key.public(key))); } else if (command === "verify") { - const key = Auth.Key.parse(readFileSync(args[0], "utf8")); - const claims = await Auth.Key.verify(key, readFileSync(args[1], "utf8").trim()); + // The grant this release should read back, spelled as `sign` takes it, each role a + // comma-separated list. + const [path, token, publish, subscribe] = args; + const key = Auth.Key.parse(readFileSync(path, "utf8")); + const claims = await Auth.Key.verify(key, readFileSync(token, "utf8").trim()); if ( claims.root !== "compat" || - JSON.stringify(claims.publish) !== '["video/**"]' || - JSON.stringify(claims.subscribe) !== '["**"]' + JSON.stringify(claims.publish) !== JSON.stringify(publish.split(",")) || + JSON.stringify(claims.subscribe) !== JSON.stringify(subscribe.split(",")) ) { throw new Error(`token scope changed: ${JSON.stringify(claims)}`); } diff --git a/test/interop/interop.sh b/test/interop/interop.sh index cb7a37b5bd4..c788e51e44e 100755 --- a/test/interop/interop.sh +++ b/test/interop/interop.sh @@ -939,8 +939,13 @@ echo "starting relay on 127.0.0.1:${PORT}..." # committed file never has to be edited for a run. if [[ "$COMPAT" -eq 1 ]]; then # Anonymous, offering only the pinned version, which comes from the executable's - # advertised CLI choices. - sed -e "s|:4443\"|:${PORT}\"|g" -e '/^version = \[/,/^\]/d' -e 's|^url = "http://127.0.0.1:4440/"|public = "**"|' \ + # advertised CLI choices. TEMPORARY MIGRATION, delete once `release` ships prefix grants: + # a released relay's help does not yet say "Prefixes an anonymous session", and it refuses + # the empty prefix at startup, reading only `**` as everything. + relay_help=$("$RELAY" --help 2>&1 | tr -s '[:space:]' ' ') + everything='""' + [[ "$relay_help" == *"Prefixes an anonymous session"* ]] || everything='"**"' + sed -e "s|:4443\"|:${PORT}\"|g" -e '/^version = \[/,/^\]/d' -e "s|^url = \"http://127.0.0.1:4440/\"|public = ${everything}|" \ "$INTEROP_DIR/interop.toml" >"$HARNESS_RUN/relay.toml" sed -i "/\[listen\]/a version = [\"${INTEROP_VERSION}\"]" "$HARNESS_RUN/relay.toml" [[ "$(grep -c '^version = ' "$HARNESS_RUN/relay.toml")" == 1 && "$(grep -c '^public = ' "$HARNESS_RUN/relay.toml")" == 1 ]] || { @@ -997,17 +1002,6 @@ rust_connect() { esac } -# The pattern a cell's token grants its publisher, or its subscribers, on . The `auto` -# pass mints patterns no prefix could carry, so AUTH must deliver them as minted. A pinned -# pass may be moq-transport, whose requests name prefixes alone, so it grants the -# broadcast's subtree. -publish_grant() { - if [[ "$VERSION" == auto ]]; then printf '%s' "$1"; else printf '%s/**' "$1"; fi -} -subscribe_grant() { - if [[ "$VERSION" == auto ]]; then printf '**/%s' "$1"; else printf '%s/**' "$1"; fi -} - # moq-cli logs each grant it receives over AUTH at debug, under `moq_net::auth`. CLI_LOG="info,${RUST_LOG:+$RUST_LOG,}moq_net::auth=debug" @@ -1016,9 +1010,9 @@ CLI_LOG="info,${RUST_LOG:+$RUST_LOG,}moq_net::auth=debug" AUTH_VERSION="${INTEROP_VERSION:-moq-lite-07-wip}" # The `auth granted` line a token minted with at most one publish and one -# subscribe pattern implies. Every client that prints one dials $AUTH_VERSION, +# subscribe prefix implies. Every client that prints one dials $AUTH_VERSION, # which carries AUTH, so a client that prints nothing never got its grant. -# Each pattern is a JSON string, so a `\` in one reads `\\`. +# Each prefix is a JSON string, so a `\` in one reads `\\`. grant_line() { local publish="${1//\\/\\\\}" subscribe="${2//\\/\\\\}" publish="${publish:+\"$publish\"}" subscribe="${subscribe:+\"$subscribe\"}" @@ -1535,7 +1529,7 @@ run_cell() { } # run_round : every subscriber dials with a -# token for alone (see subscribe_grant), and must see media with the tone its +# token for 's prefix, and must see media with the tone its # publisher sends (expect=media), time out (nothing), or fail its audio check (wrong-tone). # names the publisher in the output and the logs, and is the relay log # line the round's connections start on. @@ -1557,8 +1551,8 @@ run_round() { tone=$(publisher_tone "$publisher") codecs=$(publisher_codecs "$publisher") IFS=',' read -r -a renditions <<<"$codecs" - sub_url=$(token_url --subscribe "$(subscribe_grant "$broadcast")") - sub_grant=$(grant_line "" "**/$broadcast") + sub_url=$(token_url --subscribe "$broadcast") + sub_grant=$(grant_line "" "$broadcast") for sub in "${SUB_LIST[@]}"; do if is_broken "$sub"; then echo " FAIL $pub -> $sub (subscriber client unavailable)" @@ -1772,7 +1766,7 @@ run_finishes() { # The fixture and its .health sibling share a parent, so one prefix grant covers both: # moq-transport requests name prefixes alone. broadcast="$name-$$-$RANDOM/fixture.hang" - grant="${broadcast%/*}/**" + grant="${broadcast%/*}" ack="$LOGS/$name-ack" local pids=() fds=() names=() all_ready=1 all_clean=1 publisher_ok=1 late=0 @@ -1900,9 +1894,9 @@ run_drop_pair() { fi mkfifo "$fifo" exec 9<>"$fifo" - harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" drop + harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$broadcast")" "$version" drop pid="$HARNESS_PID" - harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" drop + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$broadcast")" "$version" drop subscriber_pid="$HARNESS_PID" harness_wait "$subscriber_pid" || true if grep -qx 'tail drop clean end=4 groups=0,2,3 bytes=786432' "$LOGS/$name-sub.log"; then @@ -1974,14 +1968,14 @@ run_tail_pair() { exec 9<>"$fifo" from=$(relay_next_line) # Each side's token grants this broadcast alone, like a media round's. - harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" "$mode" + harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$broadcast")" "$version" "$mode" pid="$HARNESS_PID" if [[ "$mode" == datagram ]]; then # Subscription metadata confirms the alias is bound. An acknowledgement per datagram # avoids testing a best-effort receive buffer's burst capacity. mkfifo "$LOGS/datagram-ack" exec 8<>"$LOGS/datagram-ack" - harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_subscriber "$LOGS/datagram-ack" "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" "$mode" + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_subscriber "$LOGS/datagram-ack" "$sub" subscribe "$broadcast" "$(token_url --subscribe "$broadcast")" "$version" "$mode" subscriber_pid="$HARNESS_PID" local ack sequence if read -r -t "$TIMEOUT" ack <&8 && [[ "$ack" == "datagram ready" ]]; then @@ -1993,7 +1987,7 @@ run_tail_pair() { exec 8>&- rm "$LOGS/datagram-ack" else - harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" "$mode" + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$broadcast")" "$version" "$mode" subscriber_pid="$HARNESS_PID" fi harness_wait "$subscriber_pid" || true @@ -2041,10 +2035,10 @@ run_late_pair() { mkfifo "$fifo" "$heads" exec 9<>"$fifo" from=$(relay_next_line) - harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" late + harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$broadcast")" "$version" late pid="$HARNESS_PID" exec 8<>"$heads" - harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_subscriber "$heads" "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" late + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_subscriber "$heads" "$sub" subscribe "$broadcast" "$(token_url --subscribe "$broadcast")" "$version" late subscriber_pid="$HARNESS_PID" if read -r -t "$TIMEOUT" head <&8 && [[ "$head" == "late head" ]]; then printf 'late tail\n' >&9 @@ -2106,7 +2100,7 @@ run_fetch_round() { from=$(relay_next_line) mkfifo "$fifo" exec 9<>"$fifo" - harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" "$mode" + harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$broadcast")" "$version" "$mode" pid="$HARNESS_PID" if run_fetch_subscriber "$sub" "$broadcast" "$version" "$name"; then printf 'clean end\n' >&9 @@ -2146,12 +2140,12 @@ run_fetch_round() { run_fetch_subscriber() { local sub="$1" broadcast="$2" version="$3" name="$4" if [[ "$sub" != rust ]]; then - run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" fetch >"$LOGS/$name-sub.log" 2>&1 && + run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$broadcast")" "$version" fetch >"$LOGS/$name-sub.log" 2>&1 && grep -qx 'fetch clean group=1 frames=3 bytes=196608' "$LOGS/$name-sub.log" return fi # At debug, a failed fetch says whether it went on the wire and how the relay answered it. - RUST_LOG="info,${RUST_LOG:+$RUST_LOG,}moq_net=debug" timeout -k 3 "$TIMEOUT" "$MOQ" --connect "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" ${version:+--connect-version "$version"} \ + RUST_LOG="info,${RUST_LOG:+$RUST_LOG,}moq_net=debug" timeout -k 3 "$TIMEOUT" "$MOQ" --connect "$(token_url --subscribe "$broadcast")" ${version:+--connect-version "$version"} \ --broadcast "$broadcast" fetch tail --group 1 --json >"$LOGS/$name.json" 2>"$LOGS/$name-sub.log" && python3 - "$LOGS/$name.json" <<'PYCODE' import base64, json, sys @@ -2247,9 +2241,9 @@ run_matrix() { overall=1 continue fi - # The exact broadcast, not its subtree. + # The broadcast's prefix, which AUTH_OK must deliver as minted. from=$(relay_next_line) - start_publisher "$pub" "$pub" "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" + start_publisher "$pub" "$pub" "$broadcast" "$(token_url --publish "$broadcast")" run_round "$pub" "$broadcast" "$PUB_PID" media "$from" if why=$(check_grant "$pub" "$LOGS/pub-$pub.log" "$(grant_line "$broadcast" "")"); then if prints_grant "$pub"; then @@ -2270,7 +2264,7 @@ run_matrix() { broadcast="interop-swapped-$$-${RANDOM}.hang" echo "=== publisher: rust broadcast: $broadcast (tone $SWAPPED_TONE, subscribers expect $STEREO_TONE) ===" from=$(relay_next_line) - start_publisher rust-swapped rust "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$SWAPPED_TONE" + start_publisher rust-swapped rust "$broadcast" "$(token_url --publish "$broadcast")" "$SWAPPED_TONE" run_round rust-swapped "$broadcast" "$PUB_PID" wrong-tone "$from" fi @@ -2279,7 +2273,7 @@ run_matrix() { for pub in "${PUB_LIST[@]}"; do if ! enforces_grant "$pub" || is_broken "$pub"; then continue; fi broadcast="interop-denied-${pub}-$$-${RANDOM}.hang" - allowed="interop-allowed-*.hang" + allowed="interop-allowed" echo "=== publisher: $pub broadcast: $broadcast (token grants only $allowed) ===" from=$(relay_next_line) start_publisher "$pub-denied" "$pub" "$broadcast" "$(token_url --publish "$allowed")" @@ -2339,7 +2333,7 @@ run_claims_case() { return fi harness_spawn "$name" "$LOGS/$name.log" timeout -k 3 "$TIMEOUT" "${command[@]}" "$CLIENTS/js-native/claims.ts" \ - "$(token_url --publish "$prefix/**")" "$(token_url --subscribe "$prefix/**")" "$prefix" "$transport" + "$(token_url --publish "$prefix")" "$(token_url --subscribe "$prefix")" "$prefix" "$transport" # Judged by its printed marker, as run_native is: the addon can crash during Bun's exit # teardown after every assertion has passed. harness_wait "$HARNESS_PID" || true @@ -2399,7 +2393,7 @@ run_replacement_case() { [[ "$runtime" != js-native-node ]] || command=(node --import tsx) harness_spawn "$name" "$LOGS/$name.log" timeout -k 3 "$TIMEOUT" \ "${command[@]}" "$CLIENTS/js-native/replace.ts" \ - "$(token_url --publish "$prefix/**")" "$(token_url --subscribe "$prefix/**")" "$prefix" "$transport" + "$(token_url --publish "$prefix")" "$(token_url --subscribe "$prefix")" "$prefix" "$transport" # Judged by its printed marker, as run_native is: the addon can crash during Bun's exit # teardown after every assertion has passed. harness_wait "$HARNESS_PID" || true @@ -2454,10 +2448,10 @@ run_catalog_fanout() { from=$(relay_next_line) before=$(relay_inflated) || why="relay metrics unavailable" # Subtree grants, which a moq-transport request can name too. - harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_catalog_publisher "$fifo" "$(token_url --publish "$broadcast/**")" "$broadcast" + harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_catalog_publisher "$fifo" "$(token_url --publish "$broadcast")" "$broadcast" pid="$HARNESS_PID" for version in "${CATALOG_VERSIONS[@]}"; do - harness_spawn "$name-$version" "$LOGS/$name-$version.log" run_catalog_reader "$runtime" "$(token_url --subscribe "$broadcast/**")" "$broadcast" "$version" + harness_spawn "$name-$version" "$LOGS/$name-$version.log" run_catalog_reader "$runtime" "$(token_url --subscribe "$broadcast")" "$broadcast" "$version" readers+=("$HARNESS_PID") done for reader in "${readers[@]}"; do harness_wait "$reader" || true; done @@ -2521,9 +2515,9 @@ run_hls_grid() { mkfifo "$fifo" exec 9<>"$fifo" for publisher in rust "$runtime"; do - harness_spawn "$name-$publisher-pub" "$LOGS/$name-$publisher-pub.log" run_hls_publisher "$fifo" "$publisher" "$(token_url --publish "$broadcast-$publisher/**")" "$broadcast-$publisher" + harness_spawn "$name-$publisher-pub" "$LOGS/$name-$publisher-pub.log" run_hls_publisher "$fifo" "$publisher" "$(token_url --publish "$broadcast-$publisher")" "$broadcast-$publisher" pid="$HARNESS_PID" - harness_spawn "$name-$publisher-read" "$LOGS/$name-$publisher-read.log" timeout -k 3 "$TIMEOUT" "$EXAMPLES/interop-hls" read "$(token_url --subscribe "$broadcast-$publisher/**")" "$broadcast-$publisher" + harness_spawn "$name-$publisher-read" "$LOGS/$name-$publisher-read.log" timeout -k 3 "$TIMEOUT" "$EXAMPLES/interop-hls" read "$(token_url --subscribe "$broadcast-$publisher")" "$broadcast-$publisher" harness_wait "$HARNESS_PID" || why="${why:-$publisher groups unreadable}" printf 'clean end\n' >&9 harness_wait "$pid" || true @@ -2621,7 +2615,7 @@ run_tails() { if [[ "$MEDIA" -eq 1 ]]; then # One token for the whole run: every media case publishes and watches its own broadcast. - MEDIA_URL=$(token_url --publish '**' --subscribe '**') + MEDIA_URL=$(token_url --publish '' --subscribe '') # Media output and lifecycle, browser to browser, against the deterministic fixture. The # negative controls below inject a defect and name the assertion that has to catch it; each # passes only by failing there, which is what keeps the positive run from being vacuous. diff --git a/test/mesh-replay/replay.py b/test/mesh-replay/replay.py index 5e2ebf8efb3..19a341fd1b5 100644 --- a/test/mesh-replay/replay.py +++ b/test/mesh-replay/replay.py @@ -493,7 +493,7 @@ def do_POST(self) -> None: self.send_response(204) self.end_headers() return - grant = {"publish": ["**"], "subscribe": ["**"]} + grant = {"publish": [""], "subscribe": [""]} if record["peer"]: grant |= {"link": "peer", "peer": True} payload = json.dumps(grant).encode() diff --git a/test/native-play/relay.toml b/test/native-play/relay.toml index 979c6e15794..0e2c4bcb934 100644 --- a/test/native-play/relay.toml +++ b/test/native-play/relay.toml @@ -11,4 +11,4 @@ tls.generate = ["localhost", "127.0.0.1"] listen = "127.0.0.1:4443" [auth] -public = "**" +public = "" diff --git a/test/portrait/relay.toml b/test/portrait/relay.toml index 9287835e0d7..07ab111f5b4 100644 --- a/test/portrait/relay.toml +++ b/test/portrait/relay.toml @@ -13,4 +13,4 @@ version = ["moq-lite-07-wip", "moq-lite-06"] listen = "127.0.0.1:4443" [auth] -public = "**" +public = "" diff --git a/test/smoke/cloudflare.sh b/test/smoke/cloudflare.sh index 3d9e6ebdc0e..c49ebfa95de 100755 --- a/test/smoke/cloudflare.sh +++ b/test/smoke/cloudflare.sh @@ -147,12 +147,12 @@ start_relay() { ready="$HTTPS_URL/fingerprint" ;; moq-dev) - # Git HEAD relay: the >= 0.15 flag names and pattern-based public auth. + # Git HEAD relay: the >= 0.15 flag names and prefix-based public auth. "$MOQ_RELAY" --listen "127.0.0.1:${PORT}" \ --listen-tls-cert "$TMP/localhost.crt" --listen-tls-key "$TMP/localhost.key" \ --web-https-listen "127.0.0.1:${PORT}" \ --web-https-cert "$TMP/localhost.crt" --web-https-key "$TMP/localhost.key" \ - --auth-public "**" >"$TMP/relay-moq-dev.log" 2>&1 & + --auth-public "" >"$TMP/relay-moq-dev.log" 2>&1 & ready="$HTTPS_URL/certificate.sha256" ;; *) diff --git a/test/smoke/moxygen.sh b/test/smoke/moxygen.sh index 1f93026a2fa..fd341fd4335 100755 --- a/test/smoke/moxygen.sh +++ b/test/smoke/moxygen.sh @@ -101,12 +101,12 @@ if ! openssl req -x509 -newkey rsa:2048 -nodes \ fi echo "starting moq-dev relay on 127.0.0.1:${PORT}..." -# Git HEAD relay: the >= 0.15 flag names and pattern-based public auth. +# Git HEAD relay: the >= 0.15 flag names and prefix-based public auth. "$MOQ_RELAY" --listen "127.0.0.1:${PORT}" \ --listen-tls-cert "$TMP/localhost.crt" --listen-tls-key "$TMP/localhost.key" \ --web-https-listen "127.0.0.1:${PORT}" \ --web-https-cert "$TMP/localhost.crt" --web-https-key "$TMP/localhost.key" \ - --auth-public "**" >"$TMP/relay.log" 2>&1 & + --auth-public "" >"$TMP/relay.log" 2>&1 & RELAY_PID=$! ready=0 diff --git a/test/smoke/runner.Dockerfile b/test/smoke/runner.Dockerfile index 3e437da2bb1..84337a48d9d 100644 --- a/test/smoke/runner.Dockerfile +++ b/test/smoke/runner.Dockerfile @@ -6,5 +6,5 @@ COPY --from=docker.io/library/busybox:stable-musl /bin /bin ENV MOQ_LISTEN=[::]:4443 \ MOQ_LISTEN_TLS_CERT=/certs/cert.pem \ MOQ_LISTEN_TLS_KEY=/certs/priv.key \ - MOQ_AUTH_PUBLIC=** + MOQ_AUTH_PUBLIC="" EXPOSE 4443/udp diff --git a/test/smoke/smoke-patterns.toml b/test/smoke/smoke-patterns.toml new file mode 100644 index 00000000000..6e1258b0cd3 --- /dev/null +++ b/test/smoke/smoke-patterns.toml @@ -0,0 +1,20 @@ +# Relay config for the cross-language interop smoke test, for published relays from +# 0.15 until grants became prefixes: they read `**` as everything and refuse a public +# rule without a wildcard. smoke.toml carries the prefix form this checkout reads. +# Delete this file once every channel ships a relay that reads prefixes. + +[log] +level = "info" + +[listen] +# QUIC on UDP. 127.0.0.1 avoids IPv6 flakiness on CI runners. +bind = "127.0.0.1:4443" +tls.generate = ["localhost", "127.0.0.1"] + +[web.http] +# HTTP + WebSocket on TCP, also serving /certificate.sha256 for cert pinning. +listen = "127.0.0.1:4443" + +[auth] +# Allow anonymous access to everything. +public = "**" diff --git a/test/smoke/smoke.sh b/test/smoke/smoke.sh index 58377fe6342..221c08702bb 100755 --- a/test/smoke/smoke.sh +++ b/test/smoke/smoke.sh @@ -656,11 +656,15 @@ if [[ ${#EXTERNAL_RELAYS[@]} -eq 0 ]]; then # smoke.toml is the source of truth; rewrite its port into a scratch copy so a # busy 4443 (a dev relay, a parallel run) doesn't require editing the committed file. # Relays before 0.15 (no --listen flag) reject the renamed keys, so they get the - # legacy layout until every channel ships 0.15. + # legacy layout until every channel ships 0.15. Relays since then took pattern + # grants until their help began "Prefixes an anonymous session", so they get the + # `**` rule until every channel reads prefixes. relay_config="$SMOKE_DIR/smoke.toml" relay_help=$("$RELAY" --help 2>&1 || true) if ! grep -q -- '--listen\b' <<<"$relay_help"; then relay_config="$SMOKE_DIR/smoke-legacy.toml" + elif ! tr -s '[:space:]' ' ' <<<"$relay_help" | grep -q 'Prefixes an anonymous session'; then + relay_config="$SMOKE_DIR/smoke-patterns.toml" fi echo "relay config: $(basename "$relay_config")" sed "s/4443/${PORT}/g" "$relay_config" >"$TMP/relay.toml" diff --git a/test/smoke/smoke.toml b/test/smoke/smoke.toml index 369ad716198..6695475edc5 100644 --- a/test/smoke/smoke.toml +++ b/test/smoke/smoke.toml @@ -1,7 +1,8 @@ # Relay config for the cross-language interop smoke test. # Anonymous access, self-signed localhost cert, QUIC + WebSocket on 127.0.0.1:4443. # Mirrors moq-dev/moq's test/smoke/smoke.toml. smoke-legacy.toml carries the -# pre-0.15 layout for relays that predate the [server] -> [listen] rename. +# pre-0.15 layout for relays that predate the [server] -> [listen] rename, and +# smoke-patterns.toml the `**` rule for relays whose grants were patterns. [log] level = "info" @@ -16,5 +17,5 @@ tls.generate = ["localhost", "127.0.0.1"] listen = "127.0.0.1:4443" [auth] -# Allow anonymous access to everything. -public = "**" +# Allow anonymous access to everything: the empty prefix covers every path. +public = "" diff --git a/test/stream-credit/relay.toml b/test/stream-credit/relay.toml index 39d1818a7de..131ecd9d682 100644 --- a/test/stream-credit/relay.toml +++ b/test/stream-credit/relay.toml @@ -15,4 +15,4 @@ version = ["moq-lite-07-wip", "moq-lite-06"] listen = "127.0.0.1:4480" [auth] -public = "**" +public = "" diff --git a/test/ts/relay.toml b/test/ts/relay.toml index b2e55c9d06a..bba15b1991f 100644 --- a/test/ts/relay.toml +++ b/test/ts/relay.toml @@ -18,4 +18,4 @@ listen = "127.0.0.1:4470" [auth] # Allow anonymous access to everything. -public = "**" +public = "" diff --git a/test/vr/relay.toml b/test/vr/relay.toml index e9a52bad3ca..3bcb6b8ab12 100644 --- a/test/vr/relay.toml +++ b/test/vr/relay.toml @@ -12,4 +12,4 @@ version = ["moq-lite-07-wip", "moq-lite-06"] listen = "127.0.0.1:4443" [auth] -public = "**" +public = ""