This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Fix #357 - invalid characters in path/query #372
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,7 @@ | |
|
||
use Psr\Http\Message\UriInterface; | ||
|
||
use function array_key_exists; | ||
use function array_keys; | ||
use function count; | ||
use function explode; | ||
use function get_class; | ||
use function gettype; | ||
|
@@ -23,10 +21,12 @@ | |
use function is_string; | ||
use function ltrim; | ||
use function parse_url; | ||
use function preg_match; | ||
use function preg_replace; | ||
use function preg_replace_callback; | ||
use function rawurlencode; | ||
use function sprintf; | ||
use function str_split; | ||
use function strpos; | ||
use function strtolower; | ||
use function substr; | ||
|
@@ -560,6 +560,8 @@ private function filterScheme(string $scheme) : string | |
*/ | ||
private function filterUserInfoPart(string $part) : string | ||
{ | ||
$part = $this->filterInvalidUtf8($part); | ||
|
||
// Note the addition of `%` to initial charset; this allows `|` portion | ||
// to match and thus prevent double-encoding. | ||
return preg_replace_callback( | ||
|
@@ -574,6 +576,8 @@ private function filterUserInfoPart(string $part) : string | |
*/ | ||
private function filterPath(string $path) : string | ||
{ | ||
$path = $this->filterInvalidUtf8($path); | ||
|
||
$path = preg_replace_callback( | ||
'/(?:[^' . self::CHAR_UNRESERVED . ')(:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/u', | ||
[$this, 'urlEncodeChar'], | ||
|
@@ -594,6 +598,25 @@ private function filterPath(string $path) : string | |
return '/' . ltrim($path, '/'); | ||
} | ||
|
||
/** | ||
* Encode invalid UTF-8 characters in given string. All other characters are unchanged. | ||
*/ | ||
private function filterInvalidUtf8(string $string) : string | ||
{ | ||
if (preg_match('//u', $string)) { | ||
return $string; | ||
} | ||
|
||
$letters = str_split($string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting here, because I had to look it up - |
||
foreach ($letters as $i => $letter) { | ||
if (! preg_match('//u', $letter)) { | ||
$letters[$i] = $this->urlEncodeChar([$letter]); | ||
} | ||
} | ||
|
||
return implode('', $letters); | ||
} | ||
|
||
/** | ||
* Filter a query string to ensure it is propertly encoded. | ||
* | ||
|
@@ -654,6 +677,8 @@ private function filterFragment(string $fragment) : string | |
*/ | ||
private function filterQueryOrFragment(string $value) : string | ||
{ | ||
$value = $this->filterInvalidUtf8($value); | ||
|
||
return preg_replace_callback( | ||
'/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/u', | ||
[$this, 'urlEncodeChar'], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't understand what this is doing, exactly.
I've tried it locally with a variety of strings from your query provider below, and every single one of them results in a positive match, which means the logic below never gets hit. Maybe you're not testing invalid UTF-8 characters anywhere?
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.
How come?
See: https://3v4l.org/TntQf
result:
Somehow tests I've added work.
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.
Turned out I was using single quotes instead of double quotes when I was doing my tests here. Once I changed the quoting style, I was able to observe the same behaviour finally.
Might be good to drop a note in indicating what this is doing, though, so future contributors/maintainers know the purpose.