@@ -10,20 +10,33 @@ private import semmle.python.dataflow.new.RemoteFlowSources
10
10
private import experimental.semmle.python.Concepts
11
11
private import semmle.python.ApiGraphs
12
12
13
- /** Provides models for the Python standard library. */
13
+ /**
14
+ * Provides models for Python's `re` library.
15
+ *
16
+ * See https://docs.python.org/3/library/re.html
17
+ */
14
18
private module Re {
15
- /** List of re methods. */
16
- private class ReMethods extends string {
17
- ReMethods ( ) {
19
+ /**
20
+ * List of `re` methods immediately executing an expression.
21
+ *
22
+ * See https://docs.python.org/3/library/re.html#module-contents
23
+ */
24
+ private class RegexExecutionMethods extends string {
25
+ RegexExecutionMethods ( ) {
18
26
this in [ "match" , "fullmatch" , "search" , "split" , "findall" , "finditer" , "sub" , "subn" ]
19
27
}
20
28
}
21
29
30
+ /**
31
+ * A class to find `re` methods immediately executing an expression.
32
+ *
33
+ * See `RegexExecutionMethods`
34
+ */
22
35
private class DirectRegex extends DataFlow:: CallCfgNode , RegexExecution:: Range {
23
36
DataFlow:: Node regexNode ;
24
37
25
38
DirectRegex ( ) {
26
- this = API:: moduleImport ( "re" ) .getMember ( any ( ReMethods m ) ) .getACall ( ) and
39
+ this = API:: moduleImport ( "re" ) .getMember ( any ( RegexExecutionMethods m ) ) .getACall ( ) and
27
40
regexNode = this .getArg ( 0 )
28
41
}
29
42
@@ -32,6 +45,14 @@ private module Re {
32
45
override string getRegexModule ( ) { result = "re" }
33
46
}
34
47
48
+ /**
49
+ * A class to find `re` methods immediately executing an expression from a
50
+ * compiled expression by `re.compile`.
51
+ *
52
+ * See `RegexExecutionMethods`
53
+ *
54
+ * See https://docs.python.org/3/library/re.html#regular-expression-objects
55
+ */
35
56
private class CompiledRegex extends DataFlow:: CallCfgNode , RegexExecution:: Range {
36
57
DataFlow:: Node regexNode ;
37
58
DataFlow:: CallCfgNode regexMethod ;
@@ -41,7 +62,7 @@ private module Re {
41
62
this .getFunction ( ) = reMethod and
42
63
patternCall = API:: moduleImport ( "re" ) .getMember ( "compile" ) .getACall ( ) and
43
64
patternCall = reMethod .getObject ( ) .getALocalSource ( ) and
44
- reMethod .getAttributeName ( ) instanceof ReMethods and
65
+ reMethod .getAttributeName ( ) instanceof RegexExecutionMethods and
45
66
regexNode = patternCall .getArg ( 0 )
46
67
)
47
68
}
@@ -51,6 +72,11 @@ private module Re {
51
72
override string getRegexModule ( ) { result = "re" }
52
73
}
53
74
75
+ /**
76
+ * A class to find `re` methods escaping an expression.
77
+ *
78
+ * See https://docs.python.org/3/library/re.html#re.escape
79
+ */
54
80
class ReEscape extends DataFlow:: CallCfgNode , RegexEscape:: Range {
55
81
DataFlow:: Node regexNode ;
56
82
DataFlow:: CallCfgNode escapeMethod ;
0 commit comments