8000 handle malformed URIs (fix #424) · vuejs/vue-router@b227648 · GitHub
[go: up one dir, main page]

Skip to content

Commit b227648

Browse files
committed
handle malformed URIs (fix #424)
1 parent 5df14c6 commit b227648

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

lib/route-recognizer.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ var specials = [
77

88
var escapeRegex = new RegExp('(\\' + specials.join('|\\') + ')', 'g')
99

10+
var noWarning = false
11+
function warn (msg) {
12+
if (!noWarning && typeof console !== 'undefined') {
13+
console.error('[vue-router] ' + msg)
14+
}
15+
}
16+
17+
function tryDecode (uri, asComponent) {
18+
try {
19+
return asComponent
20+
? decodeURIComponent(uri)
21+
: decodeURI(uri)
22+
} catch (e) {
23+
warn('malformed URI' + (asComponent ? ' component: ' : ': ') + uri)
24+
}
25+
}
26+
1027
function isArray(test) {
1128
return Object.prototype.toString.call(test) === "[object Array]"
1229
}
@@ -321,7 +338,7 @@ function addSegment(currentState, segment) {
321338
function decodeQueryParamPart(part) {
322339
// http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
323340
part = part.replace(/\+/gm, '%20')
324-
return decodeURIComponent(part)
341+
return tryDecode(part, true)
325342
}
326343

327344
// The main interface
@@ -485,20 +502,24 @@ RouteRecognizer.prototype = {
485502
return queryParams
486503
},
487504

488-
recognize: function(path) {
505+
recognize: function(path, silent) {
506+
noWarning = silent
489507
var states = [ this.rootState ],
490508
pathLen, i, l, queryStart, queryParams = {},
491509
isSlashDropped = false
492510

493-
path = decodeURI(path)
494-
495511
queryStart = path.indexOf('?')
496512
if (queryStart !== -1) {
497513
var queryString = path.substr(queryStart + 1, path.length)
498514
path = path.substr(0, queryStart)
499-
queryParams = this.parseQueryString(queryString)
515+
if (queryString) {
516+
queryParams = this.parseQueryString(queryString)
517+
}
500518
}
501519

520+
path = tryDecode(path)
521+
if (!path) return
522+
502523
// DEBUG GROUP path
503524

504525
if (path.charAt(0) !== "/") { path = "/" + path }

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class Router {
441441
*/
442442

443443
_checkGuard (path) {
444-
let matched = this._guardRecognizer.recognize(path)
444+
let matched = this._guardRecognizer.recognize(path, true)
445445
if (matched) {
446446
matched[0].handler(matched[0], matched.queryParams)
447447
return true

src/util.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ export default exports
1313

1414
export function warn (msg) {
1515
/* istanbul ignore next */
16-
if (window.console) {
17-
console.warn('[vue-router] ' + msg)
18-
if (!exports.Vue || exports.Vue.config.debug) {
19-
console.warn(new Error('warning stack trace:').stack)
20-
}
16+
if (typeof console !== 'undefined') {
17+
console.error('[vue-router] ' + msg)
2118
}
2219
}
2320

test/unit/specs/core.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('Core', function () {
1010
el = document.createElement('div')
1111
document.body.appendChild(el)
1212
spyOn(window, 'scrollTo')
13+
spyOn(console, 'error')
1314
})
1415

1516
afterEach(function () {
@@ -121,8 +122,8 @@ describe('Core', function () {
121122
[{ path: '/a', query: query }, 'A' + query.msg],
122123
// object with named route
123124
[{ name: 'b', query: query }, 'B' + query.msg],
124-
// string path
125-
['/c?msg=' + encodeURIComponent(query.msg), 'C' + query.msg]
125+
// special char
126+
['/c?msg=%!!!', 'C%!!!']
126127
], done)
127128
})
128129

0 commit comments

Comments
 (0)
0