src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Security/Checks/injectionWildcard.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2020 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a check for use of wildcard injection.
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 from Security.SecurityDefaults import SecurityDefaults
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 (checkLinuxCommandsWildcardInjection, ("S609",)),
32 ],
33 }
34
35
36 def checkLinuxCommandsWildcardInjection(reportError, context, config):
37 """
38 Function to check for use of wildcard injection.
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 subProcessFunctionNames = (
48 config["shell_injection_subprocess"]
49 if config and "shell_injection_subprocess" in config else
50 SecurityDefaults["shell_injection_subprocess"]
51 )
52
53 shellFunctionNames = (
54 config["shell_injection_shell"]
55 if config and "shell_injection_shell" in config else
56 SecurityDefaults["shell_injection_shell"]
57 )
58
59 vulnerableFunctions = ['chown', 'chmod', 'tar', 'rsync']
60 if (
61 (context.callFunctionNameQual in shellFunctionNames or
62 (context.callFunctionNameQual in subProcessFunctionNames and
63 context.checkCallArgValue('shell', 'True'))) and
64 context.callArgsCount >= 1
65 ):
66 callArgument = context.getCallArgAtPosition(0)
67 argumentString = ''
68 if isinstance(callArgument, list):
69 for li in callArgument:
70 argumentString += ' {0}'.format(li)
71 elif isinstance(callArgument, str):
72 argumentString = callArgument
73
74 if argumentString != '':
75 for vulnerableFunction in vulnerableFunctions:
76 if (
77 vulnerableFunction in argumentString and
78 '*' in argumentString
79 ):
80 lineNo = context.getLinenoForCallArg('shell')
81 if lineNo < 1:
82 lineNo = context.node.lineno
83 offset = context.getOffsetForCallArg('shell')
84 if offset < 0:
85 offset = context.node.col_offset
86 reportError(
87 lineNo - 1,
88 offset,
89 "S609",
90 "H",
91 "M",
92 context.callFunctionNameQual
93 )

eric ide

mercurial