2017-06-06 20:41:04 +00:00
|
|
|
use std::fmt;
|
2017-03-07 09:19:06 +00:00
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
2021-03-09 02:18:15 +00:00
|
|
|
use crate::Config;
|
|
|
|
use crate::http::private::cookie;
|
2018-11-09 07:37:37 +00:00
|
|
|
|
2021-03-09 02:18:15 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use self::cookie::{Cookie, SameSite, Iter};
|
2018-11-05 20:29:03 +00:00
|
|
|
|
2017-06-24 09:49:16 +00:00
|
|
|
/// Collection of one or more HTTP cookies.
|
|
|
|
///
|
2020-10-15 04:37:16 +00:00
|
|
|
/// `CookieJar` allows for retrieval of cookies from an incoming request. It
|
|
|
|
/// also tracks modifications (additions and removals) and marks them as
|
|
|
|
/// pending.
|
|
|
|
///
|
|
|
|
/// # Pending
|
|
|
|
///
|
|
|
|
/// Changes to a `CookieJar` are _not_ visible via the normal [`get()`] and
|
|
|
|
/// [`get_private()`] methods. This is typically the desired effect as a
|
|
|
|
/// `CookieJar` always reflects the cookies in an incoming request. In cases
|
2022-01-17 14:49:48 +00:00
|
|
|
/// where this is not desired, the [`get_pending()`] method is available, which
|
|
|
|
/// always returns the latest changes.
|
2020-10-15 04:37:16 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{CookieJar, Cookie};
|
|
|
|
///
|
|
|
|
/// #[get("/message")]
|
|
|
|
/// fn message(jar: &CookieJar<'_>) {
|
|
|
|
/// jar.add(Cookie::new("message", "hello!"));
|
|
|
|
/// jar.add(Cookie::new("other", "bye!"));
|
|
|
|
///
|
|
|
|
/// // `get()` does not reflect changes.
|
|
|
|
/// assert!(jar.get("other").is_none());
|
|
|
|
/// # assert_eq!(jar.get("message").map(|c| c.value()), Some("hi"));
|
|
|
|
///
|
|
|
|
/// // `get_pending()` does.
|
|
|
|
/// let other_pending = jar.get_pending("other");
|
|
|
|
/// let message_pending = jar.get_pending("message");
|
|
|
|
/// assert_eq!(other_pending.as_ref().map(|c| c.value()), Some("bye!"));
|
|
|
|
/// assert_eq!(message_pending.as_ref().map(|c| c.value()), Some("hello!"));
|
|
|
|
/// # jar.remove(Cookie::named("message"));
|
|
|
|
/// # assert_eq!(jar.get("message").map(|c| c.value()), Some("hi"));
|
|
|
|
/// # assert!(jar.get_pending("message").is_none());
|
|
|
|
/// }
|
|
|
|
/// # fn main() {
|
|
|
|
/// # use rocket::local::blocking::Client;
|
Test 'secret_key' validation, now on pre-launch.
Prior to this commit, it was not possible to test Rocket crates in
production mode without setting a global secret key or bypassing secret
key checking - the testing script did the latter. The consequence is
that it became impossible to test secret key related failures because
the tests passed regardless.
This commit undoes this. As a consequence, all tests are now aware of
the difference between debug and release configurations, the latter of
which validates 'secret_key' by default. New 'Client::debug()' and
'Client::debug_with()' simplify creating an instance of 'Client' with
configuration in debug mode to avoid undesired test failures.
The summary of changes in this commit are:
* Config 'secret_key' success and failure are now tested.
* 'secret_key' validation was moved to pre-launch from 'Config:from()'.
* 'Config::from()' only extracts the config.
* Added 'Config::try_from()' for non-panicking extraction.
* 'Config' now knows the profile it was extracted from.
* The 'Config' provider sets a profile of 'Config.profile'.
* 'Rocket', 'Client', 'Fairings', implement 'Debug'.
* 'fairing::Info' implements 'Copy', 'Clone'.
* 'Fairings' keeps track of, logs attach fairings.
* 'Rocket::reconfigure()' was added to allow modifying a config.
Internally, the testing script was refactored to properly test the
codebase with the new changes. In particular, it no longer sets a rustc
'cfg' to avoid secret-key checking.
Resolves #1543.
Fixes #1564.
2021-03-09 08:07:43 +00:00
|
|
|
/// # let client = Client::debug_with(routes![message]).unwrap();
|
2020-10-15 04:37:16 +00:00
|
|
|
/// # let response = client.get("/message")
|
|
|
|
/// # .cookie(Cookie::new("message", "hi"))
|
|
|
|
/// # .dispatch();
|
|
|
|
/// #
|
|
|
|
/// # assert!(response.status().class().is_success());
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// A type of `&CookieJar` can be retrieved via its `FromRequest` implementation
|
|
|
|
/// as a request guard or via the [`Request::cookies()`] method. Individual
|
|
|
|
/// cookies can be retrieved via the [`get()`] and [`get_private()`] methods.
|
2022-01-17 14:49:48 +00:00
|
|
|
/// Pending changes can be observed via the [`get_pending()`] method. Cookies
|
|
|
|
/// can be added or removed via the [`add()`], [`add_private()`], [`remove()`],
|
|
|
|
/// and [`remove_private()`] methods.
|
2018-10-06 13:25:17 +00:00
|
|
|
///
|
2021-03-20 01:09:13 +00:00
|
|
|
/// [`Request::cookies()`]: crate::Request::cookies()
|
2018-10-06 13:25:17 +00:00
|
|
|
/// [`get()`]: #method.get
|
|
|
|
/// [`get_private()`]: #method.get_private
|
2020-10-15 04:37:16 +00:00
|
|
|
/// [`get_pending()`]: #method.get_pending
|
2018-10-06 13:25:17 +00:00
|
|
|
/// [`add()`]: #method.add
|
|
|
|
/// [`add_private()`]: #method.add_private
|
|
|
|
/// [`remove()`]: #method.remove
|
|
|
|
/// [`remove_private()`]: #method.remove_private
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// The following example shows `&CookieJar` being used as a request guard in a
|
|
|
|
/// handler to retrieve the value of a "message" cookie.
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
2018-09-20 04:14:30 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
2020-07-22 19:21:19 +00:00
|
|
|
/// use rocket::http::CookieJar;
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// #[get("/message")]
|
2020-10-15 04:37:16 +00:00
|
|
|
/// fn message<'a>(jar: &'a CookieJar<'_>) -> Option<&'a str> {
|
|
|
|
/// jar.get("message").map(|cookie| cookie.value())
|
2017-06-24 09:49:16 +00:00
|
|
|
/// }
|
|
|
|
/// # fn main() { }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// The following snippet shows `&CookieJar` being retrieved from a `Request` in
|
|
|
|
/// a custom request guard implementation for `User`. A [private cookie]
|
2017-06-24 09:49:16 +00:00
|
|
|
/// containing a user's ID is retrieved. If the cookie exists and the ID parses
|
|
|
|
/// as an integer, a `User` structure is validated. Otherwise, the guard
|
|
|
|
/// forwards.
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// [private cookie]: #method.add_private
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
2018-09-20 04:14:30 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
2021-03-09 02:18:15 +00:00
|
|
|
/// # #[cfg(feature = "secrets")] {
|
2017-06-24 09:49:16 +00:00
|
|
|
/// use rocket::http::Status;
|
|
|
|
/// use rocket::outcome::IntoOutcome;
|
|
|
|
/// use rocket::request::{self, Request, FromRequest};
|
|
|
|
///
|
|
|
|
/// // In practice, we'd probably fetch the user from the database.
|
|
|
|
/// struct User(usize);
|
|
|
|
///
|
2020-01-31 09:34:15 +00:00
|
|
|
/// #[rocket::async_trait]
|
2021-03-15 02:57:59 +00:00
|
|
|
/// impl<'r> FromRequest<'r> for User {
|
2019-04-09 03:57:47 +00:00
|
|
|
/// type Error = std::convert::Infallible;
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2021-03-15 02:57:59 +00:00
|
|
|
/// async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
2017-06-24 09:49:16 +00:00
|
|
|
/// request.cookies()
|
|
|
|
/// .get_private("user_id")
|
2020-07-22 19:21:19 +00:00
|
|
|
/// .and_then(|c| c.value().parse().ok())
|
2017-06-24 09:49:16 +00:00
|
|
|
/// .map(|id| User(id))
|
|
|
|
/// .or_forward(())
|
|
|
|
/// }
|
|
|
|
/// }
|
2020-09-03 05:41:31 +00:00
|
|
|
/// # }
|
2017-06-24 09:49:16 +00:00
|
|
|
/// # fn main() { }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Private Cookies
|
|
|
|
///
|
|
|
|
/// _Private_ cookies are just like regular cookies except that they are
|
|
|
|
/// encrypted using authenticated encryption, a form of encryption which
|
|
|
|
/// simultaneously provides confidentiality, integrity, and authenticity. This
|
|
|
|
/// means that private cookies cannot be inspected, tampered with, or
|
|
|
|
/// manufactured by clients. If you prefer, you can think of private cookies as
|
|
|
|
/// being signed and encrypted.
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// Private cookies can be retrieved, added, and removed from a `CookieJar`
|
2018-10-06 13:25:17 +00:00
|
|
|
/// collection via the [`get_private()`], [`add_private()`], and
|
|
|
|
/// [`remove_private()`] methods.
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// ## Encryption Key
|
|
|
|
///
|
|
|
|
/// To encrypt private cookies, Rocket uses the 256-bit key specified in the
|
|
|
|
/// `secret_key` configuration parameter. If one is not specified, Rocket will
|
|
|
|
/// automatically generate a fresh key. Note, however, that a private cookie can
|
|
|
|
/// only be decrypted with the same key with which it was encrypted. As such, it
|
|
|
|
/// is important to set a `secret_key` configuration parameter when using
|
|
|
|
/// private cookies so that cookies decrypt properly after an application
|
|
|
|
/// restart. Rocket will emit a warning if an application is run in production
|
|
|
|
/// mode without a configured `secret_key`.
|
|
|
|
///
|
|
|
|
/// Generating a string suitable for use as a `secret_key` configuration value
|
|
|
|
/// is usually done through tools like `openssl`. Using `openssl`, for instance,
|
|
|
|
/// a 256-bit base64 key can be generated with the command `openssl rand -base64
|
|
|
|
/// 32`.
|
2020-07-22 19:21:19 +00:00
|
|
|
pub struct CookieJar<'a> {
|
|
|
|
jar: cookie::CookieJar,
|
2020-10-15 04:37:16 +00:00
|
|
|
ops: Mutex<Vec<Op>>,
|
2021-03-09 02:18:15 +00:00
|
|
|
config: &'a Config,
|
2020-10-15 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Clone for CookieJar<'a> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
CookieJar {
|
|
|
|
jar: self.jar.clone(),
|
|
|
|
ops: Mutex::new(self.ops.lock().clone()),
|
2021-03-09 02:18:15 +00:00
|
|
|
config: self.config,
|
2020-10-15 04:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum Op {
|
|
|
|
Add(Cookie<'static>, bool),
|
|
|
|
Remove(Cookie<'static>, bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Op {
|
|
|
|
fn cookie(&self) -> &Cookie<'static> {
|
|
|
|
match self {
|
|
|
|
Op::Add(c, _) | Op::Remove(c, _) => c
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
impl<'a> CookieJar<'a> {
|
2021-03-09 02:18:15 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub(crate) fn new(config: &'a Config) -> Self {
|
|
|
|
CookieJar::from(cookie::CookieJar::new(), config)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from(jar: cookie::CookieJar, config: &'a Config) -> Self {
|
|
|
|
CookieJar { jar, config, ops: Mutex::new(Vec::new()) }
|
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
/// Returns a reference to the _original_ `Cookie` inside this container
|
|
|
|
/// with the name `name`. If no such cookie exists, returns `None`.
|
|
|
|
///
|
2021-10-11 22:54:08 +00:00
|
|
|
/// **Note:** This method _does not_ observe changes made via additions and
|
2020-10-15 04:37:16 +00:00
|
|
|
/// removals to the cookie jar. To observe those changes, use
|
|
|
|
/// [`CookieJar::get_pending()`].
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// let cookie = jar.get("name");
|
2017-06-24 09:49:16 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-10-15 04:37:16 +00:00
|
|
|
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
|
2020-07-22 19:21:19 +00:00
|
|
|
self.jar.get(name)
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:54:08 +00:00
|
|
|
/// Retrieves the _original_ `Cookie` inside this collection with the name
|
2020-10-15 04:37:16 +00:00
|
|
|
/// `name` and authenticates and decrypts the cookie's value. If the cookie
|
|
|
|
/// cannot be found, or the cookie fails to authenticate or decrypt, `None`
|
|
|
|
/// is returned.
|
|
|
|
///
|
2021-10-11 22:54:08 +00:00
|
|
|
/// **Note:** This method _does not_ observe changes made via additions and
|
2020-10-15 04:37:16 +00:00
|
|
|
/// removals to the cookie jar. To observe those changes, use
|
2022-01-17 14:49:48 +00:00
|
|
|
/// [`CookieJar::get_pending()`].
|
2020-07-22 19:21:19 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// let cookie = jar.get_private("name");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
|
|
|
pub fn get_private(&self, name: &str) -> Option<Cookie<'static>> {
|
2021-03-09 02:18:15 +00:00
|
|
|
self.jar.private(&self.config.secret_key.key).get(name)
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
/// Returns a reference to the _original or pending_ `Cookie` inside this
|
2022-01-17 14:49:48 +00:00
|
|
|
/// container with the name `name`, irrespective of whether the cookie was
|
|
|
|
/// private or not. If no such cookie exists, returns `None`.
|
|
|
|
///
|
|
|
|
/// This _does not_ return cookies sent by the client in a request. To
|
2022-04-21 15:33:44 +00:00
|
|
|
/// retrieve such cookies, using [`CookieJar::get()`] or
|
2022-01-17 14:49:48 +00:00
|
|
|
/// [`CookieJar::get_private()`].
|
2020-10-15 04:37:16 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// let pending_cookie = jar.get_pending("name");
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn get_pending(&self, name: &str) -> Option<Cookie<'static>> {
|
|
|
|
let ops = self.ops.lock();
|
|
|
|
for op in ops.iter().rev().filter(|op| op.cookie().name() == name) {
|
|
|
|
match op {
|
|
|
|
Op::Add(c, _) => return Some(c.clone()),
|
|
|
|
Op::Remove(_, _) => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(ops);
|
2021-06-30 20:36:23 +00:00
|
|
|
self.get(name).cloned()
|
2020-10-15 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 20:29:03 +00:00
|
|
|
/// Adds `cookie` to this collection.
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// Unless a value is set for the given property, the following defaults are
|
|
|
|
/// set on `cookie` before being added to `self`:
|
|
|
|
///
|
|
|
|
/// * `path`: `"/"`
|
|
|
|
/// * `SameSite`: `Strict`
|
|
|
|
///
|
2023-04-05 20:04:39 +00:00
|
|
|
/// Furthermore, if TLS is enabled, the `Secure` cookie flag is set.
|
|
|
|
///
|
2017-06-24 09:49:16 +00:00
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, SameSite, CookieJar};
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// jar.add(Cookie::new("first", "value"));
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// let cookie = Cookie::build("other", "value_two")
|
2018-11-05 20:29:03 +00:00
|
|
|
/// .path("/")
|
|
|
|
/// .secure(true)
|
2020-07-22 19:21:19 +00:00
|
|
|
/// .same_site(SameSite::Lax);
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// jar.add(cookie.finish());
|
2017-06-24 09:49:16 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-07-22 19:21:19 +00:00
|
|
|
pub fn add(&self, mut cookie: Cookie<'static>) {
|
2023-04-05 20:04:39 +00:00
|
|
|
Self::set_defaults(self.config, &mut cookie);
|
2020-10-15 04:37:16 +00:00
|
|
|
self.ops.lock().push(Op::Add(cookie, false));
|
2020-07-22 19:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds `cookie` to the collection. The cookie's value is encrypted with
|
|
|
|
/// authenticated encryption assuring confidentiality, integrity, and
|
|
|
|
/// authenticity. The cookie can later be retrieved using
|
|
|
|
/// [`get_private`](#method.get_private) and removed using
|
|
|
|
/// [`remove_private`](#method.remove_private).
|
|
|
|
///
|
|
|
|
/// Unless a value is set for the given property, the following defaults are
|
|
|
|
/// set on `cookie` before being added to `self`:
|
|
|
|
///
|
|
|
|
/// * `path`: `"/"`
|
|
|
|
/// * `SameSite`: `Strict`
|
|
|
|
/// * `HttpOnly`: `true`
|
|
|
|
/// * `Expires`: 1 week from now
|
|
|
|
///
|
2023-04-05 20:04:39 +00:00
|
|
|
/// Furthermore, if TLS is enabled, the `Secure` cookie flag is set. These
|
|
|
|
/// defaults ensure maximum usability and security. For additional security,
|
|
|
|
/// you may wish to set the `secure` flag.
|
2020-07-22 19:21:19 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
|
|
|
///
|
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// jar.add_private(Cookie::new("name", "value"));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
|
|
|
pub fn add_private(&self, mut cookie: Cookie<'static>) {
|
2023-04-05 20:04:39 +00:00
|
|
|
Self::set_private_defaults(self.config, &mut cookie);
|
2020-10-15 04:37:16 +00:00
|
|
|
self.ops.lock().push(Op::Add(cookie, true));
|
2017-03-07 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 20:29:03 +00:00
|
|
|
/// Removes `cookie` from this collection and generates a "removal" cookies
|
|
|
|
/// to send to the client on response. For correctness, `cookie` must
|
|
|
|
/// contain the same `path` and `domain` as the cookie that was initially
|
|
|
|
/// set. Failure to provide the initial `path` and `domain` will result in
|
2020-07-22 19:21:19 +00:00
|
|
|
/// cookies that are not properly removed. For convenience, if a path is not
|
|
|
|
/// set on `cookie`, the `"/"` path will automatically be set.
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
|
|
|
/// A "removal" cookie is a cookie that has the same name as the original
|
|
|
|
/// cookie but has an empty value, a max-age of 0, and an expiration date
|
|
|
|
/// far in the past.
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// jar.remove(Cookie::named("name"));
|
2018-11-05 20:29:03 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-07-22 19:21:19 +00:00
|
|
|
pub fn remove(&self, mut cookie: Cookie<'static>) {
|
|
|
|
if cookie.path().is_none() {
|
|
|
|
cookie.set_path("/");
|
2018-11-05 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
self.ops.lock().push(Op::Remove(cookie, false));
|
2020-01-24 05:07:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
/// Removes the private `cookie` from the collection.
|
|
|
|
///
|
|
|
|
/// For correct removal, the passed in `cookie` must contain the same `path`
|
|
|
|
/// and `domain` as the cookie that was initially set. If a path is not set
|
|
|
|
/// on `cookie`, the `"/"` path will automatically be set.
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2018-11-05 20:29:03 +00:00
|
|
|
/// # Example
|
2017-06-24 09:49:16 +00:00
|
|
|
///
|
2018-11-05 20:29:03 +00:00
|
|
|
/// ```rust
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// jar.remove_private(Cookie::named("name"));
|
2017-06-24 09:49:16 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
|
|
|
pub fn remove_private(&self, mut cookie: Cookie<'static>) {
|
|
|
|
if cookie.path().is_none() {
|
|
|
|
cookie.set_path("/");
|
2018-11-05 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
self.ops.lock().push(Op::Remove(cookie, true));
|
2018-11-05 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
/// Returns an iterator over all of the _original_ cookies present in this
|
|
|
|
/// collection.
|
|
|
|
///
|
2021-10-11 22:54:08 +00:00
|
|
|
/// **Note:** This method _does not_ observe changes made via additions and
|
2020-10-15 04:37:16 +00:00
|
|
|
/// removals to the cookie jar.
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// use rocket::http::{Cookie, CookieJar};
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// #[get("/")]
|
|
|
|
/// fn handler(jar: &CookieJar<'_>) {
|
|
|
|
/// for c in jar.iter() {
|
|
|
|
/// println!("Name: {:?}, Value: {:?}", c.name(), c.value());
|
|
|
|
/// }
|
2018-11-05 20:29:03 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-10-15 04:37:16 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item=&Cookie<'static>> {
|
2020-07-22 19:21:19 +00:00
|
|
|
self.jar.iter()
|
2017-03-07 09:19:06 +00:00
|
|
|
}
|
2020-07-22 19:21:19 +00:00
|
|
|
|
|
|
|
/// Removes all delta cookies.
|
|
|
|
#[inline(always)]
|
2021-03-09 02:18:15 +00:00
|
|
|
pub(crate) fn reset_delta(&self) {
|
2020-10-15 04:37:16 +00:00
|
|
|
self.ops.lock().clear();
|
2020-07-22 19:21:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 04:37:16 +00:00
|
|
|
/// TODO: This could be faster by just returning the cookies directly via
|
|
|
|
/// an ordered hash-set of sorts.
|
2021-03-09 02:18:15 +00:00
|
|
|
pub(crate) fn take_delta_jar(&self) -> cookie::CookieJar {
|
2021-06-30 20:36:23 +00:00
|
|
|
let ops = std::mem::take(&mut *self.ops.lock());
|
2020-10-15 04:37:16 +00:00
|
|
|
let mut jar = cookie::CookieJar::new();
|
|
|
|
|
|
|
|
for op in ops {
|
|
|
|
match op {
|
|
|
|
Op::Add(c, false) => jar.add(c),
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
|
|
|
Op::Add(c, true) => {
|
|
|
|
jar.private_mut(&self.config.secret_key.key).add(c);
|
|
|
|
}
|
2020-10-15 04:37:16 +00:00
|
|
|
Op::Remove(mut c, _) => {
|
|
|
|
if self.jar.get(c.name()).is_some() {
|
|
|
|
c.make_removal();
|
|
|
|
jar.add(c);
|
|
|
|
} else {
|
|
|
|
jar.remove(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[allow(unreachable_patterns)]
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jar
|
2020-07-22 19:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an original `cookie` to this collection.
|
|
|
|
#[inline(always)]
|
2021-03-09 02:18:15 +00:00
|
|
|
pub(crate) fn add_original(&mut self, cookie: Cookie<'static>) {
|
2020-07-22 19:21:19 +00:00
|
|
|
self.jar.add_original(cookie)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an original, private `cookie` to the collection.
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
2021-03-09 02:18:15 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub(crate) fn add_original_private(&mut self, cookie: Cookie<'static>) {
|
|
|
|
self.jar.private_mut(&self.config.secret_key.key).add_original(cookie);
|
2020-07-22 19:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// For each property mentioned below, this method checks if there is a
|
|
|
|
/// provided value and if there is none, sets a default value. Default
|
|
|
|
/// values are:
|
2017-10-06 04:58:04 +00:00
|
|
|
///
|
|
|
|
/// * `path`: `"/"`
|
|
|
|
/// * `SameSite`: `Strict`
|
2018-11-05 20:29:03 +00:00
|
|
|
///
|
2023-04-05 20:04:39 +00:00
|
|
|
/// Furthermore, if TLS is enabled, the `Secure` cookie flag is set.
|
|
|
|
fn set_defaults(config: &Config, cookie: &mut Cookie<'static>) {
|
2020-07-22 19:21:19 +00:00
|
|
|
if cookie.path().is_none() {
|
|
|
|
cookie.set_path("/");
|
2017-11-30 17:30:31 +00:00
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
if cookie.same_site().is_none() {
|
|
|
|
cookie.set_same_site(SameSite::Strict);
|
2017-11-30 17:30:31 +00:00
|
|
|
}
|
2023-04-05 20:04:39 +00:00
|
|
|
|
|
|
|
if cookie.secure().is_none() && config.tls_enabled() {
|
|
|
|
cookie.set_secure(true);
|
|
|
|
}
|
2017-11-30 17:30:31 +00:00
|
|
|
}
|
2017-10-06 04:58:04 +00:00
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
/// For each property mentioned below, this method checks if there is a
|
|
|
|
/// provided value and if there is none, sets a default value. Default
|
|
|
|
/// values are:
|
2017-11-30 17:30:31 +00:00
|
|
|
///
|
|
|
|
/// * `path`: `"/"`
|
|
|
|
/// * `SameSite`: `Strict`
|
|
|
|
/// * `HttpOnly`: `true`
|
|
|
|
/// * `Expires`: 1 week from now
|
|
|
|
///
|
2023-04-05 20:04:39 +00:00
|
|
|
/// Furthermore, if TLS is enabled, the `Secure` cookie flag is set.
|
2021-03-09 02:18:15 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
2023-04-05 20:04:39 +00:00
|
|
|
fn set_private_defaults(config: &Config, cookie: &mut Cookie<'static>) {
|
2017-11-30 17:30:31 +00:00
|
|
|
if cookie.path().is_none() {
|
|
|
|
cookie.set_path("/");
|
|
|
|
}
|
2017-10-06 04:58:04 +00:00
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
if cookie.same_site().is_none() {
|
|
|
|
cookie.set_same_site(SameSite::Strict);
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:30:31 +00:00
|
|
|
if cookie.http_only().is_none() {
|
|
|
|
cookie.set_http_only(true);
|
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2017-11-30 17:30:31 +00:00
|
|
|
if cookie.expires().is_none() {
|
2020-05-27 04:37:30 +00:00
|
|
|
cookie.set_expires(time::OffsetDateTime::now_utc() + time::Duration::weeks(1));
|
2017-11-30 17:30:31 +00:00
|
|
|
}
|
2023-04-05 20:04:39 +00:00
|
|
|
|
|
|
|
if cookie.secure().is_none() && config.tls_enabled() {
|
|
|
|
cookie.set_secure(true);
|
|
|
|
}
|
2017-03-08 11:28:12 +00:00
|
|
|
}
|
2017-03-07 09:19:06 +00:00
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
impl fmt::Debug for CookieJar<'_> {
|
2019-06-13 01:33:40 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-10-15 04:37:16 +00:00
|
|
|
let pending: Vec<_> = self.ops.lock()
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.cookie())
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
f.debug_struct("CookieJar")
|
|
|
|
.field("original", &self.jar)
|
|
|
|
.field("pending", &pending)
|
|
|
|
.finish()
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
2018-04-14 23:24:41 +00:00
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|