Skip to content

Commit

Permalink
Support custom protocols as origins
Browse files Browse the repository at this point in the history
  • Loading branch information
cyu committed Aug 4, 2021
1 parent 70f9126 commit 97882eb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rack/cors/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize
def origins(*args, &blk)
@origins = args.flatten.reject { |s| s == '' }.map do |n|
case n
when Proc, Regexp, %r{^https?://}, 'file://'
when Proc, Regexp, %r{^[a-z][a-z0-9.+-]*://}
n
when '*'
@public_resources = true
Expand Down
6 changes: 6 additions & 0 deletions test/unit/cors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ def load_app(name, options = {})
last_response.headers['Access-Control-Allow-Origin'].must_equal 'file://'
end

it 'supports custom protocols in origin' do
preflight_request('custom-protocol://abcdefg', '/')
last_response.must_render_cors_success
last_response.headers['Access-Control-Allow-Origin'].must_equal 'custom-protocol://abcdefg'
end

describe '' do
let(:app) do
test = self
Expand Down
3 changes: 2 additions & 1 deletion test/unit/test.ru
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use Rack::Cors do
'127.0.0.1:3000',
%r{http://192\.168\.0\.\d{1,3}(:\d+)?},
'file://',
%r{http://(.*?)\.example\.com}
%r{http://(.*?)\.example\.com},
'custom-protocol://abcdefg'

resource '/get-only', methods: :get
resource '/', headers: :any, methods: :any
Expand Down

0 comments on commit 97882eb

Please sign in to comment.