A malformed body gets a sentence and a status; a bad document cannot empty the board (#1309) #1310
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
jaylouisw/packetforge!1310
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/bad-request-handling"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 /loademptied the board and said it workedThe live topology is replaced by whatever the body says, and
Topology.fromJSON()is (deliberately) tolerant, soa 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
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
Fixes
/loadchecks the envelope first (format,devices,links) and refuses a document with neitherdevicesnor
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,/pingand/flowsvalidate their bodies and answer400 E_BAD_REQUESTnaming what wasexpected and what was missing; engine refusals keep their
409/422and their codes;404, now naming the id.Evidence
test/api-errors.test.js— 13 checks over 21 malformed requests, asserting rules rather than today's strings:Cannot read propert…,is not a function,undefined is not,[object Object]);undefinedby accident;the port really down afterwards.
With
server/index.jsreverted, 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.
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.