Skip to content

Protect the /v1 proxy with an optional bearer token - #17

Closed
BuiltByLou wants to merge 1 commit into
gschaetz:masterfrom
BuiltByLou:feat/proxy-token
Closed

BuiltByLou wants to merge 1 commit into
gschaetz:masterfrom
BuiltByLou:feat/proxy-token

Conversation

@BuiltByLou

Copy link
Copy Markdown

Why

/v1 is open by design: any Authorization value 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 /v1 open; 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() reads MODELRELAY_PROXY_TOKEN (trimmed; empty means off). createProxyAuth() compares HMACs under a random per-process key, the same constant-time approach as admin-guard. createProxyMiddleware() skips OPTIONS and answers 401 {"error":{"message":"Invalid API key","type":"invalid_request_error","code":"invalid_api_key"}}.
  • lib/server.js: mounts the guard on /v1 after the CORS middleware, and the startup banner says when the proxy is protected and which API key to send.
  • Docs: 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 in docker-compose.yml.
  • Version 1.30.0.

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-case bearer, preflight stays 204, routes outside /v1 untouched, env value trimmed and blank treated as off.
  • Live router: with the token, /v1/models returns 401 without the header, 401 with a wrong one, 200 with it; OPTIONS 204; / 200. Without the token, /v1/models 200. The banner prints the protection line.
  • npm audit --audit-level=moderate (0 vulnerabilities) and npm run license-check pass.

Notes

  • Default behaviour is unchanged, so no existing client breaks.
  • CORS stays open on /v1; only the credential is required.

🤖 Generated with opencode

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.
@BuiltByLou BuiltByLou closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant