feat(storage): reject uploads whose extension, declared type and bytes disagree - #1682
Merged
Conversation
…s disagree Both managed lanes now enforce declared MIME == magic bytes == filename extension, so an HTML payload named .jpg is refused rather than stored under a type it is not.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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.
Summary
html-as-jpg.jpg— the fixture that has been sitting in this repo waiting to be rejected — could still be stored. The streamed lane sniffed the bytes but then relabelled the upload with what it found, so a.jpgfull of HTML was accepted astext/htmland (for animagecolumn with the default allowlist) blocked only incidentally; the presigned lane never compared the two claims it does have. Both lanes now enforce one rule:Disagreement is refusal, not renaming: an object served back under a type it isn't is the whole attack.
New shared helper
checkTypeAgreement(uploads/mime-bytes) decides agreement, with three deliberate softenings so the rule is enforceable rather than merely strict:application/octet-streamnever rejects on its own. Only two specific and incompatible types do.text/csvvstext/plainas a mismatch would reject most legitimate text uploads..docxis a zip, an.m4ais an MP4;CONTAINER_FAMILIESmaps each container to the types it legitimately carries.Violations are structured (
EXTENSION_BYTES_MISMATCH/DECLARED_BYTES_MISMATCH/DECLARED_EXTENSION_MISMATCH) so the caller can put the reason in an error message or arejection_reason.Where each lane checks
Streamed multipart (
graphile-settings/src/upload-resolver.ts) — the server has the bytes, so it checks in transit, before staging:const detected = await s3.detectContentType({ readStream, filename }); + const agreement = checkTypeAgreement({ + filename, + declaredMime: upload.mimetype, + detectedMime: detected.magic?.type // NOT detected.contentType + }); + if (!agreement.ok) { detected.stream.destroy(); throw new Error('UPLOAD_TYPE_MISMATCH: …'); } const staged = stagingKey();Compared against
magic.typerather thancontentTypeon purpose: the detector'scontentTypemay have been refined using the very extension under suspicion, so using it would let the filename vouch for itself.Presigned — the bytes go straight from client to S3, so the check splits in two:
plugin.ts), the free half: declared type vs filename extension, no bytes needed.payload.htmlclaimingimage/jpegis refused before a row or a URL exists.confirm-upload.ts), the bytes:readObjectPrefixdoes a ranged GET of the leading 4KB — validating a 2GB video costs the same as an icon — and returns a verdict rather than executing it:uploadedis the gate every process node already fires on, so validating before that transition means nothing downstream is ever handed unvalidated bytes. Absent object →expired(the client walked away) rather thanrejected(there is nothing to reject). The transition itself stays with the worker that ownsstorage:confirm_upload, which calls the generated<files>_confirm_uploaded/_reject_file/_expire_file(constructive-db#2891) — this repo defines the decision, not the state change, and the verdict is therefore testable without a database.Context: the full lifecycle design is constructive-planning#1488; SSG/site uploads are untouched — they are path-addressed presigned PUTs, and a site publish carrying no
filenamestates nothing for the extension leg to contradict.Link to Devin session: https://app.devin.ai/sessions/739f6e40a0dc44c4a49b8d84390a2268
Requested by: @pyramation