|
| 1 | +// Minimal std::regex/std::basic_string stubs, mirroring the ones in |
| 2 | +// cpp/ql/test/library-tests/regex/test.cpp. Real headers are not available |
| 3 | +// in the extractor sandbox. |
| 4 | + |
| 5 | +namespace std { |
| 6 | + |
| 7 | +template <class CharT> |
| 8 | +class basic_string { |
| 9 | +public: |
| 10 | + basic_string() {} |
| 11 | + basic_string(const CharT*) {} |
| 12 | + const CharT* c_str() const { return 0; } |
| 13 | + const CharT* data() const { return 0; } |
| 14 | + unsigned long size() const { return 0; } |
| 15 | +}; |
| 16 | + |
| 17 | +typedef basic_string<char> string; |
| 18 | + |
| 19 | +namespace regex_constants { |
| 20 | + enum syntax_option_type { |
| 21 | + ECMAScript = 1, basic = 2, extended = 4, awk = 8, grep = 16, egrep = 32, |
| 22 | + icase = 256, nosubs = 512, optimize = 1024, collate = 2048, multiline = 4096 |
| 23 | + }; |
| 24 | + enum match_flag_type { match_default = 0 }; |
| 25 | +} |
| 26 | + |
| 27 | +template <class CharT> |
| 28 | +class basic_regex { |
| 29 | +public: |
| 30 | + typedef regex_constants::syntax_option_type flag_type; |
| 31 | + basic_regex(const CharT* p) {} |
| 32 | + basic_regex(const CharT* p, flag_type f) {} |
| 33 | + basic_regex(const basic_string<CharT>& p) {} |
| 34 | +}; |
| 35 | + |
| 36 | +typedef basic_regex<char> regex; |
| 37 | + |
| 38 | +template <class CharT> |
| 39 | +bool regex_match(const basic_string<CharT>& s, const basic_regex<CharT>& re) { return false; } |
| 40 | +template <class CharT> |
| 41 | +bool regex_search(const basic_string<CharT>& s, const basic_regex<CharT>& re) { return false; } |
| 42 | +template <class CharT> |
| 43 | +basic_string<CharT> regex_replace(const basic_string<CharT>& s, const basic_regex<CharT>& re, |
| 44 | + const basic_string<CharT>& fmt) { return basic_string<CharT>(); } |
| 45 | + |
| 46 | +} // namespace std |
| 47 | + |
| 48 | +// ----------------------------------------------------------------------------- |
| 49 | +// Tests |
| 50 | +// |
| 51 | +// The exponential ReDoS query does not require a dataflow path from a |
| 52 | +// user-controlled source: the vulnerable regex term itself is the alert. |
| 53 | +// ----------------------------------------------------------------------------- |
| 54 | + |
| 55 | +void test_exp_redos(const std::string& input) { |
| 56 | + // BAD: classic nested-quantifier pattern with exponential backtracking. |
| 57 | + { |
| 58 | + std::regex re("^(a+)+$"); |
| 59 | + std::regex_match(input, re); |
| 60 | + } |
| 61 | + |
| 62 | + // BAD: alternation of identical branches inside a repetition. |
| 63 | + { |
| 64 | + std::regex re("^(a|a)*$"); |
| 65 | + std::regex_match(input, re); |
| 66 | + } |
| 67 | + |
| 68 | + // BAD: alternation with overlapping branches inside a repetition. |
| 69 | + { |
| 70 | + std::regex re("^(a|ab)*$"); |
| 71 | + std::regex_match(input, re); |
| 72 | + } |
| 73 | + |
| 74 | + // GOOD: a linear pattern. |
| 75 | + { |
| 76 | + std::regex re("^abc.*$"); |
| 77 | + std::regex_match(input, re); |
| 78 | + } |
| 79 | + |
| 80 | + // GOOD: a polynomial pattern -- reported by cpp/polynomial-redos when |
| 81 | + // reached by user input, but not by the exponential query. |
| 82 | + { |
| 83 | + std::regex re("^\\s+|\\s+$"); |
| 84 | + std::regex_replace(input, re, std::string("")); |
| 85 | + } |
| 86 | + |
| 87 | + // GOOD: the pattern is not used as an ECMAScript std::regex (basic |
| 88 | + // grammar is not modeled), so the parser does not consider it a |
| 89 | + // RegExp at all. |
| 90 | + { |
| 91 | + std::regex re("^(a+)+$", std::regex_constants::basic); |
| 92 | + std::regex_match(input, re); |
| 93 | + } |
| 94 | +} |
0 commit comments