Skip to content

feat(security): lock the local API to the Modly app - #459

Closed
lightningpixel wants to merge 2 commits into
devfrom
feat/api-token
Closed

lightningpixel wants to merge 2 commits into
devfrom
feat/api-token

Conversation

@lightningpixel

Copy link
Copy Markdown
Owner

Closes #458

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.

How it works

  • Per-launch token: Electron generates 32 random bytes and starts the backend with MODLY_API_TOKEN and MODLY_ALLOWED_ORIGINS (dev renderer origin, file://, null). Never put on the main process env.
  • Renderer: a session.webRequest.onBeforeSendHeaders hook adds X-Modly-Token to requests from the main window to the API origin (port tracked across restarts). axios, fetch, SSE, three.js loaders and downloads need no change, and the renderer never sees the token.
  • Main process: Node calls go through a shared apiClient (axios) that sends the header; net.fetch calls add it explicitly.
  • Backend (api/services/api_auth.py): pure ASGI middleware, 401 without the right token (constant-time compare). Exempt: GET /health, OPTIONS, and the OrcaSlicer route. CORS only allows the app's origins; a 401 still carries CORS headers.
  • OrcaSlicer (external app, can't send headers): Generate asks GET /export/slicer-link for a URL whose path carries an HMAC of the source path; links expire with the launch.
  • Window: will-navigate / will-redirect keep the window on its own pages (links open in the browser), so no other page can receive the token.
  • Other callers: the token is written to api-token next to api-port (0600, removed at quit). mcp_server.py and modly-cli read it (modly-cli only sends it to loopback hosts); the agent's calls back into the API and extension subprocesses get it by env.
  • Tokenless mode: without MODLY_API_TOKEN (standalone uvicorn main:app, Jetson, modly-cli serve) the API stays open as before, with a startup warning. Docs updated.

Follow-ups outside this repo

  • The npm package modly-cli-mcp (recommended in Settings > Agent) must read api-token too, or it will get 401s.

Testing

  • npm test passes (Python 489, JS 343 + 237, 0 failures); lint clean; tsc unchanged from dev
  • New tests: api/tests/test_api_auth.py, slicer signature + extension env cases, electron/main/api-auth.test.ts (token, origins, API request match, navigation guard, token file), orcaSlicerLink.test.ts, modly-cli token file + loopback-only header
  • Maintainer check in the app: the API answers 401 to a browser request
  • Still to confirm on a packaged build: which origin file:// pages send (both file:// and null are allowed)

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.

- Electron generates a token per launch and passes it to the backend
  (MODLY_API_TOKEN) with the renderer's origins (MODLY_ALLOWED_ORIGINS).
- A webRequest hook adds X-Modly-Token to every renderer request to the
  API, so axios, fetch, SSE, three.js loaders and downloads need no
  change and the renderer never sees the token. Main-process calls go
  through a shared axios client that sends it.
- The backend rejects any request without the token (401), except
  GET /health, OPTIONS and the OrcaSlicer route; CORS only allows the
  app's origins.
- OrcaSlicer (an external app) gets a URL signed with an HMAC of the
  source path from GET /export/slicer-link; links expire with the launch.
- The app window can no longer navigate away from its own pages (links
  open in the browser), so no other page can receive the token.
- The token is written to api-token next to api-port (0600, removed at
  quit) for the MCP server and modly-cli; modly-cli only sends it to
  loopback hosts. The agent and extension subprocesses get it by env.
- Without MODLY_API_TOKEN (standalone uvicorn, Jetson, modly-cli serve)
  the API stays open, with a warning at startup.

Closes #458
The webRequest hook keyed on the destination alone; it now also requires
the request to come from the main window's webContents.
@lightningpixel

Copy link
Copy Markdown
Owner Author

Replaced by #463: web pages are kept off the API by origin (MODLY_ORIGINS) instead of a token.

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