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.
209 lines
7.2 KiB
PHP
209 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace Plugi\Extensions\Form {
|
|
class DefinitionsRepository
|
|
{
|
|
public static array $form = [];
|
|
public static array $created = [];
|
|
public static array $destroyed = [];
|
|
|
|
public function findWithId($id)
|
|
{
|
|
return self::$form;
|
|
}
|
|
|
|
public function create(array $data): null|object|array
|
|
{
|
|
self::$created[] = $data;
|
|
return null;
|
|
}
|
|
|
|
public function update($instance, array $data): void
|
|
{
|
|
}
|
|
|
|
public function destroy($id): void
|
|
{
|
|
self::$destroyed[] = $id;
|
|
}
|
|
}
|
|
|
|
class EntriesRepository
|
|
{
|
|
public static array $destroyed = [];
|
|
public static array $deleted = [];
|
|
|
|
public function destroy($id): void
|
|
{
|
|
self::$deleted[] = $id;
|
|
}
|
|
|
|
public function destroyWhere($column, $value): void
|
|
{
|
|
self::$destroyed[] = [$column, $value];
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
namespace Plugi\Extensions\Form\Finishers {
|
|
class FinishersRepository
|
|
{
|
|
public function getEditorConfig(): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
use Plugi\Extensions\Form\DefinitionsRepository;
|
|
use Plugi\Extensions\Form\EntriesRepository;
|
|
|
|
class FormAdminSecurityRedirect extends RuntimeException
|
|
{
|
|
}
|
|
|
|
function assertFormAdminSecurity(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
}
|
|
|
|
function phpb_requires_authenticated(): void
|
|
{
|
|
if (!($GLOBALS['formAdminSecurityAuthenticated'] ?? false)) {
|
|
throw new RuntimeException('The form admin route did not require authentication.');
|
|
}
|
|
}
|
|
|
|
function phpb_validate_csrf(): bool
|
|
{
|
|
return $GLOBALS['formAdminSecurityCsrfValid'] ?? false;
|
|
}
|
|
|
|
function phpb_csrf_field(): void
|
|
{
|
|
echo '<input type="hidden" name="_csrf_token" value="test-token">';
|
|
}
|
|
|
|
function phpb_e(string $value): string
|
|
{
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
function phpb_url(string $module, array $parameters = []): string
|
|
{
|
|
return $module . '?' . http_build_query($parameters);
|
|
}
|
|
|
|
function phpb_redirect(string $url, array $flashData = [], int $statusCode = 302): void
|
|
{
|
|
throw new FormAdminSecurityRedirect();
|
|
}
|
|
|
|
function includeFormSecurityRoute(string $route): void
|
|
{
|
|
try {
|
|
require $route;
|
|
} catch (FormAdminSecurityRedirect) {
|
|
}
|
|
}
|
|
|
|
$projectRoot = dirname(__DIR__, 3);
|
|
$apacheRules = file_get_contents($projectRoot . '/.htaccess');
|
|
assertFormAdminSecurity(
|
|
is_string($apacheRules) && str_contains($apacheRules, 'Require all denied'),
|
|
'Apache must deny requests for hidden files such as the root .env file.'
|
|
);
|
|
|
|
$attack = '</script><script>window.pwned=1</script>';
|
|
DefinitionsRepository::$form = [
|
|
'id' => 7,
|
|
'name' => 'Test form',
|
|
'fields' => json_encode([['name' => $attack]]),
|
|
'finishers' => '[]',
|
|
];
|
|
$_GET = ['id' => '7'];
|
|
ob_start();
|
|
require dirname(__DIR__) . '/admin/Views/EditForm.php';
|
|
$editor = ob_get_clean();
|
|
assertFormAdminSecurity(
|
|
!str_contains($editor, $attack) && str_contains($editor, '\\u003C'),
|
|
'Stored form JSON must be escaped before it is emitted inside a script element.'
|
|
);
|
|
assertFormAdminSecurity(
|
|
str_contains($editor, 'name="_csrf_token"'),
|
|
'Form saves must submit a CSRF token.'
|
|
);
|
|
|
|
$GLOBALS['formAdminSecurityAuthenticated'] = true;
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = false;
|
|
$saveRoute = dirname(__DIR__) . '/routes/saveForm.php';
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$_POST = [];
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($saveRoute);
|
|
assertFormAdminSecurity(http_response_code() === 405, 'Form saves must reject non-POST requests.');
|
|
assertFormAdminSecurity(DefinitionsRepository::$created === [], 'A GET request must not save a form.');
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST = ['name' => 'Rejected form', 'fields' => '[]', 'finishers' => '[]'];
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($saveRoute);
|
|
assertFormAdminSecurity(http_response_code() === 403, 'Form saves must reject invalid CSRF tokens.');
|
|
assertFormAdminSecurity(DefinitionsRepository::$created === [], 'An invalid CSRF token must not save a form.');
|
|
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = true;
|
|
$_POST['_csrf_token'] = 'test-token';
|
|
includeFormSecurityRoute($saveRoute);
|
|
assertFormAdminSecurity(count(DefinitionsRepository::$created) === 1, 'A valid POST should save a form.');
|
|
|
|
$deleteRoute = dirname(__DIR__) . '/routes/deleteForm.php';
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$_GET = ['id' => '7'];
|
|
$_POST = [];
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($deleteRoute);
|
|
assertFormAdminSecurity(http_response_code() === 405, 'Form deletion must reject non-POST requests.');
|
|
assertFormAdminSecurity(DefinitionsRepository::$destroyed === [], 'A GET request must not delete a form.');
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST = ['id' => '7'];
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = false;
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($deleteRoute);
|
|
assertFormAdminSecurity(http_response_code() === 403, 'Form deletion must reject invalid CSRF tokens.');
|
|
assertFormAdminSecurity(DefinitionsRepository::$destroyed === [], 'An invalid CSRF token must not delete a form.');
|
|
|
|
$_POST = ['id' => '7', '_csrf_token' => 'test-token'];
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = true;
|
|
includeFormSecurityRoute($deleteRoute);
|
|
assertFormAdminSecurity(DefinitionsRepository::$destroyed === ['7'], 'A valid POST should delete the form.');
|
|
assertFormAdminSecurity(EntriesRepository::$destroyed === [['formId', '7']], 'Deleting a form should still delete its entries.');
|
|
|
|
$deleteEntryRoute = dirname(__DIR__) . '/routes/deleteEntry.php';
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$_GET = ['id' => '12', 'formId' => '7'];
|
|
$_POST = [];
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($deleteEntryRoute);
|
|
assertFormAdminSecurity(http_response_code() === 405, 'Entry deletion must reject non-POST requests.');
|
|
assertFormAdminSecurity(EntriesRepository::$deleted === [], 'A GET request must not delete an entry.');
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST = ['id' => '12', 'formId' => '7'];
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = false;
|
|
http_response_code(200);
|
|
includeFormSecurityRoute($deleteEntryRoute);
|
|
assertFormAdminSecurity(http_response_code() === 403, 'Entry deletion must reject invalid CSRF tokens.');
|
|
assertFormAdminSecurity(EntriesRepository::$deleted === [], 'An invalid CSRF token must not delete an entry.');
|
|
|
|
$_POST['_csrf_token'] = 'test-token';
|
|
$GLOBALS['formAdminSecurityCsrfValid'] = true;
|
|
includeFormSecurityRoute($deleteEntryRoute);
|
|
assertFormAdminSecurity(EntriesRepository::$deleted === ['12'], 'A valid POST should delete an entry.');
|
|
|
|
fwrite(STDOUT, "PASS: form admin actions require POST and CSRF, and stored JSON is script-safe.\n");
|
|
} |