Skip to content

Commit b17abb2

Browse files
committed
qa: updates psr-7 integration test version
Updates to 1.2.0, which adds the tests we wrote for mitigating ZF2015-05, with a few changes: - When creating the string representation of the URL, we DO NOT normalize the path to remove multiple leading slashes. In its absolute form, this is not necessary. - All normalization is done via `getPath()`; this mitigates the common XSS scenario. - It adds a test to validate that when using origin-form during a `RequestInterface::getRequestTarget()` call, it will use the results of `getPath()`, as this is a scenario where the XSS could also occur. I have removed one test from `UriTest`, as it contradicts the first point above. Since the scenario is covered in the PSR-7 integration tests, we are covered. See php-http/psr7-integration-tests#54 for more details. Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
1 parent cf1dc9e commit b17abb2

File tree

4 files changed

+116
-50
lines changed

4 files changed

+116
-50
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"ext-libxml": "*",
4646
"http-interop/http-factory-tests": "^0.9.0",
4747
"laminas/laminas-coding-standard": "^2.4.0",
48-
"php-http/psr7-integration-tests": "^1.1.1",
48+
"php-http/psr7-integration-tests": "^1.2",
4949
"phpunit/phpunit": "^9.5.26",
5050
"psalm/plugin-phpunit": "^0.18.0",
5151
"vimeo/psalm": "^5.0.0"

composer.lock

Lines changed: 101 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Uri.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __toString(): string
113113
$this->uriString = static::createUriString(
114114
$this->scheme,
115115
$this->getAuthority(),
116-
$this->getPath(), // Absolute URIs should use a "/" for an empty path
116+
$this->path, // Absolute URIs should use a "/" for an empty path
117117
$this->query,
118118
$this->fragment
119119
);
@@ -185,7 +185,18 @@ public function getPort(): ?int
185185
*/
186186
public function getPath(): string
187187
{
188-
return $this->path;
188+
if ('' === $this->path) {
189+
// No path
190+
return $this->path;
191+
}
192+
193+
if ($this->path[0] !== '/') {
194+
// Relative path
195+
return $this->path;
196+
}
197+
198+
// Ensure only one leading slash, to prevent XSS attempts.
199+
return '/' . ltrim($this->path, '/');
189200
}
190201

191202
/**
@@ -557,7 +568,7 @@ private function filterPath(string $path): string
557568
{
558569
$path = $this->filterInvalidUtf8($path);
559570

560-
$path = preg_replace_callback(
571+
return preg_replace_callback(
561572
'/(?:[^' . self::CHAR_UNRESERVED . ')(:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/u',
562573
[$this, 'urlEncodeChar'],
563574
$path

test/UriTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,6 @@ public function testFragmentIsNotDoubleEncoded(): void
575575
$this->assertSame($expected, $uri->getFragment());
576576
}
577577

578-
public function testProperlyTrimsLeadingSlashesToPreventXSS(): void
579-
{
580-
$url = 'http://example.org//zend.com';
581-
$uri = new Uri($url);
582-
$this->assertSame('http://example.org/zend.com', (string) $uri);
583-
}
584-
585578
/** @return non-empty-array<string, array{'withScheme'|'withUserInfo'|'withHost'|'withPath'|'withQuery'|'withFragment', mixed}> */
586579
public function invalidStringComponentValues(): array
587580
{

0 commit comments

Comments
 (0)