34 |
34 |
35 |
35 |
36 def checkLinuxCommandsWildcardInjection(reportError, context, config): |
36 def checkLinuxCommandsWildcardInjection(reportError, context, config): |
37 """ |
37 """ |
38 Function to check for use of wildcard injection. |
38 Function to check for use of wildcard injection. |
39 |
39 |
40 @param reportError function to be used to report errors |
40 @param reportError function to be used to report errors |
41 @type func |
41 @type func |
42 @param context security context object |
42 @param context security context object |
43 @type SecurityContext |
43 @type SecurityContext |
44 @param config dictionary with configuration data |
44 @param config dictionary with configuration data |
45 @type dict |
45 @type dict |
46 """ |
46 """ |
47 subProcessFunctionNames = ( |
47 subProcessFunctionNames = ( |
48 config["shell_injection_subprocess"] |
48 config["shell_injection_subprocess"] |
49 if config and "shell_injection_subprocess" in config else |
49 if config and "shell_injection_subprocess" in config |
50 SecurityDefaults["shell_injection_subprocess"] |
50 else SecurityDefaults["shell_injection_subprocess"] |
51 ) |
51 ) |
52 |
52 |
53 shellFunctionNames = ( |
53 shellFunctionNames = ( |
54 config["shell_injection_shell"] |
54 config["shell_injection_shell"] |
55 if config and "shell_injection_shell" in config else |
55 if config and "shell_injection_shell" in config |
56 SecurityDefaults["shell_injection_shell"] |
56 else SecurityDefaults["shell_injection_shell"] |
57 ) |
57 ) |
58 |
58 |
59 vulnerableFunctions = ['chown', 'chmod', 'tar', 'rsync'] |
59 vulnerableFunctions = ["chown", "chmod", "tar", "rsync"] |
60 if ( |
60 if ( |
61 (context.callFunctionNameQual in shellFunctionNames or |
61 context.callFunctionNameQual in shellFunctionNames |
62 (context.callFunctionNameQual in subProcessFunctionNames and |
62 or ( |
63 context.checkCallArgValue('shell', 'True'))) and |
63 context.callFunctionNameQual in subProcessFunctionNames |
64 context.callArgsCount >= 1 |
64 and context.checkCallArgValue("shell", "True") |
65 ): |
65 ) |
|
66 ) and context.callArgsCount >= 1: |
66 callArgument = context.getCallArgAtPosition(0) |
67 callArgument = context.getCallArgAtPosition(0) |
67 argumentString = '' |
68 argumentString = "" |
68 if isinstance(callArgument, list): |
69 if isinstance(callArgument, list): |
69 for li in callArgument: |
70 for li in callArgument: |
70 argumentString += ' {0}'.format(li) |
71 argumentString += " {0}".format(li) |
71 elif isinstance(callArgument, str): |
72 elif isinstance(callArgument, str): |
72 argumentString = callArgument |
73 argumentString = callArgument |
73 |
74 |
74 if argumentString != '': |
75 if argumentString != "": |
75 for vulnerableFunction in vulnerableFunctions: |
76 for vulnerableFunction in vulnerableFunctions: |
76 if ( |
77 if vulnerableFunction in argumentString and "*" in argumentString: |
77 vulnerableFunction in argumentString and |
78 lineNo = context.getLinenoForCallArg("shell") |
78 '*' in argumentString |
|
79 ): |
|
80 lineNo = context.getLinenoForCallArg('shell') |
|
81 if lineNo < 1: |
79 if lineNo < 1: |
82 lineNo = context.node.lineno |
80 lineNo = context.node.lineno |
83 offset = context.getOffsetForCallArg('shell') |
81 offset = context.getOffsetForCallArg("shell") |
84 if offset < 0: |
82 if offset < 0: |
85 offset = context.node.col_offset |
83 offset = context.node.col_offset |
86 reportError( |
84 reportError( |
87 lineNo - 1, |
85 lineNo - 1, |
88 offset, |
86 offset, |
89 "S609", |
87 "S609", |
90 "H", |
88 "H", |
91 "M", |
89 "M", |
92 context.callFunctionNameQual |
90 context.callFunctionNameQual, |
93 ) |
91 ) |