Skip to content

Serve SVGs with correct content type#763

Merged
cocomarine merged 3 commits intomainfrom
hj/serve-scratch-svg-with-correct-content-type
Apr 7, 2026
Merged

Serve SVGs with correct content type#763
cocomarine merged 3 commits intomainfrom
hj/serve-scratch-svg-with-correct-content-type

Conversation

@cocomarine
Copy link
Copy Markdown
Contributor

Copilot AI review requested due to automatic review settings April 7, 2026 10:37
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Apr 7, 2026

We require contributors to sign our Contributor License Agreement, and we don't have you on file. In order for us to review and merge your code, please complete this form and we'll get you added and review your contribution as soon as possible.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Test coverage

90.0% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/24078408566

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Scratch asset delivery so SVGs are served with the correct MIME type (to address issue #1286).

Changes:

  • Add an SVG fixture for testing.
  • Update request specs to follow redirects and assert final Content-Type for PNG and SVG.
  • Change Scratch assets show endpoint to redirect to an ActiveStorage-generated URL with an explicit content_type.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
app/controllers/api/scratch/assets_controller.rb Generates a direct ActiveStorage URL for the attachment and redirects to it while explicitly setting content_type.
spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb Adds/updates request assertions to verify the served media type after redirects for PNG and SVG.
spec/fixtures/files/test_svg_image.svg Adds an SVG fixture used by the new/updated tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +13 to +14
scratch_asset = ScratchAsset.find_by!(filename: filename_with_extension)
redirect_to scratch_asset.file.url(content_type: scratch_asset.file.content_type), allow_other_host: true
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing content_type: scratch_asset.file.content_type into ActiveStorage URL generation overrides Rails’ built-in content_types_to_serve_as_binary safety behavior (notably for image/svg+xml). If these assets can be user-uploaded, this can re-enable serving active SVG/HTML content as its original MIME type and increase XSS risk. Consider restricting this override to a whitelist of expected image types (or only SVG if that’s the requirement) and/or validating/sanitizing SVG uploads before allowing them to be served as image/svg+xml.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1263 to make sure we only apply this to Scratch global assets and not project specific ones.

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Apr 7, 2026

We require contributors to sign our Contributor License Agreement, and we don't have you on file. In order for us to review and merge your code, please complete this form and we'll get you added and review your contribution as soon as possible.

@cla-bot cla-bot bot added the cla-signed label Apr 7, 2026
@zetter-rpf zetter-rpf changed the title Serve svg with correct content type Serve SVGs with correct content type Apr 7, 2026
Copy link
Copy Markdown
Contributor

@zetter-rpf zetter-rpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

@cocomarine cocomarine merged commit 4e1da15 into main Apr 7, 2026
5 checks passed
@cocomarine cocomarine deleted the hj/serve-scratch-svg-with-correct-content-type branch April 7, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants