Skip to content

Commit 4bcadf5

Browse files
authored
Merge pull request #20 from givebutter/givebutter/security-improvement
2 parents 41075c2 + 2d5a3f4 commit 4bcadf5

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Http/Middleware/AuthenticateApiKey.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ class AuthenticateApiKey
1818
*/
1919
public function handle($request, Closure $next, $guard = null)
2020
{
21+
$forbidenRequestParams = ['apiKey', 'keyable'];
22+
23+
// Check if request has forbidden params
24+
foreach ($forbidenRequestParams as $param) {
25+
if ($request->missing($param)) {
26+
continue;
27+
}
28+
29+
$message = "Request param '{$param}' is not allowed.";
30+
31+
if ($request->wantsJson()) {
32+
return response()->json(['message' => $message], 400);
33+
}
34+
35+
return response($message, 400);
36+
}
37+
2138
//Get API token from request
2239
$token = $this->getKeyFromRequest($request);
2340

tests/Feature/AuthenticateApiKey.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Givebutter\Tests\Feature;
44

5+
use Givebutter\LaravelKeyable\Exceptions\ForbidenRequestParamException;
56
use Givebutter\Tests\TestCase;
67
use Givebutter\Tests\Support\Account;
78
use Illuminate\Support\Facades\Route;
@@ -81,4 +82,72 @@ public function request_without_api_key_responds_unauthorized()
8182

8283
$this->get("/api/posts")->assertUnauthorized();
8384
}
85+
86+
/**
87+
* @test
88+
* @dataProvider forbiddenRequestParams
89+
*/
90+
public function throw_exception_if_unauthorized_get_request_has_forbidden_request_query_params(string $queryParam): void
91+
{
92+
Route::get('/api/posts', function () {
93+
return response('All good', 200);
94+
})->middleware(['api', 'auth.apikey']);
95+
96+
$this->get("/api/posts?{$queryParam}=value")
97+
->assertBadRequest()
98+
->assertContent("Request param '{$queryParam}' is not allowed.");
99+
}
100+
101+
/**
102+
* @test
103+
* @dataProvider forbiddenRequestParams
104+
*/
105+
public function throw_exception_if_unauthorized_post_request_has_forbidden_request_body_params(string $bodyParam): void
106+
{
107+
Route::post('/api/posts', function () {
108+
return response('All good', 200);
109+
})->middleware(['api', 'auth.apikey']);
110+
111+
$this->post('/api/posts', [$bodyParam => 'value'])
112+
->assertBadRequest()
113+
->assertContent("Request param '{$bodyParam}' is not allowed.");
114+
}
115+
116+
/**
117+
* @test
118+
* @dataProvider forbiddenRequestParams
119+
*/
120+
public function throw_exception_if_unauthorized_json_get_request_has_forbidden_request_query_params(string $queryParam): void
121+
{
122+
Route::get('/api/posts', function () {
123+
return response('All good', 200);
124+
})->middleware(['api', 'auth.apikey']);
125+
126+
$this->getJson("/api/posts?{$queryParam}=value")
127+
->assertBadRequest()
128+
->assertJson(['message' => "Request param '{$queryParam}' is not allowed."]);
129+
}
130+
131+
/**
132+
* @test
133+
* @dataProvider forbiddenRequestParams
134+
*/
135+
public function throw_exception_if_unauthorized_json_post_request_has_forbidden_request_body_params(string $bodyParam): void
136+
{
137+
Route::post('/api/posts', function () {
138+
return response('All good', 200);
139+
})->middleware(['api', 'auth.apikey']);
140+
141+
$this->postJson('/api/posts', [$bodyParam => 'value'])
142+
->assertBadRequest()
143+
->assertJson(['message' => "Request param '{$bodyParam}' is not allowed."]);
144+
}
145+
146+
public function forbiddenRequestParams(): array
147+
{
148+
return [
149+
['keyable'],
150+
['apiKey'],
151+
];
152+
}
84153
}

0 commit comments

Comments
 (0)