Skip to content

Commit

Permalink
fix: Fix the Url::percentRecodeQuery method
Browse files Browse the repository at this point in the history
URLs can have several parameters with the same name (e.g. `?foo=bar&foo=baz`),
which are accessible in an array (see `Url::parseQuery()`).

However, the `percentRecodeQuery` method wasn't aware of this
possibility and was executing the `fullPercentDecode()` method directly
on the array instead of each part of the array.
  • Loading branch information
marienfressinaud committed Aug 13, 2022
1 parent 9cd437a commit 23be6a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/SpiderBits/src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ private static function percentRecodeQuery($query)
$parameters = self::parseQuery($query);
foreach ($parameters as $name => $value) {
$name = rawurlencode(self::fullPercentDecode($name));
if ($value !== null) {
if (is_array($value)) {
$value = array_map(function ($partial_value) {
return rawurlencode(self::fullPercentDecode($partial_value));
}, $value);
} elseif ($value !== null) {
$value = rawurlencode(self::fullPercentDecode($value));
}
$decoded_parameters[$name] = $value;
Expand Down
1 change: 1 addition & 0 deletions tests/lib/SpiderBits/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function sanitizeProvider()
['https://example.com/?url=http%3A%2F%2Fexample.fr%2F%3Ffoo%3Dbar&spam=egg', 'https://example.com/?url=http%3A%2F%2Fexample.fr%2F%3Ffoo%3Dbar&spam=egg'], // phpcs:ignore Generic.Files.LineLength.TooLong
['https://example.com/?foo?=bar', 'https://example.com/?foo%3F=bar'],
['https://example.com/?foo%26=bar&spam=egg', 'https://example.com/?foo%26=bar&spam=egg'],
['https://example.com/?foo=bar?&foo=baz?', 'https://example.com/?foo=bar%3F&foo=baz%3F'],
["https://domén-with-accent.com?query=with-àccent", 'https://xn--domn-with-accent-dqb.com/?query=with-%C3%A0ccent'], // phpcs:ignore Generic.Files.LineLength.TooLong
["https://host.com?query=with-%C3%A0ccent", 'https://host.com/?query=with-%C3%A0ccent'],
["https://host.com?utm_source=gazette%252B-%252Babonn%25C3%25A9s", 'https://host.com/?utm_source=gazette%2B-%2Babonn%C3%A9s'], // phpcs:ignore Generic.Files.LineLength.TooLong
Expand Down

0 comments on commit 23be6a6

Please sign in to comment.