Skip to content

Commit

Permalink
Fixed issue with AuthHeaders parser stripping trailing hyphens from t…
Browse files Browse the repository at this point in the history
…okens (#1926)
  • Loading branch information
seguer authored Feb 2, 2021
1 parent e9a1eb8 commit ab00f2d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Http/Parser/AuthHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function parse(Request $request)
{
$header = $request->headers->get($this->header) ?: $this->fromAltHeaders($request);

if ($header && preg_match('/'.$this->prefix.'\s*(\S+)\b/i', $header, $matches)) {
return $matches[1];
if ($header) {
$start = strlen($this->prefix);

return trim(substr($header, $start));
}
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Http/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,62 @@ public function it_should_return_the_token_from_the_alt_authorization_headers()
$this->assertTrue($parser->hasToken());
}

/** @test */
public function it_should_not_strip_trailing_hyphens_from_the_authorization_header()
{
$request = Request::create('foo', 'POST');
$request->headers->set('Authorization', 'Bearer foobar--');

$parser = new Parser($request);

$parser->setChain([
new QueryString,
new InputSource,
new AuthHeaders,
new RouteParams,
]);

$this->assertSame($parser->parseToken(), 'foobar--');
$this->assertTrue($parser->hasToken());
}

/**
* @test
* @dataProvider whitespaceProvider
*/
public function it_should_handle_excess_whitespace_from_the_authorization_header($whitespace)
{
$request = Request::create('foo', 'POST');
$request->headers->set('Authorization', "Bearer{$whitespace}foobar{$whitespace}");

$parser = new Parser($request);

$parser->setChain([
new QueryString,
new InputSource,
new AuthHeaders,
new RouteParams,
]);

$this->assertSame($parser->parseToken(), 'foobar');
$this->assertTrue($parser->hasToken());
}

public function whitespaceProvider()
{
return [
'space' => [' '],
'multiple spaces' => [' '],
'tab' => ["\t"],
'multiple tabs' => ["\t\t\t"],
'new line' => ["\n"],
'multiple new lines' => ["\n\n\n"],
'carriage return' => ["\r"],
'carriage returns' => ["\r\r\r"],
'mixture of whitespace' => ["\t \n \r \t \n"],
];
}

/** @test */
public function it_should_return_the_token_from_query_string()
{
Expand Down

0 comments on commit ab00f2d

Please sign in to comment.