Skip to content

Commit ab00f2d

Browse files
authored
Fixed issue with AuthHeaders parser stripping trailing hyphens from tokens (#1926)
1 parent e9a1eb8 commit ab00f2d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/Http/Parser/AuthHeaders.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public function parse(Request $request)
5353
{
5454
$header = $request->headers->get($this->header) ?: $this->fromAltHeaders($request);
5555

56-
if ($header && preg_match('/'.$this->prefix.'\s*(\S+)\b/i', $header, $matches)) {
57-
return $matches[1];
56+
if ($header) {
57+
$start = strlen($this->prefix);
58+
59+
return trim(substr($header, $start));
5860
}
5961
}
6062

tests/Http/ParserTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,62 @@ public function it_should_return_the_token_from_the_alt_authorization_headers()
108108
$this->assertTrue($parser->hasToken());
109109
}
110110

111+
/** @test */
112+
public function it_should_not_strip_trailing_hyphens_from_the_authorization_header()
113+
{
114+
$request = Request::create('foo', 'POST');
115+
$request->headers->set('Authorization', 'Bearer foobar--');
116+
117+
$parser = new Parser($request);
118+
119+
$parser->setChain([
120+
new QueryString,
121+
new InputSource,
122+
new AuthHeaders,
123+
new RouteParams,
124+
]);
125+
126+
$this->assertSame($parser->parseToken(), 'foobar--');
127+
$this->assertTrue($parser->hasToken());
128+
}
129+
130+
/**
131+
* @test
132+
* @dataProvider whitespaceProvider
133+
*/
134+
public function it_should_handle_excess_whitespace_from_the_authorization_header($whitespace)
135+
{
136+
$request = Request::create('foo', 'POST');
137+
$request->headers->set('Authorization', "Bearer{$whitespace}foobar{$whitespace}");
138+
139+
$parser = new Parser($request);
140+
141+
$parser->setChain([
142+
new QueryString,
143+
new InputSource,
144+
new AuthHeaders,
145+
new RouteParams,
146+
]);
147+
148+
$this->assertSame($parser->parseToken(), 'foobar');
149+
$this->assertTrue($parser->hasToken());
150+
}
151+
152+
public function whitespaceProvider()
153+
{
154+
return [
155+
'space' => [' '],
156+
'multiple spaces' => [' '],
157+
'tab' => ["\t"],
158+
'multiple tabs' => ["\t\t\t"],
159+
'new line' => ["\n"],
160+
'multiple new lines' => ["\n\n\n"],
161+
'carriage return' => ["\r"],
162+
'carriage returns' => ["\r\r\r"],
163+
'mixture of whitespace' => ["\t \n \r \t \n"],
164+
];
165+
}
166+
111167
/** @test */
112168
public function it_should_return_the_token_from_query_string()
113169
{

0 commit comments

Comments
 (0)