Skip to content

fix(api): answer 400, not 500, for bad request input - #1198

Open
mortondev wants to merge 3 commits into
mainfrom
fix/rest-input-errors
Open

mortondev wants to merge 3 commits into
mainfrom
fix/rest-input-errors

Conversation

@mortondev

@mortondev mortondev commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What was wrong

Several /api/v1 routes answered 500 when the caller sent bad input:

  • A body that is not JSON hit a bare request.json(). Its SyntaxError has no domain code, so handleDomainError mapped it to 500.
  • A malformed cursor or id filter went straight into a typeid column, and the driver threw.
  • ?limit=0 or -1 on the changelog answered 200 with hasMore: true and a null cursor. ?limit=abc quietly fell back to the default.
  • An article create or move into an unknown category, and feedback on an unknown article, hit a foreign key violation.

What changed

All of this lives in shared helpers (lib/server/domains/api/request.ts), and every v1 route uses them:

  • readJsonBody(request). No body gives undefined, and the route's schema says what is missing. A body that is not JSON is a 400 with Request body must be valid JSON. All 50 v1 files that read a body now call it. A guard test (json-body-guard.test.ts) fails if a bare request.json() comes back.
  • parseLimit / parseOffset. A limit below 1 or not a whole number is a 400. A limit above the cap is still capped. parsePaginationParams uses parseLimit. Applies to changelog, posts, status incidents, help center articles, conversations, messages, tickets, companies and voters.
  • parseCursor. Changelog, posts, incidents, articles, conversations and messages cursors must be a TypeID, or the request is a 400 Invalid cursor format. Any well-formed TypeID is accepted, even one of another type, and a cursor that names no row pages from the start. API-R-106 pins that behaviour. Every list hands out row ids as cursors, so real cursors still work. A new test feeds a returned cursor back in.
  • Id filters are TypeID-checked. This covers articles categoryId, conversations assignedAgentPrincipalId, and tickets requesterPrincipalId and companyId. A malformed value is a 400.
  • Help center references:
    • On create and on PATCH, the route checks the format of the article's categoryId.
    • The service refuses a category that does not exist or was deleted (400 Category not found).
    • recordArticleFeedback checks the article exists and throws ARTICLE_NOT_FOUND (404 Help center article not found). It only checks that the row exists, so feedback on draft or deleted articles behaves as before.
  • Status services:
    • The /status/services path says "service" in its malformed-id and not-found errors. The legacy /status/components path still says "component". Both share one handler and pass the noun.
    • An incident naming a service id that never existed answers An affected service does not exist. Before, it said the service had been deleted. A soft-deleted service keeps the deleted wording.
  • suggestions/:id/accept. The optional body is now checked with a schema. Before, a broken body was silently swallowed.

One passing e2e test needs updating: API-R-001 on #1167 (rest-posts.spec.ts, test/product-e2e-api-rest) pins the old lenient posts limit: limit=-5 gives 1 item and limit=abc gives 20. With this PR both answer 400, as asked for consistently across the list routes. The test's expectations should change to 400.

The moderation/*/reject routes still accept an empty body. A body that is present but not JSON is now a 400. Before, it was silently treated as {}.

Left to the sibling PRs: users list paging, roadmap posts paging and segments id handling are fixed in fix/rest-people-errors and fix/rest-taxonomy-roadmaps-apps. This PR only swaps request.json() for readJsonBody in their files. Their users route calls parsePaginationParams, so it picks up the stricter limit check. git merge-tree shows no conflicts with either of them, or with fix/rest-post-writes.

Quarantined e2e tests this fixes

On #1160 (test/product-e2e-api-rest-content):

  • API-R-106: a malformed cursor is refused as bad input
  • API-R-111: a changelog body that is not JSON
  • API-R-126: a status body that is not JSON
  • API-R-127: the services path names a service
  • API-R-132: an unknown service id is not reported as deleted
  • API-R-163: a help center body that is not JSON
  • API-R-165: a malformed categoryId filter
  • API-R-166: an article in a malformed or unknown category
  • API-R-171: feedback for an unknown article is not found

On #1165 (test/product-e2e-api-rest-keys):

  • API-K-34: a garbage cursor never answers 500 (posts, changelog, status incidents, help center articles)

Tests

All of these failed on main first:

  • routes/api/v1/__tests__/bad-input.db.test.ts runs the real route handlers against Postgres, with only the key check stubbed. It covers non-JSON bodies, malformed and unknown cursors, id filters, limits, help center references and the services wording.
  • help-center-unknown-references.db.test.ts and new cases in status.lifecycle.db.test.ts cover the service-level checks.
  • request.test.ts covers the helpers (readJsonBody, parseLimit, parseOffset, parseCursor).
  • json-body-guard.test.ts is the source guard.

responses.test.ts, articles.test.ts and help-center-article-feedback.service.test.ts were updated for the new contract.

@mortondev
mortondev force-pushed the fix/rest-input-errors branch 2 times, most recently from 0a1d053 to 3fbf326 Compare November 20, 2026 12:02
@mortondev mortondev added the run-product-e2e Run the product e2e core suite on this PR label Nov 20, 2026
A body that is not JSON, a malformed id in a cursor or filter, a bad
limit, and an unknown category or article in a help center write all
reached the database or a bare request.json() and came back as a 500.

- readJsonBody reads every /api/v1 body: no body is undefined, a body
  that is not JSON is a 400. A guard test keeps bare request.json() out.
- parseLimit and parseOffset: a limit below 1 or not a whole number is
  a 400; above the cap it is still capped.
- Id cursors and id filters (changelog, posts, incidents, articles,
  conversations, messages, tickets) are TypeID-checked; a well-formed
  unknown cursor still pages from the start.
- Articles refuse a category that does not exist or was deleted; feedback
  on an unknown article is a 404.
- The services path names a service in its errors; an incident naming a
  service that never existed says so instead of calling it deleted.
@mortondev
mortondev force-pushed the fix/rest-input-errors branch from 3fbf326 to 0d9c8e4 Compare November 20, 2026 12:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-product-e2e Run the product e2e core suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant