Skip to content

Commit b672197

Browse files
committed
Improve code comments
1 parent d4a89b2 commit b672197

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

python/ql/src/experimental/semmle/python/Concepts.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ module RegexExecution {
2424
* extend `RegexExecution` instead.
2525
*/
2626
abstract class Range extends DataFlow::Node {
27+
/**
28+
* Gets the argument containing the executed expression.
29+
*/
2730
abstract DataFlow::Node getRegexNode();
2831

32+
/**
33+
* Gets the library used to execute the regular expression.
34+
*/
2935
abstract string getRegexModule();
3036
}
3137
}
@@ -55,6 +61,9 @@ module RegexEscape {
5561
* extend `RegexEscape` instead.
5662
*/
5763
abstract class Range extends DataFlow::Node {
64+
/**
65+
* Gets the argument containing the escaped expression.
66+
*/
5867
abstract DataFlow::Node getRegexNode();
5968
}
6069
}

python/ql/src/experimental/semmle/python/frameworks/Stdlib.qll

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@ private import semmle.python.dataflow.new.RemoteFlowSources
1010
private import experimental.semmle.python.Concepts
1111
private 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+
*/
1418
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() {
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;

python/ql/src/experimental/semmle/python/security/injection/RegexInjection.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import semmle.python.dataflow.new.DataFlow
99
import semmle.python.dataflow.new.TaintTracking
1010
import semmle.python.dataflow.new.RemoteFlowSources
1111

12+
/**
13+
* A class to find methods executing regular expressions.
14+
*
15+
* See `RegexExecution`
16+
*/
1217
class RegexInjectionSink extends DataFlow::Node {
1318
string regexModule;
1419
Attribute regexMethod;

0 commit comments

Comments
 (0)