Skip to content

Commit 6b59926

Browse files
fix: escape double colon in multi-parametrical node (#307)
1 parent 917645d commit 6b59926

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,18 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
223223
let lastParamEndIndex = j
224224
for (; lastParamEndIndex < path.length; lastParamEndIndex++) {
225225
const charCode = path.charCodeAt(lastParamEndIndex)
226-
if (charCode === 58 || charCode === 47) {
227-
break
226+
const nextCharCode = path.charCodeAt(lastParamEndIndex + 1)
227+
if (charCode === 58 && nextCharCode === 58) {
228+
lastParamEndIndex++
229+
continue
228230
}
231+
if (charCode === 58 || charCode === 47) break
229232
}
230233

231-
const staticPart = path.slice(j, lastParamEndIndex)
234+
let staticPart = path.slice(j, lastParamEndIndex)
232235
if (staticPart) {
236+
staticPart = staticPart.split('::').join(':')
237+
staticPart = staticPart.split('%').join('%25')
233238
regexps.push(escapeRegExp(staticPart))
234239
}
235240

test/issue-17.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ test('Multi parametric route with regexp / 1', t => {
203203
findMyWay.lookup({ method: 'GET', url: '/at/0h42m', headers: {} }, null)
204204
})
205205

206+
test('Multi parametric route with colon separator', t => {
207+
t.plan(3)
208+
const findMyWay = FindMyWay({
209+
defaultRoute: (req, res) => {
210+
t.fail('Should not be defaultRoute')
211+
}
212+
})
213+
214+
findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
215+
t.equal(params.param, 'foo')
216+
})
217+
218+
findMyWay.on('GET', '/:param1(.*)::suffix1-:param2(.*)::suffix2/static', (req, res, params) => {
219+
t.equal(params.param1, 'foo')
220+
t.equal(params.param2, 'bar')
221+
})
222+
223+
findMyWay.lookup({ method: 'GET', url: '/foo:suffix', headers: {} }, null)
224+
findMyWay.lookup({ method: 'GET', url: '/foo:suffix1-bar:suffix2/static', headers: {} }, null)
225+
})
226+
206227
test('Multi parametric route with regexp / 2', t => {
207228
t.plan(8)
208229
const findMyWay = FindMyWay({

test/issue-206.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ test('Special chars on path parameter', t => {
9797
findMyWay.lookup(get('/reg/123%20.png'), null, { expect: { regExeParam: '123' }, handler: regexPathParam })
9898
})
9999

100+
test('Multi parametric route with encoded colon separator', t => {
101+
t.plan(1)
102+
const findMyWay = FindMyWay({
103+
defaultRoute: (req, res) => {
104+
t.fail('Should not be defaultRoute')
105+
}
106+
})
107+
108+
findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
109+
t.equal(params.param, 'foo-bar')
110+
})
111+
112+
findMyWay.lookup({ method: 'GET', url: '/foo-bar%3Asuffix', headers: {} }, null)
113+
})
114+
100115
function get (url) {
101116
return { method: 'GET', url, headers: {} }
102117
}

0 commit comments

Comments
 (0)