MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

/product REST surface is unauthenticated by default; user_id is client-supplied (cross-user IDOR) and doc_path reads arbitrary server files

Open EvolveAegis opened this issue 3 months ago • 4 comments

What

On the self-hosted MemOS server (src/memos/api/server_api.py), the /product router (src/memos/api/routers/server_router.py:68) exposes memory read/write, cube, scheduler, and chat endpoints with no authentication dependency, while auth is off by default.

  • Every /product handler is a plain def f(req) with no Depends(verify_api_key) — e.g. search_memories (server_router.py:112), add_memories (:128), create_cube (:143), chat_complete (:271), scheduler_status (:208). Compare the admin router, which gates every route with auth: dict = Depends(verify_api_key) (admin_router.py:81,128,158,190). The /product router is included with no dependencies=[...] either (server_api.py:50).
  • AUTH_ENABLED defaults to false (src/memos/api/middleware/auth.py:26), and even when enabled, verify_api_key returns an all-scopes context when AUTH_ENABLED is false (auth.py:173), so the admin routes are open by default too.
  • user_id is taken from the request (e.g. scheduler_status(user_id: str = Query(...)) at server_router.py:209), never authenticated — a caller picks any user id.
  • doc_path on add/search flows to _get_all_documents (src/memos/mem_os/core.py:233), which does Path(path).rglob("*") over the caller-supplied path and returns every .txt/.pdf/.json/.md/.ppt(x) file under it for the server to read and process.

Net effect: by default, an unauthenticated caller can read/write any user's memories (pick user_id), enumerate/read arbitrary server-side documents (doc_path=/etc, a home dir, etc.), and drive the chat/scheduler surface.

There is a related secondary bypass: is_internal_request (auth.py:147) returns True when the X-Internal-Service header equals os.getenv("INTERNAL_SERVICE_SECRET"); if that env var is unset (default) and the header is absent, the comparison is None == None → the request is treated as internal/full-scope even when AUTH_ENABLED=true.

How to reproduce

Default config (no AUTH_ENABLED, no INTERNAL_SERVICE_SECRET), server running on 127.0.0.1:5050 (or wherever it binds):

# 1. unauthenticated access — no API key, no header
curl -s http://127.0.0.1:5050/product/scheduler/allstatus | head

# 2. cross-user IDOR — pick another user's id
curl -s -X POST http://127.0.0.1:5050/product/search \
  -H 'Content-Type: application/json' \
  -d '{"user_id":"victim","query":"*","cube_id":"default"}' | head

# 3. arbitrary server-side file read via doc_path (rglob under any path)
curl -s -X POST http://127.0.0.1:5050/product/add \
  -H 'Content-Type: application/json' \
  -d '{"user_id":"attacker","cube_id":"default","doc_path":"/etc"}' | head

All three return 200 with data and no 401/403. The handler signatures in server_router.py confirm there is no auth dependency to trigger a 401.

Checked at 93e4082 (main HEAD).

Impact / scope

  • This is about the default configuration of the self-hosted REST server. If the server is reachable beyond a single trusted loopback (container with a published port, a shared host, a reverse proxy), the above is unauthenticated and remote.
  • Even on loopback, the user_id-from-request design is a cross-user IDOR (one local principal reads/writes another's memories), and doc_path is an arbitrary server-side file read limited only by the process's filesystem permissions and the extension allowlist.
  • A SECURITY.md / documented trust model would help set expectations; right now there is none in the core service, so I'm reporting the default-unauth + IDOR + file-read posture as-is.

Suggested change

  • Put a Depends(verify_api_key) (or an equivalent) on the /product router, or gate it at include_router(..., dependencies=[Depends(verify_api_key)]).
  • Default AUTH_ENABLED to true (or require an explicit opt-in to the unauthenticated mode), and document the deployment boundary.
  • Derive user_id from the authenticated principal instead of a client-supplied query/body field.
  • Clamp doc_path to an allowlisted documents root rather than rglob-ing an arbitrary caller path; and fix the None == None internal-service check (require the secret to be set, and compare securely).

Happy to open a PR for the Depends(verify_api_key) + user_id parts.

EvolveAegis avatar Aug 04 '26 01:08 EvolveAegis

Correction on the doc_path point: I over-stated it for the REST surface. On /product/add, APIADDRequest.doc_path is deprecated and converted into a file-message (product_models.py:806-817 -> {"file":{"path": doc_path}}); the file parser does not fetch local paths, so the REST endpoint does not read arbitrary server files via doc_path. The _get_all_documents rglob sink (mem_os/core.py:233) is reachable from the MCP server's add_memory(doc_path=…) (mcp_serve.py:333), not from this REST route. The unauthenticated-REST and client-supplied-user_id (IDOR) parts of this report stand as written; only the REST doc_path file-read line is withdrawn (it belongs to the MCP surface).

EvolveAegis avatar Aug 04 '26 03:08 EvolveAegis

Additional unauthenticated destructive endpoints on the same /product surface: delete_memory_by_record_id and recover_memory_by_record_id (server_router.py, ~:460-500) take a client-supplied mem_cube_id and operate directly on the graph db with no auth — so an unauthenticated caller can delete/recover any cube's memory nodes by record id. Same root cause (no Depends(verify_api_key) on the /product router, mem_cube_id/user_id client-supplied).

EvolveAegis avatar Aug 04 '26 07:08 EvolveAegis

This issue has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Sep 05 '26 02:09 github-actions[bot]

This issue has been automatically closed due to inactivity.

github-actions[bot] avatar Sep 15 '26 02:09 github-actions[bot]