From 6b0a92c3bf70405c368079b5e96032f83afaeeaa Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 7 Nov 2026 14:30:46 +0800 Subject: [PATCH] test(i18n): enforce page editor isolation --- web/tests/UserCopyPageEditorIsolationTest.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 web/tests/UserCopyPageEditorIsolationTest.php diff --git a/web/tests/UserCopyPageEditorIsolationTest.php b/web/tests/UserCopyPageEditorIsolationTest.php new file mode 100644 index 00000000..2bee242d --- /dev/null +++ b/web/tests/UserCopyPageEditorIsolationTest.php @@ -0,0 +1,110 @@ +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";