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:
2026-12-02 11:07:41 +01:00
parent 6f02b0bc8d
commit b44b1bfc72
11 changed files with 318 additions and 18 deletions
@@ -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();