Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
5.9 KiB
Code review
Phase 2 review of the whole tree. Every source and test file was read again
(server.py, router.py, handlers.py, db.py, models.py, validation.py,
responses.py, errors.py, static/index.html and all ten test modules).
The full list of weaknesses is below, then the three most important ones that
were fixed, with the tests that now cover them. The suite went from 125 to 137
tests during this pass and stays green.
Weaknesses found
| # | Where | Problem | Severity | Status |
|---|---|---|---|---|
| 1 | handlers.read_body |
Content-Length was trusted blindly: no upper bound, negative values accepted, so one request could allocate unbounded memory |
High | Fixed |
| 2 | handlers.handle_one |
The catch-all answered 500 {"error": "internal server error: <exception>"}, leaking SQL, paths and library internals to the client; sqlite3 failures were indistinguishable from bugs |
High | Fixed |
| 3 | router + db |
A mutation and its activity entry were two separate commits, and a failed statement left an open transaction on the shared connection, so a later unrelated write could commit half a change and the log could drift from the data | High | Fixed |
| 4 | validation.clean_reference |
The message used table[:-1], producing "does not reference an existing companie" |
Low | Fixed (caught by the new validation unit tests) |
| 5 | db.Store.update / delete |
The existence check ran outside the lock, so a concurrent delete could turn a request into a 500 instead of a 404 | Medium | Fixed together with #3 |
| 6 | db.Store._filters |
?q= is interpolated into a LIKE pattern without escaping, so % and _ in a search behave as wildcards |
Low | Not fixed (no injection risk, only fuzzy matching); worth an ESCAPE clause |
| 7 | router.pagination |
limit has no upper bound, so ?limit=100000000 is allowed |
Medium | Not fixed; a cap (say 1000) plus a Retry-After-free 400 would be the fix |
| 8 | db schema |
The activity table is append-only with no retention, and there are no indexes on deals.stage, notes.deal_id or activity.deal_id |
Medium | Not fixed; fine at this size, would matter on a long-lived server |
| 9 | router.serve_static |
Static files are read fully into memory per request, with no ETag/Last-Modified/caching |
Low | Not fixed |
| 10 | db.Store |
One shared connection serialises every write; no WAL mode | Low | Not fixed; adequate for a single-user local tool |
| 11 | router.handle and handlers.handle_one |
Both mapped exceptions to statuses independently, so a new error type had to be handled twice | Medium | Fixed as part of #2 (responses.error_response) |
| 12 | API semantics | PUT replaces every editable field, so a partial body silently clears the rest. Required by the spec, but easy to misuse from a client |
Low | Documented, not changed |
| 13 | static/index.html |
No optimistic concurrency: two tabs editing the same deal overwrite each other; fillForm also has a hard-to-read nested ternary |
Low | Not fixed |
| 14 | tests/test_live_server.py |
The only test that touches a real socket is skipped in sandboxes that forbid bind(), so end-to-end coverage is environment dependent |
Medium | Accepted; the in-process harness still drives the real handler class |
| 15 | Security overall | No authentication, CSRF protection or rate limiting; the server binds to 127.0.0.1 only | n/a | Accepted for a local tool |
The three fixes
1. Request body limits (handlers.py)
read_body now rejects a missing-looking or negative Content-Length with a
400 and refuses any body larger than handlers.MAX_BODY_BYTES (1 MB) with a
new PayloadTooLargeError mapped to 413. Notes are capped at 4000
characters, so the limit is far above any legitimate payload.
Covered by tests/test_robustness.py::BodyLimitTest (oversized body, body at
the limit, negative and non-numeric Content-Length).
2. One error mapping, no internal leakage (responses.py, router.py, handlers.py)
responses.error_response(exc) is now the single place that turns an
application error into a status (400 validation, 409 conflict, 413 payload,
404 not found, 405 method), and both the router and the HTTP adapter use it.
Anything unexpected is logged with a full traceback on the server side and
answered with the fixed body {"error": "internal server error"};
sqlite3.IntegrityError becomes 409 "the change conflicts with existing
data" instead of a 500. A dropped client connection (BrokenPipeError,
ConnectionResetError) no longer triggers a second send attempt.
Covered by tests/test_robustness.py::ErrorMappingTest, which asserts that a
forced sqlite3.OperationalError produces a 500 whose body does not contain
the failing SQL text.
3. Atomic mutations and transaction hygiene (db.py, router.py)
Store.transaction() is a context manager that holds the lock, commits on
success and rolls back on any exception. create, update, delete and
add_note each take a log_action argument and write their activity row
inside that same transaction, so a record and its log entry can never drift
apart. The existence checks moved inside the lock, which also removes the
check-then-act race that used to turn a concurrent delete into a 500. The
router no longer writes to the log separately.
Covered by tests/test_robustness.py::AtomicActivityTest: exactly one entry
per successful mutation, none for a rejected request, a foreign key violation
leaves both the row and the log untouched, and no transaction is left open
after a failure.
Deliberately left alone
Items 6 to 15 above are recorded rather than fixed: they are either mandated
by the specification (PUT semantics), acceptable for a loopback-only local
tool (no auth, one connection), or performance work that needs a real workload
to justify. Item 4 was a genuine bug and was fixed as a side effect of the
validation unit tests.