Skip to content

Commit 19974f0

Browse files
authored
Merge pull request github#16245 from github/tausbn/python-rename-StrConst-to-StringLiteral
Python: Rename `StrConst` to `StringLiteral`
2 parents 35d1a92 + 81246cd commit 19974f0

File tree

164 files changed

+936
-884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+936
-884
lines changed

python/ql/examples/snippets/raw_string.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
import python
1010

11-
from StrConst s
11+
from StringLiteral s
1212
where s.getPrefix().matches("%r%")
1313
select s

python/ql/examples/snippets/singlequotestring.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
import python
1111

12-
from StrConst s
12+
from StringLiteral s
1313
where s.getPrefix().charAt(_) = "'"
1414
select s

python/ql/lib/analysis/DefinitionTracking.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private predicate sets_attribute(ArgumentRefinement def, string name) {
410410
call = def.getDefiningNode() and
411411
call.getFunction().refersTo(Object::builtin("setattr")) and
412412
def.getInput().getAUse() = call.getArg(0) and
413-
call.getArg(1).getNode().(StrConst).getText() = name
413+
call.getArg(1).getNode().(StringLiteral).getText() = name
414414
)
415415
}
416416

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: deprecated
3+
---
4+
5+
- Renamed the `StrConst` class to `StringLiteral`, for greater consistency with other languages. The `StrConst` and `Str` classes are now deprecated and will be removed in a future release.

python/ql/lib/experimental/cryptography/modules/stdlib/HashlibModule.qll

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module Hashes {
2626
}
2727

2828
override string getName() {
29-
result = super.normalizeName(this.asExpr().(StrConst).getText())
29+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
3030
or
3131
// if not a known/static string, assume from an outside source and the algorithm is UNKNOWN
32-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
32+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
3333
}
3434
}
3535

@@ -49,10 +49,10 @@ module Hashes {
4949
}
5050

5151
override string getName() {
52-
result = super.normalizeName(this.asExpr().(StrConst).getText())
52+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
5353
or
5454
// if not a known/static string, assume from an outside source and the algorithm is UNKNOWN
55-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
55+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
5656
}
5757
}
5858

@@ -88,9 +88,9 @@ module Hashes {
8888
// Name is a string constant or consider the name unknown
8989
// NOTE: we are excluding hmac.new and hmac.HMAC constructor calls so we are expecting
9090
// a string or an outside configuration only
91-
result = super.normalizeName(this.asExpr().(StrConst).getText())
91+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
9292
or
93-
not this.asExpr() instanceof StrConst and
93+
not this.asExpr() instanceof StringLiteral and
9494
result = unknownAlgorithm()
9595
}
9696
}

python/ql/lib/experimental/cryptography/modules/stdlib/HmacModule.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ module Hashes {
6262
then result = super.normalizeName("MD5")
6363
else (
6464
// Else get the string name, if its a string constant, or UNKNOWN if otherwise
65-
result = super.normalizeName(this.asExpr().(StrConst).getText())
65+
result = super.normalizeName(this.asExpr().(StringLiteral).getText())
6666
or
67-
not this.asExpr() instanceof StrConst and result = unknownAlgorithm()
67+
not this.asExpr() instanceof StringLiteral and result = unknownAlgorithm()
6868
)
6969
}
7070
}

python/ql/lib/semmle/python/ApiGraphs.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ module API {
257257
*/
258258
Node getSubscript(string key) {
259259
exists(API::Node index | result = this.getSubscriptAt(index) |
260-
key = index.getAValueReachingSink().asExpr().(PY::StrConst).getText()
260+
key = index.getAValueReachingSink().asExpr().(PY::StringLiteral).getText()
261261
)
262262
}
263263

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ module Http {
855855

856856
/** Gets the URL pattern for this route, if it can be statically determined. */
857857
string getUrlPattern() {
858-
exists(StrConst str |
858+
exists(StringLiteral str |
859859
this.getUrlPatternArg().getALocalSource() = DataFlow::exprNode(str) and
860860
result = str.getText()
861861
)
@@ -983,7 +983,7 @@ module Http {
983983

984984
/** Gets the mimetype of this HTTP response, if it can be statically determined. */
985985
string getMimetype() {
986-
exists(StrConst str |
986+
exists(StringLiteral str |
987987
this.getMimetypeOrContentTypeArg().getALocalSource() = DataFlow::exprNode(str) and
988988
result = str.getText().splitAt(";", 0)
989989
)

python/ql/lib/semmle/python/Exprs.qll

+17-8
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class Call extends Call_ {
236236
string getANamedArgumentName() {
237237
result = this.getAKeyword().getArg()
238238
or
239-
result = this.getKwargs().(Dict).getAKey().(StrConst).getText()
239+
result = this.getKwargs().(Dict).getAKey().(StringLiteral).getText()
240240
}
241241

242242
/** Gets the positional argument count of this call, provided there is no more than one tuple (*) argument. */
@@ -299,7 +299,7 @@ class Repr extends Repr_ {
299299
* A bytes constant, such as `b'ascii'`. Note that unadorned string constants such as
300300
* `"hello"` are treated as Bytes for Python2, but Unicode for Python3.
301301
*/
302-
class Bytes extends StrConst {
302+
class Bytes extends StringLiteral {
303303
/* syntax: b"hello" */
304304
Bytes() { not this.isUnicode() }
305305

@@ -446,7 +446,7 @@ class NegativeIntegerLiteral extends ImmutableLiteral, UnaryExpr {
446446
* A unicode string expression, such as `u"\u20ac"`. Note that unadorned string constants such as
447447
* "hello" are treated as Bytes for Python2, but Unicode for Python3.
448448
*/
449-
class Unicode extends StrConst {
449+
class Unicode extends StringLiteral {
450450
/* syntax: "hello" */
451451
Unicode() { this.isUnicode() }
452452

@@ -599,7 +599,7 @@ class Slice extends Slice_ {
599599
/**
600600
* Returns all string prefixes in the database that are explicitly marked as Unicode strings.
601601
*
602-
* Helper predicate for `StrConst::isUnicode`.
602+
* Helper predicate for `StringLiteral::isUnicode`.
603603
*/
604604
pragma[nomagic]
605605
private string unicode_prefix() {
@@ -610,20 +610,27 @@ private string unicode_prefix() {
610610
/**
611611
* Returns all string prefixes in the database that are _not_ explicitly marked as bytestrings.
612612
*
613-
* Helper predicate for `StrConst::isUnicode`.
613+
* Helper predicate for `StringLiteral::isUnicode`.
614614
*/
615615
pragma[nomagic]
616616
private string non_byte_prefix() {
617617
result = any(Str_ s).getPrefix() and
618618
not result.charAt(_) in ["b", "B"]
619619
}
620620

621-
/** A string constant. This is a placeholder class -- use `StrConst` instead. */
622-
class Str = StrConst;
621+
/** DEPRECATED. Use `StringLiteral` instead. */
622+
deprecated class Str = StringLiteral;
623+
624+
/** DEPRECATED. Use `StringLiteral` instead. */
625+
deprecated class StrConst = StringLiteral;
623626

624627
/** A string constant. */
625-
class StrConst extends Str_, ImmutableLiteral {
628+
class StringLiteral extends Str_, ImmutableLiteral {
626629
/* syntax: "hello" */
630+
/**
631+
* Holds if this string is a unicode string, either by default (e.g. if Python 3), or with an
632+
* explicit prefix.
633+
*/
627634
predicate isUnicode() {
628635
this.getPrefix() = unicode_prefix()
629636
or
@@ -652,6 +659,8 @@ class StrConst extends Str_, ImmutableLiteral {
652659
}
653660

654661
override Object getLiteralObject() { none() }
662+
663+
override string toString() { result = "StringLiteral" }
655664
}
656665

657666
private predicate name_consts(Name_ n, string id) {

python/ql/lib/semmle/python/Files.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class File extends Container, Impl::File {
9393
exists(Stmt s | s.getLocation().getFile() = this)
9494
or
9595
// The file contains the usual `if __name__ == '__main__':` construction
96-
exists(If i, Name name, StrConst main, Cmpop op |
96+
exists(If i, Name name, StringLiteral main, Cmpop op |
9797
i.getScope().(Module).getFile() = this and
9898
op instanceof Eq and
9999
i.getTest().(Compare).compares(name, op, main) and
@@ -123,7 +123,7 @@ private predicate occupied_line(File f, int n) {
123123
exists(Location l | l.getFile() = f |
124124
l.getStartLine() = n
125125
or
126-
exists(StrConst s | s.getLocation() = l | n in [l.getStartLine() .. l.getEndLine()])
126+
exists(StringLiteral s | s.getLocation() = l | n in [l.getStartLine() .. l.getEndLine()])
127127
)
128128
}
129129

python/ql/lib/semmle/python/Module.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ class Module extends Module_, Scope, AstNode {
125125
a.getScope() = this and
126126
all.getId() = "__all__" and
127127
(
128-
a.getValue().(List).getAnElt().(StrConst).getText() = name
128+
a.getValue().(List).getAnElt().(StringLiteral).getText() = name
129129
or
130-
a.getValue().(Tuple).getAnElt().(StrConst).getText() = name
130+
a.getValue().(Tuple).getAnElt().(StringLiteral).getText() = name
131131
)
132132
)
133133
}

python/ql/lib/semmle/python/PrintAst.qll

+4-4
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,13 @@ class ParameterNode extends AstElementNode {
423423
}
424424

425425
/**
426-
* A print node for a `StrConst`.
426+
* A print node for a `StringLiteral`.
427427
*
428428
* The string has a child, if the child is used as a regular expression,
429429
* which is the root of the regular expression.
430430
*/
431-
class StrConstNode extends AstElementNode {
432-
override StrConst element;
431+
class StringLiteralNode extends AstElementNode {
432+
override StringLiteral element;
433433
}
434434

435435
/**
@@ -599,7 +599,7 @@ private module PrettyPrinting {
599599
or
600600
result = "class " + a.(Class).getName()
601601
or
602-
result = a.(StrConst).getText()
602+
result = a.(StringLiteral).getText()
603603
or
604604
result = "yield " + a.(Yield).getValue()
605605
or

python/ql/lib/semmle/python/Scope.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Scope extends Scope_ {
4848
string getName() { py_strs(result, this, 0) }
4949

5050
/** Gets the docstring for this scope */
51-
StrConst getDocString() { result = this.getStmt(0).(ExprStmt).getValue() }
51+
StringLiteral getDocString() { result = this.getStmt(0).(ExprStmt).getValue() }
5252

5353
/** Gets the entry point into this Scope's control flow graph */
5454
ControlFlowNode getEntryNode() { py_scope_flow(result, this, -1) }

python/ql/lib/semmle/python/Stmts.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class If extends If_ {
284284

285285
/** Whether this if statement takes the form `if __name__ == "__main__":` */
286286
predicate isNameEqMain() {
287-
exists(StrConst m, Name n, Compare c |
287+
exists(StringLiteral m, Name n, Compare c |
288288
this.getTest() = c and
289289
c.getOp(0) instanceof Eq and
290290
(

python/ql/lib/semmle/python/dataflow/new/BarrierGuards.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ private import semmle.python.dataflow.new.DataFlow
55

66
private predicate stringConstCompare(DataFlow::GuardNode g, ControlFlowNode node, boolean branch) {
77
exists(CompareNode cn | cn = g |
8-
exists(StrConst str_const, Cmpop op |
8+
exists(StringLiteral str_const, Cmpop op |
99
op = any(Eq eq) and branch = true
1010
or
1111
op = any(NotEq ne) and branch = false
@@ -21,7 +21,7 @@ private predicate stringConstCompare(DataFlow::GuardNode g, ControlFlowNode node
2121
op = any(NotIn ni) and branch = false
2222
|
2323
forall(ControlFlowNode elem | elem = str_const_iterable.getAnElement() |
24-
elem.getNode() instanceof StrConst
24+
elem.getNode() instanceof StringLiteral
2525
) and
2626
cn.operands(node, op, str_const_iterable)
2727
)

python/ql/lib/semmle/python/dataflow/new/SensitiveDataSources.qll

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private module SensitiveDataModeling {
9191
// Note: If this is implemented with type-tracking, we will get cross-talk as
9292
// illustrated in python/ql/test/experimental/dataflow/sensitive-data/test.py
9393
exists(DataFlow::LocalSourceNode source |
94-
source.asExpr().(StrConst).getText() = sensitiveString(classification) and
94+
source.asExpr().(StringLiteral).getText() = sensitiveString(classification) and
9595
source.flowsTo(result)
9696
)
9797
}
@@ -173,8 +173,8 @@ private module SensitiveDataModeling {
173173
}
174174

175175
pragma[nomagic]
176-
private string sensitiveStrConstCandidate() {
177-
result = any(StrConst s | not s.isDocString()).getText() and
176+
private string sensitiveStringLiteralCandidate() {
177+
result = any(StringLiteral s | not s.isDocString()).getText() and
178178
not result.regexpMatch(notSensitiveRegexp())
179179
}
180180

@@ -217,7 +217,7 @@ private module SensitiveDataModeling {
217217
result in [
218218
sensitiveNameCandidate(), sensitiveAttributeNameCandidate(),
219219
sensitiveParameterNameCandidate(), sensitiveFunctionNameCandidate(),
220-
sensitiveStrConstCandidate()
220+
sensitiveStringLiteralCandidate()
221221
]
222222
}
223223

python/ql/lib/semmle/python/dataflow/new/internal/Attributes.qll

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class AttrRef extends Node {
4040
or
4141
exists(LocalSourceNode nodeFrom |
4242
nodeFrom.flowsTo(this.getAttributeNameExpr()) and
43-
attrName = nodeFrom.(CfgNode).getNode().getNode().(StrConst).getText()
43+
attrName = nodeFrom.(CfgNode).getNode().getNode().(StringLiteral).getText()
4444
)
4545
}
4646

@@ -178,7 +178,7 @@ private class SetAttrCallAsAttrWrite extends AttrWrite, CfgNode {
178178
override ExprNode getAttributeNameExpr() { result.asCfgNode() = node.getName() }
179179

180180
override string getAttributeName() {
181-
result = this.getAttributeNameExpr().(CfgNode).getNode().getNode().(StrConst).getText()
181+
result = this.getAttributeNameExpr().(CfgNode).getNode().getNode().(StringLiteral).getText()
182182
}
183183
}
184184

@@ -254,7 +254,7 @@ private class GetAttrCallAsAttrRead extends AttrRead, CfgNode {
254254
override ExprNode getAttributeNameExpr() { result.asCfgNode() = node.getName() }
255255

256256
override string getAttributeName() {
257-
result = this.getAttributeNameExpr().(CfgNode).getNode().getNode().(StrConst).getText()
257+
result = this.getAttributeNameExpr().(CfgNode).getNode().getNode().(StringLiteral).getText()
258258
}
259259
}
260260

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPrivate.qll

+5-5
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ predicate dictStoreStep(CfgNode nodeFrom, DictionaryElementContent c, Node nodeT
813813
exists(KeyValuePair item |
814814
item = nodeTo.asCfgNode().(DictNode).getNode().(Dict).getAnItem() and
815815
nodeFrom.getNode().getNode() = item.getValue() and
816-
c.getKey() = item.getKey().(StrConst).getS()
816+
c.getKey() = item.getKey().(StringLiteral).getS()
817817
)
818818
}
819819

@@ -829,13 +829,13 @@ private predicate moreDictStoreSteps(CfgNode nodeFrom, DictionaryElementContent
829829
exists(SubscriptNode subscript |
830830
nodeTo.(PostUpdateNode).getPreUpdateNode().asCfgNode() = subscript.getObject() and
831831
nodeFrom.asCfgNode() = subscript.(DefinitionNode).getValue() and
832-
c.getKey() = subscript.getIndex().getNode().(StrConst).getText()
832+
c.getKey() = subscript.getIndex().getNode().(StringLiteral).getText()
833833
)
834834
or
835835
// see https://docs.python.org/3.10/library/stdtypes.html#dict.setdefault
836836
exists(MethodCallNode call |
837837
call.calls(nodeTo.(PostUpdateNode).getPreUpdateNode(), "setdefault") and
838-
call.getArg(0).asExpr().(StrConst).getText() = c.getKey() and
838+
call.getArg(0).asExpr().(StringLiteral).getText() = c.getKey() and
839839
nodeFrom = call.getArg(1)
840840
)
841841
}
@@ -844,7 +844,7 @@ predicate dictClearStep(Node node, DictionaryElementContent c) {
844844
exists(SubscriptNode subscript |
845845
subscript instanceof DefinitionNode and
846846
node.asCfgNode() = subscript.getObject() and
847-
c.getKey() = subscript.getIndex().getNode().(StrConst).getText()
847+
c.getKey() = subscript.getIndex().getNode().(StringLiteral).getText()
848848
)
849849
}
850850

@@ -954,7 +954,7 @@ predicate subscriptReadStep(CfgNode nodeFrom, Content c, CfgNode nodeTo) {
954954
nodeTo.getNode().(SubscriptNode).getIndex().getNode().(IntegerLiteral).getValue()
955955
or
956956
c.(DictionaryElementContent).getKey() =
957-
nodeTo.getNode().(SubscriptNode).getIndex().getNode().(StrConst).getS()
957+
nodeTo.getNode().(SubscriptNode).getIndex().getNode().(StringLiteral).getS()
958958
)
959959
}
960960

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll

+4-3
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,18 @@ newtype TContent =
606606
/** An element of a dictionary under a specific key. */
607607
TDictionaryElementContent(string key) {
608608
// {"key": ...}
609-
key = any(KeyValuePair kvp).getKey().(StrConst).getText()
609+
key = any(KeyValuePair kvp).getKey().(StringLiteral).getText()
610610
or
611611
// func(key=...)
612612
key = any(Keyword kw).getArg()
613613
or
614614
// d["key"] = ...
615-
key = any(SubscriptNode sub | sub.isStore() | sub.getIndex().getNode().(StrConst).getText())
615+
key =
616+
any(SubscriptNode sub | sub.isStore() | sub.getIndex().getNode().(StringLiteral).getText())
616617
or
617618
// d.setdefault("key", ...)
618619
exists(CallNode call | call.getFunction().(AttrNode).getName() = "setdefault" |
619-
key = call.getArg(0).getNode().(StrConst).getText()
620+
key = call.getArg(0).getNode().(StringLiteral).getText()
620621
)
621622
} or
622623
/** An element of a dictionary under any key. */

0 commit comments

Comments
 (0)