Skip to content

[HttpFoundation] Recursively order query string keys in Request::normalizeQueryString() #57190

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Add `HeaderRequestMatcher`
* Add support for `\SplTempFileObject` in `BinaryFileResponse`
* Add `verbose` argument to response test constraints
* [BC BREAK] Query string key/value pairs are now recursively sorted in `Request::normalizeQueryString()`.

7.0
---
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,15 @@ public static function normalizeQueryString(?string $qs): string
}

$qs = HeaderUtils::parseQuery($qs);
ksort($qs);
$recursive_sort = function (&$array) use (&$recursive_sort) {
foreach ($array as &$v) {
if (is_array($v)) {
$recursive_sort($v);
}
}
ksort($array);
};
$recursive_sort($qs);

return http_build_query($qs, '', '&', \PHP_QUERY_RFC3986);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,9 @@ public static function getQueryStringNormalizationData()
// PHP also does not include them when building _GET.
['foo=bar&=a=b&=x=y', 'foo=bar', 'removes params with empty key'],

// Don't reorder nested query string keys
// Provide predictable ordering of nested query string keys.
['foo[]=Z&foo[]=A', 'foo%5B0%5D=Z&foo%5B1%5D=A', 'keeps order of values'],
['foo[Z]=B&foo[A]=B', 'foo%5BZ%5D=B&foo%5BA%5D=B', 'keeps order of keys'],
['foo[Z]=A&foo[A]=B', 'foo%5BA%5D=B&foo%5BZ%5D=A', 'nested keys are reordered but values are maintained'],

['utf8=✓', 'utf8=%E2%9C%93', 'encodes UTF-8'],
];
Expand Down
Loading