Skip to content

Commit eeda435

Browse files
committed
Python: Fix missing DictionaryElementContent
1 parent 30b7fad commit eeda435

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ predicate dictStoreStep(CfgNode nodeFrom, DictionaryElementContent c, Node nodeT
809809
* TODO: Once TaintTracking no longer uses `dictStoreStep`, unify the two predicates.
810810
*/
811811
private predicate moreDictStoreSteps(CfgNode nodeFrom, DictionaryElementContent c, Node nodeTo) {
812+
// NOTE: It's important to add logic to the newtype definition of
813+
// DictionaryElementContent if you add new cases here.
812814
exists(SubscriptNode subscript |
813815
nodeTo.(PostUpdateNode).getPreUpdateNode().asCfgNode() = subscript.getObject() and
814816
nodeFrom.asCfgNode() = subscript.(DefinitionNode).getValue() and

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,19 @@ newtype TContent =
605605
} or
606606
/** An element of a dictionary under a specific key. */
607607
TDictionaryElementContent(string key) {
608-
key = any(KeyValuePair kvp).getKey().(StrConst).getS()
608+
// {"key": ...}
609+
key = any(KeyValuePair kvp).getKey().(StrConst).getText()
609610
or
611+
// func(key=...)
610612
key = any(Keyword kw).getArg()
613+
or
614+
// d["key"] = ...
615+
key = any(SubscriptNode sub | sub.isStore() | sub.getIndex().getNode().(StrConst).getText())
616+
or
617+
// d.setdefault("key", ...)
618+
exists(CallNode call | call.getFunction().(AttrNode).getName() = "setdefault" |
619+
key = call.getArg(0).getNode().(StrConst).getText()
620+
)
611621
} or
612622
/** An element of a dictionary under any key. */
613623
TDictionaryElementAnyContent() or

python/ql/test/experimental/dataflow/fieldflow/test_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_dict_update_fresh_key():
5252
# for keys used in "inline update" like this
5353
d = {}
5454
d["fresh_key"] = SOURCE
55-
SINK(d["fresh_key"]) # $ MISSING: flow="SOURCE, l:-1 -> d['fresh_key']"
55+
SINK(d["fresh_key"]) # $ flow="SOURCE, l:-1 -> d['fresh_key']"
5656

5757

5858
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)

0 commit comments

Comments
 (0)