#998·qmd

REST POST /query returns 500 or empty 200 for malformed searches

Author: ParkerRexCreated Sep 26, 2026Updated Sep 26, 2026

Impact

HTTP POST /query (and /search) does not validate the searches array. Malformed entries crash the handler with a 500 or are silently accepted:

Request body main @ 04e4dbd
{"searches":[null]} 500 Internal Server Error (TypeError on s.type)
{"searches":[{"type":"bogus","query":"x"}]} 200 {"results":[]}
{"searches":[]} 200 {"results":[]}
{"searches":[{"type":"lex","query":"\"unterminated"}]} 500 Internal Server Error
{"searches":[{"type":"vec","query":"a\nb"}]} 500 Internal Server Error

In the last two cases the store already has a clear message ("Lex query has an unmatched double quote", "queries must be single-line"), but it only reaches the server log.

Repro (main @ 04e4dbd)

bash
export QMD_CONFIG_DIR=/tmp/qmd-repro/cfg INDEX_PATH=/tmp/qmd-repro/index.sqlite
CI=true bun src/cli/qmd.ts mcp --http --port 18431 &
curl -s -w ' %{http_code}\n' -X POST -H 'Content-Type: application/json' \
  --data '{"searches":[null]}' http://localhost:18431/query
# Internal Server Error 500

Cause

src/mcp/server.ts:1044-1048 casts each entry (s.type as 'lex' | 'vec' | 'hyde') without checking it. Query syntax errors are thrown later by structuredSearch (src/store.ts:5978-5995) and fall through to the generic 500 handler.

Suggested fix

Parse searches with the same zod schema the MCP query tool uses (subSearchSchema, min 1, max 10) and run the store's query syntax checks before searching. Both return 400 with the message. A PR follows.

Follow-up to #929, which added the 400 for malformed JSON bodies.