Skip to content

Commit

Permalink
Don't crash on invalid uri query parameters. (#3453)
Browse files Browse the repository at this point in the history
* Don't crash on invalid uri query parameters

* Add try/catch at matchPattern function in matchRoutes.js and testing.

* Fix test 'handles error that are not valid URI character'
  • Loading branch information
bae-unidev authored and timdorr committed May 17, 2016
1 parent af84da2 commit 3691914
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions modules/PatternUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function compilePattern(pattern) {
* - ** Consumes (greedy) all characters up to the next character
* in the pattern, or to the end of the URL if there is none
*
* The function calls callback(error, matched) when finished.
* The return value is an object with the following properties:
*
* - remainingPathname
Expand Down
13 changes: 13 additions & 0 deletions modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ describe('Router', function () {
})
})

it('handles error that are not valid URI character', function (done) {
const errorSpy = expect.createSpy()

render((
<Router history={createHistory('/%')} onError={errorSpy}>
<Route path="*" />
</Router>
), node, function () {
expect(errorSpy).toHaveBeenCalled()
done()
})
})

})

describe('render prop', function () {
Expand Down
18 changes: 11 additions & 7 deletions modules/matchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ function matchRouteDeep(
// Only try to match the path if the route actually has a pattern, and if
// we're not just searching for potential nested absolute paths.
if (remainingPathname !== null && pattern) {
const matched = matchPattern(pattern, remainingPathname)
if (matched) {
remainingPathname = matched.remainingPathname
paramNames = [ ...paramNames, ...matched.paramNames ]
paramValues = [ ...paramValues, ...matched.paramValues ]
} else {
remainingPathname = null
try {
const matched = matchPattern(pattern, remainingPathname)
if (matched) {
remainingPathname = matched.remainingPathname
paramNames = [ ...paramNames, ...matched.paramNames ]
paramValues = [ ...paramValues, ...matched.paramValues ]
} else {
remainingPathname = null
}
} catch (error) {
callback(error)
}

// By assumption, pattern is non-empty here, which is the prerequisite for
Expand Down

0 comments on commit 3691914

Please sign in to comment.