Skip to content

Commit 02b1325

Browse files
committed
document cookie configuration
1 parent bbabd62 commit 02b1325

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ All `nil` values will fallback to their default values. `SecureHeaders::OPT_OUT`
3131

3232
```ruby
3333
SecureHeaders::Configuration.default do |config|
34-
config.secure_cookies = true # mark all cookies as "secure"
34+
config.cookies = {
35+
secure: true, # mark all cookies as "secure"
36+
httponly: true, # mark all cookies as "httponly"
37+
samesite: true
38+
}
3539
config.hsts = "max-age=#{20.years.to_i}; includeSubdomains; preload"
3640
config.x_frame_options = "DENY"
3741
config.x_content_type_options = "nosniff"
@@ -264,6 +268,55 @@ config.hpkp = {
264268
}
265269
```
266270

271+
### Cookies
272+
273+
SecureHeaders supports `Secure`, `HttpOnly` and [`SameSite`](https://tools.ietf.org/html/draft-west-first-party-cookies-06) cookies. These can be defined in the form of a boolean, or as a Hash for more refined configuration.
274+
275+
__Note__: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
276+
277+
#### Boolean-based configuration
278+
279+
Boolean-based configuration is intended to globally enable or disable a specific cookie attribute.
280+
281+
```ruby
282+
config.cookies = {
283+
secure: true, # mark all cookies as Secure
284+
httponly: false, # do not mark any cookies as HttpOnly
285+
}
286+
```
287+
288+
#### Hash-based configuration
289+
290+
Hash-based configuration allows for fine-grained control.
291+
292+
```ruby
293+
config.cookies = {
294+
secure: { except: ['_guest'], # mark all but the `_guest` cookie as Secure
295+
httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
296+
}
297+
```
298+
299+
#### SameSite
300+
301+
SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
302+
303+
```ruby
304+
config.cookies = {
305+
samesite: true # mark all cookies as SameSite (user agents default this to `Strict` enforcement mode)
306+
}
307+
```
308+
309+
`Strict` and `Lax` enforcement can also be specified using a Hash.
310+
311+
```ruby
312+
config.cookies = {
313+
samesite: {
314+
strict: { only: [`_rails_session`] },
315+
lax: { only: [`_guest`] }
316+
}
317+
}
318+
```
319+
267320
### Using with Sinatra
268321

269322
Here's an example using SecureHeaders for Sinatra applications:

0 commit comments

Comments
 (0)