|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing checks for insecure use of 'tarfile.extracall()'. |
|
8 """ |
|
9 |
|
10 # |
|
11 # This is a modified version of the one found in the bandit package. |
|
12 # |
|
13 # SPDX-License-Identifier: Apache-2.0 |
|
14 # |
|
15 |
|
16 import ast |
|
17 |
|
18 |
|
19 def getChecks(): |
|
20 """ |
|
21 Public method to get a dictionary with checks handled by this module. |
|
22 |
|
23 @return dictionary containing checker lists containing checker function and |
|
24 list of codes |
|
25 @rtype dict |
|
26 """ |
|
27 return { |
|
28 "Call": [ |
|
29 (checkTarfileUnsafeMembers, ("S202",)), |
|
30 ], |
|
31 } |
|
32 |
|
33 |
|
34 def _getMembersValue(context): |
|
35 """ |
|
36 Function to extract the value of the 'members' argument. |
|
37 |
|
38 @param context security context object |
|
39 @type SecurityContext |
|
40 @return dictionary containing the argument value |
|
41 @rtype dict |
|
42 """ |
|
43 for kw in context.node.keywords: |
|
44 if kw.arg == "members": |
|
45 arg = kw.value |
|
46 if isinstance(arg, ast.Call): |
|
47 return {"Function": arg.func.id} |
|
48 else: |
|
49 value = arg.id if isinstance(arg, ast.Name) else arg |
|
50 return {"Other": value} |
|
51 |
|
52 return {} |
|
53 |
|
54 |
|
55 def checkTarfileUnsafeMembers(reportError, context, config): |
|
56 """ |
|
57 Function to check for insecure use of 'tarfile.extracall()'. |
|
58 |
|
59 @param reportError function to be used to report errors |
|
60 @type func |
|
61 @param context security context object |
|
62 @type SecurityContext |
|
63 @param config dictionary with configuration data |
|
64 @type dict |
|
65 """ |
|
66 if all( |
|
67 [ |
|
68 context.isModuleImportedExact("tarfile"), |
|
69 "extractall" in context.callFunctionName, |
|
70 ] |
|
71 ): |
|
72 if "members" in context.callKeywords: |
|
73 members = _getMembersValue(context) |
|
74 if "Function" in members: |
|
75 reportError( |
|
76 context.node.lineno - 1, |
|
77 context.node.col_offset, |
|
78 "S202.1", |
|
79 "L", |
|
80 "L", |
|
81 str(members), |
|
82 ) |
|
83 else: |
|
84 reportError( |
|
85 context.node.lineno - 1, |
|
86 context.node.col_offset, |
|
87 "S202.2", |
|
88 "M", |
|
89 "M", |
|
90 str(members), |
|
91 ) |
|
92 else: |
|
93 reportError( |
|
94 context.node.lineno - 1, context.node.col_offset, "S202.3", "H", "H" |
|
95 ) |