Skip to content

Vue-router decodeURI twice cause URL contains encoded % an URIError: URI malformed #2708

Closed
@hq5544

Description

@hq5544

Version

3.0.3

Reproduction link

Sorry, It is hard to supply a minimal reproduction

Steps to reproduce

Visit http://a.b.c/#/test?d=%25
Vue-router will throw a warning URIError: URI malformed, and abandon all the query params.

What is expected?

DecodeURI once is enough.

What is actually happening?

DecodeURI twice.


ref:

export function getHash (): string {
// We can't use window.location.hash here because it's not
// consistent across browsers - Firefox will pre-decode it!
const href = window.location.href
const index = href.indexOf('#')
return index === -1 ? '' : decodeURI(href.slice(index + 1))
}

function parseQuery (query: string): Dictionary<string> {
const res = {}
query = query.trim().replace(/^(\?|#|&)/, '')
if (!query) {
return res
}
query.split('&').forEach(param => {
const parts = param.replace(/\+/g, ' ').split('=')
const key = decode(parts.shift())
const val = parts.length > 0
? decode(parts.join('='))
: null
if (res[key] === undefined) {
res[key] = val
} else if (Array.isArray(res[key])) {
res[key].push(val)
} else {
res[key] = [res[key], val]
}
})
return res
}

The getHash method already decoded href, but parseQuery decode it again. So caused URIError: URI malformed.
Maybe decoding in getHash is not necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions