[PR #116] [MERGED] server: opt-in CORS for /v1/* (config "cors_origins") - a page on another origin can call the API from a browser #348

Closed
opened 2026-10-29 21:24:31 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/116
Author: @sociolog
Created: 10/11/2026
Status: ✅ Merged
Merged: 10/12/2026
Merged by: @mw00

Base: main ← Head: pr-cors-v1


📝 Commits (1)

  • ba93d3d server: opt-in CORS for /v1/* (config "cors_origins") - a page on another origin (a local chat client) can call the API from a browser

📊 Changes

3 files changed (+165 additions, -2 deletions)

View changed files

📝 docs/DETAILS.md (+5 -0)
📝 serve/server.py (+52 -1)
📝 serve/test_server.py (+108 -1)

📄 Description

What

Opt-in CORS for /v1/*, so a browser page served from another origin (a local chat client, for example
http://127.0.0.1:8090) can use the API. New config key "cors_origins": exact origins, or ["*"]. Missing or empty:
no CORS header at all, byte for byte as before.

It has the shape of the opt-in trusted-origin list Strata took in its #321 (without that PR's X-Forwarded-For / CF-*
origin bypass).

Why

A browser stops a page on another origin from calling /v1/* unless the server answers the preflight and sends
Access-Control-Allow-Origin. A chat page that is a single file served beside Maya (so not by Maya's own server) needs
either a reverse proxy or this.

What it does

  • A preflight (OPTIONS) is granted only to a listed origin and only on /v1/*: 204 with Allow-Methods and
    Allow-Headers (it echoes Access-Control-Request-Headers). Anything else stays 403, as before.
  • Every /v1/* answer to a listed origin (JSON, a stream, an error) carries Access-Control-Allow-Origin, and
    Vary: Origin for an exact origin.
  • Settings, the context endpoint, MCP and the page itself are unchanged. _own_page still refuses a foreign Origin, so a
    listed page may chat but may not change settings or run tools. Its docstring said the server never grants a
    preflight; it now says it grants one to cors_origins on /v1/* only.
  • ["*"] is allowed. The docs say to set an api_key with it: otherwise any page you open can use your model.
  • A bad value (not a list of strings) is refused at start; the parsing is a small function so it is tested.

Checked

  • python -m unittest discover -s serve -p "test_*.py" -t .: 152 tests OK (3 skipped), 7 of them new
    (CorsOptIn): unset = nothing changes; a listed page gets its preflight and the header on JSON and on a stream;
    another page gets nothing; "*"; a trailing slash in the config; settings and the page stay closed; the config value.
  • The patch has been carried on our own install since 2026-10-09, on top of v1.0.33 and v1.0.34 (the identity gate
    passes with it: it only touches serve/server.py).
  • Overlap: searched the open and merged PRs and the issues for CORS / Origin / Access-Control: none. #86 and #106 touch
    serve/server.py elsewhere and do not conflict with this (their conflicts with main are README.md and
    glm_model.cu, with or without this PR).

Files: docs/DETAILS.md (+5 -0), serve/server.py (+52 -1), serve/test_server.py (+108 -1).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/116 **Author:** [@sociolog](https://github.com/sociolog) **Created:** 10/11/2026 **Status:** ✅ Merged **Merged:** 10/12/2026 **Merged by:** [@mw00](https://github.com/mw00) **Base:** `main` ← **Head:** `pr-cors-v1` --- ### 📝 Commits (1) - [`ba93d3d`](https://github.com/mw00/project-maya/commit/ba93d3d59f54f5c68a18dd9ad68ea2097bdf52e0) server: opt-in CORS for /v1/* (config "cors_origins") - a page on another origin (a local chat client) can call the API from a browser ### 📊 Changes **3 files changed** (+165 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+5 -0) 📝 `serve/server.py` (+52 -1) 📝 `serve/test_server.py` (+108 -1) </details> ### 📄 Description ## What Opt-in CORS for `/v1/*`, so a browser page served from another origin (a local chat client, for example `http://127.0.0.1:8090`) can use the API. New config key `"cors_origins"`: exact origins, or `["*"]`. Missing or empty: no CORS header at all, byte for byte as before. It has the shape of the opt-in trusted-origin list Strata took in its #321 (without that PR's X-Forwarded-For / CF-* origin bypass). ## Why A browser stops a page on another origin from calling `/v1/*` unless the server answers the preflight and sends `Access-Control-Allow-Origin`. A chat page that is a single file served beside Maya (so not by Maya's own server) needs either a reverse proxy or this. ## What it does - A preflight (OPTIONS) is granted only to a listed origin and only on `/v1/*`: 204 with `Allow-Methods` and `Allow-Headers` (it echoes `Access-Control-Request-Headers`). Anything else stays 403, as before. - Every `/v1/*` answer to a listed origin (JSON, a stream, an error) carries `Access-Control-Allow-Origin`, and `Vary: Origin` for an exact origin. - Settings, the context endpoint, MCP and the page itself are unchanged. `_own_page` still refuses a foreign Origin, so a listed page may chat but may not change settings or run tools. Its docstring said the server never grants a preflight; it now says it grants one to `cors_origins` on `/v1/*` only. - `["*"]` is allowed. The docs say to set an `api_key` with it: otherwise any page you open can use your model. - A bad value (not a list of strings) is refused at start; the parsing is a small function so it is tested. ## Checked - `python -m unittest discover -s serve -p "test_*.py" -t .`: 152 tests OK (3 skipped), 7 of them new (`CorsOptIn`): unset = nothing changes; a listed page gets its preflight and the header on JSON and on a stream; another page gets nothing; `"*"`; a trailing slash in the config; settings and the page stay closed; the config value. - The patch has been carried on our own install since 2026-10-09, on top of v1.0.33 and v1.0.34 (the identity gate passes with it: it only touches `serve/server.py`). - Overlap: searched the open and merged PRs and the issues for CORS / Origin / Access-Control: none. #86 and #106 touch `serve/server.py` elsewhere and do not conflict with this (their conflicts with main are README.md and `glm_model.cu`, with or without this PR). Files: docs/DETAILS.md (+5 -0), serve/server.py (+52 -1), serve/test_server.py (+108 -1). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:24:31 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#348
No description provided.