Repository navigation
Protect the /v1 proxy with an optional bearer token - #17
Closed
BuiltByLou wants to merge 1 commit into
Closed
BuiltByLou wants to merge 1 commit into
BuiltByLou wants to merge 1 commit into
Conversation
Anyone who can reach /v1 can spend the quota of the configured providers, which is fine on a laptop but not on a VPS or behind a public reverse proxy. Setting MODELRELAY_PROXY_TOKEN now requires `Authorization: Bearer <token>` on every /v1 request, with a 401 invalid_api_key response otherwise. Off by default, so existing clients keep working. The token is read from the environment only (like MODELRELAY_ADMIN_TOKEN), compared in constant time, and CORS preflights stay open so browser clients can still send the header.
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.
Why
/v1is open by design: anyAuthorizationvalue is accepted and ignored. That is right for a laptop, but on a VPS or behind a public reverse proxy anyone who knows the URL can spend the quota configured on the providers. #8 protected the admin API and deliberately left/v1open; this adds the missing opt-in on the proxy side without changing the default.What changed
lib/proxy-guard.js: optional bearer check for/v1.getProxyToken()readsMODELRELAY_PROXY_TOKEN(trimmed; empty means off).createProxyAuth()compares HMACs under a random per-process key, the same constant-time approach asadmin-guard.createProxyMiddleware()skipsOPTIONSand answers401 {"error":{"message":"Invalid API key","type":"invalid_request_error","code":"invalid_api_key"}}.lib/server.js: mounts the guard on/v1after the CORS middleware, and the startup banner says when the proxy is protected and which API key to send.security.md(endpoint table, new "Proxy token" section, Docker and reverse-proxy rows),configuration.md,endpoints.md,troubleshooting.md(401 invalid_api_key), the README endpoint bullet, and the commented env var indocker-compose.yml.Why env-only
Same model as
MODELRELAY_ADMIN_TOKEN: read from the environment, never written to~/.modelrelay.json, restart to change. No second secret in the config file and no dashboard surface for it.Testing
npm test: 372 pass, 7 new in "proxy token: end to end over HTTP" — open when unset, 401 without a header / with a wrong token / with a non-Bearer scheme / with a prefix, accepts lower-casebearer, preflight stays 204, routes outside/v1untouched, env value trimmed and blank treated as off./v1/modelsreturns 401 without the header, 401 with a wrong one, 200 with it;OPTIONS204;/200. Without the token,/v1/models200. The banner prints the protection line.npm audit --audit-level=moderate(0 vulnerabilities) andnpm run license-checkpass.Notes
/v1; only the credential is required.🤖 Generated with opencode