@@ -10,20 +10,33 @@ private import semmle.python.dataflow.new.RemoteFlowSources
1010private import experimental.semmle.python.Concepts
1111private import semmle.python.ApiGraphs
1212
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+ */
1418private 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 ( ) {
1826 this in [ "match" , "fullmatch" , "search" , "split" , "findall" , "finditer" , "sub" , "subn" ]
1927 }
2028 }
2129
30+ /**
31+ * A class to find `re` methods immediately executing an expression.
32+ *
33+ * See `RegexExecutionMethods`
34+ */
2235 private class DirectRegex extends DataFlow:: CallCfgNode , RegexExecution:: Range {
2336 DataFlow:: Node regexNode ;
2437
2538 DirectRegex ( ) {
26- this = API:: moduleImport ( "re" ) .getMember ( any ( ReMethods m ) ) .getACall ( ) and
39+ this = API:: moduleImport ( "re" ) .getMember ( any ( RegexExecutionMethods m ) ) .getACall ( ) and
2740 regexNode = this .getArg ( 0 )
2841 }
2942
@@ -32,6 +45,14 @@ private module Re {
3245 override string getRegexModule ( ) { result = "re" }
3346 }
3447
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+ */
3556 private class CompiledRegex extends DataFlow:: CallCfgNode , RegexExecution:: Range {
3657 DataFlow:: Node regexNode ;
3758 DataFlow:: CallCfgNode regexMethod ;
@@ -41,7 +62,7 @@ private module Re {
4162 this .getFunction ( ) = reMethod and
4263 patternCall = API:: moduleImport ( "re" ) .getMember ( "compile" ) .getACall ( ) and
4364 patternCall = reMethod .getObject ( ) .getALocalSource ( ) and
44- reMethod .getAttributeName ( ) instanceof ReMethods and
65+ reMethod .getAttributeName ( ) instanceof RegexExecutionMethods and
4566 regexNode = patternCall .getArg ( 0 )
4667 )
4768 }
@@ -51,6 +72,11 @@ private module Re {
5172 override string getRegexModule ( ) { result = "re" }
5273 }
5374
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+ */
5480 class ReEscape extends DataFlow:: CallCfgNode , RegexEscape:: Range {
5581 DataFlow:: Node regexNode ;
5682 DataFlow:: CallCfgNode escapeMethod ;
0 commit comments