Skip to content

Commit 83bbb02

Browse files
authored
Merge pull request #5 from k2tzumi/fix-spoof-authentication
Fix spoof authentication
2 parents 5b64d45 + da5b44c commit 83bbb02

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/ValidatesOpenApiSpec.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,14 @@ protected function getSpecFileType(): string
226226
*/
227227
protected function getAuthenticatedRequest(SymfonyRequest $request): SymfonyRequest
228228
{
229+
if ($request->headers->has('Authorization')) {
230+
return $request;
231+
}
232+
233+
// Spoofing when authentication headers are not present.
229234
$authenticatedRequest = clone $request;
230235
$authenticatedRequest->headers->set('Authorization', 'Bearer token');
231-
236+
232237
return $authenticatedRequest;
233238
}
234239

tests/ValidatesRequestsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ public function provideValidationScenarios()
115115
],
116116
true,
117117
];
118+
119+
yield 'Authentication required' => [
120+
[
121+
'method' => 'GET',
122+
'uri' => 'private',
123+
'server' => ['HTTP_Authorization' => 'Basic MTIzNDU2Nzg5MDo='],
124+
],
125+
true,
126+
];
118127
}
119128

120129
private function makeRequest($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

tests/ValidatorBuildAndSetupTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,23 @@ function ($faker) {
150150
*/
151151
public function testBypassesAuthenticationInRequests()
152152
{
153-
$request = $this->getAuthenticatedRequest(new SymfonyRequest());
153+
$originRequest = new SymfonyRequest();
154+
$request = $this->getAuthenticatedRequest($originRequest);
154155
$this->assertTrue($request->headers->has('Authorization'));
156+
$this->assertNotSame($originRequest, $request);
157+
}
158+
159+
/**
160+
* @test
161+
*/
162+
public function testDontSpoofAuthenticationInRequests()
163+
{
164+
$originRequest = new SymfonyRequest();
165+
$authenticationHeaderValue = 'Basic MTIzNDU2Nzg5MDo=';
166+
$originRequest->headers->set('Authorization', $authenticationHeaderValue);
167+
$request = $this->getAuthenticatedRequest($originRequest);
168+
$this->assertTrue($request->headers->has('Authorization'));
169+
$this->assertEquals($authenticationHeaderValue, $request->headers->get('Authorization'));
170+
$this->assertSame($originRequest, $request);
155171
}
156172
}

tests/fixtures/OpenAPI.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,15 @@ paths:
7272
responses:
7373
'200':
7474
description: OK
75+
/private:
76+
get:
77+
responses:
78+
'200':
79+
description: OK
80+
security:
81+
- Basic: []
82+
components:
83+
securitySchemes:
84+
Basic:
85+
type: http
86+
scheme: basic

0 commit comments

Comments
 (0)