Skip to content

Commit e39bb56

Browse files
committed
Python: Model builtin open function better
1 parent 5af1fdd commit e39bb56

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

python/ql/src/semmle/python/frameworks/Stdlib.qll

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ private module Stdlib {
651651
* WARNING: Only holds for a few predefined attributes.
652652
*/
653653
private DataFlow::Node builtins_attr(DataFlow::TypeTracker t, string attr_name) {
654-
attr_name in ["exec", "eval", "compile"] and
654+
attr_name in ["exec", "eval", "compile", "open"] and
655655
(
656656
t.start() and
657657
result = DataFlow::importNode(["builtins", "__builtin__"] + "." + attr_name)
@@ -729,6 +729,20 @@ private module Stdlib {
729729
}
730730
}
731731

732+
/**
733+
* A call to the builtin `open` function.
734+
* See https://docs.python.org/3/library/functions.html#open
735+
*/
736+
private class OpenCall extends FileSystemAccess::Range, DataFlow::CfgNode {
737+
override CallNode node;
738+
739+
OpenCall() { node.getFunction() = builtins_attr("open").asCfgNode() }
740+
741+
override DataFlow::Node getAPathArgument() {
742+
result.asCfgNode() in [node.getArg(0), node.getArgByName("file")]
743+
}
744+
}
745+
732746
/**
733747
* An exec statement (only Python 2).
734748
* Se ehttps://docs.python.org/2/reference/simple_stmts.html#the-exec-statement.
@@ -743,20 +757,6 @@ private module Stdlib {
743757
override DataFlow::Node getCode() { result = this }
744758
}
745759

746-
/**
747-
* A call to the builtin `open` function.
748-
* See https://docs.python.org/3/library/functions.html#open
749-
*/
750-
private class OpenCall extends FileSystemAccess::Range, DataFlow::CfgNode {
751-
override CallNode node;
752-
753-
OpenCall() { node.getFunction().(NameNode).getId() = "open" }
754-
755-
override DataFlow::Node getAPathArgument() {
756-
result.asCfgNode() in [node.getArg(0), node.getArgByName("file")]
757-
}
758-
}
759-
760760
// ---------------------------------------------------------------------------
761761
// base64
762762
// ---------------------------------------------------------------------------

python/ql/test/experimental/library-tests/frameworks/stdlib/FileSystemAccess.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
o = open
88

9-
o("filepath") # $ MISSING: getAPathArgument="filepath"
10-
o(file="filepath") # $ MISSING: getAPathArgument="filepath"
9+
o("filepath") # $getAPathArgument="filepath"
10+
o(file="filepath") # $getAPathArgument="filepath"
1111

1212

13-
builtins.open("filepath") # $ MISSING: getAPathArgument="filepath"
14-
builtins.open(file="filepath") # $ MISSING: getAPathArgument="filepath"
13+
builtins.open("filepath") # $getAPathArgument="filepath"
14+
builtins.open(file="filepath") # $getAPathArgument="filepath"
1515

1616

1717
io.open("filepath") # $ MISSING: getAPathArgument="filepath"

0 commit comments

Comments
 (0)