Skip to content

server: --cors-origin for browser clients, and /v1/apply-template returns a chat request's rendered prompt - #3166

Merged
ashhart merged 4 commits into
mainfrom
lead/cors-apply-template
Nov 17, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/cors-apply-template

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Closes #3111.

Two server additions for browser clients and template debugging:

  • --cors-origin ORIGIN (repeatable, off by default). Only a request origin that exactly matches a listed one is echoed back, with Vary: Origin. There is no credentials mode. Preflights are answered only after the existing Host and Origin policy passes: 204 with no Content-Length, and an unlisted origin gets 403 with no Access-Control-Allow-Origin. Allowed request headers: authorization, x-api-key, content-type, anthropic-version, anthropic-beta, anthropic-dangerous-direct-browser-access, and any x-stainless-* header (letters, digits, - and _ after the prefix), so the OpenAI and Anthropic browser SDKs pass preflight. An unknown header refuses the whole preflight.
  • /v1/apply-template returns the prompt text a chat request renders to, under the same auth as inference. It decodes the rendered token ids directly.

SECURITY.md and docs/api.md describe both.

Tests: test-cors-template-host 30, with 3 of the apply-template tests failing on main and the CORS tests not compiling there. The preflight tests cover listed and unlisted origins, SDK headers, unknown and malformed headers, and mixed case. test-server-cpu 1346 pass, 7 skip; test-server-security 35; test-http-limits-host 30; test-golden passes.

Served check pending (no GPU): with --cors-origin https://ui.invalid and a key, a listed preflight gets 204 and an unlisted one gets 403; /v1/apply-template gets 401 without a key and 200 with one; one browser SSE fetch from the listed origin; and on a real checkpoint, tokenizing the returned prompt with add_special_tokens: false gives /tokenize's ids.

…it on its replies (#3111)

--cors-origin ORIGIN is repeatable (64 at most), takes the exact
http(s)://HOST[:PORT] form --allowed-origin takes, and admits that origin
too. A preflight from a listed origin is answered before keys: 204 with
Access-Control-Allow-Origin echoing it, the API's methods and request
headers and a 600 s max age; another method or header gets 403, and an
unlisted origin gets the origin 403 as before.

The allowed request headers are authorization, x-api-key, content-type,
anthropic-version, anthropic-beta and
anthropic-dangerous-direct-browser-access, plus the x-stainless-*
headers the OpenAI and Anthropic browser SDKs send, which the reply
names lowercased after the fixed list.

Every reply to a listed origin's request, key refusals and SSE streams
included, carries Access-Control-Allow-Origin, and with the flag set
replies carry Vary: Origin. No wildcard and no credentials mode; gated
routes still need a key. Without the flag no reply changes.
…s to (#3111)

POST /apply-template and /v1/apply-template take /tokenize's messages
body (tools, reasoning_effort, chat_template_kwargs,
add_generation_prompt) and reply {"prompt": TEXT}: the ids /tokenize
would return, media expanded, decoded with their special tokens. Nothing
reaches the engine. Both routes are in the inference key scope, like
/tokenize and /detokenize.
@ashhart
ashhart force-pushed the lead/cors-apply-template branch from 5da64d0 to b76b221 Compare November 17, 2026 18:04
@ashhart
ashhart merged commit d82ce92 into main Nov 17, 2026
10 of 11 checks passed
@ashhart
ashhart deleted the lead/cors-apply-template branch November 17, 2026 18:04
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 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.

CORS for browser clients, and an /apply-template endpoint

1 participant