Skip to content

Commit 06f5545

Browse files
author
Mark Shannon
authored
Merge pull request #1798 from taus-semmle/python-regex-support-short-mode-flags
Python: Support short mode flags (e.g. `re.M`) in regexes. (ODASA-8056)
2 parents cf24c9f + f9c002e commit 06f5545

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

python/ql/src/semmle/python/types/Extensions.qll

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ class BottleRoutePointToExtension extends PointsToExtension {
128128

129129
/* Python 3.6+ regex module constants */
130130

131+
string short_flag(string flag) {
132+
(flag = "ASCII" or
133+
flag = "IGNORECASE" or
134+
flag = "LOCALE" or
135+
flag = "UNICODE" or
136+
flag = "MULTILINE" or
137+
flag = "TEMPLATE")
138+
and result = flag.prefix(1)
139+
or
140+
flag = "DOTALL" and result = "S"
141+
or
142+
flag = "VERBOSE" and result = "X"
143+
}
144+
131145
class ReModulePointToExtension extends PointsToExtension {
132146

133147
string name;
@@ -139,9 +153,10 @@ class ReModulePointToExtension extends PointsToExtension {
139153
}
140154

141155
override predicate pointsTo(Context context, ObjectInternal value, ControlFlowNode origin) {
142-
exists(ModuleObjectInternal sre_constants, CfgOrigin orig |
156+
exists(ModuleObjectInternal sre_constants, CfgOrigin orig, string flag |
157+
(name = flag or name = short_flag(flag)) and
143158
sre_constants.getName() = "sre_constants" and
144-
sre_constants.attribute("SRE_FLAG_" + name, value, orig) and
159+
sre_constants.attribute("SRE_FLAG_" + flag, value, orig) and
145160
origin = orig.asCfgNodeOrHere(this)
146161
)
147162
and pointsTo_helper(context)

python/ql/test/library-tests/regex/Mode.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
| 50 | VERBOSE |
88
| 51 | UNICODE |
99
| 52 | UNICODE |
10+
| 64 | MULTILINE |

python/ql/test/library-tests/regex/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@
6060

6161
#Misparsed on LGTM
6262
re.compile(r"\[(?P<txt>[^[]*)\]\((?P<uri>[^)]*)")
63+
64+
re.compile("", re.M) # ODASA-8056

0 commit comments

Comments
 (0)