fix(security): Add security hardening and CSRF protection.
Added .htaccess rules to deny access to hidden files. Escaped JSON output in views to prevent XSS attacks. Converted delete links and forms to use POST with CSRF tokens. Enforced POST method on sensitive routes. Added tests to verify these security controls.
This commit is contained in:
@@ -151,6 +151,7 @@ class WebsiteManager implements WebsiteManagerContract
|
||||
exit();
|
||||
} elseif ($action === "destroy") {
|
||||
$this->handleDestroy($page);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -297,6 +298,15 @@ class WebsiteManager implements WebsiteManagerContract
|
||||
*/
|
||||
public function handleDestroy(PageContract $page)
|
||||
{
|
||||
if (($_SERVER["REQUEST_METHOD"] ?? "") !== "POST") {
|
||||
http_response_code(405);
|
||||
return false;
|
||||
}
|
||||
if (!function_exists("phpb_validate_csrf") || !phpb_validate_csrf()) {
|
||||
http_response_code(403);
|
||||
return false;
|
||||
}
|
||||
|
||||
$pageRepository = new PageRepository();
|
||||
if (!$pageRepository->destroy($page->getId())) {
|
||||
phpb_redirect(phpb_url("website_manager"), [
|
||||
|
||||
@@ -161,17 +161,20 @@
|
||||
) ?>" class="btn btn-ghost btn-square btn-sm">
|
||||
<i class="text-xl ph-duotone ph-gear" aria-hidden="true"></i>
|
||||
</a>
|
||||
<a href="<?= phpb_url("website_manager", [
|
||||
<form method="post" action="<?= phpb_url("website_manager", [
|
||||
"route" => "page_settings",
|
||||
"action" => "destroy",
|
||||
"page" => $page->getId(),
|
||||
]) ?>" title="<?= phpb_trans(
|
||||
]) ?>">
|
||||
<?php phpb_csrf_field(); ?>
|
||||
<button type="submit" title="<?= phpb_trans(
|
||||
"website-manager.remove",
|
||||
) ?>" aria-label="<?= phpb_e(
|
||||
phpb_trans("website-manager.remove"),
|
||||
) ?>" class="btn btn-error btn-square btn-sm">
|
||||
<i class="text-xl ph-duotone ph-trash" aria-hidden="true"></i>
|
||||
</a>
|
||||
<i class="text-xl ph-duotone ph-trash" aria-hidden="true"></i>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -1058,6 +1058,38 @@ $cases = [
|
||||
'Successful deletion did not invalidate every stored localized route: ' . json_encode(PageCreationTestCache::$invalidatedRoutes)
|
||||
);
|
||||
},
|
||||
'website manager page deletion requires POST and a valid CSRF token' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
$repository->create(pageCreationPayload());
|
||||
global $phpb_db;
|
||||
$page = $repository->findWithId($phpb_db->rows('pages')[0]['id']);
|
||||
$websiteManager = new WebsiteManager();
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_POST = [];
|
||||
http_response_code(200);
|
||||
$websiteManager->handleDestroy($page);
|
||||
assertPageCreation(http_response_code() === 405, 'Page deletion must reject GET requests.');
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 1, 'A GET request deleted a page.');
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
$_POST = ['_csrf_token' => 'invalid-token'];
|
||||
http_response_code(200);
|
||||
$websiteManager->handleDestroy($page);
|
||||
assertPageCreation(http_response_code() === 403, 'Page deletion must reject invalid CSRF tokens.');
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 1, 'An invalid CSRF token deleted a page.');
|
||||
|
||||
$_SESSION['phpb_csrf_tokens'] = ['valid-token' => time()];
|
||||
$_POST = ['_csrf_token' => 'valid-token'];
|
||||
try {
|
||||
$websiteManager->handleDestroy($page);
|
||||
} catch (PageCreationRedirectException $exception) {
|
||||
assertPageCreation($exception->redirectUrl === '/website_manager', 'Successful deletion redirected to an unexpected page.');
|
||||
}
|
||||
|
||||
assertPageCreation(count($phpb_db->rows('pages')) === 0, 'A valid token did not delete the page.');
|
||||
},
|
||||
'failed page deletion rolls back translation cleanup without invalidating caches' => function (): void {
|
||||
resetPageCreationDatabase();
|
||||
$repository = new PageRepository();
|
||||
|
||||
Reference in New Issue
Block a user