test(i18n): enforce page editor isolation
parent
7546b4945d
commit
6b0a92c3bf
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\UserCopy\UserCopyManifest;
|
||||
use App\Services\UserCopy\UserCopyRuntimeReader;
|
||||
use App\Services\UserCopy\UserCopySourceCatalog;
|
||||
use App\Services\UserCopy\UserCopyViewRuntime;
|
||||
|
||||
$viewRoot = dirname(__DIR__) . '/resources/views/venix';
|
||||
$temporary = sys_get_temp_dir() . '/virt-user-copy-editor-isolation-'
|
||||
. bin2hex(random_bytes(8));
|
||||
if (!mkdir($temporary, 0700, true)) {
|
||||
throw new RuntimeException('Cannot create editor isolation directory.');
|
||||
}
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$remove = static function (string $directory): void {
|
||||
if (!is_dir($directory)) return;
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator(
|
||||
$directory,
|
||||
FilesystemIterator::SKIP_DOTS
|
||||
),
|
||||
RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
foreach ($iterator as $item) {
|
||||
$item->isDir()
|
||||
? rmdir($item->getPathname())
|
||||
: unlink($item->getPathname());
|
||||
}
|
||||
rmdir($directory);
|
||||
};
|
||||
|
||||
$runtime = new UserCopyViewRuntime(new UserCopyRuntimeReader(
|
||||
$temporary . '/missing-publications',
|
||||
new UserCopySourceCatalog(UserCopyManifest::load())
|
||||
));
|
||||
$render = static function (
|
||||
bool $canManage,
|
||||
string $csrfToken,
|
||||
?string $page
|
||||
) use ($runtime, $viewRoot, $temporary): string {
|
||||
$smarty = new Smarty();
|
||||
$smarty->setTemplateDir($viewRoot);
|
||||
$smarty->setCompileDir($temporary);
|
||||
$smarty->setCacheDir($temporary);
|
||||
$smarty->assign([
|
||||
'admin_can_manage' => $canManage,
|
||||
'admin_csrf_token' => $csrfToken,
|
||||
'venix_asset_version' => 'isolation-test',
|
||||
]);
|
||||
$runtime->bind(
|
||||
$smarty,
|
||||
'zh-CN',
|
||||
'/user/cloud/instances',
|
||||
$page
|
||||
);
|
||||
return $smarty->fetch(
|
||||
'string:{include file="user/user_copy_editor_head.tpl"}'
|
||||
. '{include file="user/user_copy_bootstrap.tpl"}'
|
||||
);
|
||||
};
|
||||
|
||||
try {
|
||||
$ordinary = $render(false, '', 'cloud.instances');
|
||||
$assert(substr_count($ordinary, 'id="user-copy-bootstrap"') === 1
|
||||
&& substr_count($ordinary, '/cloud/user-copy-runtime.js') === 1,
|
||||
'Ordinary runtime copy assets were not rendered once.');
|
||||
foreach ([
|
||||
'user-copy-editor-bootstrap',
|
||||
'admin-request-runtime.js',
|
||||
'admin-user-copy-page-editor.js',
|
||||
'admin-user-copy-page-editor.css',
|
||||
] as $privateAsset) {
|
||||
$assert(!str_contains($ordinary, $privateAsset),
|
||||
'Ordinary users received an editor asset: ' . $privateAsset);
|
||||
}
|
||||
|
||||
$token = str_repeat('d', 64);
|
||||
$author = $render(true, $token, 'cloud.instances');
|
||||
foreach ([
|
||||
'id="user-copy-editor-bootstrap"',
|
||||
'/cloud/admin-request-runtime.js?v=isolation-test',
|
||||
'/cloud/admin-user-copy-page-editor.js?v=isolation-test',
|
||||
'/cloud/admin-user-copy-page-editor.css?v=isolation-test',
|
||||
] as $authorAsset) {
|
||||
$assert(substr_count($author, $authorAsset) === 1,
|
||||
'Authorized editor asset was not rendered once: ' . $authorAsset);
|
||||
}
|
||||
$assert(substr_count($author, $token) === 1
|
||||
&& str_contains($author, '"page":"cloud.instances"')
|
||||
&& str_contains($author, '"runtime_locale":"zh-CN"'),
|
||||
'The editor bootstrap leaked or omitted its minimal request contract.');
|
||||
|
||||
$unmapped = $render(true, str_repeat('e', 64), null);
|
||||
$assert(!str_contains($unmapped, 'user-copy-editor-bootstrap')
|
||||
&& !str_contains($unmapped, 'admin-user-copy-page-editor')
|
||||
&& !str_contains($unmapped, 'admin-request-runtime.js'),
|
||||
'An unmapped request received page-editor assets.');
|
||||
} finally {
|
||||
$remove($temporary);
|
||||
}
|
||||
|
||||
echo "User-copy page editor assets stay isolated to mapped superadmin views.\n";
|
||||
Loading…
Reference in New Issue