Skip to content

Commit

Permalink
disallow quantifiable anchors in unicode mode (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Nov 28, 2023
1 parent c622da7 commit 5517b6c
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
15 changes: 15 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,21 @@

quantifier = parseQuantifier() || false;
if (quantifier) {
var type = anchorOrAtom.type, behavior = anchorOrAtom.behavior;
if (
type === "group" &&
(behavior === "negativeLookbehind" ||
behavior === "lookbehind" ||
(isUnicodeMode &&
(behavior === "negativeLookahead" || behavior === "lookahead")))
) {
bail(
"Invalid quantifier",
"",
quantifier.range[0],
quantifier.range[1]
);
}
quantifier.body = flattenBody(anchorOrAtom);
// The quantifier contains the atom. Therefore, the beginning of the
// quantifier range is given by the beginning of the atom.
Expand Down
12 changes: 12 additions & 0 deletions test/test-data-lookbehind.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,17 @@
"name": "SyntaxError",
"message": "atomEscape at position 1\n \\k\n ^",
"input": "\\k"
},
"(?<=.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n (?<=.){2,3}\n ^",
"input": "(?<=.){2,3}"
},
"(?<!.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n (?<!.){2,3}\n ^",
"input": "(?<!.){2,3}"
}
}
12 changes: 12 additions & 0 deletions test/test-data-unicode-set.json
Original file line number Diff line number Diff line change
Expand Up @@ -1561,5 +1561,17 @@
21
],
"raw": "[\\u{14630}-\\u{14633}]"
},
".(?=.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n .(?=.){2,3}\n ^",
"input": ".(?=.){2,3}"
},
".(?!.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n .(?!.){2,3}\n ^",
"input": ".(?!.){2,3}"
}
}
12 changes: 12 additions & 0 deletions test/test-data-unicode.json
Original file line number Diff line number Diff line change
Expand Up @@ -1228,5 +1228,17 @@
"name": "SyntaxError",
"message": "Invalid decimal escape in unicode mode at position 1\n \\2(.)\n ^",
"input": "\\2(.)"
},
".(?=.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n .(?=.){2,3}\n ^",
"input": ".(?=.){2,3}"
},
".(?!.){2,3}": {
"type": "error",
"name": "SyntaxError",
"message": "Invalid quantifier at position 6\n .(?!.){2,3}\n ^",
"input": ".(?!.){2,3}"
}
}
102 changes: 102 additions & 0 deletions test/test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -38524,5 +38524,107 @@
6
],
"raw": "(?=a)?"
},
".(?=.){2,3}": {
"type": "alternative",
"body": [
{
"type": "dot",
"range": [
0,
1
],
"raw": "."
},
{
"type": "quantifier",
"min": 2,
"max": 3,
"greedy": true,
"body": [
{
"type": "group",
"behavior": "lookahead",
"body": [
{
"type": "dot",
"range": [
4,
5
],
"raw": "."
}
],
"range": [
1,
6
],
"raw": "(?=.)"
}
],
"symbol": null,
"range": [
1,
11
],
"raw": "(?=.){2,3}"
}
],
"range": [
0,
11
],
"raw": ".(?=.){2,3}"
},
".(?!.){2,3}": {
"type": "alternative",
"body": [
{
"type": "dot",
"range": [
0,
1
],
"raw": "."
},
{
"type": "quantifier",
"min": 2,
"max": 3,
"greedy": true,
"body": [
{
"type": "group",
"behavior": "negativeLookahead",
"body": [
{
"type": "dot",
"range": [
4,
5
],
"raw": "."
}
],
"range": [
1,
6
],
"raw": "(?!.)"
}
],
"symbol": null,
"range": [
1,
11
],
"raw": "(?!.){2,3}"
}
],
"range": [
0,
11
],
"raw": ".(?!.){2,3}"
}
}

0 comments on commit 5517b6c

Please sign in to comment.