From bf53cacf75ae357af237fb4d3fe3176b235cb91f Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 1 Sep 2026 10:58:09 +0800 Subject: [PATCH] fix(frontend): resolve latest value for high-risk settings Reused setting-row controls can close over a stale item after save. Look up the current row by key for auth.registration_mode as well so cancel and persist use the last saved value, not the first-loaded one. --- frontend/src/views/system/SystemSettings.vue | 24 ++++------- .../views/system/systemSettingsEdit.test.ts | 41 +++++++++++++++++++ .../src/views/system/systemSettingsEdit.ts | 23 +++++++++++ 3 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 frontend/src/views/system/systemSettingsEdit.test.ts create mode 100644 frontend/src/views/system/systemSettingsEdit.ts diff --git a/frontend/src/views/system/SystemSettings.vue b/frontend/src/views/system/SystemSettings.vue index df2c5741..ad56acdc 100644 --- a/frontend/src/views/system/SystemSettings.vue +++ b/frontend/src/views/system/SystemSettings.vue @@ -496,6 +496,7 @@ import { type SystemSettingItem, } from '@/api/system' import { useAuthStore } from '@/stores/auth' +import { isSettingValueDirty, resolveCurrentSetting } from './systemSettingsEdit' const authStore = useAuthStore() const currentUserId = computed(() => authStore.currentUserId) @@ -866,16 +867,7 @@ async function snapSsrfWhitelistToSaved(item: SystemSettingItem) { } function isDirty(item: SystemSettingItem): boolean { - const cur = editValues[item.key] - const orig = item.value - if (Array.isArray(cur) && Array.isArray(orig)) { - if (cur.length !== orig.length) return true - for (let i = 0; i < cur.length; i++) { - if (cur[i] !== orig[i]) return true - } - return false - } - return cur !== orig + return isSettingValueDirty(editValues[item.key], item.value) } function formatDate(isoString: string): string { @@ -929,7 +921,7 @@ async function loadSettings() { // onChange persists non-SSRF settings. SSRF whitelist and system admins // have dedicated handlers with inline popconfirm. async function onChange(item: SystemSettingItem) { - const currentItem = settingsByKey.value.get(item.key) + const currentItem = resolveCurrentSetting(settingsByKey.value, item.key) if (!currentItem) return if (!isDirty(currentItem)) return @@ -943,15 +935,17 @@ async function onChange(item: SystemSettingItem) { } async function onHighRiskSelectChange(item: SystemSettingItem) { + const currentItem = resolveCurrentSetting(settingsByKey.value, item.key) + if (!currentItem) return const newValue = editValues[item.key] - if (newValue === item.value) return + if (!isDirty(currentItem)) return // Revert the select immediately so cancel leaves the saved value // visible; re-apply only after the inline popconfirm is confirmed. - editValues[item.key] = item.value + editValues[item.key] = currentItem.value const ok = await highRiskPopconfirm.ask({ - content: highRiskConfirmBody(item, newValue), + content: highRiskConfirmBody(currentItem, newValue), theme: 'danger', confirmBtn: { content: t('system.globalSettings.confirm.confirmBtn'), @@ -961,7 +955,7 @@ async function onHighRiskSelectChange(item: SystemSettingItem) { if (!ok) return editValues[item.key] = newValue - await persistSetting(item) + await persistSetting(currentItem) } function confirmSsrfListEntryChange( diff --git a/frontend/src/views/system/systemSettingsEdit.test.ts b/frontend/src/views/system/systemSettingsEdit.test.ts new file mode 100644 index 00000000..daaa9a27 --- /dev/null +++ b/frontend/src/views/system/systemSettingsEdit.test.ts @@ -0,0 +1,41 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { isSettingValueDirty, resolveCurrentSetting } from './systemSettingsEdit.ts' + +test('treats scalar edits as dirty', () => { + assert.equal(isSettingValueDirty(true, false), true) + assert.equal(isSettingValueDirty(false, false), false) + assert.equal(isSettingValueDirty('invite', 'open'), true) +}) + +test('treats list edits as dirty by value, not by reference', () => { + assert.equal(isSettingValueDirty(['a'], ['a']), false) + assert.equal(isSettingValueDirty(['a', 'b'], ['a']), true) + assert.equal(isSettingValueDirty(['a'], ['b']), true) +}) + +test('resolves the latest setting so a reused row does not keep the pre-save value', () => { + const stale = { key: 'tenant.auto_create_api_key', value: false } + const current = { key: 'tenant.auto_create_api_key', value: true } + const settingsByKey = new Map([[current.key, current]]) + + const resolved = resolveCurrentSetting(settingsByKey, stale.key) + assert.equal(resolved, current) + + // User toggled back to the original loaded value. The stale object looks + // clean; the canonical post-save object is dirty and must PUT. + const editValue = false + assert.equal(isSettingValueDirty(editValue, stale.value), false) + assert.equal(isSettingValueDirty(editValue, resolved?.value), true) +}) + +test('high-risk cancel restores the latest saved value, not the first-loaded object', () => { + const stale = { key: 'auth.registration_mode', value: 'open' } + const current = { key: 'auth.registration_mode', value: 'invite' } + const settingsByKey = new Map([[current.key, current]]) + + const resolved = resolveCurrentSetting(settingsByKey, stale.key) + assert.equal(resolved?.value, 'invite') + assert.notEqual(stale.value, resolved?.value) +}) diff --git a/frontend/src/views/system/systemSettingsEdit.ts b/frontend/src/views/system/systemSettingsEdit.ts new file mode 100644 index 00000000..9eba4158 --- /dev/null +++ b/frontend/src/views/system/systemSettingsEdit.ts @@ -0,0 +1,23 @@ +// Setting rows keep a stable Vue key (`item.key`) while persistSetting +// replaces the object in `settings`. Reused TDesign controls can therefore +// fire @change/@blur with a stale item whose `.value` is the pre-save +// snapshot. Event handlers must resolve by key from the latest map before +// dirty-checking or reverting the control. + +export function resolveCurrentSetting( + settingsByKey: ReadonlyMap, + key: string, +): T | undefined { + return settingsByKey.get(key) +} + +export function isSettingValueDirty(current: unknown, original: unknown): boolean { + if (Array.isArray(current) && Array.isArray(original)) { + if (current.length !== original.length) return true + for (let i = 0; i < current.length; i++) { + if (current[i] !== original[i]) return true + } + return false + } + return current !== original +}