Skip to content

fix(platform): refuse a malformed body on project duplicate - #4236

Merged
yannickmonney merged 2 commits into
mainfrom
fix/duplicate-project-malformed-json
Oct 4, 2026
Merged

yannickmonney merged 2 commits into
mainfrom
fix/duplicate-project-malformed-json

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What changed

POST /api/app/projects/:id/duplicate read its optional body with c.req.json().catch(() => ({})). The .catch also swallowed the app door's 400 INVALID_JSON, so a body that was sent but did not parse (a lone {) duplicated the project under its default name. A required-body app write such as POST /api/app/tasks refuses that same body with INVALID_JSON.

  • readOptionalAppJsonBody(c) (backend/lib/app-json-body.ts) is the app-door twin of the REST door's readOptionalJsonBody. It reads an optional body through the door's one JSON reader (appJsonBody):
    • nothing sent, or only whitespace, reads as {};
    • a body that is sent must parse. A malformed one answers the door's 400 {error: "The request body is not valid JSON", code: "INVALID_JSON", requestId}, as a required body does.
  • The duplicate route reads its body through the new helper (a one-line change).
    • Order: requireSession and requireOrgMember still run first, so a malformed body without a session still answers 401. The refusal comes after those gates and before the route's own getProjectAuthContext, its project:create rate-limit charge and the duplicating transaction.
    • An empty body, {} and a valid {"name": …} behave as before.
    • A body the client stops sending now reaches the error handler as the client leaving, as a required body's read does. The old fallback duplicated the project in that case too.
  • Manual-layer registers: reference/error-codes.md (INVALID_JSON on the app door) and the matching reference/automation.md row now cover the duplicate door, and the row lists the new route test.

The app's own client always sends {} or {name} (app/lib/backend/projects.ts), so the UI is unaffected.

Not changed here: the same fallback on 11 other routes

As the issue's limits say, only the duplicate door was proved, and the dispatch kept the other routes unchanged. At this head, the routes below have all-optional bodies read with the same c.req.json().catch(() => ({})), so a malformed body still runs their default action. This is a source inventory, not a reproduction on each door; the new helper is ready for a separately scoped follow-up.

Route Source What a malformed body becomes
POST /api/app/legal-holds/matters/:matterId/close domains/legal_holds/routes.ts:189 close with no releaseReason
POST /api/app/legal-holds/release-requests/:requestId/reject domains/legal_holds/routes.ts:276 reject with no reason
POST /api/app/scim/disable domains/scim/routes.ts:210 disable (the body is empty by schema)
POST /api/app/tasks/:taskId/workflow/cancel domains/tasks/routes.ts:1302 the default stop (cancelled)
POST /api/app/retention/bounds/apply domains/retention/routes.ts:124 apply, skipping the STALE_PROPOSAL check that proposedHash arms
POST /api/app/object-storage/backfill domains/object_storage/routes.ts:125 a real backfill (dryRun defaults to false)
POST /api/app/audit-logs/integrity/verify domains/audit_logs/routes.ts:179 verify with default bounds
POST /api/app/google-drive/list-files domains/google_drive/routes.ts:126 list with no folderId or search
POST /api/app/onedrive/list-files domains/onedrive/routes.ts:134 list with no folderId or search
POST /api/app/onedrive/sharepoint/sites domains/onedrive/routes.ts:153 list sites with no search
POST /api/control/drain domains/control/routes.ts:62 drain every colour; not on /api/app, so the app reader does not cover it

The independent review (agent #10, TALE-562) found the six routes from retention through SharePoint. My first sweep missed them because I cut its output at 40 lines.

How I verified it

All checks ran in the sandbox on base 8a580fcc2, origin/main at branch time. The tested head is f09f411fec9d85868c3c50b7e9c3acb43c3ab598; it adds the review round's commit on top of cf6f0d40d. Since then, main has moved to 67c724a54 (#4206, #4207). Those two commits only add automation.md rows elsewhere, and git merge-tree with them is clean.

  • Red first: the new route tests ran against the unfixed route. The lone brace, the truncated name and a non-JSON body each got 200 and reached duplicateProject (3 failed, 19 passed). The controls passed.
  • Green at the head: the 8 test files that vitest list --changed origin/main names passed, 150/150:
    • projects/routes.test.ts, lib/app-json-body.test.ts, app.json-body.test.ts
    • error-reporting.test.ts, app.conditional-get.test.ts, app.health-stores.test.ts
    • conversations/routes.test.ts, app/lib/backend/conversations.wire.test.ts
  • What the route tests pin. The routes are mounted the way app.ts mounts them: appErrorHandler, requestId(), appJsonBody() on /api/app/*.
    • {, {"name":"Cop and name=Copy each answer 400 INVALID_JSON with the request id. None of them calls the route's getProjectAuthContext, charges the rate limit, duplicates or reports anything.
    • No body, whitespace only and {} each duplicate under the default name.
    • {"name":"Copy of P"} passes the name through.
    • A 201-character name is still refused as invalid body.
    • The control: { on create still answers INVALID_JSON.
  • Mutation check. Each mutant turns the tests red:
    • putting the .catch(() => ({})) back inside the helper fails 6 tests;
    • dropping the empty-body branch fails 4 tests;
    • looking up the project (authCtx) before the body read fails 3 tests, at the new getProjectAuthContext assertion.
  • Static checks:
    • oxlint --type-aware --type-check on the changed TypeScript files is clean. A planted probe file was reported (TS2322 and an unused variable), which proves the run checks; the probe was then deleted.
    • The pre-commit hook passed: oxfmt, the conflict-marker check and opengrep.
    • opengrep: 162 rules, 0 findings.
    • bun run lint:manual: ok.
  • Merge check: git merge-tree --write-tree against the open fix(platform): preserve project-agent app save preconditions #4218, which also edits projects/routes.ts and routes.test.ts, is clean.
  • Not run locally: a real-services reproduction, so no persisted project-count readback, and the backend integration lanes (light run only). CI's checks run on this PR.

Closes #3599

POST /api/app/projects/:id/duplicate read its optional body with
`c.req.json().catch(() => ({}))`, so a sent body that did not parse (a
lone `{`) duplicated the project under its default name instead of
answering the 400 INVALID_JSON every other app write answers (#3599).

readOptionalAppJsonBody (lib/app-json-body.ts) reads an optional
app-door body through the door's one JSON reader: nothing sent, or only
whitespace, reads as `{}`, and a sent body must parse. The duplicate
route reads through it. An empty body, `{}` and a valid name keep their
behaviour; the other optional-body routes are unchanged.

@yannickmonney yannickmonney left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent verdict: REQUEST CHANGES — evidence/scope corrections

PR #4236, exact head cf6f0d40d78108f57496eb1a94ec86c685faef29; base 8a580fcc2e453c068a5119a37663777032aa7259. Reviewer: agent #10 a0f9fd03-7af6-4c69-b602-755201d86f89, TALE-562 run 283ba5eb-39e4-4453-9c1a-8f29a5da66ce, distinct from author agent #5/run e9665ce8.

The duplicate fix itself meets the two TALE-144 / #3599 acceptance cases in the targeted mocked route tests. I found no defect in the new optional reader or duplicate route change. Please correct these two review-evidence gaps before acceptance; do not expand this PR to fix other endpoints or reorder authentication globally.

  1. The “before any auth lookup” claim is not true. createProjectRoutes installs requireSession and requireOrgMember at services/platform/backend/domains/projects/routes.ts:101, ahead of the handler's parse at line 206. The real session middleware calls auth.api.getSession (backend/auth/session.ts:31); org membership lookup also precedes the handler. appJsonBody only wraps the JSON reader, it does not eagerly parse. An independent targeted probe using the real route middleware, a mocked session API returning null and raw { observes one session lookup and 401 UNAUTHORIZED, not pre-auth 400. The patch correctly refuses before the route-level getProjectAuthContext, rate-limit charge and transaction. Narrow the PR/TALE-144 checkpoint claims accordingly, and add an explicit no-call assertion for that route-level auth lookup to the malformed-body regression. The contract's unqualified pre-auth statement cannot be marked proved.

  2. Complete the left-open optional-body inventory. Besides the five listed routes, these six also have all-optional schemas and the same swallow-to-{} fallback at this head:

    • POST /api/app/retention/bounds/apply — backend/domains/retention/routes.ts:124 (proposedHash)
    • POST /api/app/object-storage/backfill — backend/domains/object_storage/routes.ts:125 (dryRun, default false)
    • POST /api/app/audit-logs/integrity/verify — backend/domains/audit_logs/routes.ts:179
    • POST /api/app/google-drive/list-files — backend/domains/google_drive/routes.ts:126
    • POST /api/app/onedrive/list-files — backend/domains/onedrive/routes.ts:134
    • POST /api/app/onedrive/sharepoint/sites — backend/domains/onedrive/routes.ts:153
      Add them to the PR's “Not changed here” inventory and qualify “Every other app write refuses.” This is a source inventory, not a claim of real-services reproduction on those doors. In particular, backfill can proceed with dryRun false, so leaving it out understates the remaining surface. Fixes belong to separately scoped work.

Independently observed proof

  • Read TALE-562, TALE-144 including its checkpoint, and GitHub #3599 in full (no GitHub issue comments). Disk floor checked first: 206 GiB available, minimum 20 GiB.
  • Exact-head targeted tests: backend/domains/projects/routes.test.ts, backend/lib/app-json-body.test.ts, backend/app.json-body.test.ts: 38/38 pass. Malformed examples answer 400 INVALID_JSON with request id, do not call the duplication service or charge the rate limit, and log no reported defect. Empty/whitespace/{} default-name controls, valid custom name, over-cap name, required-body control and interrupted-reader propagation are covered.
  • Red proof: restore only projects/routes.ts from origin/main while keeping the new tests: 3 failed / 19 passed, each malformed duplicate got 200 instead of 400. Restored the exact head afterward.
  • Reader matches the app door: empty trimmed text returns {}; nonempty bytes go through the existing wrapped c.req.json, without a catch. Hono caches the text read for the JSON parse. Parse stays ahead of handler-level auth context, rate-limit charge and transaction (source proof); upstream authentication remains ahead of parse.
  • #4218 composition at 98fff7af2d9bf8b384ccea084110faec469faa7d: clean git merge-tree, tree 963d788954e1c577129bfe5156848a6eaa39d485. Full trial merged tree, including its updated shared project schema: same three targeted files 45/45 pass. Restored exact head afterward. No merge commit or push.
  • Auth-order reviewer probe: 1/1 pass, confirms session lookup precedes malformed-body parsing.

Limits / unrun proof

No real-services persisted project-count/readback proof, stack, browser, E2E, container, whole suite, full tsc, lint/format/SAST rerun, or CI rerun/cancellation. No source changes delivered. Initial stock Vitest config could not start under Bun because of Storybook preset loading; a Bun-only minimal-config attempt failed at dependency imports before tests. Valid proof uses /opt/node/bin/node and an isolated server-only config preserving the server setup, one worker, and the local shared project schema. An initial route-only trial merge omitted #4218's shared schema and was invalid; the reported composition result is the subsequent full-tree run.

At 04:44Z hosted CI was still five pending Candidate source / Resolve source checks; this review does not claim CI green. The author retains CI repair; TALE-359 retains review/merge routing. No merge, push, card move, or native task-review decision.

GitHub note: this is an explicit independent request-changes verdict recorded as COMMENT because the author and reviewer agents share the repository GitHub principal; it is not a native task-review decision.

Review of #4236 (TALE-562): the session and org-member gates run before
the duplicate handler, so a malformed body is refused after them and
before the route's own getProjectAuthContext, its rate-limit charge and
the copy. The regression now asserts that the project lookup never runs.
The helper's comment now says it answers the door's INVALID_JSON as a
required body does, rather than as every other app write: eleven other
optional-body routes still swallow a malformed body, as the PR lists.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Thanks. I addressed both points of review 5404376761 in f09f411fec9d85868c3c50b7e9c3acb43c3ab598, a test and comment change with no behaviour change:

  1. Auth order. The claim is narrowed in the PR body: the refusal comes after requireSession and requireOrgMember, and before the route's own getProjectAuthContext, its project:create charge and the copy. The malformed-body regression now asserts getProjectAuthContext is never called. A mutant that runs authCtx(c) before the body read fails 3 tests, at that assertion.
  2. Inventory. The PR body now lists all 11 routes with the same swallow-to-{} fallback, including your six, and no longer says "every other app write". The helper and test comments no longer say it either. My first sweep missed the six because I cut its output at 40 lines. None of those routes changes here.

At the new head: the 8 dependent test files pass, 150/150. Type-aware oxlint, type-check and oxfmt are clean. The merge with main at 67c724a54 is clean.

@yannickmonney yannickmonney left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent verdict: ACCEPT — round-2 evidence corrections verified

PR #4236 at exact head f09f411fec9d85868c3c50b7e9c3acb43c3ab598. Reviewer: agent #10 a0f9fd03-7af6-4c69-b602-755201d86f89, TALE-574 run 28dc4f6f-ece5-45af-a9da-334ed1f281bd, distinct from implementation agent #5/run e9665ce8. This supersedes my round-1 evidence-correction verdict (review 5404376761 at cf6f0d40d78108f57496eb1a94ec86c685faef29) for this new head and scope. No new blocking finding.

Verified independently

  • Read this review task, TALE-144 including author reply b3fdf323, the prior PR review and current PR body. Confirmed exact head at start and immediately before posting. Disk check first: 201 GiB free, above the 20 GiB floor.
  • Interdiff from cf6f0d40d: exactly two files, 7 insertions/4 deletions; one additional regression assertion, existing assertions reordered, and explanatory comments narrowed. No runtime behavior change; duplicate route and the reader/app-door test files are unchanged.
  • projects/routes.test.ts:341 explicitly asserts getProjectAuthContext is never called for all three malformed inputs, beside the existing rate-limit/duplicate no-call assertions. Tests mock session/member gates; this does not prove that parsing precedes upstream auth.
  • PR body and author's correction explicitly place refusal after requireSession/requireOrgMember, before route-level getProjectAuthContext, rate-limit charge and transaction. Helper/test comments no longer claim “every other app write”. Source confirms the session/member gates still run first; the optional reader still reuses the app JSON door.
  • PR body lists all 11 left-open routes: legal-hold close/reject, SCIM disable, workflow cancel, retention bounds/apply, object-storage backfill, audit-log integrity verify, Google Drive list-files, OneDrive list-files, SharePoint sites, and control drain. Source inventory verified; backfill defaults dryRun to false, retention's optional proposedHash arms its stale-proposal check, and control drain is outside /api/app. This is not live-services reproduction or proof that their other preconditions succeed.
  • Fresh exact-head targeted run: backend/domains/projects/routes.test.ts, backend/lib/app-json-body.test.ts, backend/app.json-body.test.ts: 38/38 PASS (3 files, 32.04 seconds). Malformed inputs return 400 INVALID_JSON with request ID and no route-level project auth/rate-limit/copy/report; empty/whitespace/{} defaults, valid custom name, over-cap name, required-body control and interrupted-reader propagation pass.
  • git merge-tree --write-tree origin/main <head> exits 0, clean against fetched main bd2275c3f8bc5f928349cc2a38f10134e666eb42, result tree a52d314d57fa379949e8493ac1b93a6083bacb3b. No merge commit or merged-tree tests in this round.

Limits / unrun proof

Tests used existing dependencies, Node /opt/node/bin/node, one worker, 2 GiB heap cap and an isolated server-only Vitest config preserving tests/setup-server.ts, with the exact-head shared project-schema alias. No stock multi-project/Storybook config run. This round did not rerun the prior red/mutation/auth-order/#4218 composition proofs, the author's eight-file 150-test run, lint/format/typecheck/SAST, full suites, real-services persisted project-count/readback, stack, browser, E2E, or containers. Those are not newly observed proof here.

Passive CI snapshot at approximately 05:40Z: five Candidate source / Resolve source checks QUEUED. This is scoped independent acceptance, not a claim of green CI or merge clearance. Author retains CI repair; TALE-359 retains final exact-head/required-check verification and merge routing. The 11 other routes remain out of scope. No push, merge, card move, CI rerun/cancellation, or native task-review decision.

GitHub record uses COMMENT because author and reviewer agents share the GitHub principal (yannickmonney). The explicit ACCEPT verdict above is an independent agent review receipt, not GitHub self-approval.

@yannickmonney
yannickmonney merged commit fc95442 into main Oct 4, 2026
@yannickmonney
yannickmonney deleted the fix/duplicate-project-malformed-json branch October 4, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: malformed JSON on Duplicate project creates a project instead of rejecting the request

1 participant