Skip to content

Commit 1a51f0c

Browse files
committed
Ruby: regex: fix getGroupNumber
non-capture groups should not have a group number
1 parent 6b323ee commit 1a51f0c

File tree

3 files changed

+3
-5
lines changed

3 files changed

+3
-5
lines changed

ruby/ql/lib/codeql/ruby/security/performance/ParseRegExp.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ abstract class RegExp extends AST::StringlikeLiteral {
480480
/** Gets the number of the group in start,end */
481481
int getGroupNumber(int start, int end) {
482482
this.group(start, end) and
483+
not this.nonCapturingGroupStart(start, _) and
483484
result =
484485
count(int i | this.group(i, _) and i < start and not this.nonCapturingGroupStart(i, _)) + 1
485486
}
@@ -583,7 +584,7 @@ abstract class RegExp extends AST::StringlikeLiteral {
583584
private predicate nonCapturingGroupStart(int start, int end) {
584585
this.isGroupStart(start) and
585586
this.getChar(start + 1) = "?" and
586-
this.getChar(start + 2) = ":" and
587+
this.getChar(start + 2) = [":", "=", "<", "!", "#"] and
587588
end = start + 3
588589
}
589590

ruby/ql/test/library-tests/regexp/regexp.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ groupNumber
66
| regexp.rb:46:2:46:6 | (foo) | 1 |
77
| regexp.rb:47:4:47:8 | (o\|b) | 1 |
88
| regexp.rb:48:2:48:9 | (a\|b\|cd) | 1 |
9-
| regexp.rb:49:2:49:7 | (?::+) | 1 |
10-
| regexp.rb:52:2:52:11 | (?<id>\\w+) | 1 |
119
| regexp.rb:53:2:53:12 | (?'foo'fo+) | 1 |
1210
| regexp.rb:56:2:56:5 | (a+) | 1 |
13-
| regexp.rb:57:2:57:11 | (?<qux>q+) | 1 |
1411
term
1512
| regexp.rb:5:2:5:4 | abc | RegExpConstant,RegExpNormalChar |
1613
| regexp.rb:8:2:8:2 | a | RegExpConstant,RegExpNormalChar |

ruby/ql/test/query-tests/security/cwe-116/BadTagFilter.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
| test.rb:15:6:15:39 | <script[^>]*?>[\\s\\S]*?<\\/script.*> | This regular expression does not match script end tags like </script\\t\\n bar>. |
1212
| test.rb:17:6:17:40 | <script\\b[^>]*>([\\s\\S]*?)<\\/script> | This regular expression does not match script end tags like </script >. |
1313
| test.rb:18:6:18:48 | <(?:!--([\\S\|\\s]*?)-->)\|([^\\/\\s>]+)[\\S\\s]*?> | Comments ending with --> are matched differently from comments ending with --!>. The first is matched with capture group 1 and comments ending with --!> are matched with capture group 2. |
14-
| test.rb:19:6:19:147 | <(?:(?:\\/([^>]+)>)\|(?:!--([\\S\|\\s]*?)-->)\|(?:([^\\/\\s>]+)((?:\\s+[\\w\\-:.]+(?:\\s*=\\s*?(?:(?:"[^"]*")\|(?:'[^']*')\|[^\\s"'\\/>]+))?)*)[\\S\\s]*?(\\/?)>)) | Comments ending with --> are matched differently from comments ending with --!>. The first is matched with capture group 2 and comments ending with --!> are matched with capture group 1, 3, 4, 5. |
14+
| test.rb:19:6:19:147 | <(?:(?:\\/([^>]+)>)\|(?:!--([\\S\|\\s]*?)-->)\|(?:([^\\/\\s>]+)((?:\\s+[\\w\\-:.]+(?:\\s*=\\s*?(?:(?:"[^"]*")\|(?:'[^']*')\|[^\\s"'\\/>]+))?)*)[\\S\\s]*?(\\/?)>)) | Comments ending with --> are matched differently from comments ending with --!>. The first is matched with capture group 2 and comments ending with --!> are matched with capture group 3, 4. |

0 commit comments

Comments
 (0)