Fix 'get_pending()' docs, functionality.

The `get_pending()` method now properly decrypts private cookies that were
present in the jar originally.

Resolves #2591.
This commit is contained in:
Sergio Benitez 2023-08-10 16:22:28 -04:00
parent c2936fcb1e
commit c337f75f32
2 changed files with 16 additions and 7 deletions

View File

@ -243,8 +243,9 @@ impl<'a> CookieJar<'a> {
/// 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
/// retrieve such cookies, using [`CookieJar::get()`] or
/// In general, due to performance overhead, calling this method should be
/// avoided if it is known that a cookie called `name` is not pending.
/// Instead, prefer to use [`CookieJar::get()`] or
/// [`CookieJar::get_private()`].
///
/// # Example
@ -268,7 +269,14 @@ impl<'a> CookieJar<'a> {
}
drop(ops);
self.get(name).cloned()
#[cfg(feature = "secrets")] {
self.get_private(name).or_else(|| self.get(name).cloned())
}
#[cfg(not(feature = "secrets"))] {
self.get(name).cloned()
}
}
/// Adds `cookie` to this collection.

View File

@ -36,6 +36,7 @@ fn cookie_get_private(jar: &CookieJar<'_>) -> String {
assert_ne!(a, b.as_ref());
assert_ne!(a, c);
assert_ne!(b.as_ref(), c);
assert_eq!(b, jar.get_pending("b"));
format!(
"{}{}{}",
@ -49,6 +50,7 @@ fn cookie_get_private(jar: &CookieJar<'_>) -> String {
#[get("/oh-no")]
fn cookie_get(jar: &CookieJar<'_>) -> String {
let (a, b, c) = (jar.get("a"), jar.get("b"), jar.get("c"));
assert_eq!(b.cloned(), jar.get_pending("b"));
format!(
"{}{}{}",
@ -65,10 +67,8 @@ mod cookies_private_tests {
use rocket::{Build, Rocket};
fn rocket() -> Rocket<Build> {
rocket::build().mount(
"/",
routes![cookie_add_private, cookie_get, cookie_get_private],
)
rocket::build()
.mount("/", routes![cookie_add_private, cookie_get, cookie_get_private])
}
#[test]
@ -79,6 +79,7 @@ mod cookies_private_tests {
assert_eq!(cookies.iter().count(), 3);
assert_eq!(cookies.get("a").unwrap().value(), "v1");
assert_eq!(cookies.get_private("b").unwrap().value(), "v2");
assert_eq!(cookies.get_pending("b").unwrap().value(), "v2");
assert_ne!(cookies.get("b").unwrap().value(), "v2");
assert_eq!(cookies.get("c").unwrap().value(), "v3");
}