Skip to content

Commit

Permalink
feat: disable semicolon separator by default (#336)
Browse files Browse the repository at this point in the history
* feat: disable semicolon separator by default

* lazy evaluation

* upd readme
  • Loading branch information
levensta committed Dec 26, 2023
1 parent f86552b commit 0b44ecc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ router.on('GET', '/', (req, res, params, store, searchParams) => {
router.lookup({ method: 'GET', url: '/?foo=bar&baz=faz' }, null)
```

According to [RFC3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.4), find-my-way separates path and query string with `?` character. But earlier versions also used `;` as delimiter character. To support this behaviour, add the `useSemicolonDelimiter` option to `true`:

```js
const router = require('find-my-way')({
useSemicolonDelimiter: true
})
```

You can assign a `buildPrettyMeta` function to sanitize a route's `store` object to use with the `prettyPrint` functions. This function should accept a single object and return an object.

```js
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function Router (opts) {
this.maxParamLength = opts.maxParamLength || 100
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
this.constrainer = new Constrainer(opts.constraints)
this.useSemicolonDelimiter = opts.useSemicolonDelimiter || false

this.routes = []
this.trees = {}
Expand Down Expand Up @@ -569,7 +570,7 @@ Router.prototype.find = function find (method, path, derivedConstraints) {
let shouldDecodeParam

try {
sanitizedUrl = safeDecodeURI(path)
sanitizedUrl = safeDecodeURI(path, this.useSemicolonDelimiter)
path = sanitizedUrl.path
querystring = sanitizedUrl.querystring
shouldDecodeParam = sanitizedUrl.shouldDecodeParam
Expand Down
6 changes: 3 additions & 3 deletions lib/url-sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function decodeComponentChar (highCharCode, lowCharCode) {
return null
}

function safeDecodeURI (path) {
function safeDecodeURI (path, useSemicolonDelimiter) {
let shouldDecode = false
let shouldDecodeParam = false

Expand All @@ -61,8 +61,8 @@ function safeDecodeURI (path) {
}
// 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 `#`.
} else if (charCode === 63 || charCode === 59 || charCode === 35) {
// Thus, we need to split on `;` as well as `?` and `#` if the useSemicolonDelimiter option is enabled.
} else if (charCode === 63 || charCode === 35 || (charCode === 59 && useSemicolonDelimiter)) {
querystring = path.slice(i + 1)
path = path.slice(0, i)
break
Expand Down
18 changes: 16 additions & 2 deletions test/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ test('should sanitize the url - hash', t => {
findMyWay.lookup({ method: 'GET', url: '/test#hello', headers: {} }, null)
})

test('handles path and query separated by ;', t => {
test('handles path and query separated by ; with useSemicolonDelimiter enabled', t => {
t.plan(2)
const findMyWay = FindMyWay()
const findMyWay = FindMyWay({
useSemicolonDelimiter: true
})

findMyWay.on('GET', '/test', (req, res, params, store, query) => {
t.same(query, { jsessionid: '123456' })
Expand All @@ -39,3 +41,15 @@ test('handles path and query separated by ;', t => {

findMyWay.lookup({ method: 'GET', url: '/test;jsessionid=123456', headers: {} }, null)
})

test('handles path and query separated by ? using ; in the path', t => {
t.plan(2)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/test;jsessionid=123456', (req, res, params, store, query) => {
t.same(query, { foo: 'bar' })
t.ok('inside the handler')
})

findMyWay.lookup({ method: 'GET', url: '/test;jsessionid=123456?foo=bar', headers: {} }, null)
})

0 comments on commit 0b44ecc

Please sign in to comment.