Skip to content

feat(security): keep web pages off the local API (MODLY_ORIGINS) - #463

Merged
lightningpixel merged 2 commits into
devfrom
feat/api-origins
Nov 18, 2026
Merged

lightningpixel merged 2 commits into
devfrom
feat/api-origins

Conversation

@lightningpixel

@lightningpixel lightningpixel commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Closes #458. Replaces #459.

While Modly ran, any web page open in the user's browser could call the whole local API (allow_origins=["*"]): read workspace files, start generations, install packages, delete models.

This takes Ollama's approach instead of an API token: web pages are refused, everything else gets in with no setup.

How it works

  • Backend guard (api/services/api_guard.py, pure ASGI middleware, always on):
    • A request from a web page (it has an Origin, or Sec-Fetch-Site: cross-site|same-site) gets a 403 unless its origin is in MODLY_ORIGINS (comma-separated, * wildcards, e.g. http://localhost:*,chrome-extension://*, or *), or it is the API's own origin (/docs).
    • Any other request passes with no token: scripts, curl, modly-cli, the MCP server, modly-cli-mcp, OrcaSlicer, extensions, a URL typed in the browser.
    • <img> / <video> tags send no Origin: they go by the Referer's origin instead. Browsers drop it from HTTPS pages to the HTTP API, so the README points HTTPS sites to crossorigin="anonymous".
    • Host must be localhost, an IP address, or a name in MODLY_HOSTS. This blocks DNS rebinding, and the Jetson keeps working by its IP.
    • OPTIONS passes the gate and CORS answers it. A 403 still carries CORS headers, so the page can read why.
  • The app window: a packaged build loads file:// pages, which send Origin: null. A sandboxed iframe on any site sends that too. So Electron adds X-Modly-App: <per-launch secret> to the window's own API requests (session.webRequest.onBeforeSendHeaders, main window only). The secret reaches the backend by env only, and the backend removes it from os.environ so extensions don't inherit it. In dev, the renderer origin goes in MODLY_APP_ORIGINS.
  • Window navigation: will-navigate / will-redirect keep the window on its own pages, and links open in the browser.
  • Docs: api/README.md (calling the API from other apps) and the Jetson guide.

Compared with #459, this drops the api-token file, the signed OrcaSlicer links, and the token changes in modly-cli, the MCP server, the agent and the extensions. modly-cli-mcp needs no update.

Testing

  • Python: 575 tests OK, 17 new in api/tests/test_api_guard.py
  • JS: 239 pass, including the new electron/main/api-guard.test.ts. Lint clean; tsc errors are the same as on dev, none in the changed files
  • Real backend with curl:
    • script → 200
    • other site → 403
    • cross-site <img> → 403
    • Origin: null → 403, and 200 with the app header
    • origin listed in MODLY_ORIGINS → 200
    • rebinding Host → 403
    • /docs same-origin → 200
  • npm run dev: every app request is 200 (Queue polling, health, models)
  • Real Chromium (Electron) page calling the API: example.com not allowed → fetch, form-like no-cors POST and <img> all 403. Allowed → fetch and POST 200. <img> 200 from an allowed http://localhost:3000, still 403 from HTTPS (no Referer)
  • Packaged build: check that the file:// window works (generation, SSE, viewer meshes, thumbnails, OrcaSlicer link)

While Modly ran, any web page open in the browser could call the whole local
API. The backend now refuses requests from web pages (Origin, or a cross-site
Sec-Fetch-Site) unless their origin is in MODLY_ORIGINS, and refuses Host names
that are not localhost, an IP or in MODLY_HOSTS (DNS rebinding). Scripts, CLIs,
the MCP server, OrcaSlicer and extensions send neither header and need no token.

The packaged window's file:// pages send Origin: null, like a sandboxed iframe
on any site, so Electron adds X-Modly-App with a per-launch secret to the
window's API requests; the backend gets it by env and drops it from os.environ.
… Referer

Those tags send no Origin, so a site in MODLY_ORIGINS could not show a file
served by Modly. The guard now goes by the Referer's origin when there is no
Origin; a page can leave it out but not forge it. Browsers drop it from HTTPS
pages to the HTTP API, so the README points those to crossorigin="anonymous".
@lightningpixel
lightningpixel merged commit 7fe5ec4 into dev Nov 18, 2026
5 checks passed
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