Repository navigation
fix: check type and content of uploaded images - #78
Merged
Merged
Conversation
Any file was accepted for the 'image' format, including executables, HTML and scripts renamed to .jpg. - Accept only allowed extensions; by default jpg, jpeg, png, gif, bmp, webp, tif and tiff, configurable with "allowed_extensions" - Check the file signature for known image types, so the content must match the extension (a script renamed to .jpg is rejected) - Accept files without an extension (e.g. ESP32-CAM) if their content is an allowed image, and add the matching extension - Reject empty files - Return 400 with a JSON message when the file field is missing, instead of an HTML error page - Do not overwrite an image uploaded with the same name in the same second; add a counter instead - Document the new setting and add tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
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.
Problem
With
format='image', any uploaded file was stored, including executables, HTML files and scripts renamed to.jpg. Some related issues:x.exe,x.htmlFile type ".exe" is not allowed (allowed: ...)x.jpgThe content of the uploaded file does not match its extension (.jpg).x.jpgThe uploaded file is empty.No file was uploaded in the "image" field.(JSON)..._upload.jpg..._photo_1.jpga.jpg,a.JPG,a.pngChanges
allowed_extensionsfor the image entry in the configuration. Default:jpg,jpeg,png,gif,bmp,webp,tif,tiff. Values are normalised (case, leading dot) and validated at startup.Processing.image_type()detects JPEG, PNG, GIF, BMP, WebP and TIFF from the file signature (no new dependency).Processing.check_image()requires an allowed extension and, for these known types, matching content. Other allowed extensions (e.g.heic) are checked by extension only.Docs
README and getting started describe
allowed_extensions, the content check, files without an extension and the naming of stored files.Tests
TestImageUpload(signatures, valid images, files without extension, rejected files, missing field, no overwrite, configured and invalidallowed_extensions). They fail on the previous code.test_process_imageused the textTest image dataas a.jpgfile, which is exactly what is now rejected; its content now starts with the JPEG signature. The rest of the test is unchanged.Compatibility
Uploads of real images in the default formats behave as before. Uploads of other file types (e.g.
.pdf,.heic,.raw) are now rejected unless added toallowed_extensions.Generated by Claude Code