Skip to content

feat(extract): prefixed signed&private cookies #3251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

andrzejSulkowski
Copy link

@andrzejSulkowski andrzejSulkowski commented Mar 6, 2025

closes #3246

Cookie Prefixes Support

Motivation

Cookie prefixes (__Host- and __Secure-) are a web security feature that help protect against cookie manipulation and session fixation attacks. This PR enhances Axum's cookie handling by adding proper support for cookie prefixes across all cookie jar types (CookieJar, SignedCookieJar, and PrivateCookieJar).

Solution

This PR implements:

  • Support for cookie prefixes with correct attribute enforcement (e.g., Secure flag, Path=/) using the underlying cookie crate's PrefixedJar
  • Consistent prefix handling methods (add_prefixed, get_prefixed, remove_prefixed) for all cookie jar types
    Integration of prefix support with signed and private cookies, ensuring the encryption/signing is maintained
  • Extended cookie tests to verify cookie encryption/signing rather than plain-text value matching

@andrzejSulkowski andrzejSulkowski marked this pull request as draft March 6, 2025 17:16
@andrzejSulkowski andrzejSulkowski marked this pull request as ready for review March 7, 2025 05:57
@jplatte jplatte requested review from yanns, Turbo87 and mladedav June 22, 2025 21:53
@Turbo87
Copy link
Collaborator

Turbo87 commented Jun 23, 2025

@jplatte do you know if there is a reason why the inner jars are not exposed as public API? seems like that would simplify the implementation quite a bit 🤔

Copy link
Collaborator

@yanns yanns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is looking good to me.
Nitpick: I don't know if we want some println in the code through.

I don't know enough the API + our consensus about exposing the cookie API to make a decision here.

@andrzejSulkowski
Copy link
Author

@jplatte do you know if there is a reason why the inner jars are not exposed as public API? seems like that would simplify the implementation quite a bit 🤔

Are you suggesting we expose the inner jars alongside the current convenience methods, or replace the current API entirely with direct access to the underlying cookie crate jars?

@Turbo87
Copy link
Collaborator

Turbo87 commented Jun 23, 2025

Are you suggesting we expose the inner jars alongside the current convenience methods

maybe :D

I don't know the history of this extractor, so before I can form an opinion it would be better to know why this wasn't done in the first place 😅

@andrzejSulkowski
Copy link
Author

Are you suggesting we expose the inner jars alongside the current convenience methods

maybe :D

I don't know the history of this extractor, so before I can form an opinion it would be better to know why this wasn't done in the first place 😅

I don't know the historical reasoning either, and I'm not really proficient enough to predict what users might need.
But the hybrid approach seems like a good middle ground - keep the convenience methods for quick development while exposing the inner jars for more tailored use-cases that need the full flexibility of the cookie crate.

What do you think?

@jplatte
Copy link
Member

jplatte commented Jun 23, 2025

I think the historical reason was that the cookie stuff used to be in axum and David didn't want the cookie crate as part of the public API, to be able to upgrade it across major versions within a minor version of axum.

However now the cookie crate is already part of the public API anyways, so I think we might as well.. Maybe?

It's been a while that I've actually looked at the details here. If you want, I can take a closer look before time is spent on refactoring stuff here that may be reversed again at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[axum-extra] Cookie Name Prefixes
4 participants