A malformed body gets a sentence and a status; a bad document cannot empty the board (#1309) #1310

Merged
jaylouisw merged 1 commit from fix/bad-request-handling into main 2026-11-10 01:06:50 +00:00
Owner

A malformed body gets a sentence and a status, and a bad document cannot empty the board.

Fixes #1309.

Every test in this repository drove the engine. I drove the HTTP surface with requests malformed on purpose, and
three failures came out — one of them data loss.

1. POST /load emptied the board and said it worked

POST /api/topologies/default/load   {"format":"x"}
  -> 200 {"ok":true,"state":{"devices":[],"links":[]}}

The live topology is replaced by whatever the body says, and Topology.fromJSON() is (deliberately) tolerant, so
a document with the wrong format marker and no devices becomes an empty lab — reported as success, with the
operator's work gone. No shape check of any kind before the swap.

2. A missing body was dereferenced straight through

POST .../config/d1   (no body)  -> 400 {"error":"Cannot read properties of undefined (reading 'trim')"}
POST .../links       (no body)  -> 409 {"error":"Cannot read properties of undefined (reading 'deviceId')"}

The runtime's own words — and the second calls a missing body a conflict. {commands:[null]} and
{commands:"hostname X"} leak the same way.

3. A request that named nothing blamed a device

POST .../ping   (no body) -> 404 source device not found
POST .../flows  (no body) -> 400 "Unknown source device: undefined"

Fixes

  • /load checks the envelope first (format, devices, links) and refuses a document with neither devices
    nor links, because loading that empties the board. Topology.fromJSON() stays tolerant — it reads old files —
    but tolerance belongs to reading, not to replacing;
  • /config, /links, /ping and /flows validate their bodies and answer 400 E_BAD_REQUEST naming what was
    expected and what was missing; engine refusals keep their 409/422 and their codes;
  • an id that does not exist is still a 404, now naming the id.

Evidence

test/api-errors.test.js — 13 checks over 21 malformed requests, asserting rules rather than today's strings:

  • no response contains the runtime's own words (Cannot read propert…, is not a function, undefined is not,
    [object Object]);
  • 15 rejections carry the status a client can act on;
  • every rejection is a sentence of at least 12 characters that does not say undefined by accident;
  • five documents that would have emptied the board are all refused, with the board untouched;
  • a real document still loads with its verdicts;
  • the endpoints that work still work — config with a list and with the single-command form, ping, port toggle, and
    the port really down afterwards.

With server/index.js reverted, 7 of the 13 fail, quoting the exact JavaScript messages.

docs/ARCHITECTURE.md: "What a wrong request gets" — the table, and the two rules behind it.

Suite: 170 suites, 6,877 assertions, 0 failed.

A malformed body gets a sentence and a status, and a bad document cannot empty the board. Fixes #1309. Every test in this repository drove the engine. I drove the HTTP surface with requests malformed on purpose, and three failures came out — one of them data loss. ## 1. `POST /load` emptied the board and said it worked ``` POST /api/topologies/default/load {"format":"x"} -> 200 {"ok":true,"state":{"devices":[],"links":[]}} ``` The live topology is replaced by whatever the body says, and `Topology.fromJSON()` is (deliberately) tolerant, so a document with the wrong format marker and no devices becomes an empty lab — reported as success, with the operator's work gone. No shape check of any kind before the swap. ## 2. A missing body was dereferenced straight through ``` POST .../config/d1 (no body) -> 400 {"error":"Cannot read properties of undefined (reading 'trim')"} POST .../links (no body) -> 409 {"error":"Cannot read properties of undefined (reading 'deviceId')"} ``` The runtime's own words — and the second calls a missing body a **conflict**. `{commands:[null]}` and `{commands:"hostname X"}` leak the same way. ## 3. A request that named nothing blamed a device ``` POST .../ping (no body) -> 404 source device not found POST .../flows (no body) -> 400 "Unknown source device: undefined" ``` ## Fixes * `/load` checks the envelope first (`format`, `devices`, `links`) and refuses a document with neither `devices` nor `links`, because loading that empties the board. `Topology.fromJSON()` stays tolerant — it reads old files — but tolerance belongs to reading, not to replacing; * `/config`, `/links`, `/ping` and `/flows` validate their bodies and answer `400 E_BAD_REQUEST` naming what was expected and what was missing; engine refusals keep their `409`/`422` and their codes; * an id that does not exist is still a `404`, now naming the id. ## Evidence `test/api-errors.test.js` — 13 checks over **21 malformed requests**, asserting rules rather than today's strings: * no response contains the runtime's own words (`Cannot read propert…`, `is not a function`, `undefined is not`, `[object Object]`); * 15 rejections carry the status a client can act on; * every rejection is a sentence of at least 12 characters that does not say `undefined` by accident; * five documents that would have emptied the board are all refused, **with the board untouched**; * a real document still loads with its verdicts; * the endpoints that work still work — config with a list and with the single-command form, ping, port toggle, and the port really down afterwards. With `server/index.js` reverted, **7 of the 13 fail**, quoting the exact JavaScript messages. `docs/ARCHITECTURE.md`: "What a wrong request gets" — the table, and the two rules behind it. Suite: 170 suites, 6,877 assertions, 0 failed.
A malformed body gets a sentence and a status, and a bad document cannot empty the board (#1309)
All checks were successful
CI / Classify the change (pull_request) Successful in 14s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 16s
CI / Test (Node 20) (pull_request) Successful in 2m9s
CI / Test (Node 18) (pull_request) Successful in 2m17s
CI / Server smoke test (pull_request) Successful in 28s
CI / Docker build (pull_request) Successful in 41s
CI / Test (Node 22) (pull_request) Successful in 1m50s
2777b5436f
Every test in this repository drove the engine. I drove the HTTP surface with requests malformed on purpose, and
three failures came out - one of them data loss.

1. POST /load emptied the board and said it worked

  POST /api/topologies/default/load {"format":"x"}
    -> 200 {"ok":true,"state":{"devices":[],"links":[]}}

The live topology was replaced by whatever the body said, and Topology.fromJSON() is (deliberately) tolerant, so a
document with the wrong format marker and no devices became an empty lab, reported as success, with the
operator's work gone. There was no shape check of any kind before the swap.

2. A missing body was dereferenced straight through

  POST .../config/d1 (no body)  -> 400 {"error":"Cannot read properties of undefined (reading 'trim')"}
  POST .../links     (no body)  -> 409 {"error":"Cannot read properties of undefined (reading 'deviceId')"}

The runtime's own words, and the second calls a missing body a conflict. {commands:[null]} and
{commands:"hostname X"} leaked the same way.

3. A request that named nothing blamed a device

  POST .../ping  (no body) -> 404 source device not found
  POST .../flows (no body) -> 400 "Unknown source device: undefined"

Fixes:

  * /load checks the envelope first (format, devices, links) and refuses a document with neither devices nor
    links, because loading that empties the board. Topology.fromJSON() stays tolerant - it reads old files - but
    tolerance belongs to reading, not to replacing;
  * /config, /links, /ping and /flows validate their bodies and answer 400 E_BAD_REQUEST naming what was expected
    and what was missing. Errors from the engine (an impossible cable, a bad link state) keep their 409/422 and
    their codes;
  * an id that does not exist is still a 404, now naming the id.

test/api-errors.test.js: 13 checks over 21 malformed requests, asserting rules rather than today's strings -
no response contains the runtime's own words (Cannot read propert..., is not a function, undefined is not,
[object Object]); 15 rejections carry the status a client can act on; every rejection is a sentence of at least 12
characters that does not say "undefined" by accident; five documents that would have emptied the board are all
refused with the board untouched; a real document still loads with its verdicts; and the endpoints that work still
work (config with a list and with the single-command form, ping, port toggle, and the port really down afterwards).
With server/index.js reverted, 7 of the 13 fail, quoting the exact JavaScript messages.

docs/ARCHITECTURE.md: "What a wrong request gets" - the table, and the two rules behind it.

Suite: 170 suites, 6,877 assertions, 0 failed.
jaylouisw scheduled this pull request to auto merge when all checks succeed 2026-11-10 01:06:46 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge!1310
No description provided.