Skip to content

Commit a30f697

Browse files
committed
Python: Add getName to RegexExecution concept
1 parent 8f152a5 commit a30f697

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ class RegexExecution extends DataFlow::Node {
371371

372372
/** Gets a dataflow node for the string to be searched or matched against. */
373373
DataFlow::Node getString() { result = range.getString() }
374+
375+
/**
376+
* Gets the name of this regex execution, typically the name of an executing method.
377+
* This is used for nice alert messages and should include the module if possible.
378+
*/
379+
string getName() { result = range.getName() }
374380
}
375381

376382
/** Provides classes for modeling new regular-expression execution APIs. */
@@ -387,6 +393,12 @@ module RegexExecution {
387393

388394
/** Gets a dataflow node for the string to be searched or matched against. */
389395
abstract DataFlow::Node getString();
396+
397+
/**
398+
* Gets the name of this regex execution, typically the name of an executing method.
399+
* This is used for nice alert messages and should include the module if possible.
400+
*/
401+
abstract string getName();
390402
}
391403
}
392404

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,16 +1537,19 @@ private class DirectRegex extends DataFlow::CallCfgNode, RegexExecution::Range {
15371537
override DataFlow::Node getString() {
15381538
result in [this.getArg(stringArg(method)), this.getArgByName("string")]
15391539
}
1540+
1541+
override string getName() { result = "re." + method }
15401542
}
15411543

15421544
/** Helper module for tracking compiled regexes. */
15431545
private module CompiledRegexes {
1544-
private import semmle.python.dataflow.new.DataFlow2
1546+
private import semmle.python.dataflow.new.DataFlow4
15451547
private import semmle.python.RegexTreeView
15461548

1547-
// TODO: This module should be refactored once API graphs are more expressinve.
1549+
// TODO: This module should be refactored once API graphs are more expressive.
1550+
// For now it uses data flow, so we pick the verion with least change of collision (4) .
15481551
/** A configuration for finding uses of compiled regexes. */
1549-
class RegexDefinitionConfiguration extends DataFlow2::Configuration {
1552+
class RegexDefinitionConfiguration extends DataFlow4::Configuration {
15501553
RegexDefinitionConfiguration() { this = "RegexDefinitionConfiguration" }
15511554

15521555
override predicate isSource(DataFlow::Node source) { source instanceof RegexDefinitonSource }
@@ -1608,7 +1611,7 @@ private import CompiledRegexes
16081611
*
16091612
* See https://docs.python.org/3/library/re.html#regular-expression-objects
16101613
*/
1611-
private class CompiledRegex extends DataFlow::CallCfgNode, RegexExecution {
1614+
private class CompiledRegex extends DataFlow::CallCfgNode, RegexExecution::Range {
16121615
DataFlow::Node regexNode;
16131616
RegexExecutionMethod method;
16141617

@@ -1628,6 +1631,8 @@ private class CompiledRegex extends DataFlow::CallCfgNode, RegexExecution {
16281631
override DataFlow::Node getString() {
16291632
result in [this.getArg(stringArg(method) - 1), this.getArgByName("string")]
16301633
}
1634+
1635+
override string getName() { result = "re." + method }
16311636
}
16321637

16331638
/**

0 commit comments

Comments
 (0)