-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
455e3ba
commit d834596
Showing
5 changed files
with
194 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,87 @@ | ||
'use strict' | ||
|
||
const fastDecode = require('fast-decode-uri-component') | ||
|
||
// It must spot all the chars where decodeURIComponent(x) !== decodeURI(x) | ||
// The chars are: # $ & + , / : ; = ? @ | ||
const uriComponentsCharMap = new Array(53).fill(0x00) | ||
uriComponentsCharMap[50] = new Array(103).fill(0x00) | ||
uriComponentsCharMap[50][51] = true // # '%23' | ||
uriComponentsCharMap[50][52] = true // $ '%24' | ||
uriComponentsCharMap[50][54] = true // & '%26' | ||
uriComponentsCharMap[50][66] = true // + '%2B' | ||
uriComponentsCharMap[50][98] = true // + '%2b' | ||
uriComponentsCharMap[50][67] = true // , '%2C' | ||
uriComponentsCharMap[50][99] = true // , '%2c' | ||
uriComponentsCharMap[50][70] = true // / '%2F' | ||
uriComponentsCharMap[50][102] = true // / '%2f' | ||
|
||
uriComponentsCharMap[51] = new Array(103).fill(0x00) | ||
uriComponentsCharMap[51][65] = true // : '%3A' | ||
uriComponentsCharMap[51][97] = true // : '%3a' | ||
uriComponentsCharMap[51][66] = true // ; '%3B' | ||
uriComponentsCharMap[51][98] = true // ; '%3b' | ||
uriComponentsCharMap[51][68] = true // = '%3D' | ||
uriComponentsCharMap[51][100] = true // = '%3d' | ||
uriComponentsCharMap[51][70] = true // ? '%3F' | ||
uriComponentsCharMap[51][102] = true // ? '%3f' | ||
function decodeComponentChar (highCharCode, lowCharCode) { | ||
if (highCharCode === 50) { | ||
if (lowCharCode === 53) return '%' | ||
|
||
uriComponentsCharMap[52] = new Array(49).fill(0x00) | ||
uriComponentsCharMap[52][48] = true // @ '%40' | ||
if (lowCharCode === 51) return '#' | ||
if (lowCharCode === 52) return '$' | ||
if (lowCharCode === 54) return '&' | ||
if (lowCharCode === 66) return '+' | ||
if (lowCharCode === 98) return '+' | ||
if (lowCharCode === 67) return ',' | ||
if (lowCharCode === 99) return ',' | ||
if (lowCharCode === 70) return '/' | ||
if (lowCharCode === 102) return '/' | ||
return null | ||
} | ||
if (highCharCode === 51) { | ||
if (lowCharCode === 65) return ':' | ||
if (lowCharCode === 97) return ':' | ||
if (lowCharCode === 66) return ';' | ||
if (lowCharCode === 98) return ';' | ||
if (lowCharCode === 68) return '=' | ||
if (lowCharCode === 100) return '=' | ||
if (lowCharCode === 70) return '?' | ||
if (lowCharCode === 102) return '?' | ||
return null | ||
} | ||
if (highCharCode === 52 && lowCharCode === 48) { | ||
return '@' | ||
} | ||
return null | ||
} | ||
|
||
function sanitizeUrl (url, customDecoder) { | ||
let originPath = url | ||
function safeDecodeURI (path) { | ||
let shouldDecode = false | ||
let containsEncodedComponents = false | ||
let highChar | ||
let lowChar | ||
for (var i = 0, len = url.length; i < len; i++) { | ||
var charCode = url.charCodeAt(i) | ||
|
||
if (shouldDecode && !containsEncodedComponents) { | ||
if (highChar === 0 && uriComponentsCharMap[charCode]) { | ||
highChar = charCode | ||
lowChar = 0x00 | ||
} else if (highChar && lowChar === 0 && uriComponentsCharMap[highChar][charCode]) { | ||
containsEncodedComponents = true | ||
for (let i = 1; i < path.length; i++) { | ||
const charCode = path.charCodeAt(i) | ||
|
||
if (charCode === 37) { | ||
const highCharCode = path.charCodeAt(i + 1) | ||
const lowCharCode = path.charCodeAt(i + 2) | ||
|
||
if (decodeComponentChar(highCharCode, lowCharCode) === null) { | ||
shouldDecode = true | ||
} else { | ||
highChar = undefined | ||
lowChar = undefined | ||
// %25 - encoded % char. We need to encode one more time to prevent double decoding | ||
if (highCharCode === 50 && lowCharCode === 53) { | ||
shouldDecode = true | ||
path = path.slice(0, i + 1) + '25' + path.slice(i + 1) | ||
i += 2 | ||
} | ||
i += 2 | ||
} | ||
} | ||
|
||
// Some systems do not follow RFC and separate the path and query | ||
// string with a `;` character (code 59), e.g. `/foo;jsessionid=123456`. | ||
// Thus, we need to split on `;` as well as `?` and `#`. | ||
if (charCode === 63 || charCode === 59 || charCode === 35) { | ||
originPath = url.slice(0, i) | ||
} else if (charCode === 63 || charCode === 59 || charCode === 35) { | ||
path = path.slice(0, i) | ||
break | ||
} else if (charCode === 37) { | ||
shouldDecode = true | ||
highChar = 0x00 | ||
} | ||
} | ||
const decoded = shouldDecode ? decodeURI(originPath) : originPath | ||
return shouldDecode ? decodeURI(path) : path | ||
} | ||
|
||
return { | ||
path: decoded, | ||
originPath, | ||
sliceParameter: containsEncodedComponents | ||
? sliceAndDecode | ||
: slicePath | ||
} | ||
function safeDecodeURIComponent (uriComponent, startIndex = 0) { | ||
let decoded = '' | ||
let lastIndex = startIndex | ||
|
||
function sliceAndDecode (from, to) { | ||
return (customDecoder || fastDecode)(this.originPath.slice(from, to)) | ||
} | ||
} | ||
for (let i = startIndex; i < uriComponent.length; i++) { | ||
if (uriComponent.charCodeAt(i) === 37) { | ||
const highCharCode = uriComponent.charCodeAt(i + 1) | ||
const lowCharCode = uriComponent.charCodeAt(i + 2) | ||
|
||
module.exports = sanitizeUrl | ||
const decodedChar = decodeComponentChar(highCharCode, lowCharCode) | ||
decoded += uriComponent.slice(lastIndex, i) + decodedChar | ||
|
||
function slicePath (from, to) { | ||
return this.path.slice(from, to) | ||
lastIndex = i + 3 | ||
} | ||
} | ||
return uriComponent.slice(0, startIndex) + decoded + uriComponent.slice(lastIndex) | ||
} | ||
|
||
module.exports = { safeDecodeURI, safeDecodeURIComponent } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const test = t.test | ||
const FindMyWay = require('..') | ||
|
||
test('Match static url without encoding option', t => { | ||
t.plan(2) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
const handler = () => {} | ||
|
||
findMyWay.on('GET', '/🍌', handler) | ||
|
||
t.same(findMyWay.find('GET', '/🍌').handler, handler) | ||
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C').handler, handler) | ||
}) | ||
|
||
test('Match parametric url with encoding option', t => { | ||
t.plan(2) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
findMyWay.on('GET', '/🍌/:param', () => {}) | ||
|
||
t.same(findMyWay.find('GET', '/🍌/@').params, { param: '@' }) | ||
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/@').params, { param: '@' }) | ||
}) | ||
|
||
test('Match encoded parametric url with encoding option', t => { | ||
t.plan(2) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
findMyWay.on('GET', '/🍌/:param', () => {}) | ||
|
||
t.same(findMyWay.find('GET', '/🍌/%23').params, { param: '#' }) | ||
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/%23').params, { param: '#' }) | ||
}) | ||
|
||
test('Decode url components', t => { | ||
t.plan(3) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
findMyWay.on('GET', '/:param1/:param2', () => {}) | ||
|
||
t.same(findMyWay.find('GET', '/foo%23bar/foo%23bar').params, { param1: 'foo#bar', param2: 'foo#bar' }) | ||
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/%F0%9F%8D%8C').params, { param1: '🍌', param2: '🍌' }) | ||
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/foo%23bar').params, { param1: '🍌', param2: 'foo#bar' }) | ||
}) | ||
|
||
test('Decode url components', t => { | ||
t.plan(5) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
findMyWay.on('GET', '/foo🍌bar/:param1/:param2', () => {}) | ||
findMyWay.on('GET', '/user/:id', () => {}) | ||
|
||
t.same(findMyWay.find('GET', '/foo%F0%9F%8D%8Cbar/foo%23bar/foo%23bar').params, { param1: 'foo#bar', param2: 'foo#bar' }) | ||
t.same(findMyWay.find('GET', '/user/maintainer+tomas').params, { id: 'maintainer+tomas' }) | ||
t.same(findMyWay.find('GET', '/user/maintainer%2Btomas').params, { id: 'maintainer+tomas' }) | ||
t.same(findMyWay.find('GET', '/user/maintainer%20tomas').params, { id: 'maintainer tomas' }) | ||
t.same(findMyWay.find('GET', '/user/maintainer%252Btomas').params, { id: 'maintainer%2Btomas' }) | ||
}) | ||
|
||
test('Decode url components', t => { | ||
t.plan(18) | ||
|
||
const findMyWay = FindMyWay() | ||
|
||
findMyWay.on('GET', '/:param1', () => {}) | ||
t.same(findMyWay.find('GET', '/foo%23bar').params, { param1: 'foo#bar' }) | ||
t.same(findMyWay.find('GET', '/foo%24bar').params, { param1: 'foo$bar' }) | ||
t.same(findMyWay.find('GET', '/foo%26bar').params, { param1: 'foo&bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2bbar').params, { param1: 'foo+bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2Bbar').params, { param1: 'foo+bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2cbar').params, { param1: 'foo,bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2Cbar').params, { param1: 'foo,bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2fbar').params, { param1: 'foo/bar' }) | ||
t.same(findMyWay.find('GET', '/foo%2Fbar').params, { param1: 'foo/bar' }) | ||
|
||
t.same(findMyWay.find('GET', '/foo%3abar').params, { param1: 'foo:bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3Abar').params, { param1: 'foo:bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3bbar').params, { param1: 'foo;bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3Bbar').params, { param1: 'foo;bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3dbar').params, { param1: 'foo=bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3Dbar').params, { param1: 'foo=bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3fbar').params, { param1: 'foo?bar' }) | ||
t.same(findMyWay.find('GET', '/foo%3Fbar').params, { param1: 'foo?bar' }) | ||
|
||
t.same(findMyWay.find('GET', '/foo%40bar').params, { param1: 'foo@bar' }) | ||
}) |