Skip to content

fix(server): answer only same-origin pages and loopback host names - #131

Merged
jvdd merged 8 commits into
mainfrom
fix/same-origin-server
Sep 29, 2026
Merged

jvdd merged 8 commits into
mainfrom
fix/same-origin-server

Conversation

@jvdd

@jvdd jvdd commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Only the pages the server renders itself may read its answers.

Change

  • No CORS headers. The CORSMiddleware with allow_origins=["*"] is removed, together with its comment, which named a CORS_ORIGINS setting that did not exist.
  • The notebook shows the server's own page. show() in a notebook displayed a data: page in its iframe. That page has an opaque origin and called the server cross-origin, which was the only reason for the open CORS. Now the notebook and the browser both post the spec to /share and show the returned /view URL (_share_url), in an iframe or in the browser (_deliver_notebook(url, height), _deliver_browser(url, block)).
  • Loopback Host check. run_server(host, port) (server.py) is now the one place where show() and flexviz serve start uvicorn. A bind is loopback when every address it resolves to is loopback (is_loopback_bind), so 127.1, 127.0.0.2 and 0:0:0:0:0:0:0:1 count too. On a loopback bind it serves only a plain name[:port] Host whose name is localhost, a loopback IP literal, or the bind host; a DNS rebinding page gets 400. Starlette's TrustedHostMiddleware is not used: it splits Host at the first colon and so cannot accept [::1]:port. A non-loopback bind serves every Host: it is a deliberate network exposure with unknown names, and flexviz serve already warns about it (now also decided by address). An app you run yourself or mount with mount_into gets neither check; the embedding guide says so.
  • A notebook cell never blocks. show(notebook=False) inside a notebook opened the browser and then waited for Ctrl-C, which held the cell, although the docstrings say block is ignored in a notebook. _deliver_browser now waits only outside a running event loop, so the documented remote workaround works.
  • show_server is removed (separate commit). It was a third, unused start path (no caller, not exported) that would have skipped the Host check.
  • Docs: docs/index.md (notebooks on a remote kernel), Architecture.md (new "Origin" section, adapter tree, CLI line, a tech-debt row that called the /share step a debt), SECURITY.md, docs/guides/ai-agents.md, docs/guides/embedding.md, CHANGELOG.md.

Impact

  • Before, any page in the user's browser could read the registered data (as aggregates) through a running server: the server answered every origin with Access-Control-Allow-Origin: *. Measured in Chromium: a page on http://localhost:<port> read http://127.0.0.1:<port>/sources. Pages on a public origin may additionally meet Chromium's Local Network Access prompt; that was not measured.
  • A request with a foreign Host (DNS rebinding) was served. Now it gets 400 on a loopback bind.
  • ECharts (deprecated): in a notebook, height now sets only the iframe height, as in the browser path.

Review fixes

An external review found four gaps, all confirmed and fixed in separate commits:

  • A loopback bind spelled 127.1, 127.0.0.2 or 0:0:0:0:0:0:0:1 skipped the Host check (measured: a foreign Host got 200, against 400 for 127.0.0.1).
  • The Host parser accepted localhost:bad, evil@localhost and localhost/evil (not exploitable from a browser, but not the stated rule).
  • show(notebook=False) in a notebook blocked the cell, so the documented remote workaround hung.
  • SECURITY.md, Architecture.md and the server.py docstring said only the server's own pages read its answers; that holds among browser pages, not for scripts or the CLI.

Manual checks

  • VS Code, local kernel: notebook show() renders and all interactions work.
  • VS Code Remote-SSH: show(notebook=True) stays blank, because VS Code does not forward the port for a notebook output (not even after a wait, or after a click on a 127.0.0.1 link in the output). This is not a regression: before this PR the data: page showed the dashboard shell, but its requests to 127.0.0.1:<port> failed for the same reason. show(notebook=False) works, because VS Code forwards the port when it opens the browser, and after that show(notebook=True) on the same port works too. docs/index.md documents this. A notebook transport that needs no port is Notebook show() on a remote kernel needs a forwarded port #132.

Tests

  • TestSameOriginServer (tests/test_integration.py): no Access-Control-Allow-Origin for another origin, also on a preflight; run_server refuses a foreign or malformed Host ([, localhost:bad, evil@localhost, localhost/evil) on every loopback spelling (127.0.0.1, localhost, ::1, 127.1, 127.0.0.2, 0:0:0:0:0:0:0:1), accepts localhost (any case), [::1] and loopback IP literals, and serves every Host on 0.0.0.0. Each case fails before its fix. The 127.1 case is skipped on Windows, which does not resolve that short form (there it cannot be bound at all).
  • test_serve_warns_only_for_a_network_bind (tests/test_cli.py): no network warning for 0:0:0:0:0:0:0:1 or ::1, a warning for 0.0.0.0.
  • TestBrowserDelivery (tests/test_adapters.py): inside a running event loop _deliver_browser opens the page and does not wait; outside it still waits.
  • TestNotebookDelivery (tests/test_adapters.py, plotly and echarts): against a live server, the notebook iframe src is the server's /view URL. Fails before the fix (data:text/html;base64,...).
  • TestSameOriginBrowser (tests/test_browser.py): a page on another loopback origin cannot fetch the server (fails before the fix: 'read'); a dashboard renders inside an iframe on a page of another origin, the shape a notebook uses.
  • Tree merged with main, pytest tests flexviz_polars/tests: 2452 passed, 2 skipped.
  • Tree merged with main, pytest -m browser tests: 354 passed, 16 skipped. mkdocs build --strict passes.

@codspeed

codspeed Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing fix/same-origin-server (5117da9) with main (54241cf)

Open in CodSpeed

@jvdd
jvdd marked this pull request as ready for review September 29, 2026 13:19
@jvdd
jvdd merged commit 835c799 into main Sep 29, 2026
18 checks passed
@jvdd
jvdd deleted the fix/same-origin-server branch September 29, 2026 17:12
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