Skip to content

Commit 355793f

Browse files
committed
Swift: Add support for \u{hhhhhh} escaped characters in regular expressions.
1 parent 49dfe5d commit 355793f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

swift/ql/lib/codeql/swift/regex/internal/ParseRegex.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ abstract class RegExp extends Expr {
386386
// wide hex char \Uhhhhhhhh
387387
this.getChar(start + 1) = "U" and end = start + 10
388388
or
389+
// variable width hex char \x{hh...} or \u{hh...} (1-6 digits)
390+
this.getChar(start + 1) = ["x", "u"] and
391+
this.getChar(start + 2) = "{" and
392+
this.getChar(end - 1) = "}" and
393+
end > start and
394+
end <= start + 10 and
395+
not exists(int i | start + 2 < i and i < end - 1 | this.getChar(i) = "}")
396+
or
389397
// escape not handled above; update when adding a new case
390398
not this.getChar(start + 1) in ["x", "u", "U"] and
391399
not exists(this.getChar(start + 1).toInt()) and

swift/ql/test/library-tests/regex/redos_variants.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,10 @@ func myRegexpVariantsTests(myUrl: URL) throws {
516516

517517
// BAD TODO: we should get this one
518518
// attack string: "X" + "a" x lots
519-
_ = try Regex(#"X(\x{061}|a)*Y"#).firstMatch(in: tainted) // $ hasParseFailure= MISSING: redos-vulnerable=
519+
_ = try Regex(#"X(\x{061}|a)*Y"#).firstMatch(in: tainted) // $ MISSING: redos-vulnerable=
520520

521521
// GOOD
522-
_ = try Regex(#"X(\x{061}|b)+Y"#).firstMatch(in: tainted) // $ hasParseFailure
522+
_ = try Regex(#"X(\x{061}|b)+Y"#).firstMatch(in: tainted)
523523

524524
// BAD
525525
// attack string: "X" + "7" x lots

0 commit comments

Comments
 (0)