Skip to content

Commit

Permalink
Merge commit from fork
Browse files Browse the repository at this point in the history
* Fix backtracking protection

* Add test
  • Loading branch information
blakeembrey authored Dec 5, 2024
1 parent 0c71192 commit f01c26a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,26 @@ function pathToRegexp(path, keys, options) {
path = path.replace(
/\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
function (match, slash, format, key, capture, star, optional, offset) {
pos = offset + match.length;

if (match[0] === '\\') {
backtrack += match;
pos += 2;
return match;
}

if (match === '.') {
backtrack += '\\.';
extraOffset += 1;
pos += 1;
return '\\.';
}

backtrack = slash || format ? '' : path.slice(pos, offset);
if (slash || format) {
backtrack = '';
} else {
backtrack += path.slice(pos, offset);
}

pos = offset + match.length;

if (match === '*') {
extraOffset += 3;
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ describe('path-to-regexp', function () {
}, /path must be a string, array of strings, or regular expression/);
});

it('should generate a regex without backtracking', function () {
assert.deepEqual(pathToRegExp('/:a-:b'), /^(?:\/([^/]+?))-(?:((?:(?!\/|-).)+?))\/?$/i);
});

describe('strings', function () {
it('should match simple paths', function () {
var params = [];
Expand Down

0 comments on commit f01c26a

Please sign in to comment.