Skip to content

Commit c70427d

Browse files
committed
Python: Add globallyDefinedName and extend monkeyPatchedBuiltin
1 parent af7ac40 commit c70427d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,38 @@ OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) { call = result.getCall
19881988
module DuckTyping {
19891989
private import semmle.python.ApiGraphs
19901990

1991+
/**
1992+
* Holds if `name` is a globally defined name (a builtin or VM-defined name).
1993+
*/
1994+
predicate globallyDefinedName(string name) {
1995+
exists(API::builtin(name))
1996+
or
1997+
name = "WindowsError"
1998+
or
1999+
name = "_" and exists(Module m | m.getName() = "gettext")
2000+
or
2001+
name in ["__file__", "__builtins__", "__name__"]
2002+
}
2003+
2004+
/**
2005+
* Holds if `name` is monkey-patched into the builtins module.
2006+
*/
2007+
predicate monkeyPatchedBuiltin(string name) {
2008+
any(DataFlow::AttrWrite aw)
2009+
.writes(API::moduleImport("builtins").getAValueReachableFromSource(), name, _)
2010+
or
2011+
// B.__dict__["name"] = value
2012+
exists(SubscriptNode subscr |
2013+
subscr.isStore() and
2014+
subscr.getObject() =
2015+
API::moduleImport("builtins")
2016+
.getMember("__dict__")
2017+
.getAValueReachableFromSource()
2018+
.asCfgNode() and
2019+
subscr.getIndex().getNode().(StringLiteral).getText() = name
2020+
)
2021+
}
2022+
19912023
/**
19922024
* Holds if `cls` or any of its resolved superclasses declares a method with the given `name`.
19932025
*/

0 commit comments

Comments
 (0)