Skip to content

Commit a9ce2e1

Browse files
authored
Merge pull request github#15781 from RasmusWL/dict-update
Python: Fix missing DictionaryElementContents
2 parents ab288d0 + 16cb6c2 commit a9ce2e1

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed missing flow for dictionary updates (`d[<key>] = ...`) when `<key>` is a string constant not used in dictionary literals or as name of keyword-argument.

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ def test_dict_update():
4545
SINK(d["key"]) # $ flow="SOURCE, l:-1 -> d['key']"
4646
SINK(d.get("key")) # $ flow="SOURCE, l:-2 -> d.get(..)"
4747

48+
49+
def test_dict_update_fresh_key():
50+
# we had a regression where we did not create a dictionary element content
51+
# for keys used in "inline update" like this
52+
d = {}
53+
d["fresh_key"] = SOURCE
54+
SINK(d["fresh_key"]) # $ flow="SOURCE, l:-1 -> d['fresh_key']"
55+
56+
4857
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
4958
def test_dict_setdefault():
5059
d = {}

0 commit comments

Comments
 (0)