Skip to content

Commit 9dcc2b0

Browse files
committed
changed instances of \s* with \s? in regular expressions to reduce the risk of potential catastrophic backtracking vulnerability (audit courtesy of James Davis)
1 parent 4d6967c commit 9dcc2b0

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Diff for: src/nwmatcher-noqsa.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@
7676
Optimize, identifier, extensions = '.+',
7777

7878
Patterns = {
79-
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,
80-
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|(?:matches|not)\(\s*(:nth(?:-last)?(?:-child|-of-type)\(\s*(?:even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\)|[^()]*)\s*\))?(.*)/i,
79+
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s?(even|odd|(?:[-+]{0,1}\d*n\s?)?[-+]{0,1}\s?\d*)\s?\))?(.*)/i,
80+
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|(?:matches|not)\(\s?(:nth(?:-last)?(?:-child|-of-type)\(\s?(?:even|odd|(?:[-+]{0,1}\d*n\s?)?[-+]{0,1}\s?\d*)\s?\)|[^()]*)\s?\))?(.*)/i,
8181
epseudos: /^((?:[:]{1,2}(?:after|before|first-letter|first-line))|(?:[:]{2,2}(?:selection|backdrop|placeholder)))?(.*)/i,
82-
children: RegExp('^' + whitespace + '*\\>' + whitespace + '*(.*)'),
83-
adjacent: RegExp('^' + whitespace + '*\\+' + whitespace + '*(.*)'),
84-
relative: RegExp('^' + whitespace + '*\\~' + whitespace + '*(.*)'),
82+
children: RegExp('^' + whitespace + '?\\>' + whitespace + '?(.*)'),
83+
adjacent: RegExp('^' + whitespace + '?\\+' + whitespace + '?(.*)'),
84+
relative: RegExp('^' + whitespace + '?\\~' + whitespace + '?(.*)'),
8585
ancestor: RegExp('^' + whitespace + '+(.*)'),
8686
universal: RegExp('^\\*(.*)')
8787
},

Diff for: src/nwmatcher.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@
121121
// precompiled Regular Expressions
122122
Patterns = {
123123
// structural pseudo-classes and child selectors
124-
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,
124+
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s?(even|odd|(?:[-+]{0,1}\d*n\s?)?[-+]{0,1}\s?\d*)\s?\))?(.*)/i,
125125
// uistates + dynamic + negation pseudo-classes
126-
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|(?:matches|not)\(\s*(:nth(?:-last)?(?:-child|-of-type)\(\s*(?:even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\)|[^()]*)\s*\))?(.*)/i,
126+
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|(?:matches|not)\(\s?(:nth(?:-last)?(?:-child|-of-type)\(\s?(?:even|odd|(?:[-+]{0,1}\d*n\s?)?[-+]{0,1}\s?\d*)\s?\)|[^()]*)\s?\))?(.*)/i,
127127
// pseudo-elements selectors
128128
epseudos: /^((?:[:]{1,2}(?:after|before|first-letter|first-line))|(?:[:]{2,2}(?:selection|backdrop|placeholder)))?(.*)/i,
129129
// E > F
130-
children: RegExp('^' + whitespace + '*\\>' + whitespace + '*(.*)'),
130+
children: RegExp('^' + whitespace + '?\\>' + whitespace + '?(.*)'),
131131
// E + F
132-
adjacent: RegExp('^' + whitespace + '*\\+' + whitespace + '*(.*)'),
132+
adjacent: RegExp('^' + whitespace + '?\\+' + whitespace + '?(.*)'),
133133
// E ~ F
134-
relative: RegExp('^' + whitespace + '*\\~' + whitespace + '*(.*)'),
134+
relative: RegExp('^' + whitespace + '?\\~' + whitespace + '?(.*)'),
135135
// E F
136136
ancestor: RegExp('^' + whitespace + '+(.*)'),
137137
// all

0 commit comments

Comments
 (0)