Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c671017

Browse files
committed
Python: Model fabric Connection (version 2.x)
1 parent f750238 commit c671017

2 files changed

Lines changed: 215 additions & 7 deletions

File tree

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

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,218 @@ private module FabricV1 {
144144
}
145145
}
146146
}
147+
148+
/**
149+
* Provides classes modeling security-relevant aspects of the `fabric` PyPI package, for
150+
* version 2.x.
151+
*
152+
* See http://docs.fabfile.org/en/2.5/getting-st arted.html.
153+
*/
154+
private module FabricV2 {
155+
/** Gets a reference to the `fabric` module. */
156+
private DataFlow::Node fabric(DataFlow::TypeTracker t) {
157+
t.start() and
158+
result = DataFlow::importNode("fabric")
159+
or
160+
exists(DataFlow::TypeTracker t2 | result = fabric(t2).track(t2, t))
161+
}
162+
163+
/** Gets a reference to the `fabric` module. */
164+
DataFlow::Node fabric() { result = fabric(DataFlow::TypeTracker::end()) }
165+
166+
/**
167+
* Gets a reference to the attribute `attr_name` of the `fabric` module.
168+
* WARNING: Only holds for a few predefined attributes.
169+
*/
170+
private DataFlow::Node fabric_attr(DataFlow::TypeTracker t, string attr_name) {
171+
attr_name in ["connection",
172+
// connection.py
173+
"Connection",
174+
// group.py
175+
"group", "SerialGroup", "ThreadingGroup",
176+
// tasks.py
177+
"tasks", "task"] and
178+
(
179+
t.start() and
180+
result = DataFlow::importNode("fabric" + "." + attr_name)
181+
or
182+
t.startInAttr(attr_name) and
183+
result = fabric()
184+
)
185+
or
186+
// Due to bad performance when using normal setup with `fabric_attr(t2, attr_name).track(t2, t)`
187+
// we have inlined that code and forced a join
188+
exists(DataFlow::TypeTracker t2 |
189+
exists(DataFlow::StepSummary summary |
190+
fabric_attr_first_join(t2, attr_name, result, summary) and
191+
t = t2.append(summary)
192+
)
193+
)
194+
}
195+
196+
pragma[nomagic]
197+
private predicate fabric_attr_first_join(
198+
DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res, DataFlow::StepSummary summary
199+
) {
200+
DataFlow::StepSummary::step(fabric_attr(t2, attr_name), res, summary)
201+
}
202+
203+
/**
204+
* Gets a reference to the attribute `attr_name` of the `fabric` module.
205+
* WARNING: Only holds for a few predefined attributes.
206+
*/
207+
private DataFlow::Node fabric_attr(string attr_name) {
208+
result = fabric_attr(DataFlow::TypeTracker::end(), attr_name)
209+
}
210+
211+
/** Provides models for the `fabric` module. */
212+
module fabric {
213+
// -------------------------------------------------------------------------
214+
// fabric.connection
215+
// -------------------------------------------------------------------------
216+
/** Gets a reference to the `fabric.connection` module. */
217+
DataFlow::Node connection() { result = fabric_attr("connection") }
218+
219+
/** Provides models for the `fabric.connection` module */
220+
module connection {
221+
/**
222+
* Gets a reference to the attribute `attr_name` of the `fabric.connection` module.
223+
* WARNING: Only holds for a few predefined attributes.
224+
*/
225+
private DataFlow::Node connection_attr(DataFlow::TypeTracker t, string attr_name) {
226+
attr_name in ["Connection"] and
227+
(
228+
t.start() and
229+
result = DataFlow::importNode("fabric.connection" + "." + attr_name)
230+
or
231+
t.startInAttr(attr_name) and
232+
result = connection()
233+
)
234+
or
235+
// Due to bad performance when using normal setup with `connection_attr(t2, attr_name).track(t2, t)`
236+
// we have inlined that code and forced a join
237+
exists(DataFlow::TypeTracker t2 |
238+
exists(DataFlow::StepSummary summary |
239+
connection_attr_first_join(t2, attr_name, result, summary) and
240+
t = t2.append(summary)
241+
)
242+
)
243+
}
244+
245+
pragma[nomagic]
246+
private predicate connection_attr_first_join(
247+
DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res,
248+
DataFlow::StepSummary summary
249+
) {
250+
DataFlow::StepSummary::step(connection_attr(t2, attr_name), res, summary)
251+
}
252+
253+
/**
254+
* Gets a reference to the attribute `attr_name` of the `fabric.connection` module.
255+
* WARNING: Only holds for a few predefined attributes.
256+
*/
257+
private DataFlow::Node connection_attr(string attr_name) {
258+
result = connection_attr(DataFlow::TypeTracker::end(), attr_name)
259+
}
260+
261+
/**
262+
* Provides models for the `fabric.connection.Connection` class
263+
*
264+
* See https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.
265+
*/
266+
module Connection {
267+
/** Gets a reference to the `fabric.connection.Connection` class. */
268+
private DataFlow::Node classRef(DataFlow::TypeTracker t) {
269+
t.start() and
270+
result = connection_attr("Connection")
271+
or
272+
// handle `fabric.Connection` alias
273+
t.start() and
274+
result = fabric_attr("Connection")
275+
or
276+
exists(DataFlow::TypeTracker t2 | result = classRef(t2).track(t2, t))
277+
}
278+
279+
/** Gets a reference to the `fabric.connection.Connection` class. */
280+
DataFlow::Node classRef() { result = classRef(DataFlow::TypeTracker::end()) }
281+
282+
/**
283+
* A source of an instance of `fabric.connection.Connection`.
284+
*
285+
* This can include instantiation of the class, return value from function
286+
* calls, or a special parameter that will be set when functions are call by external
287+
* library.
288+
*
289+
* Use `Connection::instance()` predicate to get references to instances of `fabric.connection.Connection`.
290+
*/
291+
abstract class InstanceSource extends DataFlow::Node { }
292+
293+
private class ClassInstantiation extends InstanceSource, DataFlow::CfgNode {
294+
override CallNode node;
295+
296+
ClassInstantiation() { node.getFunction() = classRef().asCfgNode() }
297+
}
298+
299+
/** Gets a reference to an instance of `fabric.connection.Connection`. */
300+
private DataFlow::Node instance(DataFlow::TypeTracker t) {
301+
t.start() and
302+
result instanceof InstanceSource
303+
or
304+
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
305+
}
306+
307+
/** Gets a reference to an instance of `fabric.connection.Connection`. */
308+
DataFlow::Node instance() { result = instance(DataFlow::TypeTracker::end()) }
309+
310+
/**
311+
* Gets a reference to either `run`, `sudo`, or `local` method on a
312+
* `fabric.connection.Connection` instance.
313+
*
314+
* See
315+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.run
316+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.sudo
317+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.local
318+
*/
319+
private DataFlow::Node instanceRunMethods(DataFlow::TypeTracker t) {
320+
t.startInAttr(["run", "sudo", "local"]) and
321+
result = instance()
322+
or
323+
exists(DataFlow::TypeTracker t2 | result = instanceRunMethods(t2).track(t2, t))
324+
}
325+
326+
/**
327+
* Gets a reference to either `run`, `sudo`, or `local` method on a
328+
* `fabric.connection.Connection` instance.
329+
*
330+
* See
331+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.run
332+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.sudo
333+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.local
334+
*/
335+
DataFlow::Node instanceRunMethods() {
336+
result = instanceRunMethods(DataFlow::TypeTracker::end())
337+
}
338+
}
339+
}
340+
341+
/**
342+
* A call to either `run`, `sudo`, or `local` on a `fabric.connection.Connection` instance.
343+
* See
344+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.run
345+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.sudo
346+
* - https://docs.fabfile.org/en/2.5/api/connection.html#fabric.connection.Connection.local
347+
*/
348+
private class FabricConnectionRunSudoLocalCall extends SystemCommandExecution::Range,
349+
DataFlow::CfgNode {
350+
override CallNode node;
351+
352+
FabricConnectionRunSudoLocalCall() {
353+
node.getFunction() = fabric::connection::Connection::instanceRunMethods().asCfgNode()
354+
}
355+
356+
override DataFlow::Node getCommand() {
357+
result.asCfgNode() = [node.getArg(0), node.getArgByName("command")]
358+
}
359+
}
360+
}
361+
}

python/ql/test/experimental/library-tests/frameworks/fabric/ConceptsTest.expected

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
| fabric_v2_test.py:13:22:13:47 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
2-
| fabric_v2_test.py:14:24:14:49 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
3-
| fabric_v2_test.py:15:23:15:48 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
4-
| fabric_v2_test.py:17:32:17:57 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
5-
| fabric_v2_test.py:18:30:18:55 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
6-
| fabric_v2_test.py:19:31:19:56 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
7-
| fabric_v2_test.py:23:23:23:48 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
81
| fabric_v2_test.py:29:66:29:91 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
92
| fabric_v2_test.py:32:25:32:50 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |
103
| fabric_v2_test.py:35:62:35:87 | Comment # $getCommand="cmd1; cmd2" | Missing result:getCommand="cmd1; cmd2" |

0 commit comments

Comments
 (0)