Skip to content

Commit

Permalink
Merge pull request #5 from k2tzumi/fix-spoof-authentication
Browse files Browse the repository at this point in the history
Fix spoof authentication
  • Loading branch information
zerodahero authored Jul 11, 2022
2 parents 5b64d45 + da5b44c commit 83bbb02
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/ValidatesOpenApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,14 @@ protected function getSpecFileType(): string
*/
protected function getAuthenticatedRequest(SymfonyRequest $request): SymfonyRequest
{
if ($request->headers->has('Authorization')) {
return $request;
}

// Spoofing when authentication headers are not present.
$authenticatedRequest = clone $request;
$authenticatedRequest->headers->set('Authorization', 'Bearer token');

return $authenticatedRequest;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ValidatesRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function provideValidationScenarios()
],
true,
];

yield 'Authentication required' => [
[
'method' => 'GET',
'uri' => 'private',
'server' => ['HTTP_Authorization' => 'Basic MTIzNDU2Nzg5MDo='],
],
true,
];
}

private function makeRequest($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
Expand Down
18 changes: 17 additions & 1 deletion tests/ValidatorBuildAndSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,23 @@ function ($faker) {
*/
public function testBypassesAuthenticationInRequests()
{
$request = $this->getAuthenticatedRequest(new SymfonyRequest());
$originRequest = new SymfonyRequest();
$request = $this->getAuthenticatedRequest($originRequest);
$this->assertTrue($request->headers->has('Authorization'));
$this->assertNotSame($originRequest, $request);
}

/**
* @test
*/
public function testDontSpoofAuthenticationInRequests()
{
$originRequest = new SymfonyRequest();
$authenticationHeaderValue = 'Basic MTIzNDU2Nzg5MDo=';
$originRequest->headers->set('Authorization', $authenticationHeaderValue);
$request = $this->getAuthenticatedRequest($originRequest);
$this->assertTrue($request->headers->has('Authorization'));
$this->assertEquals($authenticationHeaderValue, $request->headers->get('Authorization'));
$this->assertSame($originRequest, $request);
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/OpenAPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ paths:
responses:
'200':
description: OK
/private:
get:
responses:
'200':
description: OK
security:
- Basic: []
components:
securitySchemes:
Basic:
type: http
scheme: basic

0 comments on commit 83bbb02

Please sign in to comment.