Skip to content

Commit 9f3cde6

Browse files
authored
Merge pull request #812 from murphysean/securesession
Session cookie secure attribute
2 parents fb8f6d4 + 849df8f commit 9f3cde6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/sessions/middleware.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct SessionMiddleware<Store> {
5555
cookie_domain: Option<String>,
5656
session_ttl: Option<Duration>,
5757
save_unchanged: bool,
58+
secure: Option<bool>,
5859
same_site_policy: SameSite,
5960
key: Key,
6061
}
@@ -67,6 +68,7 @@ impl<Store: SessionStore> std::fmt::Debug for SessionMiddleware<Store> {
6768
.field("cookie_name", &self.cookie_name)
6869
.field("cookie_domain", &self.cookie_domain)
6970
.field("session_ttl", &self.session_ttl)
71+
.field("secure", &self.secure)
7072
.field("same_site_policy", &self.same_site_policy)
7173
.field("key", &"..")
7274
.field("save_unchanged", &self.save_unchanged)
@@ -92,7 +94,10 @@ where
9294
session.expire_in(ttl);
9395
}
9496

95-
let secure_cookie = request.url().scheme() == "https";
97+
let mut secure_cookie = request.url().scheme() == "https";
98+
if let Some(secure) = self.secure {
99+
secure_cookie = secure;
100+
}
96101
request.set_ext(session.clone());
97102

98103
let mut response = next.run(request).await;
@@ -141,6 +146,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
141146
/// * cookie path: "/"
142147
/// * cookie name: "tide.sid"
143148
/// * session ttl: one day
149+
/// * secure: request.scheme == 'https'
144150
/// * same site: strict
145151
/// * save unchanged: enabled
146152
///
@@ -161,6 +167,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
161167
/// .with_cookie_name("custom.cookie.name")
162168
/// .with_cookie_path("/some/path")
163169
/// .with_cookie_domain("www.rust-lang.org")
170+
/// .with_secure(true)
164171
/// .with_same_site_policy(SameSite::Lax)
165172
/// .with_session_ttl(Some(Duration::from_secs(1)))
166173
/// .without_save_unchanged(),
@@ -173,6 +180,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
173180
cookie_path: "/".into(),
174181
cookie_name: "tide.sid".into(),
175182
cookie_domain: None,
183+
secure: None,
176184
same_site_policy: SameSite::Lax,
177185
session_ttl: Some(Duration::from_secs(24 * 60 * 60)),
178186
key: Key::derive_from(secret),
@@ -218,6 +226,14 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
218226
self
219227
}
220228

229+
/// Sets the secure attribute of the cookie.
230+
/// Defaults to true if the incoming request scheme is 'https'
231+
/// Can optionally be set to true or false to override
232+
pub fn with_secure(mut self, secure: bool) -> Self {
233+
self.secure = Some(secure);
234+
self
235+
}
236+
221237
/// Sets the same site policy for the session cookie. Defaults to
222238
/// SameSite::Lax. See [incrementally better
223239
/// cookies](https://tools.ietf.org/html/draft-west-cookie-incrementalism-01)

tests/sessions.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ async fn test_customized_sessions() -> tide::Result<()> {
6464
.with_cookie_name("custom.cookie.name")
6565
.with_cookie_path("/nested")
6666
.with_cookie_domain("www.rust-lang.org")
67-
.with_same_site_policy(SameSite::Strict)
67+
.with_secure(true)
68+
.with_same_site_policy(SameSite::Lax)
6869
.with_session_ttl(Some(Duration::from_secs(1)))
6970
.without_save_unchanged(),
7071
);
@@ -99,7 +100,8 @@ async fn test_customized_sessions() -> tide::Result<()> {
99100
assert!(cookies.get("tide.sid").is_none());
100101
let cookie = &cookies["custom.cookie.name"];
101102
assert_eq!(cookie.http_only(), Some(true));
102-
assert_eq!(cookie.same_site(), Some(SameSite::Strict));
103+
assert_eq!(cookie.secure(), Some(true));
104+
assert_eq!(cookie.same_site(), Some(SameSite::Lax));
103105
assert_eq!(cookie.path(), Some("/nested"));
104106
assert_eq!(cookie.domain(), Some("www.rust-lang.org"));
105107
let cookie_value = cookie.value().to_string();

0 commit comments

Comments
 (0)