Skip to content

Commit 5829feb

Browse files
geofffrankstcdowney
authored andcommitted
Introduce a limit to number of destinations per rule
Signed-off-by: Marc Paquette <[email protected]> Signed-off-by: Geoff Franks <[email protected]>
1 parent efc2f4f commit 5829feb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

app/messages/validators/security_group_rule_validator.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class RulesValidator < ActiveModel::Validator
1111
type
1212
].freeze
1313

14+
MAX_DESTINATIONS_PER_RULE = 6000
15+
1416
def validate(record)
1517
unless record.rules.is_a?(Array)
1618
record.errors.add :rules, 'must be an array'
@@ -28,7 +30,10 @@ def validate(record)
2830
add_rule_error("protocol must be 'tcp', 'udp', 'icmp', or 'all'", record, index) unless valid_protocol(rule[:protocol])
2931

3032
if valid_destination_type(rule[:destination], record, index)
31-
rule[:destination].split(',').each do |d|
33+
rules = rule[:destination].split(',')
34+
add_rule_error("maximum destinations per rule exceeded - must be under #{MAX_DESTINATIONS_PER_RULE}", record, index) unless rules.length <= MAX_DESTINATIONS_PER_RULE
35+
36+
rules.each do |d|
3237
validate_destination(d, record, index)
3338
end
3439
end

spec/unit/messages/validators/security_group_rule_validator_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,22 @@ def self.name
388388
expect(subject.errors.full_messages).to include expected_error
389389
end
390390
end
391+
392+
context 'more than 6000 destinations per rule' do
393+
let(:rules) do
394+
[
395+
{
396+
protocol: 'all',
397+
destination: (['192.168.1.3'] * 7000).join(',')
398+
}
399+
]
400+
end
401+
402+
it 'throws an error' do
403+
expect(subject).not_to be_valid
404+
expect(subject.errors.full_messages).to include 'Rules[0]: maximum destinations per rule exceeded - must be under 6000'
405+
end
406+
end
391407
end
392408
end
393409

0 commit comments

Comments
 (0)