@@ -11,20 +11,51 @@ def check
1111 exec_resources . each do |command_resources |
1212 # Iterate over each command in execs and check for unsafe interpolations
1313 command_resources [ :tokens ] . each do |token |
14- # Check if any tokens in command are a varibale
15- if token . type == :VARIABLE
16- warning_message = "unsafe interpolation of variable '#{ token . value } ' in exec command"
17- notify_warning ( token , warning_message )
14+ # We are only interested in tokens from command onwards
15+ next unless token . type == :NAME
16+ # Don't check the command if it is parameterised
17+ next if parameterised? ( token )
18+
19+ check_command ( token ) . each do |t |
20+ notify_warning ( t )
1821 end
1922 end
2023 end
2124 end
2225
2326 # Raises a warning given a token and message
24- def notify_warning ( token , message )
27+ def notify_warning ( token )
2528 notify :warning ,
26- message : message ,
29+ message : "unsafe interpolation of variable ' #{ token . value } ' in exec command" ,
2730 line : token . line ,
2831 column : token . column
2932 end
33+
34+ # Iterates over the tokens in a command and adds it to an array of violations if it is an input variable
35+ def check_command ( token )
36+ # Initialise variables needed in while loop
37+ rule_violations = [ ]
38+ current_token = token
39+
40+ # Iterate through tokens in command
41+ while current_token . type != :NEWLINE
42+ # Check if token is a varibale and if it is parameterised
43+ if current_token . type == :VARIABLE
44+ rule_violations . append ( current_token )
45+ end
46+ current_token = current_token . next_token
47+ end
48+
49+ rule_violations
50+ end
51+
52+ # A command is parameterised if its args are placed in an array
53+ # This function checks if the current token is a :FARROW and if so, if it is followed by an LBRACK
54+ def parameterised? ( token )
55+ current_token = token
56+ while current_token . type != :NEWLINE
57+ return true if current_token . type == :FARROW && current_token . next_token . next_token . type == :LBRACK
58+ current_token = current_token . next_token
59+ end
60+ end
3061end
0 commit comments