Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions core/tests/Unit/Manager/ElementListCsrfTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use EvolutionCMS\Traits\Models\ManagerActions;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;

it('renders session tokens on every mutating element list link', function (string $view, string $model, int $expectedLinks) {
$source = file_get_contents(dirname(__DIR__, 4) . '/manager/views/page/resources/elements/' . $view . '.blade.php');
preg_match_all('/(?:href|data-enable-href|data-disable-href)="({{ \$item->makeUrl\(.*?)"/', $source, $matches);

$item = new class {
use ManagerActions;

public bool $exists = true;
public array $managerActionsMap;

public function getKey(): int
{
return 42;
}
};
$item->managerActionsMap = (new ReflectionClass($model))->getDefaultProperties()['managerActionsMap'];
$originalSession = $_SESSION ?? null;
$_SESSION = ['_token' => str_repeat('a', 40)];
$compiler = new BladeCompiler(new Filesystem(), sys_get_temp_dir());
$mutating = 0;

try {
// The module name runs it for managers who can execute but cannot edit modules.
foreach (['actions.edit', 'actions.run'] as $action) {
foreach ($matches[1] as $fragment) {
ob_start();
try {
eval('?>' . $compiler->compileString($fragment));
$url = html_entity_decode(ob_get_contents(), ENT_QUOTES, 'UTF-8');
} finally {
ob_end_clean();
}
parse_str((string) parse_url($url, PHP_URL_QUERY), $query);
expect($query['id'])->toBe('42');

if ($query['a'] === (string) $item->managerActionsMap['id']['actions.edit']) {
expect($query)->not->toHaveKey('_token');
continue;
}

expect($query['_token'] ?? null)->toBe($_SESSION['_token']);
$mutating++;
}
}
expect($mutating)->toBe($expectedLinks);
} finally {
$_SESSION = $originalSession;
}
})->with([
'templates' => ['template', EvolutionCMS\Models\SiteTemplate::class, 4],
'chunks' => ['chunk', EvolutionCMS\Models\SiteHtmlsnippet::class, 8],
'snippets' => ['snippet', EvolutionCMS\Models\SiteSnippet::class, 8],
'plugins' => ['plugin', EvolutionCMS\Models\SitePlugin::class, 8],
'TVs' => ['tv', EvolutionCMS\Models\SiteTmplvar::class, 4],
'modules' => ['module', EvolutionCMS\Models\SiteModule::class, 11],
]);
6 changes: 6 additions & 0 deletions core/tests/Unit/Security/ManagerCsrfMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ function csrfPassed(Request $request): bool
expect(csrfPassed(csrfRequest('GET', ['a' => 27, 'id' => 3, 'action' => 'delete'])))->toBeTrue();
});

it('does not reach an element delete processor without the current session token', function (int $action) {
expect(csrfPassed(csrfRequest('GET', ['a' => $action, 'id' => 3])))->toBeFalse()
->and(csrfPassed(csrfRequest('GET', ['a' => $action, 'id' => 3, '_token' => 'stale'])))->toBeFalse()
->and(csrfPassed(csrfRequest('GET', ['a' => $action, 'id' => 3, '_token' => str_repeat('a', 40)])))->toBeTrue();
})->with([21, 80, 25, 104, 303, 110]);

it('accepts destructive GET actions carrying the session token', function () {
$request = csrfRequest('GET', ['a' => 112, 'id' => 3, '_token' => str_repeat('a', 40)]);

Expand Down
8 changes: 4 additions & 4 deletions manager/views/page/resources/elements/chunk.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
onclick="actionDisableElement(this)"
title="@if($item->disabled) {{ ManagerTheme::getLexicon('enable') }} @else {{ ManagerTheme::getLexicon('disable') }} @endif"
data-disabled="{{ $item->disabled }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0]) }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0, '_token' => csrf_token()]) }}"
data-enable-title="{{ ManagerTheme::getLexicon('enable') }}"
data-enable-icon="{{ $_style['icon_enable'] }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1]) }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1, '_token' => csrf_token()]) }}"
data-disable-title="{{ ManagerTheme::getLexicon('disable') }}"
data-disable-icon="{{ $_style['icon_disable'] }}"
>
Expand All @@ -55,14 +55,14 @@
@endif
@if(evolutionCMS()->hasPermission('new_chunk'))
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
@endif
@if(evolutionCMS()->hasPermission('delete_chunk'))
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_htmlsnippet') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_htmlsnippet') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
12 changes: 6 additions & 6 deletions manager/views/page/resources/elements/module.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@if(empty($action))
<span class="man_el_name">
@else
<a class="man_el_name site_modules" target="main" data-type="site_modules" data-id="{{ $item->id }}" data-catid="{{ $item->category }}" href="{{ $item->makeUrl($action) }}">
<a class="man_el_name site_modules" target="main" data-type="site_modules" data-id="{{ $item->id }}" data-catid="{{ $item->category }}" href="{{ $item->makeUrl($action, false, $action === 'actions.run' ? ['_token' => csrf_token()] : []) }}">
@endif
@if(empty($item->icon))
<i class="{{ $_style['icon_module'] }}"></i>
Expand Down Expand Up @@ -45,7 +45,7 @@
<ul class="elements_buttonbar">
@if(evolutionCMS()->hasPermission('exec_module'))
<li>
<a href="{{ $item->makeUrl('actions.run') }}" target="main" title="{{ ManagerTheme::getLexicon('run_module') }}">
<a href="{{ $item->makeUrl('actions.run', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('run_module') }}">
<i class="{{ $_style['icon_play'] }}"></i>
</a>
</li>
Expand All @@ -61,10 +61,10 @@
onclick="actionDisableElement(this)"
title="@if($item->disabled) {{ ManagerTheme::getLexicon('enable') }} @else {{ ManagerTheme::getLexicon('disable') }} @endif"
data-disabled="{{ $item->disabled }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0]) }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0, '_token' => csrf_token()]) }}"
data-enable-title="{{ ManagerTheme::getLexicon('enable') }}"
data-enable-icon="{{ $_style['icon_enable'] }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1]) }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1, '_token' => csrf_token()]) }}"
data-disable-title="{{ ManagerTheme::getLexicon('disable') }}"
data-disable-icon="{{ $_style['icon_disable'] }}"
>
Expand All @@ -74,14 +74,14 @@
@endif
@if(evolutionCMS()->hasPermission('new_module') && evolutionCMS()->hasPermission('save_module'))
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
@endif
@if(evolutionCMS()->hasPermission('delete_module'))
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_module') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_module') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
8 changes: 4 additions & 4 deletions manager/views/page/resources/elements/plugin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
onclick="actionDisableElement(this)"
title="@if($item->disabled) {{ ManagerTheme::getLexicon('enable') }} @else {{ ManagerTheme::getLexicon('disable') }} @endif"
data-disabled="{{ $item->disabled }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0]) }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0, '_token' => csrf_token()]) }}"
data-enable-title="{{ ManagerTheme::getLexicon('enable') }}"
data-enable-icon="{{ $_style['icon_enable'] }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1]) }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1, '_token' => csrf_token()]) }}"
data-disable-title="{{ ManagerTheme::getLexicon('disable') }}"
data-disable-icon="{{ $_style['icon_disable'] }}"
>
Expand All @@ -55,14 +55,14 @@
@endif
@if(evolutionCMS()->hasPermission('new_plugin'))
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
@endif
@if(evolutionCMS()->hasPermission('delete_plugin'))
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_plugin') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_plugin') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
8 changes: 4 additions & 4 deletions manager/views/page/resources/elements/snippet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
onclick="actionDisableElement(this)"
title="@if($item->disabled) {{ ManagerTheme::getLexicon('enable') }} @else {{ ManagerTheme::getLexicon('disable') }} @endif"
data-disabled="{{ $item->disabled }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0]) }}"
data-enable-href="{{ $item->makeUrl('actions.enable', false, ['disabled' => 0, '_token' => csrf_token()]) }}"
data-enable-title="{{ ManagerTheme::getLexicon('enable') }}"
data-enable-icon="{{ $_style['icon_enable'] }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1]) }}"
data-disable-href="{{ $item->makeUrl('actions.disable', false, ['disabled' => 1, '_token' => csrf_token()]) }}"
data-disable-title="{{ ManagerTheme::getLexicon('disable') }}"
data-disable-icon="{{ $_style['icon_disable'] }}"
>
Expand All @@ -55,14 +55,14 @@
@endif
@if(evolutionCMS()->hasPermission('new_snippet'))
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
@endif
@if(evolutionCMS()->hasPermission('delete_snippet'))
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_snippet') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_snippet') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions manager/views/page/resources/elements/template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
@endif
@if(evolutionCMS()->hasPermission('new_template'))
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
@endif
@if(evolutionCMS()->hasPermission('delete_template'))
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_template') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_template') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions manager/views/page/resources/elements/tv.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
</a>
</li>
<li>
<a href="{{ $item->makeUrl('actions.duplicate') }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<a href="{{ $item->makeUrl('actions.duplicate', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('resource_duplicate') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_duplicate_record') }}')">
<i class="{{ $_style['icon_clone'] }}"></i>
</a>
</li>
<li>
<a href="{{ $item->makeUrl('actions.delete') }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_tmplvars') }}')">
<a href="{{ $item->makeUrl('actions.delete', false, ['_token' => csrf_token()]) }}" target="main" title="{{ ManagerTheme::getLexicon('delete') }}" onclick="return confirm('{{ ManagerTheme::getLexicon('confirm_delete_tmplvars') }}')">
<i class="{{ $_style['icon_trash'] }}"></i>
</a>
</li>
Expand Down
Loading