Skip to content

Commit 0222159

Browse files
committed
Specify vulnerable args instead of safe ones
1 parent a3885cd commit 0222159

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,14 @@ class FileSystemAccess extends DataFlow::Node instanceof FileSystemAccess::Range
118118
DataFlow::Node getAPathArgument() { result = super.getAPathArgument() }
119119

120120
/**
121-
* Gets an argument to this file system access that is interpreted as a path,
122-
* but which is not vulnerable to path injection.
121+
* Gets an argument to this file system access that is interpreted as a path
122+
* which is vulnerable to path injection.
123+
*
124+
* By default all path arguments are considered vulnerable, but this can be overridden to
125+
* exclude certain arguments that are known to be safe, for example because they are
126+
* restricted to a specific directory.
123127
*/
124-
DataFlow::Node getASafePathArgument() { result = super.getASafePathArgument() }
128+
DataFlow::Node getAVulnerablePathArgument() { result = super.getAVulnerablePathArgument() }
125129
}
126130

127131
/** Provides a class for modeling new file system access APIs. */
@@ -138,10 +142,14 @@ module FileSystemAccess {
138142
abstract DataFlow::Node getAPathArgument();
139143

140144
/**
141-
* Gets an argument to this file system access that is interpreted as a path,
142-
* but which is not vulnerable to path injection.
145+
* Gets an argument to this file system access that is interpreted as a path
146+
* which is vulnerable to path injection.
147+
*
148+
* By default all path arguments are considered vulnerable, but this can be overridden to
149+
* exclude certain arguments that are known to be safe, for example because they are
150+
* restricted to a specific directory.
143151
*/
144-
DataFlow::Node getASafePathArgument() { none() }
152+
DataFlow::Node getAVulnerablePathArgument() { result = this.getAPathArgument() }
145153
}
146154
}
147155

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,11 @@ module Flask {
625625
result = this.getArgByName(["directory", "filename"])
626626
}
627627

628-
override DataFlow::Node getASafePathArgument() {
629-
// as described in the docs, the `filename` argument is restrained to be within
628+
override DataFlow::Node getAVulnerablePathArgument() {
629+
result = this.getAPathArgument() and
630+
// as described in the docs, the `filename` argument is restricted to be within
630631
// the provided directory, so is not exposed to path-injection.
631-
result in [this.getArg(1), this.getArgByName("filename")]
632+
not result in [this.getArg(1), this.getArgByName("filename")]
632633
}
633634
}
634635

python/ql/lib/semmle/python/security/dataflow/PathInjectionCustomizations.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ module PathInjection {
5757
*/
5858
class FileSystemAccessAsSink extends Sink {
5959
FileSystemAccessAsSink() {
60-
this = any(FileSystemAccess e).getAPathArgument() and
61-
not this = any(FileSystemAccess e).getASafePathArgument() and
60+
this = any(FileSystemAccess e).getAVulnerablePathArgument() and
6261
// since implementation of Path.open in pathlib.py is like
6362
// ```py
6463
// def open(self, ...):

0 commit comments

Comments
 (0)