Skip to content

Commit dd6fcc4

Browse files
jhawthorngmcgibbon
andcommitted
Call inspect on ids in RecordNotFound error
[CVE-2025-55193] Co-authored-by: Gannon McGibbon <[email protected]>
1 parent 1ca278a commit dd6fcc4

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def find(*ids) # :nodoc:
276276
return super if StatementCache.unsupported_value?(id)
277277

278278
cached_find_by([primary_key], [id]) ||
279-
raise(RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}", name, primary_key, id))
279+
raise(RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id.inspect}", name, primary_key, id))
280280
end
281281

282282
def find_by(*args) # :nodoc:

activerecord/lib/active_record/relation/finder_methods.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,13 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
424424
error << " with#{conditions}" if conditions
425425
raise RecordNotFound.new(error, name, key)
426426
elsif Array.wrap(ids).size == 1
427-
error = "Couldn't find #{name} with '#{key}'=#{ids}#{conditions}"
427+
id = Array.wrap(ids)[0]
428+
error = "Couldn't find #{name} with '#{key}'=#{id.inspect}#{conditions}"
428429
raise RecordNotFound.new(error, name, key, ids)
429430
else
430431
error = +"Couldn't find all #{name.pluralize} with '#{key}': "
431-
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})."
432-
error << " Couldn't find #{name.pluralize(not_found_ids.size)} with #{key.to_s.pluralize(not_found_ids.size)} #{not_found_ids.join(', ')}." if not_found_ids
432+
error << "(#{ids.map(&:inspect).join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})."
433+
error << " Couldn't find #{name.pluralize(not_found_ids.size)} with #{key.to_s.pluralize(not_found_ids.size)} #{not_found_ids.map(&:inspect).join(', ')}." if not_found_ids
433434
raise RecordNotFound.new(error, name, key, ids)
434435
end
435436
end

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ def test_collection_singular_ids_through_setter_raises_exception_when_invalid_id
10661066
author = authors(:david)
10671067
ids = [categories(:general).name, "Unknown"]
10681068
e = assert_raises(ActiveRecord::RecordNotFound) { author.essay_category_ids = ids }
1069-
msg = "Couldn't find all Categories with 'name': (General, Unknown) (found 1 results, but was looking for 2). Couldn't find Category with name Unknown."
1069+
msg = %{Couldn't find all Categories with 'name': ("General", "Unknown") (found 1 results, but was looking for 2). Couldn't find Category with name "Unknown".}
10701070
assert_equal msg, e.message
10711071
end
10721072

activerecord/test/cases/finder_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ def test_find_one_message_with_custom_primary_key
18731873
e = assert_raises(ActiveRecord::RecordNotFound) do
18741874
model.find "Hello World!"
18751875
end
1876-
assert_equal "Couldn't find MercedesCar with 'name'=Hello World!", e.message
1876+
assert_equal %{Couldn't find MercedesCar with 'name'="Hello World!"}, e.message
18771877
end
18781878
end
18791879

@@ -1883,7 +1883,7 @@ def test_find_some_message_with_custom_primary_key
18831883
e = assert_raises(ActiveRecord::RecordNotFound) do
18841884
model.find "Hello", "World!"
18851885
end
1886-
assert_equal "Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2).", e.message
1886+
assert_equal %{Couldn't find all MercedesCars with 'name': ("Hello", "World!") (found 0 results, but was looking for 2).}, e.message
18871887
end
18881888
end
18891889

0 commit comments

Comments
 (0)