[PR #3853] [CLOSED] [GUI] Share listener: answer browser CORS preflights when an API key is set (#3208)App sharing: the shared and tunnel listener answers browser CORS when… #4801

Closed
opened 2026-11-07 15:39:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3853
Author: @MaKla89
Created: 10/23/2026
Status: ❌ Closed

Base: main ← Head: fix-3208-share-cors


📝 Commits (1)

  • 1dc28d7 App sharing: the shared and tunnel listener answers browser CORS when an API key is set - the preflight (OPTIONS, which never carries the key) before the path and key checks, Access-Control headers on every answer; without a key nothing changes, so a keyless share still reaches devices on the network only and no web page of another site can read the model; regression tests and the DETAILS CORS line (#3208)

📊 Changes

3 files changed (+104 additions, -3 deletions)

View changed files

📝 apps/host/server/local-share.mjs (+15 -1)
➕ apps/host/tests/cors-share.test.mjs (+83 -0)
📝 docs/DETAILS.md (+6 -2)

📄 Description

… an API key is set - the preflight (OPTIONS, which never carries the key) before the path and key checks, Access-Control headers on every answer; without a key nothing changes, so a keyless share still reaches devices on the network only and no web page of another site can read the model; regression tests and the DETAILS CORS line (#3208)

Problem and result

A browser page on another machine cannot call the app's API over the local network. The
browser's CORS preflight (OPTIONS) gets 401 Invalid API key, and no answer carries
Access-Control-Allow-Origin, so the browser blocks the call. The API itself is fine —
the same request with a key returns the model list.

This is the case in #3208: a chatbot page served from a NAS, calling the app's shared
address (port 47832) from a browser on another device.

The app has two listeners, and only one of them handled CORS:

  • the app's own API address (127.0.0.1, port 47831 or the next free one) sets
    Access-Control-Allow-Origin: * and answers OPTIONS — but the desktop shell starts it
    on loopback only (STRATA_BIND=127.0.0.1 in apps/desktop/src-tauri/src/main.rs), so a
    second device can never reach it;
  • the share listener (0.0.0.0, the address shown under Serving when you share) is the one
    other devices use, and it sent no CORS headers at all. It passed OPTIONS straight to
    openai(), which checks the API key first — and a browser never sends a key on a
    preflight, so every preflight failed.

After this change the preflight is answered and every answer carries the CORS headers, so
the browser lets the call through. The key still decides who gets an answer.

#3208. The reporter's probes there (2026-10-21) are the reproduction, and they match what I
measured at v0.2.3.1 (see Validation). The 0.2.1 help text (d8a6a0e) says the app's API
"already allows pages of any site"; that is true for the loopback listener and not for the
shared address a second device has to use.

Approach

apps/host/server/local-share.mjs answers the preflight and sets the headers, before the
path allowlist and the key check:

  • OPTIONS → 204, because a browser sends no key with a preflight (the spec says so).
    The request that follows still goes through the path check and the key check, so a
    preflight grants nothing: /api/* stays 404 on this listener.
  • Access-Control-Allow-Origin: *, Allow-Methods: GET, POST, OPTIONS, Allow-Headers
    (the list the preflight asks for, else the headers the SDKs and Anthropic browser clients
    send), Max-Age: 600 — the same shape the engine's own server uses (serve/server.py,
    _cors()). Set with setHeader before dispatch, so the 404, 401 and 500 answers carry
    them too; a browser then sees the real 401 instead of a CORS wall.
  • Only when an API key is set. Without a key the listener behaves exactly as before and
    sends no CORS headers. That keeps the promise in SECURITY.md and in the app's own warning:
    a keyless share reaches devices on your network, and a web page of another site cannot
    read your model through it. A page can point its own name at a LAN address, and this
    listener has no Host check, so unconditional * would have widened that. With a key the
    key decides, and * is safe: the browser cannot produce the key, and the preflight itself
    answers nothing.

No new setting, nothing to switch on. The app's own listener, openai(),
serving.authorized(), the server.cors expert switch and its help text in eight languages
are untouched, so the default path is unchanged. The Cloudflare tunnel listener is the same
factory and is fixed by the same change.

Changed files: apps/host/server/local-share.mjs (+15), apps/host/tests/cors-share.test.mjs
(new, 4 checks), docs/DETAILS.md (the CORS paragraph). One commit, 1dc28d7b, on v0.2.3.1.

Validation

Revision: 1dc28d7b (one commit on 7ddc17a3 / tag v0.2.3.1). Environment: Linux x86_64,
Node 24.22.1, npm 11.19.1, no GPU model loaded (simulated engine).

Passed

  • npm ci && npm test in apps/host: 1081 tests, 976 passed, 1 failed (below), 104 skipped.
    The skips are gated on this machine: 97 need STRATA_HARNESS_DIR (a built Harness), one
    needs STRATA_TEST_BROWSER=1, one is the Windows PowerShell check.

  • The reporter's probes, run against a real host with sharing switched on through the app's
    own route (POST /api/serving/share {enabled:true}), an API key set, and the probes sent
    to the share port. Same script, same probes, both revisions:

    probe v0.2.3.1 (7ddc17a3) this change (1dc28d7b)
    OPTIONS /v1/models, Origin + Access-Control-Request-*, no auth 401, no CORS headers 204, allow-origin: *, allow-methods, allow-headers: authorization,content-type (echoed), max-age: 600
    GET /v1/models + key + Origin 200, no Access-Control-Allow-Origin 200 + access-control-allow-origin: *
    GET /v1/models, no key 401, no CORS headers 401 + the CORS headers
    GET /api/serving on the share port 404 404

    The left column reproduces the table in #3208 line for line.

  • New apps/host/tests/cors-share.test.mjs: preflight answered without a key and the
    requested headers echoed; Access-Control-Allow-Origin on the 200 and on the 401; a
    preflight grants nothing (/api/tasks still 404); and with no key set nothing changes —
    no CORS headers anywhere, and the keyless share still answers a device on the network.

  • One check that the headers survive the handlers: setHeader before a handler's
    writeHead(status, {...}) keeps both (Node 24, small probe), and a header the handler
    sets itself wins over the listener's. That is why no handler needed changing.

Failed

  • apps/host/tests/timeskip-real.test.mjs → "Stop ends the job and everything it started,
    deletes the copies, leaves the person's files alone, and the run then reads as cancelled":
    AssertionError: the revision command (a grandchild of the job) was ended too. It fails
    the same way at 7ddc17a3 in a clean worktree, and the test never touches the changed
    code. Pre-existing on this machine, not from this change.

Not run

  • CI: no workflow runs the apps/host tests. scripts-test.yml covers .github/scripts
    only, and its paths do not match this change. So the runs above are the whole evidence.
  • A real loaded model on a GPU: the native gateway's own writeHead path is covered by the
    check above and by reading, not by a live run. Needs a machine with a card.
  • A real browser on a second device, and Windows and macOS.

Limits

  • The end-to-end probes ran with the simulated engine, so the answers came from openai()'s
    JSON path. The native gateway path is verified by reading and by the header check, not by
    a live model.
  • Access-Control-Allow-Origin: * means cookies are never usable here; the API is keyed by
    header only, which is what OpenAI- and Anthropic-style browser clients send.
  • cors_origins still belongs to the engine's own server only. This change does not let
    anyone limit origins on the app's shared address: with a key set, any page may call it,
    and the key is the gate. A per-origin list there would be a separate feature.

🔄 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/Niko1221/Strata/pull/3853 **Author:** [@MaKla89](https://github.com/MaKla89) **Created:** 10/23/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix-3208-share-cors` --- ### 📝 Commits (1) - [`1dc28d7`](https://github.com/Niko1221/Strata/commit/1dc28d7bc25a270e169855087c1c52f497e9b313) App sharing: the shared and tunnel listener answers browser CORS when an API key is set - the preflight (OPTIONS, which never carries the key) before the path and key checks, Access-Control headers on every answer; without a key nothing changes, so a keyless share still reaches devices on the network only and no web page of another site can read the model; regression tests and the DETAILS CORS line (#3208) ### 📊 Changes **3 files changed** (+104 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `apps/host/server/local-share.mjs` (+15 -1) ➕ `apps/host/tests/cors-share.test.mjs` (+83 -0) 📝 `docs/DETAILS.md` (+6 -2) </details> ### 📄 Description … an API key is set - the preflight (OPTIONS, which never carries the key) before the path and key checks, Access-Control headers on every answer; without a key nothing changes, so a keyless share still reaches devices on the network only and no web page of another site can read the model; regression tests and the DETAILS CORS line (#3208) ## Problem and result A browser page on another machine cannot call the app's API over the local network. The browser's CORS preflight (`OPTIONS`) gets `401 Invalid API key`, and no answer carries `Access-Control-Allow-Origin`, so the browser blocks the call. The API itself is fine — the same request with a key returns the model list. This is the case in #3208: a chatbot page served from a NAS, calling the app's shared address (port 47832) from a browser on another device. The app has two listeners, and only one of them handled CORS: - the app's own API address (`127.0.0.1`, port 47831 or the next free one) sets `Access-Control-Allow-Origin: *` and answers `OPTIONS` — but the desktop shell starts it on loopback only (`STRATA_BIND=127.0.0.1` in `apps/desktop/src-tauri/src/main.rs`), so a second device can never reach it; - the share listener (`0.0.0.0`, the address shown under Serving when you share) is the one other devices use, and it sent no CORS headers at all. It passed `OPTIONS` straight to `openai()`, which checks the API key first — and a browser never sends a key on a preflight, so every preflight failed. After this change the preflight is answered and every answer carries the CORS headers, so the browser lets the call through. The key still decides who gets an answer. ## Related discussion #3208. The reporter's probes there (2026-10-21) are the reproduction, and they match what I measured at v0.2.3.1 (see Validation). The 0.2.1 help text (d8a6a0e) says the app's API "already allows pages of any site"; that is true for the loopback listener and not for the shared address a second device has to use. ## Approach `apps/host/server/local-share.mjs` answers the preflight and sets the headers, before the path allowlist and the key check: - `OPTIONS` → `204`, because a browser sends no key with a preflight (the spec says so). The request that follows still goes through the path check and the key check, so a preflight grants nothing: `/api/*` stays 404 on this listener. - `Access-Control-Allow-Origin: *`, `Allow-Methods: GET, POST, OPTIONS`, `Allow-Headers` (the list the preflight asks for, else the headers the SDKs and Anthropic browser clients send), `Max-Age: 600` — the same shape the engine's own server uses (`serve/server.py`, `_cors()`). Set with `setHeader` before dispatch, so the 404, 401 and 500 answers carry them too; a browser then sees the real 401 instead of a CORS wall. - **Only when an API key is set.** Without a key the listener behaves exactly as before and sends no CORS headers. That keeps the promise in SECURITY.md and in the app's own warning: a keyless share reaches devices on your network, and a web page of another site cannot read your model through it. A page can point its own name at a LAN address, and this listener has no Host check, so unconditional `*` would have widened that. With a key the key decides, and `*` is safe: the browser cannot produce the key, and the preflight itself answers nothing. No new setting, nothing to switch on. The app's own listener, `openai()`, `serving.authorized()`, the `server.cors` expert switch and its help text in eight languages are untouched, so the default path is unchanged. The Cloudflare tunnel listener is the same factory and is fixed by the same change. Changed files: `apps/host/server/local-share.mjs` (+15), `apps/host/tests/cors-share.test.mjs` (new, 4 checks), `docs/DETAILS.md` (the CORS paragraph). One commit, `1dc28d7b`, on v0.2.3.1. ## Validation Revision: `1dc28d7b` (one commit on `7ddc17a3` / tag v0.2.3.1). Environment: Linux x86_64, Node 24.22.1, npm 11.19.1, no GPU model loaded (simulated engine). **Passed** - `npm ci && npm test` in `apps/host`: 1081 tests, 976 passed, 1 failed (below), 104 skipped. The skips are gated on this machine: 97 need `STRATA_HARNESS_DIR` (a built Harness), one needs `STRATA_TEST_BROWSER=1`, one is the Windows PowerShell check. - The reporter's probes, run against a real host with sharing switched on through the app's own route (`POST /api/serving/share {enabled:true}`), an API key set, and the probes sent to the share port. Same script, same probes, both revisions: | probe | v0.2.3.1 (`7ddc17a3`) | this change (`1dc28d7b`) | | --- | --- | --- | | `OPTIONS /v1/models`, `Origin` + `Access-Control-Request-*`, no auth | `401`, no CORS headers | `204`, `allow-origin: *`, `allow-methods`, `allow-headers: authorization,content-type` (echoed), `max-age: 600` | | `GET /v1/models` + key + `Origin` | `200`, no `Access-Control-Allow-Origin` | `200` + `access-control-allow-origin: *` | | `GET /v1/models`, no key | `401`, no CORS headers | `401` + the CORS headers | | `GET /api/serving` on the share port | `404` | `404` | The left column reproduces the table in #3208 line for line. - New `apps/host/tests/cors-share.test.mjs`: preflight answered without a key and the requested headers echoed; `Access-Control-Allow-Origin` on the 200 and on the 401; a preflight grants nothing (`/api/tasks` still 404); and with no key set nothing changes — no CORS headers anywhere, and the keyless share still answers a device on the network. - One check that the headers survive the handlers: `setHeader` before a handler's `writeHead(status, {...})` keeps both (Node 24, small probe), and a header the handler sets itself wins over the listener's. That is why no handler needed changing. **Failed** - `apps/host/tests/timeskip-real.test.mjs` → "Stop ends the job and everything it started, deletes the copies, leaves the person's files alone, and the run then reads as cancelled": `AssertionError: the revision command (a grandchild of the job) was ended too`. It fails the same way at `7ddc17a3` in a clean worktree, and the test never touches the changed code. Pre-existing on this machine, not from this change. **Not run** - CI: no workflow runs the `apps/host` tests. `scripts-test.yml` covers `.github/scripts` only, and its paths do not match this change. So the runs above are the whole evidence. - A real loaded model on a GPU: the native gateway's own `writeHead` path is covered by the check above and by reading, not by a live run. Needs a machine with a card. - A real browser on a second device, and Windows and macOS. ## Limits - The end-to-end probes ran with the simulated engine, so the answers came from `openai()`'s JSON path. The native gateway path is verified by reading and by the header check, not by a live model. - `Access-Control-Allow-Origin: *` means cookies are never usable here; the API is keyed by header only, which is what OpenAI- and Anthropic-style browser clients send. - `cors_origins` still belongs to the engine's own server only. This change does not let anyone limit origins on the app's shared address: with a key set, any page may call it, and the key is the gate. A per-origin list there would be a separate feature. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:39:09 +07:00
Sign in to join this conversation.
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
Niko1221/Strata#4801
No description provided.