Skip to content

Commit 57f6382

Browse files
authored
Allow psych v4 (#98)
In psych v4, the YAML.load method has been updated to provide a similar interface and semantics to YAML.safe_load. Refactor the code to use YAML.safe_load for a consistent behaviour between psych V3 and V4. Psych will automatically convert strings that look like dates to Ruby Date objects. We need to permit this class when loading CloudFormation YAML. The permitted_classes keyword argument for YAML.safe_load was introduced in psych v3.1.0. So this is now the minimum compatible version of psych.
1 parent 3ca8460 commit 57f6382

File tree

9 files changed

+20
-18
lines changed

9 files changed

+20
-18
lines changed

Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PATH
33
specs:
44
cfn-model (9.9.9)
55
kwalify (= 0.7.2)
6-
psych (~> 3)
6+
psych (>= 3.1, < 5)
77

88
GEM
99
remote: https://rubygems.org/
@@ -16,7 +16,8 @@ GEM
1616
parallel (1.19.1)
1717
parser (2.7.0.2)
1818
ast (~> 2.4.0)
19-
psych (3.1.0)
19+
psych (4.0.3)
20+
stringio
2021
rainbow (3.0.0)
2122
rake (13.0.3)
2223
rspec (3.9.0)
@@ -46,6 +47,7 @@ GEM
4647
simplecov_json_formatter (~> 0.1)
4748
simplecov-html (0.12.3)
4849
simplecov_json_formatter (0.1.2)
50+
stringio (3.0.1)
4951
unicode-display_width (1.6.1)
5052

5153
PLATFORMS

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ upon which static analysis can be conducted.
3232
The `CfnModel` is a container for other object that have been parsed, wrapped and potentially linked to
3333
other wrapped objects.
3434

35-
The raw Hash output of `YAML.load` is also available from `CfnModel`.
35+
The raw Hash output of `YAML.safe_load` is also available from `CfnModel`.
3636

3737
require 'cfn-model'
3838

cfn-model.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ Gem::Specification.new do |s|
2626
s.add_development_dependency('simplecov', '~> 0.11')
2727

2828
s.add_runtime_dependency('kwalify', '0.7.2')
29-
s.add_runtime_dependency('psych', '~> 3')
29+
s.add_runtime_dependency('psych', '>= 3.1', '< 5')
3030
end

lib/cfn-model/model/cfn_model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CfnModel
66
attr_reader :resources, :parameters, :line_numbers, :conditions, :globals, :mappings, :element_types
77

88
##
9-
# if you really want it, here it is - the raw Hash from YAML.load. you'll have to mess with structural nits of
9+
# if you really want it, here it is - the raw Hash from YAML.safe_load. you'll have to mess with structural nits of
1010
# CloudFormation and deal with variations between yaml/json refs and all that
1111
#
1212
attr_accessor :raw_model

lib/cfn-model/parser/cfn_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def parse_without_parameters(cloudformation_yml, with_line_numbers=false, condit
6565
if with_line_numbers
6666
parse_with_line_numbers(cloudformation_yml)
6767
else
68-
YAML.load cloudformation_yml
68+
YAML.safe_load cloudformation_yml, permitted_classes: [Date]
6969
end
7070

7171
# Transform raw resources in template as performed by

lib/cfn-model/validator/cloudformation_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate(cloudformation_string)
1212

1313
schema = SchemaGenerator.new.generate cloudformation_string
1414
validator = Kwalify::Validator.new(schema)
15-
validator.validate(YAML.load(cloudformation_string))
15+
validator.validate(YAML.safe_load(cloudformation_string, permitted_classes: [Date]))
1616
rescue ArgumentError, IOError, NameError => e
1717
raise ParserError, e.inspect
1818
end

lib/cfn-model/validator/resource_type_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class ResourceTypeValidator
66

77
def self.validate(cloudformation_yml)
8-
hash = YAML.load cloudformation_yml
8+
hash = YAML.safe_load cloudformation_yml, permitted_classes: [Date]
99
if hash == false || hash.nil?
1010
raise ParserError.new 'yml empty'
1111
end

lib/cfn-model/validator/schema_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def generate(cloudformation_yml)
1919
parameters_schema = generate_schema_for_parameter_keys cloudformation_hash
2020
resources_schema = generate_schema_for_resource_keys cloudformation_hash
2121

22-
main_schema = YAML.load IO.read(schema_file('schema.yml.erb'))
22+
main_schema = YAML.safe_load IO.read(schema_file('schema.yml.erb'))
2323
if parameters_schema.empty?
2424
main_schema['mapping'].delete 'Parameters'
2525
else
@@ -82,7 +82,7 @@ def schema_for_type(type)
8282
if !File.exist? schema_file_path
8383
nil
8484
else
85-
YAML.load IO.read(schema_file_path)
85+
YAML.safe_load IO.read(schema_file_path)
8686
end
8787
end
8888
end

spec/validator/reference_validator_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
JimBob: !Ref someResource
2424
END
2525

26-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
26+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
2727
expect(unresolved_references).to eq Set.new(%w(dino))
2828
end
2929
end
@@ -48,7 +48,7 @@
4848
JimBob: !Ref someResource
4949
END
5050

51-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
51+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
5252
expect(unresolved_references).to eq Set.new(%w(dino))
5353
end
5454
end
@@ -72,7 +72,7 @@
7272
END
7373

7474
expect {
75-
_ = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
75+
_ = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
7676
}.to raise_error(ParserError, 'Ref target must be string literal: {"Ref"=>{"Fn::GetAtt"=>["someResource", "Fred"]}}')
7777
end
7878
end
@@ -90,7 +90,7 @@
9090
Barney: !Ref AWS::Region
9191
END
9292

93-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
93+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
9494
expect(unresolved_references).to eq Set.new([])
9595
end
9696
end
@@ -115,7 +115,7 @@
115115
JimBob: !Ref someResource
116116
END
117117

118-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
118+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
119119
expect(unresolved_references).to eq Set.new(%w(dino2))
120120
end
121121
end
@@ -144,7 +144,7 @@
144144
JimBob: !Ref someResource
145145
END
146146

147-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_missing_ref)
147+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_missing_ref)
148148
expect(unresolved_references).to eq Set.new(%w(dino2))
149149
end
150150
end
@@ -170,7 +170,7 @@
170170
Ricky: !Ref someResource.Version
171171
END
172172

173-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_pseudo_refs)
173+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_pseudo_refs)
174174
expect(unresolved_references).to eq Set.new(%w())
175175
end
176176
end
@@ -196,7 +196,7 @@
196196
Ricky: !Ref bogus.Version
197197
END
198198

199-
unresolved_references = ReferenceValidator.new.unresolved_references YAML.load(cfn_yaml_with_pseudo_refs)
199+
unresolved_references = ReferenceValidator.new.unresolved_references YAML.safe_load(cfn_yaml_with_pseudo_refs)
200200
expect(unresolved_references).to eq Set.new(%w(bogus.Version))
201201
end
202202
end

0 commit comments

Comments
 (0)