feat: enforce per-tool scopes for scoped agent credentials#14
Open
aurkenb wants to merge 1 commit into
Open
Conversation
The ZenRows app now issues scoped, revocable agent credentials. This adds per-tool scope enforcement to the MCP server, fully backward-compatible with existing full-access API keys. - src/auth.ts: introspect a presented credential via GET /me/credential. 200 → scoped agent credential (enforce scopes); 401 → legacy key / invalid (pass through; the Scraper API backstops); outage → fail open. 60s per-key cache. - scrape requires scrape:write (+ extract:json for structured extraction); all browser_* tools require browser:session_write (gated once in the shared tfetch). Denials return an insufficient_scope message pointing to Settings → Agents. - tests/: node --test coverage for the scope logic; add `test` script. - ZENROWS_INTROSPECTION_URL config; README + .env.example notes. typecheck + lint + tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds per-tool scope enforcement for scoped ZenRows agent credentials, while keeping existing full-access API keys working exactly as before.
The ZenRows app now issues scoped, revocable, expiring agent credentials (via OAuth / CLI / OTP / ID-JAG) and exposes a credential-introspection endpoint. This MCP server already passes a presented credential straight through to the Scraper/Browser APIs and already advertises the OAuth discovery metadata — so scoped credentials already flow through. The missing piece was enforcing what each credential is allowed to do.
How
src/auth.ts—introspect(apiKey)callsGET /me/credential:200→ a scoped agent credential; enforce its scopes.401(legacy user API key, or revoked/invalid) → no scope data; pass through unchanged (a truly invalid key is still rejected by the Scraper API on use).scraperequiresscrape:write(+extract:jsonwhenautoparse/css_extractor/outputsis used); allbrowser_*tools requirebrowser:session_write(a production scope requiring approval) — gated once in the sharedtfetch, so all 30+ browser tools are covered without per-handler edits.insufficient_scopemessage pointing to Settings → Agents.ZENROWS_INTROSPECTION_URLconfig (defaulthttps://api.zenrows.com/me/credential).Compatibility
Fully backward compatible: plain API keys are never scope-gated (introspection returns 401 → pass-through). Only OAuth-issued agent credentials are enforced.
Tests
Adds a
node --testharness +testscript.npm run typecheck,npm run lint, andnpm test(5 tests) all pass. Zero new runtime dependencies.