Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize routes and decode resolved query #8430

Merged
merged 29 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/config/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import pick from 'lodash/pick'
import uniq from 'lodash/uniq'
import consola from 'consola'
import destr from 'destr'
import { TARGETS, MODES, guardDir, isNonEmptyString, isPureObject, isUrl, getMainModule, urlJoin, getPKG } from '@nuxt/utils'
import {
TARGETS, MODES, guardDir, isNonEmptyString,
isPureObject, isUrl, getMainModule, urlJoin, getPKG, safeEncode
} from '@nuxt/utils'
import { defaultNuxtConfigFile, getDefaultNuxtConfig } from './config'

export function getNuxtConfig (_options) {
Expand Down Expand Up @@ -126,7 +129,7 @@ export function getNuxtConfig (_options) {
if (!/\/$/.test(options.router.base)) {
options.router.base += '/'
}
options.router.base = encodeURI(decodeURI(options.router.base))
options.router.base = safeEncode(options.router.base)

// Legacy support for export
if (options.export) {
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export const stripWhitespace = function stripWhitespace (string) {
})
return string
}

export function safeEncode (str) {
return /%[0-9a-fA-F]{2}/.test(str) ? str : encodeURI(str)
}
21 changes: 16 additions & 5 deletions packages/vue-app/template/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vue from 'vue'
import Router from 'vue-router'
import { interopDefault } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
import { interopDefault, safeEncode } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
import scrollBehavior from './router.scrollBehavior.js'

<% function recursiveRoutes(routes, tab, components, indentCount) {
Expand Down Expand Up @@ -105,16 +105,27 @@ export const routerOptions = {
fallback: <%= router.fallback %>
}

function decodeObj(obj) {
for (const key in obj) {
if (typeof obj[key] === 'string') {
obj[key] = decodeURIComponent(obj[key])
}
}
}

export function createRouter () {
const router = new Router(routerOptions)
const resolve = router.resolve.bind(router)

// encodeURI(decodeURI()) ~> support both encoded and non-encoded urls
const resolve = router.resolve.bind(router)
router.resolve = (to, current, append) => {
if (typeof to === 'string') {
to = encodeURI(decodeURI(to))
to = safeEncode(to)
}
const r = resolve(to, current, append)
if (r && r.resolved && r.resolved.query) {
decodeObj(r.resolved.query)
}
return resolve(to, current, append)
return r
}

return router
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-app/template/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
<% if (features.middleware) { %>middlewareSeries,<% } %>
<% if (features.middleware && features.layouts) { %>sanitizeComponent,<% } %>
getMatchedComponents,
promisify
promisify,
safeEncode
} from './utils.js'
<% if (features.fetch) { %>import fetchMixin from './mixins/fetch.server'<% } %>
import { createApp<% if (features.layouts) { %>, NuxtError<% } %> } from './index.js'
Expand Down Expand Up @@ -50,7 +51,7 @@ const createNext = ssrContext => (opts) => {
opts.path = urlJoin(routerBase, opts.path)
}
// Avoid loop redirect
if (encodeURI(decodeURI(opts.path)) === ssrContext.url) {
if (safeEncode(opts.path) === safeEncode(ssrContext.url)) {
ssrContext.redirected = false
return
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vue-app/template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export function getLocation (base, mode) {

const fullPath = (path || '/') + window.location.search + window.location.hash

return encodeURI(fullPath)
return safeEncode(fullPath)
}

// Imported from path-to-regexp
Expand Down Expand Up @@ -692,3 +692,7 @@ export function setScrollRestoration (newVal) {
window.history.scrollRestoration = newVal;
} catch(e) {}
}

export function safeEncode(str) {
return /%[0-9a-fA-F]{2}/.test(str) ? str : encodeURI(str)
}
6 changes: 6 additions & 0 deletions test/dev/encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('encoding', () => {
expect(response).toContain('Unicode base works!')
})

test('/ö/dynamic?q=food,coffee (encodeURIComponent)', async () => {
const { body: response } = await rp(url('/ö/dynamic?q=food%252Ccoffee'))

expect(response).toContain('food,coffee')
})

// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
Expand Down
42 changes: 29 additions & 13 deletions test/fixtures/encoding/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
<template>
<div>
<div>
<NLink to="/тест">
/тест
</NLink>
<NLink :to="encodeURI('/тест')">
/тест (encoded)
</NLink>
<br>
<NLink to="/тест?spa">
/тест (SPA)
</NLink>
<NLink :to="encodeURI('/тест?spa')">
/тест (SPA encoded)
</NLink>
<ul>
<li v-for="link in links" :key="link">
<NLink :to="link">
{{ link }}
</NLink>
<NLink :to="link.includes('?') ? link.replace('?', '?spa&') : '?spa'">
(spa)
</NLink>
<a :href="encodeURI('/ö') + link">(direct)</a>
</li>
</ul>
</div>
<Nuxt />
</div>
</template>

<script>
export default {
computed: {
links () {
return [
'/redirect',
'/тест',
encodeURI('/тест'),
'/dynamic/سلام چطوری?q=cofee,food,دسر',
encodeURI('/dynamic/سلام چطوری?q=cofee,food,دسر'),
// Using encodeURIComponent on each segment
'/dynamic/%D8%B3%D9%84%D8%A7%D9%85%20%DA%86%D8%B7%D9%88%D8%B1%DB%8C?q=cofee%2Cfood%2C%D8%AF%D8%B3%D8%B1'
]
}
}
}
</script>

<style scoped>
a {
color: grey;
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/encoding/pages/dynamic/_id.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div>
<div>Query (SSR): <pre v-text="q" /> </div>
<div>Params (SSR): <pre v-text="p" /> </div>
</div>
</template>

<script>
export default {
asyncData ({ route }) {
return {
q: JSON.stringify(route.query),
p: JSON.stringify(route.params)
}
}
}
</script>
13 changes: 13 additions & 0 deletions test/fixtures/encoding/pages/redirect.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div>
Redirecting...
</div>
</template>

<script>
export default {
asyncData ({ redirect }) {
return redirect('/dynamic/重新导向?foo bar')
}
}
</script>