修复跨源请求与长操作超时并加固DNS验证和错误脱敏

This commit is contained in:
xuans committed 2026-09-15 21:11:46 +08:00
1 parent b0a846df0f
commit 00ea12cbd9
14 files changed
+673 -34

No files matched your search

+47 -9
View File
@@ -37,6 +37,9 @@ export function useDashboard() {
const settingsError = ref("");
const dnsTesting = ref(false);
const dnsTestError = ref("");
const verificationClock = ref(Date.now());
let verificationTimer;
let dnsRequestVersion = 0;
const settingsForm = ref(createEmptySettingsForm());
const showCertificateImport = ref(false);
const importDomain = ref(null);
@@ -194,14 +197,25 @@ export function useDashboard() {
return (
current.provider !== saved?.provider ||
!saved?.credential_configured ||
Boolean(current.test_domain.trim()) ||
Object.values(current.credentials || {}).some((value) => String(value || "").trim()) ||
Boolean(current.verification_token) ||
current.verification_state === "pending"
);
});
const dnsReadyToSave = computed(
() =>
!dnsVerificationRequired.value ||
(Boolean(settingsForm.value.dns.credential_ref) &&
Boolean(settingsForm.value.dns.verification_token)),
const dnsReadyToSave = computed(() => {
verificationClock.value;
if (dnsTesting.value) return false;
if (!dnsVerificationRequired.value) return true;
const dns = settingsForm.value.dns;
return Boolean(dns.credential_ref && dns.verification_token) &&
new Date(dns.expires_at).getTime() > Date.now();
});
watch(
() => [settingsForm.value.dns.provider, settingsForm.value.dns.test_domain, settingsForm.value.dns.credentials],
handleDNSProviderChange,
{ deep: true, flush: "sync" },
);
const defaultCertificatePath = "/ssl/{域名}/{证书类型}/cert.pem";
@@ -332,6 +346,7 @@ export function useDashboard() {
credentials: {},
credential_ref: "",
verification_token: "",
expires_at: "",
test_report: null,
},
notification: {
@@ -379,6 +394,7 @@ export function useDashboard() {
credentials: {},
credential_ref: "",
verification_token: "",
expires_at: "",
test_domain: "",
test_report: null,
},
@@ -696,13 +712,18 @@ export function useDashboard() {
}
function handleDNSProviderChange() {
dnsRequestVersion += 1;
dnsTesting.value = false;
const dns = settingsForm.value.dns;
const saved = settings.value?.dns;
dnsTestError.value = "";
dns.credential_ref = "";
dns.verification_token = "";
dns.expires_at = "";
dns.test_report = null;
if (dns.provider && dns.provider === saved?.provider && saved.credential_configured) {
const hasNewInput = Boolean(dns.test_domain.trim()) ||
Object.values(dns.credentials || {}).some((value) => String(value || "").trim());
if (!hasNewInput && dns.provider && dns.provider === saved?.provider && saved.credential_configured) {
dns.verification_state = saved.verification_state || "verified";
return;
}
@@ -715,6 +736,7 @@ export function useDashboard() {
}
async function testDNS() {
handleDNSProviderChange();
const dns = settingsForm.value.dns;
const provider = selectedDNSProvider.value;
dnsTestError.value = "";
@@ -740,6 +762,8 @@ export function useDashboard() {
return;
}
dnsTesting.value = true;
dns.verification_state = "pending";
const requestVersion = dnsRequestVersion;
try {
const response = await fetchAdmin("/api/v1/dns/test", {
method: "POST",
@@ -751,22 +775,25 @@ export function useDashboard() {
}),
});
const result = await response.json().catch(() => ({}));
if (requestVersion !== dnsRequestVersion) return;
dns.test_report = result.test_report || null;
if (!response.ok) {
throw new Error(result.error || "DNS API 在线测试失败");
}
dns.credential_ref = result.credential_ref || "";
dns.verification_token = result.verification_token || "";
dns.expires_at = result.expires_at || "";
dns.verification_state = "verified";
dns.credential_configured = true;
notice.value = "DNS API 在线测试通过,可以保存系统设置";
} catch (err) {
if (requestVersion !== dnsRequestVersion) return;
dns.credential_ref = "";
dns.verification_token = "";
dns.verification_state = "pending";
dnsTestError.value = err.message || "DNS API 在线测试失败";
} finally {
dnsTesting.value = false;
if (requestVersion === dnsRequestVersion) dnsTesting.value = false;
}
}
@@ -778,11 +805,15 @@ export function useDashboard() {
}
function closeSettings() {
if (!settingsSaving.value) showSettings.value = false;
if (!settingsSaving.value) {
handleDNSProviderChange();
showSettings.value = false;
}
}
async function saveSettings() {
settingsError.value = "";
verificationClock.value = Date.now();
if (!dnsReadyToSave.value) {
settingsError.value = "DNS 服务商配置尚未通过在线测试,请先完成测试";
return;
@@ -1061,8 +1092,15 @@ export function useDashboard() {
}
}
onMounted(loadData);
onMounted(() => {
loadData();
verificationTimer = window.setInterval(() => {
verificationClock.value = Date.now();
}, 1000);
});
onBeforeUnmount(() => {
dnsRequestVersion += 1;
if (verificationTimer) window.clearInterval(verificationTimer);
document.body.style.overflow = previousBodyOverflow;
});
@@ -0,0 +1,83 @@
import assert from "node:assert/strict";
import test from "node:test";
import { nextTick } from "vue";
import { useDashboard } from "./Dashboard.js";
import { aliyunProvider, deferred, mountComposable, verifiedResponse } from "../../test-support/mount.js";
async function setup(t, request) {
const { state } = await mountComposable(t, useDashboard, async (path) => {
if (path === "/api/v1/dns/test") return request();
if (path.endsWith("/dns/providers")) return Response.json({ items: [aliyunProvider] });
if (path.endsWith("/settings")) return Response.json({ dns: { provider: "aliyun", credential_configured: true, verification_state: "verified" } });
return Response.json({ items: [] });
});
state.openSettings();
await nextTick();
return state;
}
async function fill(state) {
state.settingsForm.value.dns.credentials = { access_key_id: "fake-key" };
state.settingsForm.value.dns.test_domain = "example.test";
await nextTick();
}
test("已保存凭据不变可保存,输入新凭据必须重新验证", async (t) => {
const state = await setup(t, () => verifiedResponse());
assert.equal(state.dnsReadyToSave.value, true);
await fill(state);
assert.equal(state.dnsReadyToSave.value, false);
await state.testDNS();
assert.equal(state.dnsReadyToSave.value, true);
state.settingsForm.value.dns.credentials.access_key_id = "changed-key";
assert.equal(state.dnsReadyToSave.value, false);
});
test("管理设置不接受旧输入的在途 DNS 响应", async (t) => {
const pending = deferred();
const state = await setup(t, () => pending.promise);
await fill(state);
const task = state.testDNS();
state.settingsForm.value.dns.test_domain = "other.test";
await nextTick();
pending.resolve(verifiedResponse());
await task;
assert.equal(state.dnsReadyToSave.value, false);
assert.equal(state.settingsForm.value.dns.verification_token, "");
});
test("管理设置拒绝过期验证令牌", async (t) => {
const state = await setup(t, () => Response.json({ credential_ref: "secret://test/only", verification_token: "expired", expires_at: "2000-01-01T00:00:00Z" }));
await fill(state);
await state.testDNS();
assert.equal(state.dnsReadyToSave.value, false);
});
test("关闭设置后不显示旧 DNS 请求的成功提示", async (t) => {
const pending = deferred();
const state = await setup(t, () => pending.promise);
await fill(state);
const task = state.testDNS();
state.closeSettings();
pending.resolve(verifiedResponse());
await task;
assert.equal(state.settingsForm.value.dns.verification_token, "");
assert.equal(state.notice.value, "");
});
test("管理设置新请求成功后旧失败不能删除令牌", async (t) => {
const first = deferred();
const second = deferred();
let count = 0;
const state = await setup(t, () => (++count === 1 ? first.promise : second.promise));
await fill(state);
const oldTask = state.testDNS();
const newTask = state.testDNS();
second.resolve(verifiedResponse("new-token"));
await newTask;
first.reject(new Error("旧请求失败"));
await oldTask;
assert.equal(state.settingsForm.value.dns.verification_token, "new-token");
assert.equal(state.dnsTestError.value, "");
assert.equal(state.dnsReadyToSave.value, true);
});
+11 -3
View File
@@ -41,6 +41,7 @@ export function useSetupWizard(props, emit) {
const emailAutofillElement = ref(null);
let emailVisibleEdited = false;
let verificationTimer;
let dnsRequestVersion = 0;
let emailAutofillTimer;
const targetType = ref(props.status.web_public_ip ? "ip" : "hostname");
const form = ref({
@@ -141,6 +142,8 @@ export function useSetupWizard(props, emit) {
});
function invalidateDNSVerification() {
dnsRequestVersion += 1;
dnsTesting.value = false;
dnsVerification.value = null;
dnsTestError.value = "";
}
@@ -415,6 +418,7 @@ export function useSetupWizard(props, emit) {
}
dnsTesting.value = true;
const requestVersion = dnsRequestVersion;
try {
const response = await fetch("/api/v1/setup/dns/test", {
method: "POST",
@@ -426,15 +430,17 @@ export function useSetupWizard(props, emit) {
}),
});
const result = await response.json().catch(() => ({}));
if (requestVersion !== dnsRequestVersion) return;
if (result.test_report) {
dnsVerification.value = { test_report: result.test_report };
}
if (!response.ok) throw new Error(result.error || "DNS API 在线验证失败");
dnsVerification.value = result;
} catch (err) {
if (requestVersion !== dnsRequestVersion) return;
dnsTestError.value = err.message || "DNS API 在线验证失败";
} finally {
dnsTesting.value = false;
if (requestVersion === dnsRequestVersion) dnsTesting.value = false;
}
}
@@ -773,10 +779,11 @@ export function useSetupWizard(props, emit) {
dnsCredentials.value = {};
invalidateDNSVerification();
},
{ flush: "sync" },
);
watch(dnsCredentials, invalidateDNSVerification, { deep: true });
watch(dnsTestDomain, invalidateDNSVerification);
watch(dnsCredentials, invalidateDNSVerification, { deep: true, flush: "sync" });
watch(dnsTestDomain, invalidateDNSVerification, { flush: "sync" });
onMounted(() => {
loadDNSProviders();
@@ -789,6 +796,7 @@ export function useSetupWizard(props, emit) {
});
onUnmounted(() => {
invalidateDNSVerification();
if (verificationTimer) window.clearInterval(verificationTimer);
if (emailAutofillTimer) window.clearInterval(emailAutofillTimer);
});
@@ -0,0 +1,77 @@
import assert from "node:assert/strict";
import test from "node:test";
import { nextTick } from "vue";
import { useSetupWizard } from "./SetupWizard.js";
import { aliyunProvider, deferred, mountComposable, verifiedResponse } from "../../test-support/mount.js";
async function setup(t, request) {
const { state, unmount } = await mountComposable(t, () => useSetupWizard({ status: { dns_provider: "aliyun" } }, () => {}),
async (path) => path.endsWith("/providers") ? Response.json({ items: [aliyunProvider] }) : request());
state.dnsCredentials.value = { access_key_id: "fake-key" };
state.dnsTestDomain.value = "example.test";
await nextTick();
return { state, unmount };
}
test("初始化不接受输入修改前的在途 DNS 响应", async (t) => {
const pending = deferred();
const { state } = await setup(t, () => pending.promise);
const task = state.testDNS();
state.dnsCredentials.value.access_key_id = "changed-key";
await nextTick();
pending.resolve(verifiedResponse());
await task;
assert.equal(state.dnsVerificationValid.value, false);
assert.equal(state.dnsVerification.value, null);
});
test("初始化仅接受最新请求,旧失败不能覆盖新成功", async (t) => {
const first = deferred();
const second = deferred();
let count = 0;
const { state } = await setup(t, () => (++count === 1 ? first.promise : second.promise));
const oldTask = state.testDNS();
const newTask = state.testDNS();
second.resolve(verifiedResponse("new-token"));
await newTask;
first.reject(new Error("旧请求失败"));
await oldTask;
assert.equal(state.dnsVerification.value.verification_token, "new-token");
assert.equal(state.dnsTestError.value, "");
});
test("初始化当前 DNS 成功有效,修改域名立即失效", async (t) => {
const { state } = await setup(t, () => verifiedResponse());
await state.testDNS();
assert.equal(state.dnsVerificationValid.value, true);
state.dnsTestDomain.value = "other.test";
assert.equal(state.dnsVerificationValid.value, false);
});
test("初始化卸载后不恢复旧 DNS 验证结果", async (t) => {
const pending = deferred();
const { state, unmount } = await setup(t, () => pending.promise);
const task = state.testDNS();
unmount();
pending.resolve(verifiedResponse());
await task;
assert.equal(state.dnsVerification.value, null);
});
test("初始化旧请求结束不清除新请求的加载状态", async (t) => {
const first = deferred();
const second = deferred();
let count = 0;
const { state } = await setup(t, () => (++count === 1 ? first.promise : second.promise));
const oldTask = state.testDNS();
state.dnsTestDomain.value = "other.test";
const newTask = state.testDNS();
first.resolve(verifiedResponse());
await oldTask;
assert.equal(state.dnsTesting.value, true);
assert.equal(state.dnsVerification.value, null);
second.resolve(verifiedResponse("current"));
await newTask;
assert.equal(state.dnsTesting.value, false);
assert.equal(state.dnsVerification.value.verification_token, "current");
});