feat(security): harden gateway boundaries and usage policies

Consolidate subscription usage policy enforcement, privacy-safe persistence, and gateway security hardening into one reviewable change.

Includes bounded HTTP and execution envelopes, header and protocol guards, DNS and relay validation, authentication and secret projection hardening, secure backup/install paths, and regression coverage.
This commit is contained in:
elky
2026-09-04 03:45:52 +08:00
parent ddcbeb3ae9
commit 579f2c7cc1
1019 changed files with 190437 additions and 26080 deletions
@@ -267,6 +267,7 @@ import { getApiUrl } from '@/utils/url'
import { getOAuthIcon } from '@/utils/oauth-icons'
import { navigateAfterLogin } from '@/features/auth/utils/loginRedirect'
import { useI18n } from '@/i18n'
import { safeInternalNavigationPath } from '@/utils/navigationSecurity'
const props = defineProps<{
modelValue: boolean
@@ -401,10 +402,8 @@ function consumeStoredRedirectPath(): string | null {
if (redirectPath) {
sessionStorage.removeItem('redirectPath')
}
if (!redirectPath || redirectPath === '/' || !redirectPath.startsWith('/') || redirectPath.startsWith('//')) {
return null
}
return redirectPath
const safePath = safeInternalNavigationPath(redirectPath)
return safePath === '/' ? null : safePath
}
function handleOAuthLogin(providerType: string) {
@@ -503,6 +503,7 @@ const isLoading = ref(false)
const loadingText = ref(t('auth.register.submit'))
const isSendingCode = ref(false)
const emailVerified = ref(false)
const emailVerificationToken = ref('')
const verificationError = ref(false)
const codeSentAt = ref<number | null>(null)
const cooldownSeconds = ref(0)
@@ -643,10 +644,10 @@ const canSubmit = computed(() => {
// 查询并恢复验证状态
const checkAndRestoreVerificationStatus = async (email: string) => {
if (!email || !props.requireEmailVerification) return
if (!email || !props.requireEmailVerification || !emailVerificationToken.value) return
try {
const status = await authApi.getVerificationStatus(email)
const status = await authApi.getVerificationStatus(email, emailVerificationToken.value)
// 注意:不恢复 is_verified 状态
// 刷新页面后需要重新发送验证码并验证,防止验证码被他人使用
@@ -675,6 +676,7 @@ watch(
// 邮箱变化时重置验证状态
if (newEmail !== oldEmail) {
emailVerified.value = false
emailVerificationToken.value = ''
verificationError.value = false
codeSentAt.value = null
cooldownSeconds.value = 0
@@ -751,6 +753,7 @@ const resetForm = () => {
verificationCode: ''
}
emailVerified.value = false
emailVerificationToken.value = ''
verificationError.value = false
isSendingCode.value = false
codeSentAt.value = null
@@ -795,6 +798,7 @@ const handleSendCode = async () => {
)
if (response.success) {
emailVerificationToken.value = response.verification_token
resetTurnstile()
codeSentAt.value = Date.now()
if (response.expire_minutes) {
@@ -834,7 +838,16 @@ const handleCodeComplete = async (code: string) => {
verificationError.value = false
try {
const response = await authApi.verifyEmail(formData.value.email, code)
if (!emailVerificationToken.value) {
verificationError.value = true
showError(t('auth.register.codeRetry'), t('auth.register.verifyFailed'))
return
}
const response = await authApi.verifyEmail(
formData.value.email,
code,
emailVerificationToken.value
)
if (response.success) {
emailVerified.value = true
@@ -899,6 +912,9 @@ const handleSubmit = async () => {
if (formData.value.email && formData.value.email.trim()) {
registerData.email = formData.value.email
}
if (props.requireEmailVerification && emailVerificationToken.value) {
registerData.email_verification_token = emailVerificationToken.value
}
if (turnstileRequired.value && currentTurnstileAction.value === 'register') {
registerData.turnstile_token = turnstileToken.value
}
@@ -161,6 +161,7 @@ beforeEach(() => {
success: true,
message: 'ok',
expire_minutes: 5,
verification_token: 'verification-session-token',
})
})
@@ -147,6 +147,7 @@ describe('RegisterDialog Turnstile flow', () => {
success: true,
message: 'ok',
expire_minutes: 5,
verification_token: 'verification-session-token',
})
toastErrorMock.mockReset()
toastSuccessMock.mockReset()
@@ -38,6 +38,16 @@ async function createAbortedNavigationFailure(path: string) {
}
describe('navigateAfterLogin', () => {
it('never passes a cross-origin target to router or document navigation', async () => {
const push = vi.fn<Router['push']>().mockResolvedValue(undefined)
const documentNavigate = vi.fn()
await navigateAfterLogin(createRouterMock(push), '//attacker.example/steal', documentNavigate)
expect(push).toHaveBeenCalledWith('/')
expect(documentNavigate).not.toHaveBeenCalled()
})
it('treats duplicated Vue Router navigation as a completed login navigation', async () => {
const push = vi.fn<Router['push']>().mockResolvedValue(await createDuplicatedNavigationFailure('/dashboard'))
const documentNavigate = vi.fn()
@@ -1,4 +1,5 @@
import { isNavigationFailure, NavigationFailureType, type Router } from 'vue-router'
import { safeInternalNavigationPath } from '@/utils/navigationSecurity'
export type LoginNavigationResult = 'router' | 'already-there' | 'document'
@@ -13,21 +14,22 @@ export async function navigateAfterLogin(
targetPath: string,
documentNavigate: DocumentNavigate = defaultDocumentNavigate,
): Promise<LoginNavigationResult> {
const safeTargetPath = safeInternalNavigationPath(targetPath) ?? '/'
try {
const navigationFailure = await router.push(targetPath)
const navigationFailure = await router.push(safeTargetPath)
if (isNavigationFailure(navigationFailure, NavigationFailureType.duplicated)) {
return 'already-there'
}
if (navigationFailure) {
documentNavigate(targetPath)
documentNavigate(safeTargetPath)
return 'document'
}
return 'router'
} catch {
documentNavigate(targetPath)
documentNavigate(safeTargetPath)
return 'document'
}
}