Skip to content

Commit 3691914

Browse files
bae-unidevtimdorr
authored andcommitted
Don't crash on invalid uri query parameters. (#3453)
* 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'
1 parent af84da2 commit 3691914

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

modules/PatternUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function compilePattern(pattern) {
7171
* - ** Consumes (greedy) all characters up to the next character
7272
* in the pattern, or to the end of the URL if there is none
7373
*
74+
* The function calls callback(error, matched) when finished.
7475
* The return value is an object with the following properties:
7576
*
7677
* - remainingPathname

modules/__tests__/Router-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ describe('Router', function () {
276276
})
277277
})
278278

279+
it('handles error that are not valid URI character', function (done) {
280+
const errorSpy = expect.createSpy()
281+
282+
render((
283+
<Router history={createHistory('/%')} onError={errorSpy}>
284+
<Route path="*" />
285+
</Router>
286+
), node, function () {
287+
expect(errorSpy).toHaveBeenCalled()
288+
done()
289+
})
290+
})
291+
279292
})
280293

281294
describe('render prop', function () {

modules/matchRoutes.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ function matchRouteDeep(
8888
// Only try to match the path if the route actually has a pattern, and if
8989
// we're not just searching for potential nested absolute paths.
9090
if (remainingPathname !== null && pattern) {
91-
const matched = matchPattern(pattern, remainingPathname)
92-
if (matched) {
93-
remainingPathname = matched.remainingPathname
94-
paramNames = [ ...paramNames, ...matched.paramNames ]
95-
paramValues = [ ...paramValues, ...matched.paramValues ]
96-
} else {
97-
remainingPathname = null
91+
try {
92+
const matched = matchPattern(pattern, remainingPathname)
93+
if (matched) {
94+
remainingPathname = matched.remainingPathname
95+
paramNames = [ ...paramNames, ...matched.paramNames ]
96+
paramValues = [ ...paramValues, ...matched.paramValues ]
97+
} else {
98+
remainingPathname = null
99+
}
100+
} catch (error) {
101+
callback(error)
98102
}
99103

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

0 commit comments

Comments
 (0)