@@ -38,6 +38,8 @@ const { safeDecodeURI, safeDecodeURIComponent } = require('./lib/url-sanitizer')
38
38
39
39
const FULL_PATH_REGEXP = / ^ h t t p s ? : \/ \/ .* ?\/ /
40
40
const OPTIONAL_PARAM_REGEXP = / ( \/ : [ ^ / ( ) ] * ?) \? ( \/ ? ) /
41
+ const ESCAPE_REGEXP = / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g
42
+ const REMOVE_DUPLICATE_SLASHES_REGEXP = / \/ \/ + / g
41
43
42
44
if ( ! isRegexSafe ( FULL_PATH_REGEXP ) ) {
43
45
throw new Error ( 'the FULL_PATH_REGEXP is not safe, update this module' )
@@ -47,6 +49,14 @@ if (!isRegexSafe(OPTIONAL_PARAM_REGEXP)) {
47
49
throw new Error ( 'the OPTIONAL_PARAM_REGEXP is not safe, update this module' )
48
50
}
49
51
52
+ if ( ! isRegexSafe ( ESCAPE_REGEXP ) ) {
53
+ throw new Error ( 'the ESCAPE_REGEXP is not safe, update this module' )
54
+ }
55
+
56
+ if ( ! isRegexSafe ( REMOVE_DUPLICATE_SLASHES_REGEXP ) ) {
57
+ throw new Error ( 'the REMOVE_DUPLICATE_SLASHES_REGEXP is not safe, update this module' )
58
+ }
59
+
50
60
function Router ( opts ) {
51
61
if ( ! ( this instanceof Router ) ) {
52
62
return new Router ( opts )
@@ -755,11 +765,11 @@ Router.prototype.all = function (path, handler, store) {
755
765
module . exports = Router
756
766
757
767
function escapeRegExp ( string ) {
758
- return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g , '\\$&' )
768
+ return string . replace ( ESCAPE_REGEXP , '\\$&' )
759
769
}
760
770
761
771
function removeDuplicateSlashes ( path ) {
762
- return path . replace ( / \/ \/ + / g , '/' )
772
+ return path . indexOf ( '//' ) !== - 1 ? path . replace ( REMOVE_DUPLICATE_SLASHES_REGEXP , '/' ) : path
763
773
}
764
774
765
775
function trimLastSlash ( path ) {
0 commit comments