~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
131 lines · 6.8 KB Raw History
# PR 33: encoded RPC audiences

Reviewed on November 15, 2026, before deployment. [PR 33](https://tangled.org/zat.dev/zds/pulls/33/latest)
fixes an existing ZDS authorization bug. No blocking finding in the reviewed
patch. This review did not merge or deploy it.

## revisions and reproduction

Base: `c4c5cd392fc63c9d139a273a5091bf6b82cb2e70` (v0.14.13).
PR: `c0ec9a7fd8f3353d1338ada37c093c7ba03ddc61`.
The patch was applied to an isolated checkout of the base. An initial API run
used the main workspace's base plus unrelated preview changes; the final run
used a pristine base binary and the isolated patched binary.

The reproduction starts each binary with a disposable SQLite database and a
loopback proxy upstream. It seeds one active account and OAuth token rows with
valid HS256 JWTs bound to a generated P-256 DPoP key. Each request first obtains
a nonce challenge, then sends a fresh signed DPoP proof including the access-token
hash. It calls `GET /xrpc/chat.bsky.convo.listConvos` with
`atproto-proxy: did:web:api.bsky.chat#bsky_chat`.

The minimal failing stored grant is:

```text
atproto rpc:chat.bsky.convo.listConvos?aud=did%3Aweb%3Aapi.bsky.chat%23bsky_chat
```

| Stored grant / token condition | Base | PR 33 |
| --- | --- | --- |
| Fully encoded correct audience | 403 InsufficientScope | 200 |
| Only the audience's `#` encoded as `%23` | 403 InsufficientScope | 200 |
| Plain correct audience | 200 | 200 |
| Wrong service DID | 403 | 403 |
| Wrong service fragment | 403 | 403 |
| Wrong method | 403 | 403 |
| Truncated escape | 403 | 403 |
| Double-encoded fragment `%2523` | 403 | 403 |
| Missing DPoP proof | 401 | 401 |
| Revoked token | 401 | 401 |

Successful calls reach the mock upstream exactly once. Denied calls never reach
it. The upstream checks the forwarded service JWT's audience and method, and
that the caller's DPoP proof is not forwarded. It does not cryptographically
verify that service JWT.

Local review artifacts are under `bench/results/pr33-review/`: `pr33.patch`,
`proxy-repro.py`, `proxy-results.json`, `proxy-repro.log`, server logs,
`tests.log`, `build.log`, `base-build.log`, and `smoke.log`. The standalone
reproducer accepts base and patched binary paths:

```sh
uv run --script bench/results/pr33-review/proxy-repro.py /path/to/base/zds /path/to/patched/zds
```

`just test`, `zig build smoke-build`, and `just smoke` completed successfully
for the patch. The smoke suite included browser sign-in, consent, CSRF, account
binding, expiry and revocation checks. The PR's unit coverage also checks that
an encoded `include:` audience expands to a singly encoded RPC audience.
`git diff --check` passed.

## contract and reference comparison

The [permission specification](https://atproto.com/specs/permission) requires
query-component encoding for scope parameters. An RPC audience includes the
service fragment; `include:` can supply that audience to inherited RPC grants.
The audience must be decoded once for comparison. Decoding an inherited audience
before serializing its expanded grants prevents `%23` becoming `%2523`.

The reference implementations were inspected at these revisions, not exercised
as running servers:

- Official PDS, current `main` at `7dd086d4de20d22777a64043ee29f4afa9a5811b`:
  [scope parsing](https://github.com/bluesky-social/atproto/blob/7dd086d4de20d22777a64043ee29f4afa9a5811b/packages/oauth/oauth-scopes/src/lib/syntax-string.ts)
  uses `URLSearchParams`;
  [RPC matching](https://github.com/bluesky-social/atproto/blob/7dd086d4de20d22777a64043ee29f4afa9a5811b/packages/oauth/oauth-scopes/src/scopes/rpc-permission.ts)
  compares the decoded audience and method.
  [Authorization](https://github.com/bluesky-social/atproto/blob/7dd086d4de20d22777a64043ee29f4afa9a5811b/packages/pds/src/auth-verifier.ts)
  verifies OAuth/DPoP and account status before invoking the route's permission
  check. The getTimeline route computes the proxy audience and asserts RPC permission.
- Tranquil, refreshed to `b95f999e639fbf392ffe9c21a84a38b21704536c`:
  [parser](https://tangled.org/tranquil.farm/tranquil-pds/blob/b95f999e639fbf392ffe9c21a84a38b21704536c/crates/tranquil-scopes/src/parser.rs)
  decodes query values;
  [proxy](https://tangled.org/tranquil.farm/tranquil-pds/blob/b95f999e639fbf392ffe9c21a84a38b21704536c/crates/tranquil-pds/src/api/proxy.rs)
  validates the token, DPoP and active account, then checks `did#service` and the
  method through `check_rpc_scope` and `ScopePermissions::assert_rpc`.

Both reference paths support the corrected audience comparison. Their broader
parsers differ from ZDS: the official parser validates DID-reference syntax,
while Tranquil's query decoder falls back to raw text on decode errors. ZDS's
new comparison refuses malformed escapes. This review does not assert complete
scope-parser parity.

## existing sessions and review boundary

Existing singly encoded grants benefit immediately from the new comparison.
Already double-encoded grants still fail, as the denial test demonstrates.
Source inspection shows `expandScopes` replaces the original `include:` scope
with expanded grants at issuance. Refresh passes the stored scope back to
`issueTokenResponse`, and `rotateOAuthToken` retains that stored scope. Refresh
therefore cannot repair an already double-encoded audience. Those clients need
a fresh authorization/login after the fix. No database migration is in this PR.

The API reproduction seeds OAuth rows; it does not execute client discovery,
PAR, permission-set DNS resolution, or a real client's callback. The unit test
covers the expansion transformation, and the existing smoke suite covers browser
login separately. No particular friend's client was identified or tested end to
end, so this proves the PDS permission failure and fix, not that every reported
login failure has the same cause.

## release verification

PR 33 was merged as `40a58b7209fa0eba47e0906e101a31620dc9f548` for v0.14.14.
The percent-decoding helper stays in `src/internal/scopes.zig`: this change is
ZDS's existing scope-text storage and matching boundary, and does not change Zat.

The focused `bench/rpc_scopes.zig` probe alternates a matching encoded audience
and a different service fragment. One million operations produced exactly
500,000 grants at 48 ns/op on the development Mac, ReleaseFast. This is an
in-process permission-check measurement, not proxy throughput or a production
latency estimate. Run it with:

```sh
zig run -O ReleaseFast --dep scopes -Mroot=bench/rpc_scopes.zig -Mscopes=src/internal/scopes.zig
```

Neither inspected upstream scope package has a directly comparable benchmark
at this seam. Cross-implementation timing remains unmeasured; the comparison
above is source-level contract evidence, not a performance-parity claim.
A future comparison needs equivalent scope parsing/storage setup and identical
allow/deny inputs across all three implementations. The malformed-input parser
differences noted above also remain outside this release's scope.