Skip to content
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

Add legacy_hash_algo to support backward-compatible hash_algo changes #351

Merged
merged 2 commits into from
Jul 5, 2024

Conversation

martijnc
Copy link
Contributor

The goal of this PR is to allow backward-compatible signed cookie hash_algo upgrades. With the current code any existing signed cookies would be rejected when the hash_algo is changed which makes upgrading to a more secure hashing algorithm tricky.

This PR addresses this by;

  • including the used hash algorithm in the signed cookie value;
  • introducing legacy_hash_algo to support a previously configured algorithm during verification.

This is a stepping stone to address #324 but does not yet fix it. Changing the hash_algo default value would still break any existing cookies that don't include the hash algorithm in their signed value yet; the legacy_hash_algo would need to be configured manually for them to keep working. I don't currently see a backward-compatible way to change the hash_algo default value without also (potentially) opening the door for downgrade attacks (e.g. by setting a default value for legacy_hash_algo or hardcoding a sha256 fallback).

This PR also adds a SignerInterface to allow for custom signers.

@Seldaek
Copy link
Member

Seldaek commented Jul 1, 2024

I think it is great to have the option for setting a legacy hash algo, so people can smoothly upgrade cookies, thanks!

IMO having the algo set together with the cookie doesn't add much value, we could simply try to sign using algo, and if that fails try legacy algo if there is one, if it still fails then fail the check?

Another point, it would be nice if when detecting a cookie signed with a legacy algo, a response listener would be registered to update the cookie with a new signature, that way we ensure that cookies get rotated ASAP, and it's easier to say "we set legacy algo for 30days, and after that remove it", making sure that any user hitting the website in those 30days will be fully migrated. Obviously that response listener should be careful to not overwrite cookie removals, or newly written cookies with older values.

Finally just thinking of how we can upgrade the default algo, I guess we'd need to trigger a deprecation if algo is unset to make people set it to something else than sha256, and then in the next major we can change the default.

@martijnc martijnc force-pushed the feature/cookie-algo-upgrade-path branch from 114f320 to 15ce1d6 Compare July 3, 2024 18:15
@martijnc martijnc marked this pull request as draft July 3, 2024 20:41
@martijnc
Copy link
Contributor Author

martijnc commented Jul 4, 2024

Thanks for the feedback!

Updated this PR and removed the changes that added the used algorithm to the cookie value.

Also opened #355 to deprecated the default hash algorithm in the configuration for the next minor.

The upgrade listener is a bit trickier because the bundle needs extra information (expiration, path, ...) than it gets from the browser (only cookie name and value). The application will need to provide the other options for this to work correctly and securely. I've explored this in #356.

@martijnc martijnc marked this pull request as ready for review July 4, 2024 20:00
src/Signer.php Outdated
for ($i = 0, $j = \strlen($signature); $i < $j; ++$i) {
$result |= \ord($signature[$i]) ^ \ord($signature2[$i]);
$expectedLegacySignature = $this->generateSignature($value, $this->legacyAlgo);
if (hash_equals($expectedLegacySignature, $signature)) {
Copy link
Member

Choose a reason for hiding this comment

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

Ah nice, didn't realize we still were using some legacy version of hash_equals here :D

@Seldaek Seldaek merged commit 7de9ce4 into nelmio:master Jul 5, 2024
14 checks passed
@Seldaek
Copy link
Member

Seldaek commented Jul 5, 2024

Thanks!

@martijnc martijnc deleted the feature/cookie-algo-upgrade-path branch July 5, 2024 07:52
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.

2 participants