Skip to content

Commit de3f815

Browse files
authored
Allow provision of csrf cookie secure flag (#35)
1 parent 3ee0257 commit de3f815

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/angular_rails_csrf/concern.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ def set_xsrf_token_cookie
1212
return unless protect_against_forgery? && !respond_to?(:__exclude_xsrf_token_cookie?)
1313

1414
config = Rails.application.config
15-
domain = config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil
15+
16+
cookie_options = {
17+
value: form_authenticity_token,
18+
domain: config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil
19+
}
20+
cookie_options[:secure] = config.angular_rails_csrf_secure if config.respond_to?(:angular_rails_csrf_secure)
21+
1622
cookie_name = config.respond_to?(:angular_rails_csrf_cookie_name) ? config.angular_rails_csrf_cookie_name : 'XSRF-TOKEN'
17-
cookies[cookie_name] = {value: form_authenticity_token, domain: domain}
23+
cookies[cookie_name] = cookie_options
1824
end
1925

2026
def verified_request?

test/angular_rails_csrf_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ def config.angular_rails_csrf_domain
4141
assert @response.headers['Set-Cookie'].include?('.test.host')
4242
assert_valid_cookie
4343
assert_response :success
44+
ensure
45+
config.instance_eval('undef :angular_rails_csrf_domain')
46+
end
47+
48+
test 'the secure flag is set if configured' do
49+
@request.headers['HTTPS'] = 'on'
50+
51+
config = Rails.application.config
52+
config.define_singleton_method(:angular_rails_csrf_secure) { true }
53+
54+
get :index
55+
assert @response.headers['Set-Cookie'].include?('secure')
56+
assert_valid_cookie
57+
assert_response :success
58+
ensure
59+
@request.headers['HTTPS'] = nil
60+
config.instance_eval('undef :angular_rails_csrf_secure')
4461
end
4562

4663
test 'a custom name is used if present' do

0 commit comments

Comments
 (0)