fix(vue-app): sanitize path with trailing slash in getLocation (#6744) (#6745)

This commit is contained in:
Konstantin Barabanov 2020-06-30 20:16:21 +03:00 committed by GitHub
parent 6b06ab201e
commit b4d3ebf472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 1 deletions

View File

@ -282,7 +282,8 @@ export function getLocation (base, mode) {
if (mode === 'hash') { if (mode === 'hash') {
return window.location.hash.replace(/^#\//, '') return window.location.hash.replace(/^#\//, '')
} }
if (base && path.indexOf(base) === 0) { // To get matched with sanitized router.base add trailing slash
if (base && (path.endsWith('/') ? path : path + '/').startsWith(base)) {
path = path.slice(base.length) path = path.slice(base.length)
} }
return (path || '/') + window.location.search + window.location.hash return (path || '/') + window.location.search + window.location.hash

View File

@ -0,0 +1,71 @@
import Browser from '../utils/browser'
import { loadFixture, getPort, Nuxt } from '../utils'
let port
const browser = new Browser()
const url = route => 'http://localhost:' + port + route
let nuxt = null
let page = null
describe('spa router base browser', () => {
beforeAll(async () => {
const config = await loadFixture('spa-base')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
await browser.start({
// slowMo: 50,
// headless: false
})
})
test('Open /app (router base)', async () => {
page = await browser.page(url('/app'))
expect(await page.evaluate(() => location.href)).toBe(url('/app'))
expect(await page.html()).not.toContain('This page could not be found')
expect(await page.evaluate(() => {
const headings = document.evaluate("//div[text()='Hello SPA!']", document, null, XPathResult.ANY_TYPE, null)
return headings.iterateNext()
})).not.toBe(null)
})
test('Open /app/ (router base with trailing slash)', async () => {
page = await browser.page(url('/app/'))
expect(await page.evaluate(() => location.href)).toBe(url('/app/'))
expect(await page.html()).not.toContain('This page could not be found')
})
test('Open /app/mounted', async () => {
page = await browser.page(url('/app/mounted'))
expect(await page.$text('h1')).toMatch('Test: updated')
})
test('/app/unknown', async () => {
page = await browser.page(url('/app/unknown'))
expect(await page.evaluate(() => location.href)).toBe(url('/app/unknown'))
expect(await page.html()).toContain('This page could not be found')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})
// Stop browser
afterAll(async () => {
await page.close()
await browser.close()
})
})

13
test/fixtures/spa-base/nuxt.config.js vendored Normal file
View File

@ -0,0 +1,13 @@
import { resolve } from 'path'
import defaultsDeep from 'lodash/defaultsDeep'
import baseNuxtConfig from '../spa/nuxt.config'
const config = {
buildDir: resolve(__dirname, '.nuxt'),
srcDir: resolve(__dirname, '..', 'spa'),
router: {
base: '/app'
}
}
export default defaultsDeep(config, baseNuxtConfig)

View File

@ -4,3 +4,4 @@ import { buildFixture } from '../../utils/build'
// That's why building both from same test file. // That's why building both from same test file.
buildFixture('spa') buildFixture('spa')
buildFixture('spa-hash') buildFixture('spa-hash')
buildFixture('spa-base')