Draft
C++ regex parse tree and RegexTreeViewSig implementation (Phase 1 of ReDoS support)#22200
Conversation
Copilot
AI
changed the title
[WIP] Add C++ regular-expression parse tree foundation
C++ regex parse tree and RegexTreeViewSig implementation (Phase 1 of ReDoS support)
Jul 15, 2026
Copilot stopped work on behalf of
jketema due to an error
July 16, 2026 13:10
| */ | ||
|
|
||
| import cpp | ||
| private import semmle.code.cpp.dataflow.new.DataFlow |
| private import cpp | ||
| private import semmle.code.cpp.regex.RegexTreeView::RegexTreeView as TreeView | ||
| import codeql.regex.nfa.SuperlinearBackTracking::Make<TreeView> as SuperlinearBackTracking | ||
| private import semmle.code.cpp.ir.dataflow.DataFlow |
|
|
||
| // Instantiate the shared analysis modules with our RegexTreeView. | ||
| // If RegexTreeView does not satisfy RegexTreeViewSig, these lines cause a compile error. | ||
| private module TestSuperlinear = SuperlinearBackTracking::Make<RegexTreeView>; |
| // If RegexTreeView does not satisfy RegexTreeViewSig, these lines cause a compile error. | ||
| private module TestSuperlinear = SuperlinearBackTracking::Make<RegexTreeView>; | ||
|
|
||
| private module TestNfaUtils = NfaUtils::Make<RegexTreeView>; |
| @@ -0,0 +1 @@ | |||
| query: Security/CWE/CWE-1333/ReDoS.ql | |||
| @@ -0,0 +1 @@ | |||
| query: Security/CWE/CWE-1333/PolynomialReDoS.ql | |||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
C++ (
codeql/cpp-all) has no regex parse tree and noRegexTreeViewSigimplementation, blocking instantiation of the sharedcodeql/regexReDoS engines. This PR adds the self-contained foundation — parser + tree view — with no queries and no dataflow (Phase 2).New:
ParseRegExp.qllcpp/ql/lib/semmle/code/cpp/regex/internal/ParseRegExp.qllclass RegExp extends StringLiteralproviding offset-span predicates for the full ECMAScript construct set:*,+,?,{n},{n,m},{n,}(greedy and lazy)(...),(?:...),(?<name>...),(?=...),(?!...),(?<=...),(?<!...).,^,$,\b,\B)\1and ECMAScript named\k<name>\n,\r,\t,\xhh,\uhhhh,\u{...}, identity escapesECMAScript semantics throughout (targeting
std::regex's default mode). Phase 1 conservatively treats allStringLiterals as candidate regexes; Phase 2 will restrict viastd::regexconstruction-site dataflow.New:
RegexTreeView.qllcpp/ql/lib/semmle/code/cpp/regex/RegexTreeView.qllprivate newtype TRegExpParentwith 9 variants +module Impl implements RegexTreeViewSig:RegExpQuantifier/RegExpStar/RegExpPlus/RegExpOpt/RegExpRange,RegExpAlt,RegExpSequence,RegExpGroup,RegExpCharacterClass/RegExpCharacterRange,RegExpNormalChar/RegExpEscape/RegExpCharacterClassEscape,RegExpSpecialChar,RegExpDot/RegExpAnchor/RegExpCaret/RegExpDollar/RegExpWordBoundary/RegExpNonWordBoundary,RegExpSubPattern/lookahead/lookbehind variants,RegExpBackRef,RegExpConstantisPossessive → none(),matchesAnyPrefix/AnySuffix → any(),isIgnoreCase/isDotAll → none()(conservative; TODO for Phase 2 construction-site flag detection)hasLocationInfomaps term offsets back to source columns within the enclosingStringLiteralimport Impl as RegexTreeView(functor argument) andimport Impl(direct use)Compile check
cpp/ql/test/library-tests/regex/CompileCheck.qlinstantiates bothSuperlinearBackTracking::Make<RegexTreeView>andNfaUtils::Make<RegexTreeView>to verify the signature is satisfied end-to-end.Tests
cpp/ql/test/library-tests/regex/— test source covering quantifiers, nested quantifiers (relevant for ReDoS:(a+)+), named groups, lookahead/lookbehind, backreferences, anchors, character classes; plus parse-tree printer (parse.ql), table query (regexp.ql), andfailedToParseconsistency check (Consistency.ql).Packaging
cpp/ql/lib/qlpack.yml— addscodeql/regex: ${workspace}dependency.