Skip to content

fix(attachment): fail closed with 413 on truncated upload (wiki-v2 #308) - #39

Merged
PMQ9 merged 3 commits into
wiki-v2from
fix/308-attachment-truncation
Sep 18, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
fix/308-attachment-truncation

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 18, 2026 •

Copy link
Copy Markdown

TL;DR

Fail closed with 413 on an oversize/truncated attachment upload instead of silently storing a truncated file and returning 200. Fork half of the paired change for cccvu/wiki-v2 issue 308 (the super PR carries the platform-edge fix + the docmost#404 feature).

Context

@fastify/multipart in stream mode does not error the file stream when the fileSize cap is hit — busboy pushes bytes up to the cap, sets file.truncated = true, emits 'limit', and ends the stream cleanly (the latent RequestFileTooLargeError is realized only by .toBuffer()/parts(), which the streaming upload path bypasses via skipBuffer: true). Docmost never checked .truncated and recorded fileSize = getBytesRead() (the post-truncation byte count), so a > 50 MiB upload was stored truncated at exactly the cap and returned 200 with a corrupted attachment. Found in production on cccvu/wiki-v2 issue 308.

What changed (all documented in the super repo's UPSTREAM_MODIFICATIONS.md docmost#145–docmost#151)

  • core/attachment/services/attachment.service.ts — uploadFile: wrap the storage write to delete a partial object on a mid-stream abort, and after the stream drains, if file.truncated → delete the object + throw PayloadTooLargeException, before getBytesRead() and the DB row. No row/object survives a truncated upload.
  • core/attachment/attachment.controller.ts — both statusCode === 413 catch branches now throw PayloadTooLargeException (413) instead of BadRequestException (400): the real 413 is preserved, matching the platform /v1 contract.
  • integrations/import/services/import.service.ts — the same fail-closed guard on the zip-import sibling (importZip, FILE_IMPORT_SIZE_LIMIT), which had the identical latent bug.
  • package.json + pnpm-lock.yaml — bump @fastify/multipart ^10.0.0 → ^10.1.1 (CVE-2026-18549: truncate + client-abort → hung handler + temp-file leak). Behavior-preserving for our truncation contract (file.truncated/'limit'/throwFileSizeLimit are identical across the two releases).

Scope note: only the streaming paths were affected. The .toBuffer() paths (uploadImage, importPage) already fail closed via throwFileSizeLimit, so they are unchanged.

How this was tested

  • New co-located regression specs — attachment.service.spec.ts, import.service.spec.ts: truncated → PayloadTooLargeException + storageService.delete called + no row / index-job / file-task; plus the mid-stream storage-error cleanup path.
  • cd apps/server && npx jest attachment import upload → green (35 passed, 6 pre-existing skips). npx tsc --noEmit -p tsconfig.build.json clean. eslint clean.
  • The super repo's scripts/check-upstream-boundary.sh recognizes all four changed upstream files + the two new specs as documented seams (green).
  • End-to-end truncation → 413 (with no commit) through the real streaming relay is proven on the super-repo side (attachment-upload.integration.spec.ts), which relays to this route.

Delivery / pairing

Fork-first: merge this PR, then the super PR (cccvu/wiki-v2, feat/308-404-attachment-upload) re-pins the docmost gitlink to this PR's merge SHA and carries the platform-edge fix + the docmost#404 out-of-band upload feature. The super PR closes cccvu/wiki-v2 issue 308 and issue 404.

Brought up to date with wiki-v2. This branch merged fork wiki-v2 (which now carries the UI/UX-polish pass, PR 37) so it sits on current base; the effective diff stays scoped to the seven docmost#308 files. The super repo's UPSTREAM seams for these files were renumbered #123–#129 → #145–#151 (PR 37 claimed #123–#144).

🤖 Generated with Claude Code

PMQ9 and others added 3 commits September 18, 2026 12:30
@fastify/multipart stream mode truncates an oversize file at the fileSize cap
and ends the stream cleanly WITHOUT erroring it, so uploadFile recorded
fileSize=getBytesRead() (the truncated byte count) and persisted the row — a
corrupted attachment returned as a 200 success. Fail closed: on file.truncated,
delete the partial object and throw PayloadTooLargeException before the row
insert; wrap the storage write to clean up a mid-stream abort. Same guard for
the zip-import sibling (importZip). Preserve the real 413 in the controller
(both branches were downgraded to 400). Bump @fastify/multipart 10.0.0 -> 10.1.1
(CVE-2026-18549: truncate+client-abort handler hang + temp-file leak).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…TE + surface the real 413 (docmost#308 review)

Round-1 review fixes for cccvu/wiki-v2 PR 412 / issue 308:

- DATA-LOSS BLOCKER (Correctness P4): the docmost#308 cleanup deleted `filePath` unconditionally, but on the
  OVERWRITE path (`opts.attachmentId`, e.g. a diagram re-save or a `/v1` upload with `?attachmentId=`)
  `filePath` is the EXISTING attachment's object and `updateAttachment` never changes it — so a
  truncated/aborted oversize overwrite deleted a good attachment and left its row pointing at a deleted
  file (on S3 a failed PutObject leaves the original intact, which the new catch then destroyed). Gate
  BOTH cleanup deletes on `!isUpdate` — delete only what this call created. import.service is a fresh
  `fileTaskId` path (never an overwrite), so it is unaffected.

- Real 413 was still downgraded to 400 on the service path: the service throws a NestJS
  `PayloadTooLargeException` (status on `getStatus()`, no `.statusCode`), so the controller's
  `err?.statusCode === 413`-only catch let it fall through to `BadRequestException`. Match
  `err instanceof PayloadTooLargeException || err?.statusCode === 413`.

- Observability (DevOps): the partial-object cleanup deletes no longer swallow storage-delete errors —
  a failed delete is logged (orphaned-object signal) via small `deletePartialObject`/`deletePartialImport`
  helpers.

- Tests: new `attachment.controller.spec.ts` (both 413 branches → PayloadTooLargeException; non-413 → 400),
  and overwrite regression cases in `attachment.service.spec.ts` (truncation/abort on overwrite → no delete,
  no row update).

Documented as UPSTREAM_MODIFICATIONS.md docmost#145/docmost#146/docmost#147/docmost#152 in the super repo.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit aa7538c into wiki-v2 Sep 18, 2026
@PMQ9
PMQ9 deleted the fix/308-attachment-truncation branch September 18, 2026 21:23
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.

1 participant