Skip to content

Commit

Permalink
Fix SignedCookieJar with custom key type (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Apr 1, 2022
1 parent 1b4c54c commit da3ca22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning].
# Unreleased

- **added:** Re-export `SameSite` and `Expiration` from the `cookie` crate.
- **fixed:** Fix `SignedCookieJar` when using custom key types ([#899])

[#899]: https://github.com/tokio-rs/axum/pull/899

# 0.2.0 (31. March, 2022)

Expand Down
19 changes: 15 additions & 4 deletions axum-extra/src/extract/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ where
type Rejection = <axum::Extension<K> as FromRequest<B>>::Rejection;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let Extension(key) = Extension::from_request(req).await?;
let key = Extension::<K>::from_request(req).await?.0.into();

let mut jar = cookie_lib::CookieJar::new();
let mut signed_jar = jar.signed_mut(&key);
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<K> SignedCookieJar<K> {
}
}

impl IntoResponseParts for SignedCookieJar {
impl<K> IntoResponseParts for SignedCookieJar<K> {
type Error = Infallible;

fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
Expand All @@ -397,7 +397,7 @@ fn set_cookies(jar: cookie_lib::CookieJar, headers: &mut HeaderMap) {
// jar so it cannot be called multiple times.
}

impl IntoResponse for SignedCookieJar {
impl<K> IntoResponse for SignedCookieJar<K> {
fn into_response(self) -> Response {
(self, ()).into_response()
}
Expand Down Expand Up @@ -448,7 +448,8 @@ mod tests {
.route("/set", get(set_cookie))
.route("/get", get(get_cookie))
.route("/remove", get(remove_cookie))
.layer(Extension(Key::generate()));
.layer(Extension(Key::generate()))
.layer(Extension(CustomKey(Key::generate())));

let res = app
.clone()
Expand Down Expand Up @@ -492,6 +493,16 @@ mod tests {

cookie_test!(plaintext_cookies, CookieJar);
cookie_test!(signed_cookies, SignedCookieJar);
cookie_test!(signed_cookies_with_custom_key, SignedCookieJar<CustomKey>);

#[derive(Clone)]
struct CustomKey(Key);

impl From<CustomKey> for Key {
fn from(custom: CustomKey) -> Self {
custom.0
}
}

#[tokio::test]
async fn signed_cannot_access_invalid_cookies() {
Expand Down

0 comments on commit da3ca22

Please sign in to comment.