Content Security Policy is only active for HTML responses but not for image/svg+xml
Rails only applies its Content Security Policy to responses with an HTML content type, so an SVG served inline as image/svg+xml runs embedded script without the policy. An attacker who can upload such a file and get a victim to open it could execute script in the victim's session.
Originally disclosed on HackerOne
- Fixed in 8 months
- Disclosed after 23 months
- Setup plain Rails application with a simple route and controller
- Configure a Content Security Policy (CSP) for downloading/preview endpoints with
default-src 'none'(via https://edgeguides.rubyonrails.org/security.html#content-security-policy) - Send a malicious SVG* file (e.g. attached example) from the controller via
send_dataorsend_filewithdisposition: 'inline'(e.g.send_file '/path/malicious.svg', type: 'image/svg+xml', disposition: 'inline'/send_data malicious_svg.content, , type: 'image/svg+xml', disposition: 'inline') - Expect CSP to prevent Java Script code execution as recommended by SVG Working Group specifications (see: https://github.com/w3c/svgwg/issues/266#issuecomment-270482690)
- See that malicious SVG Java Script is executed
- Find out that Rails only applies CSP for responses with Content-Type HTML (see: https://github.com/rails/rails/blob/c236ff686c6fa987924b8eefeec93c2abcc07843/actionpack/lib/action_dispatch/http/content_security_policy.rb#L20)
Note: ActiveStorage prevents this by filtering via ActiveStorage.content_types_allowed_inline
Impact
The attacker can trick victim users to execute Java Script in the scope of their active session by uploading the malicious file to the server and sending a link to that file to the victim.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the CSP middleware in Rails only attaches the policy when the response content type is HTML, leaving other renderable types such as SVG without protection.
Impact
An attacker could trick a victim into running script in the scope of their active session by uploading a malicious file and sending a link to it, though ActiveStorage blocks this by filtering inline content types.
What the attacker needed
Ability to upload an SVG file; victim opens the file link; endpoint serves SVG inline
How it was found
- Set up a plain Rails application with a simple route and controller.
- Configure a Content Security Policy with default-src 'none' for download or preview endpoints.
- Serve a malicious SVG from the controller inline as image/svg+xml.
- Observe that the script inside the SVG executes despite the policy.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A Content Security Policy is a set of browser rules that blocks unwanted scripts on a page. Rails was only sending these rules for normal web pages, not for SVG images, which browsers can treat as pages that run script. So a crafted image could run code in the victim's browser.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.