Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-1273) Allowlist Ruby symbols in YAML validator #624

Merged
merged 2 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/pdk/validate/yaml/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Validate
class YAML
class Syntax < BaseValidator
IGNORE_DOTFILES = false
YAML_WHITELISTED_CLASSES = [Symbol].freeze

def self.name
'yaml-syntax'
Expand Down Expand Up @@ -75,7 +76,7 @@ def self.invoke(report, options = {})
end

begin
::YAML.safe_load(File.read(target), [], [], true)
::YAML.safe_load(File.read(target), YAML_WHITELISTED_CLASSES, [], true)

report.add_event(
file: target,
Expand All @@ -97,6 +98,17 @@ def self.invoke(report, options = {})
},
)
return_val = 1
rescue Psych::DisallowedClass => e
report.add_event(
file: target,
source: name,
state: :failure,
severity: 'error',
message: _('Unsupported class: %{message}') % {
message: e.message,
},
)
return_val = 1
end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/unit/pdk/validate/yaml/syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@
end
end

context 'when a target is provided that contains valid YAML with a symbol value' do
let(:targets) do
[
{ name: '.sync.yml', content: "---\n foo: :bar" },
]
end

it 'adds a passing event to the report' do
expect(report).to receive(:add_event).with(
file: targets.first[:name],
source: 'yaml-syntax',
state: :passed,
severity: 'ok',
)
expect(return_value).to eq(0)
end
end

context 'when a target is provided that contains invalid YAML' do
let(:targets) do
[
Expand All @@ -100,6 +118,25 @@
end
end

context 'when a target is provided that contains an unsupported class' do
let(:targets) do
[
{ name: 'file.yml', content: "--- !ruby/object:File {}\n" },
]
end

it 'adds a failure event to the report' do
expect(report).to receive(:add_event).with(
file: targets.first[:name],
source: 'yaml-syntax',
state: :failure,
severity: 'error',
message: a_string_matching(%r{unspecified class: file}i),
)
expect(return_value).to eq(1)
end
end

context 'when targets are provided that contain valid and invalid YAML' do
let(:targets) do
[
Expand Down