|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a check for setting too permissive file permissions. |
|
8 """ |
|
9 |
|
10 # |
|
11 # This is a modified version of the one found in the bandit package. |
|
12 # |
|
13 # Original Copyright 2014 Hewlett-Packard Development Company, L.P. |
|
14 # |
|
15 # SPDX-License-Identifier: Apache-2.0 |
|
16 # |
|
17 |
|
18 import stat |
|
19 |
|
20 |
|
21 def getChecks(): |
|
22 """ |
|
23 Public method to get a dictionary with checks handled by this module. |
|
24 |
|
25 @return dictionary containing checker lists containing checker function and |
|
26 list of codes |
|
27 @rtype dict |
|
28 """ |
|
29 return { |
|
30 "Call": [ |
|
31 (checkFilePermissions, ("S102",)), |
|
32 ], |
|
33 } |
|
34 |
|
35 |
|
36 def checkFilePermissions(reportError, context, config): |
|
37 """ |
|
38 Function to check for setting too permissive file permissions. |
|
39 |
|
40 @param reportError function to be used to report errors |
|
41 @type func |
|
42 @param context security context object |
|
43 @type SecurityContext |
|
44 @param config dictionary with configuration data |
|
45 @type dict |
|
46 """ |
|
47 if 'chmod' in context.callFunctionName: |
|
48 if context.callArgsCount == 2: |
|
49 mode = context.getCallArgAtPosition(1) |
|
50 |
|
51 if ( |
|
52 mode is not None and |
|
53 isinstance(mode, int) and |
|
54 (mode & stat.S_IWOTH or mode & stat.S_IXGRP) |
|
55 ): |
|
56 # world writable is an HIGH, group executable is a MEDIUM |
|
57 if mode & stat.S_IWOTH: |
|
58 severity = "H" |
|
59 else: |
|
60 severity = "M" |
|
61 |
|
62 filename = context.getCallArgAtPosition(0) |
|
63 if filename is None: |
|
64 filename = 'NOT PARSED' |
|
65 |
|
66 reportError( |
|
67 context.node.lineno - 1, |
|
68 context.node.col_offset, |
|
69 "S103", |
|
70 severity, |
|
71 "H", |
|
72 oct(mode), |
|
73 filename |
|
74 ) |