Skip to content

Commit

Permalink
Merge pull request #112 from attack/safe-load
Browse files Browse the repository at this point in the history
Use YAML#safe_load_file with permitted_classes
  • Loading branch information
JoeSouthan authored Jan 31, 2022
2 parents 553dfb3 + 53bb953 commit 11ef1e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/business/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "yaml"
require "date"
require "pathname"

module Business
class Calendar
Expand Down Expand Up @@ -37,9 +38,10 @@ def self.find_calendar_data(calendar_name)
if path.is_a?(Hash)
break path[calendar_name] if path[calendar_name]
else
next unless File.exist?(File.join(path, "#{calendar_name}.yml"))
calendar_path = Pathname.new(path).join("#{calendar_name}.yml")
next unless calendar_path.exist?

break YAML.load_file(File.join(path, "#{calendar_name}.yml"))
break YAML.safe_load(calendar_path.read, permitted_classes: [Date])
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/business/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
subject(:load_calendar) { described_class.load(calendar) }

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

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

Expand All @@ -25,7 +25,10 @@
after { described_class.load_paths = nil }

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

load_calendar
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/calendars/ecb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ working_days:
- friday

holidays:
- January 1st, 2013
- 2013-01-01
- March 29th, 2013
- April 1st, 2013
- May 1st, 2013
Expand Down

0 comments on commit 11ef1e0

Please sign in to comment.