Skip to content

fix(memory): refuse browser cross-origin requests on admin routes (#468) - #517

Open
insuffer wants to merge 2 commits into
MemTensor:mainfrom
insuffer:fix/admin-route-browser-guard
Open

insuffer wants to merge 2 commits into
MemTensor:mainfrom
insuffer:fix/admin-route-browser-guard

Conversation

@insuffer

Copy link
Copy Markdown
Contributor

What

Fixes #468 (unauthenticated local denial of service): POST /api/v1/admin/shutdown is reachable from a web page the user visits, which stops the Memory service.

Root cause

/api/v1/admin/shutdown and /api/v1/admin/reload-config guard on requireAdminWrite(principal). With no localServiceToken configured — the default — authenticate() falls through to the anonymous principal, which carries scopes: ["*"], and requireAnyScope short-circuits on *. So the guard always passes for an anonymous caller. The service listens on 127.0.0.1:18960, which does not stop a browser page from reaching it (directly, or via a DNS rebinding that makes the browser treat the loopback address as same-origin).

Fix

Admin routes now refuse requests that can only originate from a browser page on another origin:

  • a non-loopback Origin header, or
  • Sec-Fetch-Site: cross-site (catches rebinding-shaped attempts that smuggle no Origin).

The check mirrors what assertLocalViewerRequest already applies to the Viewer API (Memory/src/server/viewer-api.ts — the new assertNotBrowserCrossOrigin lives beside it and reuses its loopback logic). Loopback Origins — the desktop renderer — stay allowed. Non-browser clients (the desktop app's main process, the CLI in runtime-installer.ts, the backend) send neither header and are unaffected.

Verification

Windows 11, Memory workspace:

  • 6 new tests in Memory/tests/admin-browser-guard.test.ts: cross-origin Origin → 403; Sec-Fetch-Site: cross-site without Origin → 403; unparseable Origin → 403; headerless request (the desktop/CLI shape) → 200 and the shutdown callback fires; loopback Origin → 200; the sibling admin route guarded the same way.
  • Reverting only the source change makes the 4 attack-vector tests fail, so the tests capture the vulnerability.
  • tsc -p tsconfig.json --noEmit clean; existing viewer-api, http-startup, server-entrypoint, server-lock suites pass (26 tests).

Scope notes

cleanGeneratedTitle only trimmed quotes, prefixes and punctuation, so a
reasoning model answering the title prompt with its own chain of thought
stored the raw <think>…</think> text as the session title (MemTensor#432).
reasoningEffort is none, but inherently-reasoning models ignore it, so
strip defensively with the existing stripThink helper. When the model
produces only thinking, the title stays empty and the session keeps its
default title instead of exposing reasoning.

Adds unit tests for cleanGeneratedTitle and two WebuiTitleService tests;
each fails on the unpatched tree.
…mTensor#468)

The Memory service is a machine-local API, but /api/v1/admin/shutdown
and /api/v1/admin/reload-config accepted any caller that passed
requireAdminWrite — and with no localServiceToken configured the
anonymous principal carries scopes ["*"], so the check always passed.
A page the user visits can therefore POST /api/v1/admin/shutdown and
stop the service: a local denial of service.

Admin routes now reject requests that can only come from a browser page
on another origin: a non-loopback Origin header, or Sec-Fetch-Site:
cross-site (which also catches DNS-rebinding-shaped attempts). The
check mirrors the one assertLocalViewerRequest already applies to the
Viewer API; loopback Origins (the desktop renderer) stay allowed, and
non-browser clients — the desktop main process, the CLI, the backend —
send neither header and are unaffected.

Adds 6 tests: each browser vector is refused (403), and the headerless
and loopback-Origin paths still reach the route and fire the shutdown
callback. Fixes MemTensor#468.
@insuffer

Copy link
Copy Markdown
Contributor Author

Hi — this is a security fix for #468 (a web page can shut the local Memory service down through an unauthenticated POST /api/v1/admin/shutdown). Admin routes now refuse requests that can only come from a browser page on another origin — a non-loopback Origin header or Sec-Fetch-Site: cross-site — mirroring the Viewer API's existing local-request guard; the desktop app, CLI, and backend are non-browser clients and are unaffected. The Linux CLI installer and Windows Memory validation workflow runs are waiting for fork approval — could a maintainer approve them so CI can validate the change? Thanks!

This branch has not been deployed

No deployments
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.

Local sensitive logic exposure lead to Dos attack

1 participant