Skip to content

Commit f2fbeed

Browse files
committed
Python: Model os.path-functions
1 parent 81adb7d commit f2fbeed

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ private module StdlibPrivate {
195195
}
196196
}
197197

198+
/**
199+
* A call to `os.path.exists` or `os.path.lexists` will check if a file exists on the file system.
200+
* The `os.path` module offers e number of methods for checking if a file exists and/or has certain
201+
* properties, leading to a file system access.
202+
* (Although, on some platforms, the check may return `false` due to missing permissions.)
203+
* See:
204+
* - https://docs.python.org/3/library/os.path.html#os.path.exists
205+
* - https://docs.python.org/3/library/os.path.html#os.path.lexists
206+
* - https://docs.python.org/3/library/os.path.html#os.path.isfile
207+
* - https://docs.python.org/3/library/os.path.html#os.path.isdir
208+
* - https://docs.python.org/3/library/os.path.html#os.path.islink
209+
* - https://docs.python.org/3/library/os.path.html#os.path.ismount
210+
*/
211+
private class OsPathProbingCall extends FileSystemAccess::Range, DataFlow::CallCfgNode {
212+
OsPathProbingCall() {
213+
this =
214+
os::path()
215+
.getMember(["exists", "lexists", "isfile", "isdir", "islink", "ismount"])
216+
.getACall()
217+
}
218+
219+
override DataFlow::Node getAPathArgument() {
220+
result in [this.getArg(0), this.getArgByName("path")]
221+
}
222+
}
223+
198224
/**
199225
* A call to `os.path.normpath`.
200226
* See https://docs.python.org/3/library/os.path.html#os.path.normpath

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def through_function(open_file):
2929
through_function(f)
3030

3131
from os import path
32-
path.exists("filepath") # $ MISSING: getAPathArgument="filepath"
33-
path.isfile("filepath") # $ MISSING: getAPathArgument="filepath"
34-
path.isdir("filepath") # $ MISSING: getAPathArgument="filepath"
35-
path.islink("filepath") # $ MISSING: getAPathArgument="filepath"
36-
path.ismount("filepath") # $ MISSING: getAPathArgument="filepath"
32+
path.exists("filepath") # $ getAPathArgument="filepath"
33+
path.isfile("filepath") # $ getAPathArgument="filepath"
34+
path.isdir("filepath") # $ getAPathArgument="filepath"
35+
path.islink("filepath") # $ getAPathArgument="filepath"
36+
path.ismount("filepath") # $ getAPathArgument="filepath"

0 commit comments

Comments
 (0)