1818from coretrace_python .abstract import (
1919 ATTRIBUTES ,
2020 ELEMENTS ,
21+ KEYS ,
2122 HeapAnalysis ,
2223 HeapFacts ,
2324 HeapLocation ,
@@ -130,12 +131,18 @@ class TaintFlow:
130131 # What the arguments of the sink call denote, to decide the conditions on them.
131132 sink_arguments : Arguments | None = None
132133 # How the tainted value is passed to the sink call: at a position, by a keyword, or
133- # (None, None) unpacked with ``*`` or ``**``; ``f(x, key=x)`` passes it twice.
134+ # (None, None) unpacked with ``*`` or ``**``; ``f(x, key=x)`` passes it twice. The
135+ # keyword ``KEYWORD_NAMES`` says the value's keys are the keyword names of the call.
134136 passed_as : frozenset [tuple [int | None , str | None ]] = frozenset ()
135137
136138
137139Key = Value | HeapLocation
138140
141+ # The ``keyword`` of a ``passed_as`` way whose value's keys become the keyword names of
142+ # the call: ``f(**m)`` passes the keys of ``m`` as ``(None, KEYWORD_NAMES)``.
143+ KEYWORD_NAMES = "**"
144+ _DICT = SymbolId ("python.builtins.dict" )
145+
139146
140147class TaintFacts :
141148 def __init__ (self , taints : Mapping [Key , Taint ], flows : tuple [TaintFlow , ...]) -> None :
@@ -217,6 +224,14 @@ def loaded(self, instruction: GetAttr | GetItem | GetIter, state: Mapping[Key, T
217224 taint = taint .join (state .get (location , Taint .none ()))
218225 return taint
219226
227+ def keyed (self , value : Value , state : Mapping [Key , Taint ]) -> Taint :
228+ """The taint of the keys of the mappings ``value`` points to."""
229+
230+ taint = Taint .none ()
231+ for location in self .heap .locations (value , KEYS ):
232+ taint = taint .join (state .get (location , Taint .none ()))
233+ return taint
234+
220235 def initial (self ) -> State :
221236 state : dict [Key , Taint ] = {
222237 self .function .parameters [index ]: Taint (source .kinds , frozenset ({source }))
@@ -246,6 +261,17 @@ def evaluate(
246261 self .store (state , instruction .object , ATTRIBUTES , state .get (instruction .value , Taint .none ()))
247262 elif isinstance (instruction , SetItem ):
248263 self .store (state , instruction .object , ELEMENTS , state .get (instruction .value , Taint .none ()))
264+ key = state .get (instruction .key , Taint .none ())
265+ if key :
266+ self .store (state , instruction .object , KEYS , key )
267+ elif isinstance (instruction , BuildDict ):
268+ keys = Taint .none ()
269+ for item_key , _ in instruction .items :
270+ keys = keys .join (state .get (item_key , Taint .none ()))
271+ for unpacked in instruction .unpacked : # ``{**d}`` copies d's keys
272+ keys = keys .join (self .keyed (unpacked , state ))
273+ if keys :
274+ self .store (state , instruction .result , KEYS , keys )
249275 if instruction .result is None :
250276 continue
251277 if isinstance (instruction , Phi ):
@@ -352,6 +378,10 @@ def call(self, call: Call, state: dict[Key, Taint], flows: list[TaintFlow]) -> T
352378 project , through , arguments , keywords , everything , call , flows , state , (), bound
353379 )
354380 return self .declared_clean (target .symbol , result )
381+ if target .symbol == _DICT :
382+ keys = self .constructed_keys (call , state )
383+ if keys :
384+ self .store (state , call .result , KEYS , keys )
355385 symbol = target .symbol
356386 if not self .modelled (symbol ):
357387 # ``get_conn().execute`` derived ``app.database.get_conn.execute``; what
@@ -376,6 +406,23 @@ def call(self, call: Call, state: dict[Key, Taint], flows: list[TaintFlow]) -> T
376406 return self .external (returned , everything , call , state , flows )
377407 return everything .join (state .get (call .callee , Taint .none ()))
378408
409+ def constructed_keys (self , call : Call , state : Mapping [Key , Taint ]) -> Taint :
410+ """What the keys of ``dict(...)`` carry: the keys of the mapping it copies
411+ (``dict(m)``, ``dict(**m)``) and the first element of each pair of a sequence
412+ literal (``dict([(k, v)])``); a keyword is a constant key, and an iterable built
413+ elsewhere (``zip(...)``, a parameter) gives keys the engine cannot establish."""
414+
415+ taint = Taint .none ()
416+ for value in (* call .arguments [:1 ], * (v for name , v in call .keywords if name is None )):
417+ taint = taint .join (self .keyed (value , state ))
418+ made = self .defs .get (value )
419+ if isinstance (made , BuildList | BuildTuple | BuildSet ) and not made .unpacked :
420+ for element in made .elements :
421+ pair = self .defs .get (element )
422+ if isinstance (pair , BuildTuple | BuildList ) and pair .elements :
423+ taint = taint .join (state .get (pair .elements [0 ], Taint .none ()))
424+ return taint
425+
379426 def nonlocal_result (self , instruction : NonlocalResult , state : dict [Key , Taint ]) -> Taint :
380427 """The taint a nested callee left in its nonlocal ``name``, in the caller's terms."""
381428
@@ -455,6 +502,11 @@ def external(
455502 self .report (
456503 flows , sink , self .rendered (argument , state ), argument , call , None , None , None , given , keyword = name
457504 )
505+ if name is None :
506+ # The keys of a ``**mapping`` are keyword names of the call: only an
507+ # advisory asks who chooses a name, no other sink reads one.
508+ keys = self .keyed (argument , state ).without (~ TaintKind .ADVISORY )
509+ self .report (flows , sink , keys , argument , call , None , None , None , given , keyword = KEYWORD_NAMES )
458510 self .template_filters (symbol , call , given , state , flows )
459511 cleared = self .clearing .clears (symbol , given )
460512 if cleared :
0 commit comments