Skip to content

Commit aa5d851

Browse files
committed
Only convert Hash#values_at if all keys are known
Fixes #1635
1 parent f283ecd commit aa5d851

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/brakeman/processors/alias_processor.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,13 @@ def process_call exp
324324
end
325325
when :values_at
326326
if node_type? target, :hash
327-
exp = hash_values_at target, exp.args
327+
res = hash_values_at target, exp.args
328+
329+
# Only convert to array of values if _all_ keys
330+
# are present in the hash.
331+
unless res.any?(&:nil?)
332+
exp = res
333+
end
328334
end
329335
end
330336

lib/brakeman/processors/lib/call_conversion_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def process_hash_access hash, index, original_exp = nil
8989
end
9090
end
9191

92+
# You must check the return value for `nil`s -
93+
# which indicate a key could not be found.
9294
def hash_values_at hash, keys
9395
values = keys.map do |key|
9496
process_hash_access hash, key

test/tests/alias_processor.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ def test_hash_values_at
273273
end
274274

275275
def test_hash_values_at_missing
276+
assert_alias '{ a: 1, b: 2, c: x }.values_at(:a, :b, :z)', <<-RUBY
277+
h = { a: 1, b: 2, c: x }
278+
h.values_at(:a, :b, :z)
279+
RUBY
280+
end
281+
282+
def test_hash_values_at_missing_safe
276283
assert_alias '[1, 2, :BRAKEMAN_SAFE_LITERAL]', <<-RUBY
277284
h = { a: 1, b: 2, c: 3 }
278285
h.values_at(:a, :b, :z)

0 commit comments

Comments
 (0)