fix(uploads): cap attachment size and refuse browser-executable MIME types#42
Merged
Conversation
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
Attachment endpoints accepted any file of any size:
shutil.copyfileobj(file.file, dest)streamed straight to disk with no upper bound and no content-type validation. Two real failure modes:Content-Type: text/htmlfor inline rendering, so the attack worked end to end.Fix:
core/config.py:MAX_ATTACHMENT_BYTES(default 25 MiB) — generous enough for screenshots, junit bundles, short screen recordings; well below disk-fill territory.BLOCKED_ATTACHMENT_MIME_TYPES(CSV) — defaults blocktext/html,application/xhtml+xml,application/javascript,text/javascript,image/svg+xml,application/x-msdownload,application/x-httpd-php,text/x-php. Parsed on demand via ablocked_attachment_mime_typesproperty so admins can override per deployment.services/uploads.read_attachment(file)validates the MIME (415 on hit) then drains bytes through the existingread_boundedhelper (413 on overflow). Returns the bytes for the caller to persist.shutil.copyfileobj+os.path.getsizepattern withpayload = uploads.read_attachment(file)+len(payload).Tests (
backend/tests/security/test_upload_caps.py, parametrized × 5 endpoints):text/htmlbody returns 415 with the offending MIME echoed in the detail.image/svg+xmlbody returns 415 (covers the script-bearing SVG vector specifically).text/plainpayload still succeeds with 201.Full backend suite: 894 passing.