Skip to content

Commit 4ede738

Browse files
committedSep 28, 2018
Only set 'Access-Control-Allow-Credentials' if required for preflight to succeed
1 parent b00dd98 commit 4ede738

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
 

‎lib/pact/mock_service/request_handlers/options.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Options < BaseRequestHandler
1313
ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin".freeze
1414
ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods".freeze
1515
ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers".freeze
16+
AUTHORIZATION = "authorization".freeze
17+
COOKIE = "cookie".freeze
1618
HTTP_ORIGIN = "HTTP_ORIGIN".freeze
1719
ALL_METHODS = "DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT, PATCH".freeze
1820
REQUEST_METHOD = "REQUEST_METHOD".freeze
@@ -31,12 +33,15 @@ def match? env
3133

3234
def respond env
3335
cors_headers = {
34-
ACCESS_CONTROL_ALLOW_CREDENTIALS => 'true',
3536
ACCESS_CONTROL_ALLOW_ORIGIN => env.fetch(HTTP_ORIGIN,'*'),
3637
ACCESS_CONTROL_ALLOW_HEADERS => env.fetch(HTTP_ACCESS_CONTROL_REQUEST_HEADERS, '*'),
3738
ACCESS_CONTROL_ALLOW_METHODS => ALL_METHODS
3839
}
3940

41+
if is_request_with_credentials?(env)
42+
cors_headers[ACCESS_CONTROL_ALLOW_CREDENTIALS] = "true"
43+
end
44+
4045
logger.info "Received OPTIONS request for mock service administration endpoint #{env[HTTP_ACCESS_CONTROL_REQUEST_METHOD]} #{env['PATH_INFO']}. Returning CORS headers: #{cors_headers}."
4146
[200, cors_headers, []]
4247
end
@@ -48,6 +53,11 @@ def is_options_request? env
4853
def is_administration_request? env
4954
(env[HTTP_ACCESS_CONTROL_REQUEST_HEADERS] || '').match(X_PACT_MOCK_SERVICE_REGEXP)
5055
end
56+
57+
def is_request_with_credentials? env
58+
headers = (env[HTTP_ACCESS_CONTROL_REQUEST_HEADERS] || '').split(",").map { |header| header.strip.downcase }
59+
headers.include?(AUTHORIZATION) || headers.include?(COOKIE)
60+
end
5161
end
5262
end
5363
end

‎spec/lib/pact/mock_service/request_handlers/options_spec.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module RequestHandlers
2323
subject { response[1] }
2424

2525
it { is_expected.to include 'Access-Control-Allow-Methods' => 'DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT, PATCH' }
26-
it { is_expected.to include 'Access-Control-Allow-Credentials' => 'true' }
26+
it { is_expected.to_not include 'Access-Control-Allow-Credentials' => 'true' }
2727

2828
context "with Origin" do
2929
it { is_expected.to include 'Access-Control-Allow-Origin' => 'foo.com' }
@@ -48,6 +48,22 @@ module RequestHandlers
4848

4949
it { is_expected.to include 'Access-Control-Allow-Headers' => '*' }
5050
end
51+
52+
context "with 'Authorization' in Access-Control-Request-Headers" do
53+
before do
54+
env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] = 'foo, Authorization, bar'
55+
end
56+
57+
it { is_expected.to include 'Access-Control-Allow-Credentials' => 'true' }
58+
end
59+
60+
context "with 'Cookie' in Access-Control-Request-Headers" do
61+
before do
62+
env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] = 'foo, Cookie, bar'
63+
end
64+
65+
it { is_expected.to include 'Access-Control-Allow-Credentials' => 'true' }
66+
end
5167
end
5268
end
5369
end

0 commit comments

Comments
 (0)
Please sign in to comment.