Skip to content

Commit 901eda6

Browse files
committed
cookie_defaults takes HTTP scheme
1 parent c3c57b4 commit 901eda6

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/gleam/http.gleam

+4-5
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,16 @@ pub type CookieAttributes {
603603

604604
/// Helper to create sensible default attributes for a set cookie.
605605
///
606-
/// NOTE these defaults ensure you cookie is always available to you application.
607-
/// However this is not a fully secure solution.
608-
/// You should consider setting a Secure and/or SameSite attribute.
606+
/// Note these defaults may not be sufficient to secure your application.
607+
/// You should consider setting the SameSite field.
609608
///
610609
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Attributes
611-
pub fn cookie_defaults() {
610+
pub fn cookie_defaults(scheme: Scheme) {
612611
CookieAttributes(
613612
max_age: option.None,
614613
domain: option.None,
615614
path: Some("/"),
616-
secure: False,
615+
secure: scheme == Https,
617616
http_only: True,
618617
same_site: option.None,
619618
)

test/gleam/http_test.gleam

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import gleam/atom
22
import gleam/dynamic
33
import gleam/string_builder
44
import gleam/uri.{Uri}
5-
import gleam/http
5+
import gleam/http.{Http, Https}
66
import gleam/option.{None, Some}
77
import gleam/should
88

@@ -836,7 +836,7 @@ pub fn set_resp_cookie_test() {
836836
|> should.equal(Ok("k1=v1"))
837837

838838
http.response(200)
839-
|> http.set_resp_cookie("k1", "v1", http.cookie_defaults())
839+
|> http.set_resp_cookie("k1", "v1", http.cookie_defaults(Http))
840840
|> http.get_resp_header("set-cookie")
841841
|> should.equal(Ok("k1=v1; Path=/; HttpOnly"))
842842

@@ -860,7 +860,7 @@ pub fn set_resp_cookie_test() {
860860

861861
pub fn expire_resp_cookie_test() {
862862
http.response(200)
863-
|> http.expire_resp_cookie("k1", http.cookie_defaults())
863+
|> http.expire_resp_cookie("k1", http.cookie_defaults(Http))
864864
|> http.get_resp_header("set-cookie")
865865
|> should.equal(
866866
Ok("k1=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; MaxAge=0; Path=/; HttpOnly"),

0 commit comments

Comments
 (0)