mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 18:07:47 +08:00
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:
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { ensureUserLoaded } from '@/router/guards/authGuard'
|
||||
|
||||
describe('ensureUserLoaded', () => {
|
||||
it('restores a page-load session before deciding a protected route is unauthenticated', async () => {
|
||||
const store = {
|
||||
token: null as string | null,
|
||||
user: null,
|
||||
restoreSession: vi.fn(async function (this: { token: string | null }) {
|
||||
this.token = 'restored-access-token'
|
||||
return true
|
||||
}),
|
||||
fetchCurrentUser: vi.fn(async function (this: { user: unknown }) {
|
||||
this.user = { id: 'user-1' }
|
||||
}),
|
||||
logout: vi.fn(),
|
||||
}
|
||||
|
||||
await expect(ensureUserLoaded(store as never)).resolves.toBe(true)
|
||||
expect(store.restoreSession).toHaveBeenCalledTimes(1)
|
||||
expect(store.fetchCurrentUser).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('denies a protected route when the refresh cookie cannot restore a session', async () => {
|
||||
const store = {
|
||||
token: null,
|
||||
user: null,
|
||||
restoreSession: vi.fn(async () => false),
|
||||
fetchCurrentUser: vi.fn(),
|
||||
logout: vi.fn(),
|
||||
}
|
||||
|
||||
await expect(ensureUserLoaded(store as never)).resolves.toBe(false)
|
||||
expect(store.fetchCurrentUser).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -46,6 +46,13 @@ describe('resolveHomeRedirect', () => {
|
||||
expect(sessionStorage.getItem('redirectPath')).toBeNull()
|
||||
})
|
||||
|
||||
it('discards cross-origin stored redirect paths', () => {
|
||||
sessionStorage.setItem('redirectPath', '//attacker.example/steal')
|
||||
|
||||
expect(resolveHomeRedirect(route('/'), route('/external'), authStore({ isAuthenticated: true }))).toBe('/dashboard')
|
||||
expect(sessionStorage.getItem('redirectPath')).toBeNull()
|
||||
})
|
||||
|
||||
it('routes authenticated users to the correct dashboard by role', () => {
|
||||
expect(resolveHomeRedirect(route('/'), route('/external'), authStore({ isAuthenticated: true }))).toBe('/dashboard')
|
||||
expect(resolveHomeRedirect(route('/'), route('/external'), authStore({ isAuthenticated: true, canAccessAdmin: true }))).toBe('/admin/dashboard')
|
||||
|
||||
@@ -16,6 +16,10 @@ function isNetworkError(error: unknown): boolean {
|
||||
export async function ensureUserLoaded(
|
||||
authStore: ReturnType<typeof useAuthStore>
|
||||
): Promise<boolean> {
|
||||
if (!authStore.token) {
|
||||
await authStore.restoreSession()
|
||||
}
|
||||
|
||||
if (authStore.token && !authStore.user) {
|
||||
try {
|
||||
await authStore.fetchCurrentUser()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { RouteLocationNormalized } from 'vue-router'
|
||||
import type { useAuthStore } from '@/stores/auth'
|
||||
import { safeInternalNavigationPath } from '@/utils/navigationSecurity'
|
||||
|
||||
/**
|
||||
* 处理已认证用户访问首页时的重定向。
|
||||
@@ -27,9 +28,10 @@ export function resolveHomeRedirect(
|
||||
|
||||
// 已登录用户首次访问首页(非返回/刷新场景),根据角色跳转到对应仪表盘
|
||||
const redirectPath = sessionStorage.getItem('redirectPath')
|
||||
if (redirectPath && redirectPath !== '/') {
|
||||
if (redirectPath) {
|
||||
sessionStorage.removeItem('redirectPath')
|
||||
return redirectPath
|
||||
const safePath = safeInternalNavigationPath(redirectPath)
|
||||
if (safePath && safePath !== '/') return safePath
|
||||
}
|
||||
|
||||
return authStore.canAccessAdmin ? '/admin/dashboard' : '/dashboard'
|
||||
|
||||
@@ -16,10 +16,11 @@ const router = createRouter({
|
||||
})
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const authStore = useAuthStore()
|
||||
const moduleStore = useModuleStore()
|
||||
const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
|
||||
|
||||
try {
|
||||
const authStore = useAuthStore()
|
||||
const moduleStore = useModuleStore()
|
||||
const isAuthenticated = await ensureUserLoaded(authStore)
|
||||
|
||||
// 首页重定向
|
||||
@@ -27,7 +28,6 @@ router.beforeEach(async (to, from, next) => {
|
||||
if (homeRedirect !== null) return next(homeRedirect === '' ? undefined : homeRedirect)
|
||||
|
||||
// 需要认证但未认证
|
||||
const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
|
||||
if (requiresAuth && !isAuthenticated) {
|
||||
sessionStorage.setItem('redirectPath', to.fullPath)
|
||||
log.debug('No valid token found, redirecting to home')
|
||||
@@ -50,7 +50,10 @@ router.beforeEach(async (to, from, next) => {
|
||||
next()
|
||||
} catch (error) {
|
||||
log.error('Router guard error', error)
|
||||
// 发生错误时,直接放行,不要乱跳转
|
||||
if (requiresAuth) {
|
||||
sessionStorage.setItem('redirectPath', to.fullPath)
|
||||
return next('/')
|
||||
}
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user