Skip to content

Commit

Permalink
Keep null origins from masquarding as "file://"
Browse files Browse the repository at this point in the history
Browsers send null origins when an iframe contains html code for its
source instead of a URL. This means that websites who used rack-cors and
allowed "file://" were open to attacks from malicious pages that used
this fact to send "null" origins.
  • Loading branch information
bfad authored and cyu committed Feb 15, 2017
1 parent be1d356 commit 80450a0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
6 changes: 2 additions & 4 deletions lib/rack/cors.rb
Expand Up @@ -278,13 +278,11 @@ def public_resources?
def allow_origin?(source,env = {})
return true if public_resources?

effective_source = (source == 'null' ? 'file://' : source)

return !! @origins.detect do |origin|
if origin.is_a?(Proc)
origin.call(source,env)
else
origin === effective_source
origin === source
end
end
end
Expand Down Expand Up @@ -323,7 +321,7 @@ def initialize(public_resource, path, opts={})
else
ensure_enum(opts[:methods]) || [:get]
end.map{|e| e.to_s }

self.expose = opts[:expose] ? [opts[:expose]].flatten : nil
end

Expand Down
6 changes: 0 additions & 6 deletions test/unit/cors_test.rb
Expand Up @@ -256,12 +256,6 @@ def load_app(name)
last_response.headers['Access-Control-Allow-Origin'].must_equal '*'
end

it 'should "null" origin, allowed as "file://", returned as "null" in header' do
preflight_request('null', '/')
should_render_cors_success
last_response.headers['Access-Control-Allow-Origin'].must_equal 'null'
end

it 'should return "file://" as header with "file://" as origin' do
preflight_request('file://', '/')
should_render_cors_success
Expand Down

0 comments on commit 80450a0

Please sign in to comment.