10000 [HttpClient] relax auth bearer format requirements · symfony/symfony@ac20594 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac20594

Browse files
xabbuhnicolas-grekas
authored andcommitted
[HttpClient] relax auth bearer format requirements
1 parent 65b41de commit ac20594

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,13 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
110110
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, "%s" given.', \gettype($options['auth_basic'])));
111111
}
112112

113-
if (isset($options['auth_bearer']) && (!\is_string($options['auth_bearer']) || !preg_match('{^[-._=:~+/0-9a-zA-Z]++$}', $options['auth_bearer']))) {
114-
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, '.(\is_string($options['auth_bearer']) ? 'invalid string given.' : '"%s" given.'), \gettype($options['auth_bearer'])));
113+
if (isset($options['auth_bearer'])) {
114+
if (!\is_string($options['auth_bearer'])) {
115+
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string, "%s" given.', \gettype($options['auth_bearer'])));
116+
}
117+
if (preg_match('{[^\x21-\x7E]}', $options['auth_bearer'])) {
118+
throw new InvalidArgumentException('Invalid character found in option "auth_bearer": '.json_encode($options['auth_bearer']).'.');
119+
}
115120
}
116121

117122
if (isset($options['auth_basic'], $options['auth_bearer'])) {

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ public function testAuthBearerOption()
179179
public function testInvalidAuthBearerOption()
180180
{
181181
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
182-
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, "object" given.');
182+
$this->expectExceptionMessage('Option "auth_bearer" must be a string, "object" given.');
183183
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => new \stdClass()], HttpClientInterface::OPTIONS_DEFAULTS);
184184
}
185185

186186
public function testInvalidAuthBearerValue()
187187
{
188188
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
189-
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, invalid string given.');
189+
$this->expectExceptionMessage('Invalid character found in option "auth_bearer": "a\nb".');
190190
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => "a\nb"], HttpClientInterface::OPTIONS_DEFAULTS);
191191
}
192192

0 commit comments

Comments
 (0)
0