Skip to content

Commit 11ef1e0

Browse files
authored
Merge pull request #112 from attack/safe-load
Use YAML#safe_load_file with permitted_classes
2 parents 553dfb3 + 53bb953 commit 11ef1e0

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

lib/business/calendar.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "yaml"
44
require "date"
5+
require "pathname"
56

67
module Business
78
class Calendar
@@ -37,9 +38,10 @@ def self.find_calendar_data(calendar_name)
3738
if path.is_a?(Hash)
3839
break path[calendar_name] if path[calendar_name]
3940
else
40-
next unless File.exist?(File.join(path, "#{calendar_name}.yml"))
41+
calendar_path = Pathname.new(path).join("#{calendar_name}.yml")
42+
next unless calendar_path.exist?
4143

42-
break YAML.load_file(File.join(path, "#{calendar_name}.yml"))
44+
break YAML.safe_load(calendar_path.read, permitted_classes: [Date])
4345
end
4446
end
4547
end

spec/business/calendar_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
subject(:load_calendar) { described_class.load(calendar) }
1414

1515
let(:dummy_calendar) { { "working_days" => ["monday"] } }
16+
let(:fixture_path) { File.join(File.dirname(__FILE__), "../fixtures", "calendars") }
1617

1718
before do
18-
fixture_path = File.join(File.dirname(__FILE__), "../fixtures", "calendars")
1919
described_class.load_paths = [fixture_path, { "foobar" => dummy_calendar }]
2020
end
2121

@@ -25,7 +25,10 @@
2525
after { described_class.load_paths = nil }
2626

2727
it "loads the yaml file" do
28-
expect(YAML).to receive(:load_file).with(/ecb\.yml$/).and_return({})
28+
path = Pathname.new(fixture_path).join("ecb.yml")
29+
expect(YAML).to receive(:safe_load).
30+
with(path.read, permitted_classes: [Date]).
31+
and_return({})
2932

3033
load_calendar
3134
end

spec/fixtures/calendars/ecb.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ working_days:
66
- friday
77

88
holidays:
9-
- January 1st, 2013
9+
- 2013-01-01
1010
- March 29th, 2013
1111
- April 1st, 2013
1212
- May 1st, 2013

0 commit comments

Comments
 (0)