Skip to content

Commit f5241ad

Browse files
[DependencyInjection][Routing][HttpClient] Reject URIs that contain invalid characters
1 parent 6d5c652 commit f5241ad

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

EnvVarProcessor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
310310
if (!isset($params['scheme'], $params['host'])) {
311311
throw new RuntimeException(\sprintf('Invalid URL in env var "%s": scheme and host expected.', $name));
312312
}
313+
if (('\\' !== \DIRECTORY_SEPARATOR || 'file' !== $params['scheme']) && false !== ($i = strpos($env, '\\')) && $i < strcspn($env, '?#')) {
314+
throw new RuntimeException(\sprintf('Invalid URL in env var "%s": backslashes are not allowed.', $name));
315+
}
316+
if (\ord($env[0]) <= 32 || \ord($env[-1]) <= 32 || \strlen($env) !== strcspn($env, "\r\n\t")) {
317+
throw new RuntimeException(\sprintf('Invalid URL in env var "%s": leading/trailing ASCII control characters or whitespaces are not allowed.', $name));
318+
}
313319
$params += [
314320
'port' => null,
315321
'user' => null,

Tests/EnvVarProcessorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,27 @@ public static function provideGetEnvUrlPath()
996996
];
997997
}
998998

999+
/**
1000+
* @testWith ["http://foo.com\\bar"]
1001+
* ["\\\\foo.com/bar"]
1002+
* ["a\rb"]
1003+
* ["a\nb"]
1004+
* ["a\tb"]
1005+
* ["\u0000foo"]
1006+
* ["foo\u0000"]
1007+
* [" foo"]
1008+
* ["foo "]
1009+
* [":"]
1010+
*/
1011+
public function testGetEnvBadUrl(string $url)
1012+
{
1013+
$this->expectException(RuntimeException::class);
1014+
1015+
(new EnvVarProcessor(new Container()))->getEnv('url', 'foo', static function () use ($url): string {
1016+
return $url;
1017+
});
1018+
}
1019+
9991020
/**
10001021
* @testWith ["", "string"]
10011022
* [null, ""]

0 commit comments

Comments
 (0)