fix(frontend): 修复前端审计问题并补充回归测试

This commit is contained in:
yangjianbo
2026-02-14 11:56:08 +08:00
parent d04b47b3ca
commit f6bff97d26
27 changed files with 772 additions and 219 deletions

View File

@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { resolveDocumentTitle } from '@/router/title'
describe('resolveDocumentTitle', () => {
it('路由存在标题时,使用“路由标题 - 站点名”格式', () => {
expect(resolveDocumentTitle('Usage Records', 'My Site')).toBe('Usage Records - My Site')
})
it('路由无标题时,回退到站点名', () => {
expect(resolveDocumentTitle(undefined, 'My Site')).toBe('My Site')
})
it('站点名为空时,回退默认站点名', () => {
expect(resolveDocumentTitle('Dashboard', '')).toBe('Dashboard - Sub2API')
expect(resolveDocumentTitle(undefined, ' ')).toBe('Sub2API')
})
it('站点名变更时仅影响后续路由标题计算', () => {
const before = resolveDocumentTitle('Admin Dashboard', 'Alpha')
const after = resolveDocumentTitle('Admin Dashboard', 'Beta')
expect(before).toBe('Admin Dashboard - Alpha')
expect(after).toBe('Admin Dashboard - Beta')
})
})

View File

@@ -8,6 +8,7 @@ import { useAuthStore } from '@/stores/auth'
import { useAppStore } from '@/stores/app'
import { useNavigationLoadingState } from '@/composables/useNavigationLoading'
import { useRoutePrefetch } from '@/composables/useRoutePrefetch'
import { resolveDocumentTitle } from './title'
/**
* Route definitions with lazy loading
@@ -389,12 +390,7 @@ router.beforeEach((to, _from, next) => {
// Set page title
const appStore = useAppStore()
const siteName = appStore.siteName || 'Sub2API'
if (to.meta.title) {
document.title = `${to.meta.title} - ${siteName}`
} else {
document.title = siteName
}
document.title = resolveDocumentTitle(to.meta.title, appStore.siteName)
// Check if route requires authentication
const requiresAuth = to.meta.requiresAuth !== false // Default to true

View File

@@ -0,0 +1,12 @@
/**
* 统一生成页面标题,避免多处写入 document.title 产生覆盖冲突。
*/
export function resolveDocumentTitle(routeTitle: unknown, siteName?: string): string {
const normalizedSiteName = typeof siteName === 'string' && siteName.trim() ? siteName.trim() : 'Sub2API'
if (typeof routeTitle === 'string' && routeTitle.trim()) {
return `${routeTitle.trim()} - ${normalizedSiteName}`
}
return normalizedSiteName
}