整合插件配置入口并保留权限与未保存保护
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 14:17:14 +08:00
1 parent 82f740d128
commit 6a17af13ed
13 files changed
+346 -250

No files matched your search

+14 -20
View File
@@ -5,7 +5,7 @@ import { compileFunction } from 'node:vm'
import { computed, reactive, ref } from './vue.mjs'
const source = readFileSync(new URL('../../frontend/src/views/components/ConfigSettings/ConfigNavigation.js', import.meta.url), 'utf8')
const components = ['BaseConfig', 'EmailConfig', 'SslConfig', 'PluginsConfig', 'ConfigMaintenance']
const components = ['BaseConfig', 'EmailConfig', 'SslConfig', 'ConfigMaintenance']
const createNavigation = compileFunction(
source.replace(/^import .*$/gm, '').replace('export function useNavigation', 'function useNavigation') + '\nreturn useNavigation()',
['computed', 'nextTick', 'ref', 'useGlobalStatusStore', ...components]
@@ -18,20 +18,14 @@ function fixture(capabilities = ['config.read']) {
return { page, allowed }
}
test('配置菜单包含三个独立插件三级页面,无权限的维护入口不可打开', async () => {
test('配置管理只保留服务配置与维护入口,不再包含插件菜单', async () => {
const { page, allowed } = fixture()
assert.equal(new Set(page.sectionOptions.value.map(item => item.component)).size, 4)
assert.deepEqual(page.sectionOptions.value.map(item => item.value), ['base', 'mail', 'ssl', 'plugins'])
const children = page.sectionOptions.value.find(item => item.value === 'plugins').children
assert.deepEqual(children.map(item => item.value), ['spam_block', 'telegram_push', 'wechat_push'])
await page.selectSection('plugins')
assert.equal(page.selectedSection.value, 'spam_block')
assert.equal(page.activePage.value.label, '垃圾邮件拦截')
await page.selectSection('telegram_push')
assert.equal(page.activePage.value.label, 'Telegram 推送')
await page.selectSection('plugins')
assert.equal(page.selectedSection.value, 'telegram_push')
await page.selectSection('base')
assert.equal(new Set(page.sectionOptions.value.map(item => item.component)).size, 3)
assert.deepEqual(page.sectionOptions.value.map(item => item.value), ['base', 'mail', 'ssl'])
for (const value of ['plugins', 'spam_block', 'telegram_push', 'wechat_push']) {
await page.selectSection(value)
assert.equal(page.selectedSection.value, 'base')
}
assert.equal(page.maintenanceOptions.value.length, 0)
await page.selectSection('audit')
await page.selectSection('invalid')
@@ -44,15 +38,15 @@ test('配置菜单包含三个独立插件三级页面,无权限的维护入
assert.equal(page.activePage.value, undefined)
})
test('插件三级菜单切换保留离开确认及权限检查', async () => {
test('配置页面切换保留离开确认及权限检查', async () => {
const { page, allowed } = fixture()
await page.selectSection('wechat_push')
await page.selectSection('ssl')
page.currentPage.value = { beforeLeave: async () => false }
await page.selectSection('spam_block')
assert.equal(page.selectedSection.value, 'wechat_push')
await page.selectSection('base')
assert.equal(page.selectedSection.value, 'ssl')
page.currentPage.value = { beforeLeave: async () => { allowed.delete('config.read'); return true } }
await page.selectSection('telegram_push')
assert.equal(page.selectedSection.value, 'wechat_push')
await page.selectSection('mail')
assert.equal(page.selectedSection.value, 'ssl')
assert.equal(page.activePage.value, undefined)
})
+114
View File
@@ -0,0 +1,114 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { readFileSync } from 'node:fs'
import { compileFunction } from 'node:vm'
import { computed, reactive, ref } from './vue.mjs'
const root = new URL('../../frontend/src/views/components/PluginSettings/', import.meta.url)
const source = readFileSync(new URL('PluginSettings.js', root), 'utf8')
const createPage = compileFunction(
source.replace(/^import .*$/gm, '').replace('export function usePage', 'function usePage') + '\nreturn usePage()',
['computed', 'nextTick', 'onMounted', 'onBeforeUnmount', 'ref', 'http', 'useGlobalStatusStore']
)
function fixture(capabilities = ['config.read', 'plugin.read', 'plugin.write_own'], get = async () => ({ errorNo: 0, data: [] })) {
const allowed = reactive(new Set(capabilities))
const requests = []
let mount, dispose
const page = createPage(computed, async () => {}, fn => { mount = fn }, fn => { dispose = fn }, ref,
{ get: async url => { requests.push(url); return get(url) } },
() => ({ hasCapability: value => allowed.has(value) }))
return { page, allowed, requests, mount, dispose }
}
test('独立插件配置包含三个运行配置页,停用插件不依赖运行列表也能配置', async () => {
const { page, mount } = fixture()
assert.deepEqual(page.sectionOptions.value.map(item => item.value), ['spam_block', 'telegram_push', 'wechat_push'])
assert.equal(page.activePage.value.label, '垃圾邮件拦截')
await mount()
for (const key of ['telegram_push', 'wechat_push', 'spam_block']) {
await page.selectSection(key)
assert.equal(page.activePage.value.kind, 'config')
assert.equal(page.activePage.value.value, key)
}
assert.deepEqual(page.accountOptions.value.map(item => item.value), ['telegram-user'])
})
test('普通账号只保留本人通知设置,不读取或进入全局配置和管理员插件列表', async () => {
const { page, mount, requests, allowed } = fixture(['plugin.read', 'plugin.write_own'])
await mount()
assert.equal(requests.length, 0)
assert.deepEqual(page.sectionOptions.value, [])
assert.equal(page.activePage.value.kind, 'account')
await page.selectSection('spam_block')
assert.equal(page.selectedSection.value, 'telegram-user')
allowed.delete('plugin.write_own')
assert.equal(page.activePage.value, undefined)
})
test('只有配置读取权限的账号仍可进入插件配置,但不能读取无权限的插件列表', async () => {
const { page, mount, requests } = fixture(['config.read'])
await mount()
assert.equal(requests.length, 0)
assert.equal(page.sectionOptions.value.length, 3)
assert.equal(page.accountOptions.value.length, 0)
})
test('插件切换等待离开确认,取消、并发点击及权限收回不会丢弃当前页', async () => {
const { page, allowed } = fixture()
let resolve
let prompts = 0
page.currentPage.value = { beforeLeave: () => { prompts++; return new Promise(done => { resolve = done }) } }
const attempt = page.selectSection('telegram_push')
await page.selectSection('wechat_push')
assert.equal(prompts, 1)
resolve(false)
await attempt
assert.equal(page.selectedSection.value, 'spam_block')
const second = page.selectSection('wechat_push')
resolve(true)
await second
assert.equal(page.selectedSection.value, 'wechat_push')
page.currentPage.value = { beforeLeave: async () => { allowed.delete('config.read'); return true } }
await page.selectSection('telegram_push')
assert.equal(page.selectedSection.value, 'wechat_push')
assert.equal(page.activePage.value, undefined)
})
test('未知插件只使用安全名称生成隔离页面,内置插件别名不得重复出现', async () => {
const { page, mount, requests } = fixture(undefined, async () => ({ errorNo: 0, data: [
'spam_block', 'telegram_push', 'wechat_push', 'SpamBlock', 'TelegramPush', 'WechatPush',
'WeChatPushHook', 'ExtraPlugin', 'ExtraPlugin', '../bad', '<script>', null
] }))
await mount()
assert.deepEqual(requests, ['/api/v1/plugin/list'])
assert.deepEqual(page.externalOptions.value.map(item => item.value), ['external:ExtraPlugin'])
await page.selectSection('external:ExtraPlugin')
assert.equal(page.activePage.value.src, '/api/v1/plugin/settings/ExtraPlugin/index.html')
const template = readFileSync(new URL('PluginSettings.vue', root), 'utf8')
assert.match(template, /sandbox="allow-scripts"/)
assert.doesNotMatch(template, /allow-same-origin|v-for=".*pluginEntries/)
assert.match(template, /:key="selectedSection"[\s\S]*:page-key="selectedSection"/)
assert.match(template, /defineExpose\(\{ beforeLeave \}\)/)
})
test('插件列表故障不隐藏三个运行配置页,支持重试并丢弃卸载后的响应', async () => {
let fail = true
const { page, mount } = fixture(undefined, async () => {
if (fail) throw new Error('读取失败')
return { errorNo: 0, data: [] }
})
await mount()
assert.equal(page.errorMessage.value, '读取失败')
assert.equal(page.sectionOptions.value.length, 3)
fail = false
await page.load()
assert.equal(page.errorMessage.value, '')
let resolve
const late = fixture(undefined, () => new Promise(done => { resolve = done }))
const loading = late.mount()
late.dispose()
resolve({ errorNo: 0, data: ['ExtraPlugin'] })
await loading
assert.deepEqual(late.page.externalOptions.value, [])
})
+9 -6
View File
@@ -49,17 +49,21 @@ test('一级菜单保留能力过滤和当前分类标题联动', () => {
hasCapability: value => capabilities.has(value)
})
const page = createPage(computed, ref, () => {}, () => {}, { innerWidth: 1440 }, () => store,
{ security: '安全' }, () => ({}), ...names.map(name => ({ name })))
{ security: '安全', plugin_settings: '插件配置' }, () => ({}), ...names.map(name => ({ name })))
assert.deepEqual(page.settingsMenu.value.map(item => item.key), ['security', 'third-party', 'group', 'rule'])
assert.equal(page.settingsMenu.value.find(item => item.key === 'third-party').label, '邮件客户端')
assert.equal(page.activeSettings.value.label, '安全')
capabilities.add('config.read')
store.settingsActiveTab = 'config'
assert.equal(page.activeSettings.value.label, '配置管理')
assert.equal(page.settingsMenu.value.length, 5)
assert.equal(page.settingsMenu.value.length, 6)
assert.equal(page.settingsMenu.value.find(item => item.key === 'plugins').label, '插件配置')
assert.ok(page.settingsMenu.value.every(item => item.icon && item.hint))
capabilities.delete('config.read')
assert.equal(page.activeSettings.value, undefined)
assert.equal(page.settingsMenu.value.some(item => item.key === 'plugins'), false)
capabilities.add('plugin.read')
assert.equal(page.settingsMenu.value.some(item => item.key === 'plugins'), true)
})
test('三级菜单只发送当前分组的可见子项,选中状态仍由父页面确认', () => {
@@ -79,7 +83,7 @@ test('三级菜单只发送当前分组的可见子项,选中状态仍由父
})
test('菜单模板可编译并保留按钮、当前项语义及原有选区处理器', () => {
for (const name of ['HomeHeader', 'SettingsSubmenu', 'SecuritySettings', 'ConfigSettings']) {
for (const name of ['HomeHeader', 'SettingsSubmenu', 'SecuritySettings', 'ConfigSettings', 'PluginSettings']) {
const source = read(`${name}/${name}.vue`)
const { descriptor, errors } = parse(source)
assert.deepEqual(errors, [])
@@ -88,7 +92,7 @@ test('菜单模板可编译并保留按钮、当前项语义及原有选区处
const submenu = read('SettingsSubmenu/SettingsSubmenu.vue')
assert.match(submenu, /<button[\s\S]*type="button"/)
assert.match(submenu, /:aria-current="modelValue === item.value \? 'page' : undefined"/)
for (const name of ['SecuritySettings', 'ConfigSettings']) {
for (const name of ['SecuritySettings', 'ConfigSettings', 'PluginSettings']) {
assert.match(read(`${name}/${name}.vue`), /<SettingsSubmenu[\s\S]*@select="selectSection"/)
}
assert.match(read('HomeHeader/HomeHeader.vue'), /globalStatus.settingsDrawerVisible && globalStatus.settingsActiveTab === item.key/)
@@ -108,8 +112,7 @@ test('一级标签样式不会穿透到插件内部,抽屉样式可命中传
test('两级菜单分别提供选中、非选中悬停和键盘焦点样式', () => {
for (const path of [
'HomeHeader/HomeHeader.css',
'SettingsSubmenu/SettingsSubmenu.css',
'PluginSettings/PluginSettings.css'
'SettingsSubmenu/SettingsSubmenu.css'
]) {
const source = read(path)
assert.match(source, /:hover:not\(/)
+31 -2
View File
@@ -8,7 +8,7 @@ import { computed, reactive, ref } from './vue.mjs'
const root = new URL('../../frontend/src/views/components/PluginSettings/', import.meta.url)
const source = readFileSync(new URL('TelegramSettings/TelegramSettings.js', root), 'utf8')
const create = compileFunction(source.replace(/^import .*$/gm, '').replace('export function ', 'function ') + '\nreturn useTelegramSettings()',
['computed', 'onMounted', 'onBeforeUnmount', 'reactive', 'ref', 'ElMessage', 'http'])
['computed', 'onMounted', 'onBeforeUnmount', 'reactive', 'ref', 'ElMessage', 'http', 'ElMessageBox'])
test('Telegram 原生设置保留版本、限制用户字段,不依赖插件 iframe', async () => {
const requests = []
@@ -66,7 +66,7 @@ test('Telegram 模板独立编译,其他插件继续保持 sandbox 隔离', ()
const require = createRequire(new URL('../../frontend/package.json', import.meta.url))
const { parse, compileScript, compileStyle } = require('@vue/compiler-sfc')
const parent = readFileSync(new URL('PluginSettings.vue', root), 'utf8')
assert.match(parent, /TelegramSettings v-else-if="entry.logicalID === 'telegram_push'"/)
assert.match(parent, /TelegramSettings v-else-if="activePage\?\.kind === 'account'"/)
assert.match(parent, /sandbox="allow-scripts"/)
assert.doesNotMatch(parent, /allow-same-origin/)
const { descriptor, errors } = parse(readFileSync(new URL('TelegramSettings/TelegramSettings.vue', root), 'utf8'))
@@ -75,3 +75,32 @@ test('Telegram 模板独立编译,其他插件继续保持 sandbox 隔离', ()
assert.deepEqual(compileStyle({ source: readFileSync(new URL('TelegramSettings/TelegramSettings.css', root), 'utf8'), id: 'telegram-settings', scoped: true }).errors, [])
assert.doesNotMatch(source, /window\.parent|parent\.document|postMessage|localStorage/)
})
test('Telegram 通知编辑在切页和重新加载前确认,取消保留输入,保存后解除保护', async () => {
let cancel = true
let reads = 0
let prompts = 0
const page = create(computed, () => {}, () => {}, reactive, ref, { success() {} }, {
get: async url => {
reads++
return { errorNo: 0, data: url.endsWith('/bot') ? {} : { version: 1, settings: { chatId: '123' } } }
},
put: async (url, body) => ({ errorNo: 0, data: { version: 2, settings: body.settings } })
}, { confirm: async () => { prompts++; if (cancel) throw new Error('取消') } })
await page.load()
assert.equal(await page.beforeLeave(), true)
page.settings.chatId = '456'
assert.equal(await page.beforeLeave(), false)
await page.refresh()
assert.equal(reads, 2)
assert.equal(page.settings.chatId, '456')
cancel = false
assert.equal(await page.beforeLeave(), true)
await page.submit('save')
assert.equal(page.hasChanges.value, false)
const count = prompts
assert.equal(await page.beforeLeave(), true)
assert.equal(prompts, count)
page.working.value = 'save'
assert.equal(await page.beforeLeave(), false)
})