Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e447a79

Browse files
committed
fix(nuxt): reject cross-origin paths in reloadNuxtApp
Refs: GHSA-c9cv-mq2m-ppp3
1 parent d72a89e commit e447a79

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

packages/nuxt/src/app/composables/chunk.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface ReloadNuxtAppOptions {
2121
* The path to reload. If this is different from the current window location it will
2222
* trigger a navigation and add an entry in the browser history.
2323
*
24-
* URLs with script-like protocols (e.g. `javascript:`, `data:`) are rejected.
24+
* Cross-origin paths and URLs with script-like protocols (e.g. `javascript:`, `data:`) are rejected.
2525
* @default {window.location.pathname}
2626
*/
2727
path?: string
@@ -32,9 +32,12 @@ export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {
3232
if (import.meta.server) { return }
3333
const path = options.path || window.location.pathname
3434

35-
const { protocol } = new URL(path, window.location.href)
36-
if (protocol && isScriptProtocol(protocol)) {
37-
throw new Error(`Cannot navigate to a URL with '${protocol}' protocol.`)
35+
const url = new URL(path, window.location.href)
36+
if (url.host !== window.location.host) {
37+
throw new Error(`Cannot navigate to a URL with a different host: '${path}'.`)
38+
}
39+
if (url.protocol && isScriptProtocol(url.protocol)) {
40+
throw new Error(`Cannot navigate to a URL with '${url.protocol}' protocol.`)
3841
}
3942

4043
let handledPath: Record<string, any> = {}

test/nuxt/composables.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,27 @@ describe('routing utilities: `navigateTo`', () => {
662662
})
663663
it('reloadNuxtApp should disallow paths with data/script URLs', () => {
664664
const urls = [
665-
['javascript:alert("hi")', 'javascript'],
666-
['data:alert("hi")', 'data'],
667-
['\0data:alert("hi")', 'data'],
665+
'javascript:alert("hi")',
666+
'data:alert("hi")',
667+
'\0data:alert("hi")',
668668
]
669-
for (const [url, protocol] of urls) {
670-
expect(() => reloadNuxtApp({ path: url })).toThrow(`Cannot navigate to a URL with '${protocol}:' protocol.`)
669+
for (const url of urls) {
670+
expect(() => reloadNuxtApp({ path: url })).toThrow(`Cannot navigate to a URL with a different host: '${url}'.`)
671+
}
672+
})
673+
it('reloadNuxtApp should disallow cross-origin paths', () => {
674+
const urls = [
675+
'//evil.com',
676+
'https://evil.com',
677+
'\\\\evil.com',
678+
]
679+
for (const url of urls) {
680+
expect(() => reloadNuxtApp({ path: url })).toThrow(`Cannot navigate to a URL with a different host: '${url}'.`)
671681
}
672682
})
683+
it('reloadNuxtApp should allow same-origin paths', () => {
684+
expect(() => reloadNuxtApp({ path: '/legit/path' })).not.toThrow()
685+
})
673686
it('navigateTo should replace current navigation state if called within middleware', () => {
674687
const nuxtApp = useNuxtApp()
675688
nuxtApp._processingMiddleware = true

0 commit comments

Comments
 (0)