Skip to content

Commit

Permalink
allow explicit SameSite=None cookies (actix#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored and JohnTitor committed Jan 23, 2020
1 parent 2e9ab06 commit a328794
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

* Setting a cookie's SameSite property, explicitly, to `SameSite::None` will now
result in `SameSite=None` being sent with the response Set-Cookie header.
To create a cookie without a SameSite attribute, remove any calls setting same_site.

## 2.0.0

* `HttpServer::start()` renamed to `HttpServer::run()`. It also possible to
Expand Down
6 changes: 6 additions & 0 deletions actix-http/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

# [Unreleased]

### Fixed

* Allow `SameSite=None` cookies to be sent in a response.

## [1.0.1] - 2019-12-20

### Fixed
Expand Down
16 changes: 12 additions & 4 deletions actix-http/src/cookie/draft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ use std::fmt;
/// attribute is "Strict", then the cookie is never sent in cross-site requests.
/// If the `SameSite` attribute is "Lax", the cookie is only sent in cross-site
/// requests with "safe" HTTP methods, i.e, `GET`, `HEAD`, `OPTIONS`, `TRACE`.
/// If the `SameSite` attribute is not present (made explicit via the
/// `SameSite::None` variant), then the cookie will be sent as normal.
/// If the `SameSite` attribute is not present then the cookie will be sent as
/// normal. In some browsers, this will implicitly handle the cookie as if "Lax"
/// and in others, "None". It's best to explicitly set the `SameSite` attribute
/// to avoid inconsistent behavior.
///
/// **Note:** Depending on browser, the `Secure` attribute may be required for
/// `SameSite` "None" cookies to be accepted.
///
/// **Note:** This cookie attribute is an HTTP draft! Its meaning and definition
/// are subject to change.
///
/// More info about these draft changes can be found in the draft spec:
/// - https://tools.ietf.org/html/draft-west-cookie-incrementalism-00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SameSite {
/// The "Strict" `SameSite` attribute.
Strict,
/// The "Lax" `SameSite` attribute.
Lax,
/// No `SameSite` attribute.
/// The "None" `SameSite` attribute.
None,
}

Expand Down Expand Up @@ -92,7 +100,7 @@ impl fmt::Display for SameSite {
match *self {
SameSite::Strict => write!(f, "Strict"),
SameSite::Lax => write!(f, "Lax"),
SameSite::None => Ok(()),
SameSite::None => write!(f, "None"),
}
}
}
6 changes: 2 additions & 4 deletions actix-http/src/cookie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,7 @@ impl<'c> Cookie<'c> {
}

if let Some(same_site) = self.same_site() {
if !same_site.is_none() {
write!(f, "; SameSite={}", same_site)?;
}
write!(f, "; SameSite={}", same_site)?;
}

if let Some(path) = self.path() {
Expand Down Expand Up @@ -1037,7 +1035,7 @@ mod tests {
let cookie = Cookie::build("foo", "bar")
.same_site(SameSite::None)
.finish();
assert_eq!(&cookie.to_string(), "foo=bar");
assert_eq!(&cookie.to_string(), "foo=bar; SameSite=None");
}

#[test]
Expand Down

0 comments on commit a328794

Please sign in to comment.