Skip to content

Commit 32e8aa6

Browse files
authored
Merge pull request rails#46570 from andyw8/andyw8/add-details-to-cookie-overflow-exception
Add details of cookie name and size to `CookieOverflow` exception
2 parents d1eebcb + 0ec8f21 commit 32e8aa6

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add details of cookie name and size to `CookieOverflow` exception.
2+
3+
*Andy Waite*
4+
15
* Don't double log the `controller` or `action` when using `ActiveRecord::QueryLog`
26

37
Previously if you set `config.active_record.query_log_tags` to an array that included

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,9 @@ def parse(name, signed_message, purpose: nil)
632632
def commit(name, options)
633633
options[:value] = @verifier.generate(serialize(options[:value]), **cookie_metadata(name, options))
634634

635-
raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
635+
if options[:value].bytesize > MAX_COOKIE_SIZE
636+
raise CookieOverflow, "#{name} cookie overflowed with size #{options[:value].bytesize} bytes"
637+
end
636638
end
637639
end
638640

@@ -684,7 +686,9 @@ def parse(name, encrypted_message, purpose: nil)
684686
def commit(name, options)
685687
options[:value] = @encryptor.encrypt_and_sign(serialize(options[:value]), **cookie_metadata(name, options))
686688

687-
raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
689+
if options[:value].bytesize > MAX_COOKIE_SIZE
690+
raise CookieOverflow, "#{name} cookie overflowed with size #{options[:value].bytesize} bytes"
691+
end
688692
end
689693
end
690694

actionpack/test/dispatch/cookies_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,10 @@ def test_delete_and_set_cookie
852852
end
853853

854854
def test_raise_data_overflow
855-
assert_raise(ActionDispatch::Cookies::CookieOverflow) do
855+
error = assert_raise(ActionDispatch::Cookies::CookieOverflow) do
856856
get :raise_data_overflow
857857
end
858+
assert_equal "foo cookie overflowed with size 5522 bytes", error.message
858859
end
859860

860861
def test_tampered_cookies

actionpack/test/dispatch/session/cookie_store_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,10 @@ def test_deserializes_unloaded_classes_on_get_value
189189

190190
def test_close_raises_when_data_overflows
191191
with_test_route_set do
192-
assert_raise(ActionDispatch::Cookies::CookieOverflow) {
192+
error = assert_raise(ActionDispatch::Cookies::CookieOverflow) {
193193
get "/raise_data_overflow"
194194
}
195+
assert_equal "_myapp_session cookie overflowed with size 5612 bytes", error.message
195196
end
196197
end
197198

0 commit comments

Comments
 (0)