-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[HttpFoundation] Fix AcceptHeader overwrites items with different parameters #62287
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
[HttpFoundation] Fix AcceptHeader overwrites items with different parameters #62287
Conversation
src/Symfony/Component/HttpFoundation/Tests/AcceptHeaderTest.php
Outdated
Show resolved
Hide resolved
b72a7a0 to
c99d747
Compare
|
Hi @yoeunes, From what I understand, this looks more like an enhancement of the existing behaviour rather than a bug fix. Is that correct? Thanks! |
|
Hi @Spomky, I consider this a bug fix because the current implementation incorrectly overwrites header items (e.g., If it's confirmed as a bug fix, should I rebase this onto 6.4 instead? Let me know your thoughts or if more details are needed |
andesk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to look into the issue I've created yesterday! 👍
It feels like the implementation might be a bit brittle in regards of some assumptions made on the case-(in)sensitivity comparison of two media type strings? Also, certainly having more data to take into account for matching makes things harder. Having test coverage is great, maybe we can improve/simplify the logic still in some areas?
9e19845 to
bebe53d
Compare
|
Hi @andesk, I'm consolidating my responses to your comments here to keep the thread tidy. Thanks for the detailed review. I've pushed a full refactor that applies your suggestions, makes the code more readable, and adds explanatory comments. Regarding case-insensitivity, you are correct. I'm now normalizing parameter names using The PR should be ready for another look. Thanks! |
bebe53d to
c1dee8f
Compare
derrabus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this issue.
I think, we should add additional tests for Request::getAcceptableContentTypes() and Request::getPreferredFormat() because they're directly impacted by this change.
src/Symfony/Component/HttpFoundation/Tests/AcceptHeaderTest.php
Outdated
Show resolved
Hide resolved
7f880eb to
284531e
Compare
|
Hi @derrabus, thank you for your review and feedback! I've just pushed an update to add the requested tests for Let me know if anything else is needed. |
284531e to
9b1fb5c
Compare
9b1fb5c to
33f535e
Compare
Spomky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Many thanks!
33f535e to
95d3d74
Compare
95d3d74 to
53ba9dd
Compare
53ba9dd to
caeb1a0
Compare
|
Thank you @yoeunes. |
|
This fails on 7.4, with some implicit dependency:
We missed something. Could you please have a look @yoeunes? Not related to the failure, but this would be nicer than an array_map: --- a/src/Symfony/Component/HttpFoundation/AcceptHeader.php
+++ b/src/Symfony/Component/HttpFoundation/AcceptHeader.php
@@ -48,16 +48,19 @@ class AcceptHeader
{
$parts = HeaderUtils::split($headerValue ?? '', ',;=');
- return new self(array_map(function ($subParts) {
- static $index = 0;
+ $items = [];
+ $index = 0;
+ foreach ($parts as $subParts) {
$part = array_shift($subParts);
$attributes = HeaderUtils::combine($subParts);
$item = new AcceptHeaderItem($part[0], $attributes);
$item->setIndex($index++);
- return $item;
- }, $parts));
+ $items[] = $item;
+ }
+
+ return new self($items);
} |
|
FYI I'm having a look at my own previous comment. |
…kas) This PR was merged into the 6.4 branch. Discussion ---------- [HttpFoundation] Fix RequestTest insulation | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Missing insulation is how we wrote bad tests in 7.4 Also addressing my patch proposal at #62287 (comment) Commits ------- 2d3779a [HttpFoundation] Fix RequestTest insulation
This PR fixes a bug in
AcceptHeaderwhere items with the same media type but different parameters (e.g.,text/plain;format=flowedandtext/plain;format=fixed) would overwrite each other.The
add()method now uses a canonical key that includes all parameters (exceptq), ensuring distinct items are stored correctly.Consequently, the
get()method has been updated to perform proper content negotiation (as described in RFC 9110), including matching parameters and sorting by specificity (e.g.,text/plain>text/*>*/*). New tests are added to cover these cases.