Skip to content

Commit 57f8994

Browse files
committed
CONT-234 Create MVP
This PR is an MVP and gets the current minimal set of tests passing. - modified Craig's tests to include a test for no problems - added a check which gets the tests passing. ie the check: - identifies exec resources - then it identifies any commands (command, onlyif and unless) in the exec - it parses the command statement and checks for any input variables - if so it raises an warning - removed dependabot for now TODO: - this is an MVP so more functionality needs to be added on top - will add extra checks on commands for various instances of unsanitised input
1 parent 5870bca commit 57f8994

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ RSpec::Core::RakeTask.new(:spec) do |t|
55
t.exclude_pattern = "spec/acceptance/**/*.rb"
66
end
77

8+
task default: :spec
9+
810
begin
911
require 'github_changelog_generator/task'
1012

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PuppetLint.new_check(:check_unsafe_interpolations) do
2+
COMMANDS = Set['command', 'onlyif', 'unless']
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
9+
end
10+
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
18+
if token.type == :VARIABLE
19+
20+
# Raise warning since input variable is unsanitised
21+
warning_message = "unsafe interpolation of variable '#{token.value}' in exec command"
22+
notify_warning(token, warning_message)
23+
break
24+
end
25+
26+
token = token.next_token
27+
end
28+
end
29+
end
30+
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+
41+
def notify_warning(token, message)
42+
notify :warning, message: message,
43+
line: token.line,
44+
column: token.column
45+
end
46+
end
Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
require 'spec_helper'
22

33
describe 'check_unsafe_interpolations' do
4-
let(:code) do
5-
<<-PUPPET
6-
class foo {
4+
let(:msg){ "unsafe interpolation of variable 'foo' in exec command" }
5+
context 'with fix disabled' do
6+
context 'code with unsafe interpolation' do
7+
let(:code) do
8+
<<-PUPPET
9+
class foo {
710
8-
exec { 'bar':
9-
command => "echo ${foo}",
10-
}
11+
exec { 'bar':
12+
command => "echo ${foo}",
13+
}
1114
12-
}
13-
PUPPET
14-
end
15+
}
16+
PUPPET
17+
end
18+
19+
it 'detects an unsafe exec command argument' do
20+
expect(problems).to have(1).problems
21+
end
22+
23+
it 'should create a warning' do
24+
expect(problems).to contain_warning(msg)
25+
end
26+
end
27+
28+
context 'code with no problems' do
29+
let(:code) do
30+
<<-PUPPET
31+
class foo {
32+
33+
exec { 'bar':
34+
command => "echo foo",
35+
}
36+
37+
}
38+
PUPPET
39+
end
1540

16-
it 'detects an unsafe exec command argument' do
17-
expect(problems).to have(1).problem
18-
expect(problems).to contain_warning("unsafe interpolation of variable 'foo' in exec command")
41+
it 'should not detect any problems' do
42+
expect(problems).to have(0).problems
43+
end
44+
end
1945
end
2046
end

0 commit comments

Comments
 (0)