fix: scope csrf fields and block past dates
This commit is contained in:
@@ -2,6 +2,12 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once PLUGIN_ROOT . '/exec/lib/CsrfGuard.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/Settings.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/Lang.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/DirectAdminUser.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/RuleRepository.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/AuditLog.php';
|
||||
require_once PLUGIN_ROOT . '/exec/lib/AppContext.php';
|
||||
|
||||
test('csrf token is bound to user session action and time', function (): void {
|
||||
$guard = new CsrfGuard('secret', 'alice', 'session-1');
|
||||
@@ -13,3 +19,21 @@ test('csrf token is bound to user session action and time', function (): void {
|
||||
assert_false($guard->validate('create', (string)($issued['ts'] - 7200), $issued['token'], 3600, 'session-1'));
|
||||
assert_false($guard->validate('create', 'bad', $issued['token'], 3600, 'session-1'));
|
||||
});
|
||||
|
||||
test('csrf form fields use plugin scoped names to avoid directadmin token collisions', function (): void {
|
||||
$settings = Settings::load(TEST_TMP . '/missing-csrf-form.conf');
|
||||
$ctx = new AppContext(
|
||||
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
|
||||
$settings,
|
||||
new RuleRepository(TEST_TMP . '/data'),
|
||||
new CsrfGuard('secret', 'alice', 'sid'),
|
||||
Lang::load('pl', $settings),
|
||||
new AuditLog(TEST_TMP . '/audit.log')
|
||||
);
|
||||
|
||||
$html = $ctx->csrfField('create');
|
||||
assert_contains('name="gar_csrf_ts"', $html);
|
||||
assert_contains('name="gar_csrf_sid"', $html);
|
||||
assert_contains('name="gar_csrf_token"', $html);
|
||||
assert_false(str_contains($html, 'name="csrf_token"'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user