[PR #1636] [MERGED] Validate uploaded image bytes instead of trusting declared MIME type #5167

Closed
opened 2026-10-22 04:13:53 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1636
Author: @Romuromylus
Created: 5/30/2026
Status: ✅ Merged
Merged: 5/30/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/validate-image-upload-bytes


📝 Commits (3)

  • ea67161 Validate uploaded image bytes instead of trusting declared MIME type
  • e40d14f Fix image-byte validation test fixture and accept SVG uploads
  • 003aae5 Require as the SVG document root, not just the substring

📊 Changes

2 files changed (+167 additions, -1 deletions)

View changed files

📝 src-tauri/src/commands/storage/shared.rs (+164 -0)
📝 src-tauri/src/http_dispatch.rs (+3 -1)

📄 Description

Why this change

decode_uploaded_image_file (shared upload decoder) already rejects empty payloads, oversized payloads, and non-image declared content types, but it still trusted a declared image/* MIME type without validating the decoded bytes. A caller could declare image/png and send arbitrary non-image bytes, which passed the decoder and were stored / later served as an image.

What changed

  • src-tauri/src/commands/storage/shared.rs: after decoding (and after the existing size checks), validate the decoded bytes. For raster types, image::guess_format rejects bytes that match no recognized image signature (accepting every real raster format, including ones whose decoder feature is not compiled in, e.g. GIF). SVG is an accepted upload type (image/svg+xml, in IMAGE_EXTS, reachable via accept="image/*") but is XML text with no binary magic, so it is special-cased to require a recognizable <svg root instead, which still rejects non-SVG bytes declared as SVG.
  • src-tauri/src/http_dispatch.rs: the shared upload test fixture emitted only the 4-byte PNG prefix; emit the full 8-byte PNG signature so it is recognized as a real image.
  • Tests: declared-image/png with non-image bytes is rejected; a real 1x1 PNG is accepted; a real SVG is accepted; svg-typed non-SVG bytes are rejected.

Verification

  • cargo clippy --workspace --all-targets -- -D warnings clean.
  • Full Rust test suite green, including the three dispatch_supports_remote_*_upload tests (fixture fixed) and the decode_uploaded_image_* suite (6 tests).

The end-user trigger (uploading through an image/background path) reaches this decoder via the native file picker, which the automated tiers don't drive; the decoder's byte-level behavior is fully covered by the tests.

Linked issue

Closes #1534.

Summary by CodeRabbit

  • Bug Fixes

    • Strengthened image upload validation to properly distinguish between SVG and raster image formats using format-specific detection methods.
  • Tests

    • Expanded test coverage for image upload validation scenarios.

Review Change Stack


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/1636 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/30/2026 **Status:** ✅ Merged **Merged:** 5/30/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/validate-image-upload-bytes` --- ### 📝 Commits (3) - [`ea67161`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ea6716138a4c2e29477bf1a4693af31816bcc678) Validate uploaded image bytes instead of trusting declared MIME type - [`e40d14f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e40d14f3f469fd087179dd89001c5263aaa9276b) Fix image-byte validation test fixture and accept SVG uploads - [`003aae5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/003aae56ab698bd94f7efb30daa326dc5e77125c) Require <svg> as the SVG document root, not just the substring ### 📊 Changes **2 files changed** (+167 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/shared.rs` (+164 -0) 📝 `src-tauri/src/http_dispatch.rs` (+3 -1) </details> ### 📄 Description ## Why this change `decode_uploaded_image_file` (shared upload decoder) already rejects empty payloads, oversized payloads, and non-image declared content types, but it still trusted a declared `image/*` MIME type without validating the decoded bytes. A caller could declare `image/png` and send arbitrary non-image bytes, which passed the decoder and were stored / later served as an image. ## What changed - `src-tauri/src/commands/storage/shared.rs`: after decoding (and after the existing size checks), validate the decoded bytes. For raster types, `image::guess_format` rejects bytes that match no recognized image signature (accepting every real raster format, including ones whose decoder feature is not compiled in, e.g. GIF). SVG is an accepted upload type (`image/svg+xml`, in `IMAGE_EXTS`, reachable via `accept="image/*"`) but is XML text with no binary magic, so it is special-cased to require a recognizable `<svg` root instead, which still rejects non-SVG bytes declared as SVG. - `src-tauri/src/http_dispatch.rs`: the shared upload test fixture emitted only the 4-byte PNG prefix; emit the full 8-byte PNG signature so it is recognized as a real image. - Tests: declared-`image/png` with non-image bytes is rejected; a real 1x1 PNG is accepted; a real SVG is accepted; svg-typed non-SVG bytes are rejected. ## Verification - `cargo clippy --workspace --all-targets -- -D warnings` clean. - Full Rust test suite green, including the three `dispatch_supports_remote_*_upload` tests (fixture fixed) and the `decode_uploaded_image_*` suite (6 tests). The end-user trigger (uploading through an image/background path) reaches this decoder via the native file picker, which the automated tiers don't drive; the decoder's byte-level behavior is fully covered by the tests. ## Linked issue Closes #1534. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Strengthened image upload validation to properly distinguish between SVG and raster image formats using format-specific detection methods. * **Tests** * Expanded test coverage for image upload validation scenarios. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/Pasta-Devs/Marinara-Engine/pull/1636?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:13:53 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5167
No description provided.