Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
9325 | 1 | # -*- coding: utf-8 -*- |
2 | ||
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10683
diff
changeset
|
3 | # Copyright (c) 2022 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
9325 | 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": [ | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
29 | (checkTarfileUnsafeMembers, ("S-202",)), |
9325 | 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 | ||
10638
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
55 | def _isFilterData(context): |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
56 | """ |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
57 | Function to check for the filter argument to be 'data'. |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
58 | |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
59 | @param context security context object |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
60 | @type SecurityContext |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
61 | @return flag indicating the 'data' filter |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
62 | @rtype bool |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
63 | """ |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
64 | for kw in context.node.keywords: |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
65 | if kw.arg == "filter": |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
66 | arg = kw.value |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
67 | return isinstance(arg, ast.Str) and arg.s == "data" |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
68 | |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
69 | return False |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
70 | |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
71 | |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10638
diff
changeset
|
72 | def checkTarfileUnsafeMembers(reportError, context, _config): |
9325 | 73 | """ |
74 | Function to check for insecure use of 'tarfile.extracall()'. | |
75 | ||
76 | @param reportError function to be used to report errors | |
77 | @type func | |
78 | @param context security context object | |
79 | @type SecurityContext | |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10638
diff
changeset
|
80 | @param _config dictionary with configuration data (unused) |
9325 | 81 | @type dict |
82 | """ | |
83 | if all( | |
84 | [ | |
85 | context.isModuleImportedExact("tarfile"), | |
86 | "extractall" in context.callFunctionName, | |
87 | ] | |
88 | ): | |
10638
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
89 | if "filter" in context.callKeywords and _isFilterData(context): |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
90 | return |
12558008c269
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
91 | |
9325 | 92 | if "members" in context.callKeywords: |
93 | members = _getMembersValue(context) | |
94 | if "Function" in members: | |
95 | reportError( | |
96 | context.node.lineno - 1, | |
97 | context.node.col_offset, | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
98 | "S-202.1", |
9325 | 99 | "L", |
100 | "L", | |
101 | str(members), | |
102 | ) | |
103 | else: | |
104 | reportError( | |
105 | context.node.lineno - 1, | |
106 | context.node.col_offset, | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
107 | "S-202.2", |
9325 | 108 | "M", |
109 | "M", | |
110 | str(members), | |
111 | ) | |
112 | else: | |
113 | reportError( | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
114 | context.node.lineno - 1, context.node.col_offset, "S-202.3", "H", "H" |
9325 | 115 | ) |