Skip to content

Commit 278c071

Browse files
committed
Python: Modernise werkzeug FileStorage modeling
1 parent d0fdb54 commit 278c071

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

python/ql/src/experimental/semmle/python/frameworks/Flask.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private module Flask_Private {
325325
RequestInputFiles() { attr_name = "files" }
326326
}
327327
// TODO: Somehow specify that elements of `RequestInputFiles` are
328-
// Werkzeug::Datastructures::FileStorage and should have those additional taint steps
328+
// Werkzeug::werkzeug::datastructures::FileStorage and should have those additional taint steps
329329
// AND that the 0-indexed argument to its' save method is a sink for path-injection.
330330
// https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage.save
331331
}

python/ql/src/experimental/semmle/python/frameworks/Werkzeug.qll

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ module Werkzeug {
5858
*/
5959
DataFlow::Node getlist() { result = getlist(DataFlow::TypeTracker::end()) }
6060
}
61+
62+
/**
63+
* Provides models for the `werkzeug.datastrctures.FileStorage` class
64+
*
65+
* See https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage.
66+
*/
67+
module FileStorage {
68+
/**
69+
* A source of an instance of `werkzeug.datastrctures.FileStorage`.
70+
*
71+
* This can include instantiation of the class, return value from function
72+
* calls, or a special parameter that will be set when functions are call by external
73+
* library.
74+
*
75+
* Use `FileStorage::instance()` predicate to get references to instances of `werkzeug.datastrctures.FileStorage`.
76+
*/
77+
abstract class InstanceSource extends DataFlow::Node { }
78+
79+
/** Gets a reference to an instance of `werkzeug.datastrctures.FileStorage`. */
80+
private DataFlow::Node instance(DataFlow::TypeTracker t) {
81+
t.start() and
82+
result instanceof InstanceSource
83+
or
84+
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
85+
}
86+
87+
/** Gets a reference to an instance of `werkzeug.datastrctures.FileStorage`. */
88+
DataFlow::Node instance() { result = instance(DataFlow::TypeTracker::end()) }
89+
}
6190
}
6291
}
6392

@@ -76,34 +105,21 @@ module Werkzeug {
76105
}
77106
}
78107

79-
module Datastructures {
80-
// ---------------------------------------------------------------------- //
81-
// FileStorage //
82-
// ---------------------------------------------------------------------- //
83-
/**
84-
* A Node representing an instance of a werkzeug.datastructures.FileStorage
85-
*
86-
* See https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage
87-
*/
88-
abstract class FileStorage extends DataFlow::Node { }
89-
90-
private class FileStorageAdditionalTaintStep extends TaintTracking::AdditionalTaintStep {
91-
override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
92-
// TODO: should be `nodeFrom = tracked(any(FileStorage fs))`
93-
nodeFrom instanceof FileStorage and
94-
exists(string name |
95-
name in ["filename",
96-
// str
97-
"name", "content_type", "mimetype",
98-
// file-like
99-
"stream",
100-
// TODO: werkzeug.datastructures.Headers
101-
"headers",
102-
// dict[str, str]
103-
"mimetype_params"] and
104-
nodeTo.asCfgNode().(AttrNode).getObject(name) = nodeFrom.asCfgNode()
105-
)
106-
}
108+
private class FileStorageAdditionalTaintStep extends TaintTracking::AdditionalTaintStep {
109+
override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
110+
nodeFrom = werkzeug::datastructures::FileStorage::instance() and
111+
exists(DataFlow::AttrRead read | nodeTo = read |
112+
read.getAttributeName() in ["filename",
113+
// str
114+
"name", "content_type", "mimetype",
115+
// file-like
116+
"stream",
117+
// TODO: werkzeug.datastructures.Headers
118+
"headers",
119+
// dict[str, str]
120+
"mimetype_params"] and
121+
read.getObject() = nodeFrom
122+
)
107123
}
108124
}
109125
}

0 commit comments

Comments
 (0)