File tree Expand file tree Collapse file tree
swift/ql/src/queries/Security/CWE-730 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE qhelp PUBLIC
2+ "-//Semmle//qhelp//EN"
3+ "qhelp.dtd">
4+ <qhelp >
5+
6+ <overview >
7+ <p >
8+ Constructing a regular expression with unsanitized user input is dangerous,
9+ since a malicious user may be able to modify the meaning of the expression. In
10+ particular, such a user may be able to provide a regular expression fragment
11+ that takes exponential time in the worst case, and use that to perform a Denial
12+ of Service attack.
13+ </p >
14+ </overview >
15+
16+ <recommendation >
17+ <p >
18+ Before embedding user input into a regular expression, use a sanitization
19+ function such as <code >NSRegularExpression::escapedPattern(for:)</code > to escape
20+ meta-characters that have special meaning.
21+ </p >
22+ </recommendation >
23+
24+ <example >
25+ <p >
26+ The following examples construct regular expressions from user input without
27+ sanitizing it first:
28+ </p >
29+ <sample src =" RegexInjectionBad.swift" />
30+ <p >
31+ If user input is used to construct a regular expression it should be sanitized
32+ first. This ensures that the user cannot insert characters that have special
33+ meanings in regular expressions.
34+ </p >
35+ <sample src =" RegexInjectionGood.swift" />
36+ </example >
37+
38+ <references >
39+ <li >
40+ OWASP:
41+ <a href =" https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS" >Regular expression Denial of Service - ReDoS</a >.
42+ </li >
43+ <li >
44+ Wikipedia: <a href =" https://en.wikipedia.org/wiki/ReDoS" >ReDoS</a >.
45+ </li >
46+ <li >
47+ Swift: <a href =" https://developer.apple.com/documentation/foundation/nsregularexpression/1408386-escapedpattern" >NSRegularExpression.escapedPattern(for:)</a >.
48+ </li >
49+ </references >
50+ </qhelp >
Original file line number Diff line number Diff line change 1+ func processRemoteInput( remoteInput: String ) {
2+ ...
3+
4+ # BAD: Unsanitized user input is used to construct a regular expression
5+ let regex1 = try Regex ( remoteInput)
6+
7+ # BAD: Unsanitized user input is used to construct a regular expression
8+ let regexStr = " abc| \( remoteInput) "
9+ let regex2 = try NSRegularExpression ( pattern: regexStr)
10+
11+ ...
12+ }
Original file line number Diff line number Diff line change 1+ func processRemoteInput( remoteInput: String ) {
2+ ...
3+
4+ # GOOD: Regular expression is not derived from user input
5+ let regex1 = try Regex ( myRegex)
6+
7+ # GOOD: Sanitized user input is used to construct a regular expression
8+ let escapedInput = NSRegularExpression . escapedPattern ( for: remoteInput)
9+ let regexStr = " abc| \( escapedInput) "
10+ let regex2 = try NSRegularExpression ( pattern: regexStr)
11+
12+ ...
13+ }
You can’t perform that action at this time.
0 commit comments