Skip to content

Commit 4e1b8c2

Browse files
Adam Panzeroreoshake
authored andcommitted
Cookie config needs a default (#366)
* Give cookies a default config #365 * An extra test * Test to make sure I can also be explicit * Update readme
1 parent 483f4d7 commit 4e1b8c2

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The gem will automatically apply several headers that are related to security.
2222
- Expect-CT - Only use certificates that are present in the certificate transparency logs. [Expect-CT draft specification](https://datatracker.ietf.org/doc/draft-stark-expect-ct/).
2323
- Clear-Site-Data - Clearing browser data for origin. [Clear-Site-Data specification](https://w3c.github.io/webappsec-clear-site-data/).
2424

25-
It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes (when configured to do so).
25+
It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes. This is on default but can be turned off by using `config.cookies = SecureHeaders::OPT_OUT`.
2626

2727
`secure_headers` is a library with a global config, per request overrides, and rack middleware that enables you customize your application settings.
2828

lib/secure_headers/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def deep_copy_if_hash(value)
132132
end
133133

134134
def initialize(&block)
135-
@cookies = nil
135+
@cookies = self.class.send(:deep_copy_if_hash, Cookie::COOKIE_DEFAULTS)
136136
@clear_site_data = nil
137137
@csp = nil
138138
@csp_report_only = nil

lib/secure_headers/middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def call(env)
1717
Kernel.warn(HPKP_SAME_HOST_WARNING)
1818
end
1919

20-
flag_cookies!(headers, override_secure(env, config.cookies)) if config.cookies
20+
flag_cookies!(headers, override_secure(env, config.cookies)) unless config.cookies == OPT_OUT
2121
headers.merge!(SecureHeaders.header_hash_for(req))
2222
[status, headers, response]
2323
end

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,27 @@ module SecureHeaders
9191
end
9292
}.to raise_error(ArgumentError)
9393
end
94+
95+
it "gives cookies a default config" do
96+
expect(Configuration.default.cookies).to eq({httponly: true, secure: true, samesite: {lax: true}})
97+
end
98+
99+
it "allows OPT_OUT" do
100+
Configuration.default do |config|
101+
config.cookies = OPT_OUT
102+
end
103+
104+
config = Configuration.get
105+
expect(config.cookies).to eq(OPT_OUT)
106+
end
107+
108+
it "allows me to be explicit too" do
109+
Configuration.default do |config|
110+
config.cookies = {httponly: true, secure: true, samesite: {lax: false}}
111+
end
112+
113+
config = Configuration.get
114+
expect(config.cookies).to eq({httponly: true, secure: true, samesite: {lax: false}})
115+
end
94116
end
95117
end

0 commit comments

Comments
 (0)