Repository navigation
fix(attachment): fail closed with 413 on truncated upload (wiki-v2 #308) - #39
Merged
Merged
Conversation
@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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Fail closed with
413on an oversize/truncated attachment upload instead of silently storing a truncated file and returning200. 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/multipartin stream mode does not error the file stream when thefileSizecap is hit — busboy pushes bytes up to the cap, setsfile.truncated = true, emits'limit', and ends the stream cleanly (the latentRequestFileTooLargeErroris realized only by.toBuffer()/parts(), which the streaming upload path bypasses viaskipBuffer: true). Docmost never checked.truncatedand recordedfileSize = getBytesRead()(the post-truncation byte count), so a> 50 MiBupload was stored truncated at exactly the cap and returned200with a corrupted attachment. Found in production on cccvu/wiki-v2 issue 308.What changed (all documented in the super repo's
UPSTREAM_MODIFICATIONS.mddocmost#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, iffile.truncated→ delete the object + throwPayloadTooLargeException, beforegetBytesRead()and the DB row. No row/object survives a truncated upload.core/attachment/attachment.controller.ts— bothstatusCode === 413catch branches now throwPayloadTooLargeException(413) instead ofBadRequestException(400): the real 413 is preserved, matching the platform/v1contract.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'/throwFileSizeLimitare identical across the two releases).Scope note: only the streaming paths were affected. The
.toBuffer()paths (uploadImage,importPage) already fail closed viathrowFileSizeLimit, so they are unchanged.How this was tested
attachment.service.spec.ts,import.service.spec.ts: truncated →PayloadTooLargeException+storageService.deletecalled + 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.jsonclean. eslint clean.scripts/check-upstream-boundary.shrecognizes all four changed upstream files + the two new specs as documented seams (green).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 thedocmostgitlink 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.🤖 Generated with Claude Code