Skip to content

Commit c9292c4

Browse files
authored
Merge pull request #373 from Dorian/bugs/more-invalid-rules
Bugs: Fix more invalid rules
2 parents 8d3e191 + 51d28e0 commit c9292c4

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

lib/ice_cube/parsers/ical_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def self.rule_from_ical(ical)
3131

3232
ical.split(';').each do |rule|
3333
(name, value) = rule.split('=')
34+
raise ArgumentError, "Invalid iCal rule component" if value.nil?
3435
value.strip!
3536
case name
3637
when 'FREQ'

lib/ice_cube/rule.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,42 @@ class << self
7373
# Convert from a hash and create a rule
7474
def from_hash(original_hash)
7575
hash = IceCube::FlexibleHash.new original_hash
76-
return nil unless match = hash[:rule_type].match(/\:\:(.+?)Rule/)
76+
77+
unless hash[:rule_type] && match = hash[:rule_type].match(/\:\:(.+?)Rule/)
78+
raise ArgumentError, 'Invalid rule type'
79+
end
7780

7881
interval_type = match[1].downcase.to_sym
79-
raise ArgumentError, "Invalid rule frequency type: #{match[1]}" unless INTERVAL_TYPES.include?(interval_type)
82+
83+
unless INTERVAL_TYPES.include?(interval_type)
84+
raise ArgumentError, "Invalid rule frequency type: #{match[1]}"
85+
end
8086

8187
rule = IceCube::Rule.send(interval_type, hash[:interval] || 1)
82-
rule.interval(hash[:interval] || 1, TimeUtil.wday_to_sym(hash[:week_start] || 0)) if match[1] == "Weekly"
88+
89+
if match[1] == "Weekly"
90+
rule.interval(hash[:interval] || 1, TimeUtil.wday_to_sym(hash[:week_start] || 0))
91+
end
92+
8393
rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until]
8494
rule.count(hash[:count]) if hash[:count]
95+
8596
hash[:validations] && hash[:validations].each do |name, args|
8697
apply_validation(rule, name, args)
8798
end
99+
88100
rule
89101
end
90102

91103
private
92104

93105
def apply_validation(rule, name, args)
94106
name = name.to_sym
95-
raise ArgumentError, "Invalid rule validation type: #{name}" unless ValidatedRule::VALIDATION_ORDER.include?(name)
107+
108+
unless ValidatedRule::VALIDATION_ORDER.include?(name)
109+
raise ArgumentError, "Invalid rule validation type: #{name}"
110+
end
111+
96112
args.is_a?(Array) ? rule.send(name, *args) : rule.send(name, args)
97113
end
98114

spec/examples/from_ical_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,36 @@ def sorted_ical(ical)
399399
let(:ical_str) { 'RRULE::' }
400400
it_behaves_like 'an invalid ical string'
401401
end
402+
403+
describe 'invalid rules' do
404+
let(:ical_str) { 'RRULE::A' }
405+
it_behaves_like 'an invalid ical string'
406+
end
407+
408+
describe 'incomplete rule' do
409+
let(:ical_str) { 'RRULE:FREQ' }
410+
it_behaves_like 'an invalid ical string'
411+
end
412+
413+
describe 'invalid rule with invalid sensitive key' do
414+
let(:ical_str) { 'RRULE:FREQ=WEKLY;WKST=SU' }
415+
it_behaves_like 'an invalid ical string'
416+
end
417+
418+
describe 'invalid rule with invalid value' do
419+
let(:ical_str) { 'RRULE:FREQ=WEEKLY;BYDAY=MO,WE,FR;WKST=SE' }
420+
it_behaves_like 'an invalid ical string'
421+
end
422+
423+
describe 'invalid rule with invalid key' do
424+
let(:ical_str) { 'RRULE:FREQ=WEEKLY;BDAY=MO,WE,FR;WKST=SU' }
425+
it_behaves_like 'an invalid ical string'
426+
end
427+
428+
describe 'invalid rule with attempt to execute code' do
429+
let(:ical_str) { 'RRULE:FREQ=to_yaml' }
430+
it_behaves_like 'an invalid ical string'
431+
end
402432
end
403433
end
404434

0 commit comments

Comments
 (0)