Skip to content

Commit 595f600

Browse files
williamdesDeepSeek
andcommitted
security: Answer 403 for an unknown controller instead of signing out
e452f05 routed the decision of hasAccessToAction() through a boolean, and the caller destroys the session on false: a read-only developer who requested a controller that does not exist, GET /does-not-exist/index, was signed out by the request. Answer with a 403 when no controller class of that name exists, and keep the existing denial for the ones that do. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 3b1bf73 commit 595f600

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎src/Middleware/AuthenticationMiddleware.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace App\Middleware;
66

7+
use Cake\Core\App;
8+
use Cake\Http\Exception\ForbiddenException;
79
use Cake\Http\Response;
810
use Cake\Http\ServerRequest;
911
use Cake\ORM\TableRegistry;
@@ -15,6 +17,7 @@
1517

1618
use function assert;
1719
use function in_array;
20+
use function is_string;
1821

1922
class AuthenticationMiddleware implements MiddlewareInterface
2023
{
@@ -142,6 +145,13 @@ protected function hasAccessToAction(ServerRequest $request): bool
142145

143146
// Check for the controller name
144147
if (! isset(self::READ_ONLY_ACCESS_CONTROL_LIST[$controllerName])) {
148+
if (! $this->controllerExists($controllerName)) {
149+
// There is nothing to grant access to: the developer must not
150+
// be signed out for requesting a controller which does not
151+
// exist, the request simply ends with a 403.
152+
throw new ForbiddenException();
153+
}
154+
145155
return false;// The controller is not in the list
146156
}
147157

@@ -157,4 +167,21 @@ protected function hasAccessToAction(ServerRequest $request): bool
157167

158168
return false;// phpcs:ignore SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn.UselessIfCondition
159169
}
170+
171+
/**
172+
* Tells whether the controller name of the request matches a controller
173+
* class of the application.
174+
*
175+
* @param mixed $controllerName the controller name of the request
176+
*
177+
* @return bool true when the controller exists
178+
*/
179+
private function controllerExists(mixed $controllerName): bool
180+
{
181+
if (! is_string($controllerName) || $controllerName === '') {
182+
return false;
183+
}
184+
185+
return App::className($controllerName, 'Controller', 'Controller') !== null;
186+
}
160187
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Test\TestCase\Middleware;
6+
7+
use App\Test\Fixture\DevelopersFixture;
8+
use Cake\TestSuite\IntegrationTestTrait;
9+
use Cake\TestSuite\TestCase;
10+
11+
class AuthenticationMiddlewareTest extends TestCase
12+
{
13+
use IntegrationTestTrait;
14+
15+
public function getFixtures(): array
16+
{
17+
return [
18+
DevelopersFixture::class,
19+
];
20+
}
21+
22+
public function testAnUnknownControllerDoesNotDestroyTheSession(): void
23+
{
24+
$this->session(['Developer.id' => 1, 'read_only' => true]);
25+
26+
$this->get('/does-not-exist/index');
27+
28+
$this->assertResponseCode(403);
29+
30+
// Reading the session through its Session object after the request
31+
// would restart it, and a session restart in CLI resets it, so the
32+
// session storage is inspected instead.
33+
$this->assertSame(1, $_SESSION['Developer']['id'] ?? null);
34+
$this->assertSame(true, $_SESSION['read_only'] ?? null);
35+
}
36+
37+
public function testAReadOnlyDeveloperIsStillRedirectedFromAProtectedAction(): void
38+
{
39+
$this->session(['Developer.id' => 1, 'read_only' => true]);
40+
41+
$this->get('/stats');
42+
43+
$this->assertResponseCode(307);
44+
$this->assertRedirect('/');
45+
$this->assertFlashMessage(
46+
'You need to have commit access on phpmyadmin/phpmyadmin repository on Github.com to do this action (Stats:stats)',
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)