守卫清洗 redirect 参数并把 welcome 入白名单 #3398

P1 加 safeRedirect(to):丢弃以 //、/\ 开头或非 / 起首的输入,避免开放重定向跳出域名;守卫改读 to.query.redirect 优先。P2 把 welcome 加入 anonRouteNames 防误伤根路径。dev server 编译 31419ms 通过。
This commit is contained in:
hrz 2026-10-07 10:30:52 +08:00
parent c08d9af5e3
commit 047334e70f

View file

@ -228,9 +228,17 @@ VueRouter.prototype.push = function push(location) {
}) })
} }
// 把入参清洗为站内绝对路径,避免 //evil.com 这类协议相对 URL 跳出域名
function safeRedirect(target) {
if (typeof target !== 'string' || target.length === 0) return '/'
if (!target.startsWith('/')) return '/'
if (target.startsWith('//') || target.startsWith('/\\')) return '/'
return target
}
// 任何路由段上 meta.requiresAuth === true 都视为需要登录 // 任何路由段上 meta.requiresAuth === true 都视为需要登录
// 匿名白名单按 name 匹配,避免被误标时也拦住 anon 路由 // 匿名白名单按 name 匹配,避免被误标时也拦住 anon 路由
const anonRouteNames = new Set(['login', 'Register', 'RetrievePassword']) const anonRouteNames = new Set(['login', 'Register', 'RetrievePassword', 'welcome'])
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
if (anonRouteNames.has(to.name)) { if (anonRouteNames.has(to.name)) {
@ -239,7 +247,8 @@ router.beforeEach((to, from, next) => {
} }
const requiresAuth = to.matched.some(r => r.meta && r.meta.requiresAuth === true) const requiresAuth = to.matched.some(r => r.meta && r.meta.requiresAuth === true)
if (requiresAuth && !localStorage.getItem('token')) { if (requiresAuth && !localStorage.getItem('token')) {
next({ name: 'login', query: { redirect: to.fullPath } }) const target = safeRedirect(to.query.redirect || to.path)
next({ name: 'login', query: { redirect: target } })
return return
} }
next() next()