Skip to content

Commit 4fcda3c

Browse files
bug #38647 [HttpClient] relax auth bearer format requirements (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] relax auth bearer format requirements | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38609 | License | MIT | Doc PR | Commits ------- ac20594267 [HttpClient] relax auth bearer format requirements
2 parents 64db290 + 85b2573 commit 4fcda3c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

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'])) {

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)