Skip to content

Commit 2a72b30

Browse files
committed
Avoid exceeding 4K cookie size limit by setting an upper limit on path size when using login_return_to_requested_location?
Returning to requested location is a nice to have, but not a hard requirement, and few legitimate cases will exceed the limit. Not having a limit allows an easy way to unauthenticated users to force cookie too large exceptions in applications. This adds a login_return_to_requested_location_max_path_size configuration method to change the limit.
1 parent d44ae3c commit 2a72b30

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
=== master
2+
3+
* Avoid exceeding 4K cookie size limit by setting an upper limit on path size when using login_return_to_requested_location? (jeremyevans)
4+
15
=== 2.38.0 (2025-01-15)
26

37
* Make verify-account-resend page work if verify_account_resend_explanatory_text calls verify_account_email_recently_sent? (jeremyevans)

doc/login.rdoc

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ login_page_title :: The page title to use on the login form.
2323
login_redirect :: Where to redirect after a sucessful login.
2424
login_redirect_session_key :: The key in the session hash storing the location to redirect to after successful login.
2525
login_return_to_requested_location? :: Whether to redirect to the originally requested location after successful login when +require_login+ was used, false by default.
26+
login_return_to_requested_location_max_path_size :: The maximum path size in bytes to allow when returning to requested location, 2048 by default to avoid exceeding the 4K cookie size limit
2627
login_route :: The route to the login action. Defaults to +login+.
2728
multi_phase_login_forms :: An array of entries for authentication methods that can be used to login when using multi phase login. Each entry is an array of three elements, sort order (integer), HTML, and method to call if this entry is the only authentication method available (or nil to not call a method).
2829
multi_phase_login_page_title :: The page title to use on the login form after login has been entered when using multi phase login.

lib/rodauth/features/login.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Rodauth
1515
auth_value_method :login_error_status, 401
1616
translatable_method :login_form_footer_links_heading, '<h2 class="rodauth-login-form-footer-links-heading">Other Options</h2>'
1717
auth_value_method :login_return_to_requested_location?, false
18+
auth_value_method :login_return_to_requested_location_max_path_size, 2048
1819
auth_value_method :use_multi_phase_login?, false
1920

2021
session_key :login_redirect_session_key, :login_redirect
@@ -95,7 +96,7 @@ def login(auth_type)
9596
end
9697

9798
def login_required
98-
if login_return_to_requested_location? && (path = login_return_to_requested_location_path)
99+
if login_return_to_requested_location? && (path = login_return_to_requested_location_path) && path.bytesize <= login_return_to_requested_location_max_path_size
99100
set_session_value(login_redirect_session_key, path)
100101
end
101102
super

spec/login_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,33 @@
191191
page.html.must_include 'Passed Login Required: bar'
192192
end
193193

194+
it "should not return to requested path size if it is too long" do
195+
path = "/a"*1024
196+
rodauth do
197+
enable :login, :logout
198+
login_return_to_requested_location? true
199+
login_return_to_requested_location_path do
200+
path
201+
end
202+
login_redirect '/'
203+
end
204+
roda do |r|
205+
r.rodauth
206+
rodauth.require_login
207+
""
208+
end
209+
210+
visit '/page'
211+
login(:visit=>false)
212+
page.current_path.must_equal path
213+
214+
path = "/a"*4096
215+
logout
216+
visit '/page'
217+
login(:visit=>false)
218+
page.current_path.must_equal "/"
219+
end
220+
194221
it "should not allow login to unverified account" do
195222
rodauth do
196223
enable :login

0 commit comments

Comments
 (0)