|
1 | 1 | PuppetLint.new_check(:check_unsafe_interpolations) do |
2 | | - COMMANDS = Set['command', 'onlyif', 'unless'] |
| 2 | + COMMANDS = Array['command', 'onlyif', 'unless'] |
3 | 3 | def check |
4 | | - exec = false |
5 | | - |
6 | | - # Look for exec blocks |
7 | | - tokens.select { |token| check_exec?(token) }.each do |token| |
8 | | - exec = true |
| 4 | + # Gather any exec commands' resources into an array |
| 5 | + exec_resources = resource_indexes.map do |resource| |
| 6 | + resource_parameters = resource[:param_tokens].map(&:value) |
| 7 | + resource if resource[:type].value == 'exec' && !(COMMANDS & resource_parameters).empty? |
9 | 8 | end |
10 | 9 |
|
11 | | - # Look for commands in exec blocks |
12 | | - tokens.select { |token| exec && check_command?(token) }.each do |token| |
13 | | - |
14 | | - # Loop over exec command to find command statement |
15 | | - while token.type != :NEWLINE |
16 | | - |
17 | | - # Check if command contains an input variable |
| 10 | + # Iterate over each command found in any exec |
| 11 | + exec_resources.each do |command_resources| |
| 12 | + # Iterate over each command in execs and check for unsafe interpolations |
| 13 | + command_resources[:tokens].each do |token| |
| 14 | + # Check if any tokens in command are a varibale |
18 | 15 | if token.type == :VARIABLE |
19 | | - |
20 | | - # Raise warning since input variable is unsanitised |
21 | 16 | warning_message = "unsafe interpolation of variable '#{token.value}' in exec command" |
22 | 17 | notify_warning(token, warning_message) |
23 | | - break |
24 | 18 | end |
25 | | - |
26 | | - token = token.next_token |
27 | 19 | end |
28 | 20 | end |
29 | 21 | end |
30 | 22 |
|
31 | | - def check_exec?(token) |
32 | | - return true if token.value == 'exec' |
33 | | - return false |
34 | | - end |
35 | | - |
36 | | - def check_command?(token) |
37 | | - return true if COMMANDS.include?(token.value) |
38 | | - return false |
39 | | - end |
40 | | - |
| 23 | + # Raises a warning given a token and message |
41 | 24 | def notify_warning(token, message) |
42 | | - notify :warning, message: message, |
43 | | - line: token.line, |
44 | | - column: token.column |
| 25 | + notify :warning, |
| 26 | + message: message, |
| 27 | + line: token.line, |
| 28 | + column: token.column |
45 | 29 | end |
46 | 30 | end |
0 commit comments