Repository navigation
feat(security): keep web pages off the local API (MODLY_ORIGINS) - #463
Merged
Merged
Conversation
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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
api/services/api_guard.py, pure ASGI middleware, always on):Origin, orSec-Fetch-Site: cross-site|same-site) gets a 403 unless its origin is inMODLY_ORIGINS(comma-separated,*wildcards, e.g.http://localhost:*,chrome-extension://*, or*), or it is the API's own origin (/docs).modly-cli, the MCP server,modly-cli-mcp, OrcaSlicer, extensions, a URL typed in the browser.<img>/<video>tags send noOrigin: they go by theReferer's origin instead. Browsers drop it from HTTPS pages to the HTTP API, so the README points HTTPS sites tocrossorigin="anonymous".Hostmust belocalhost, an IP address, or a name inMODLY_HOSTS. This blocks DNS rebinding, and the Jetson keeps working by its IP.OPTIONSpasses the gate and CORS answers it. A 403 still carries CORS headers, so the page can read why.file://pages, which sendOrigin: null. A sandboxed iframe on any site sends that too. So Electron addsX-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 fromos.environso extensions don't inherit it. In dev, the renderer origin goes inMODLY_APP_ORIGINS.will-navigate/will-redirectkeep the window on its own pages, and links open in the browser.api/README.md(calling the API from other apps) and the Jetson guide.Compared with #459, this drops the
api-tokenfile, the signed OrcaSlicer links, and the token changes in modly-cli, the MCP server, the agent and the extensions.modly-cli-mcpneeds no update.Testing
api/tests/test_api_guard.pyelectron/main/api-guard.test.ts. Lint clean;tscerrors are the same as ondev, none in the changed files<img>→ 403Origin: null→ 403, and 200 with the app headerMODLY_ORIGINS→ 200/docssame-origin → 200npm run dev: every app request is 200 (Queue polling, health, models)fetch, form-likeno-corsPOST and<img>all 403. Allowed →fetchand POST 200.<img>200 from an allowedhttp://localhost:3000, still 403 from HTTPS (no Referer)file://window works (generation, SSE, viewer meshes, thumbnails, OrcaSlicer link)