You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2026/09/17 17:56:16 UTC

(allura) 09/15: [#8624] bound the pagination offset

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch db/8624b
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 5de302aad44e72aa84672294ac34ca4a8597b262
Author: Dave Brondsema <db...@slashdotmedia.com>
AuthorDate: Tue Sep 8 16:55:30 2026 +0000

    [#8624] bound the pagination offset
    
    handle_paging() capped the page size but not the page number, so the
    resulting offset could be made arbitrarily large from a query string and
    both Solr and Mongo have to walk to it before returning a window. Clamp it
    to page_offset_max, which 0 disables.
    
    Co-Authored-By: Claude Opus 5 <no...@anthropic.com>
    Claude-Session: https://claude.ai/code/session_01CbUqCrWrninjwW3vSrxxbC
---
 Allura/allura/lib/app_globals.py                        |  1 +
 Allura/allura/lib/helpers.py                            | 16 ++++++++++++++++
 Allura/allura/tests/functional/test_root.py             | 12 ++++++++++++
 Allura/allura/tests/test_globals.py                     |  1 +
 Allura/allura/tests/test_helpers.py                     |  3 +++
 Allura/development.ini                                  |  5 +++++
 ForgeTracker/forgetracker/tests/functional/test_root.py |  4 ++++
 7 files changed, 42 insertions(+)

diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index f7e722011..ba369d403 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -468,6 +468,7 @@ def handle_paging(self, limit, page, default=25):
             page = max(int(page), 0)
         except ValueError:
             page = 0
+        page = h.bound_page(page, limit)
         start = page * int(limit)
         return (limit, page, start)
 
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index d51057b7f..e31e688ba 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -744,6 +744,21 @@ def needs_email_verification(user) -> dict | None:
     return None
 
 
+def bound_page(page: int, limit: int) -> int:
+    """Refuse offsets deep enough that Solr and Mongo must walk the whole result set.
+
+    Machines get a 400 so they stop looping; browsers get sent to the last reachable page.
+    """
+    max_offset = asint(tg.config.get('page_offset_max', 50_000))
+    if not max_offset or page * limit <= max_offset:
+        return page
+    max_page = max_offset // limit
+    if request.path.startswith('/rest/') or utils.is_ajax(request):
+        raise webob.exc.HTTPBadRequest(
+            json_body={'error': f'page must be {max_page} or less when limit is {limit}'})
+    tg.redirect(querystring(request, {'page': str(max_page)}))
+
+
 def paging_sanitizer(limit, page, total_count=sys.maxsize, zero_based_pages=True):
     """Return limit, page - both converted to int and constrained to
     valid ranges based on total_count.
@@ -763,6 +778,7 @@ def paging_sanitizer(limit, page, total_count=sys.maxsize, zero_based_pages=True
         page = int(page or 0)
     except ValueError:
         page = 0
+    page = bound_page(page, limit)
     page = min(max(page, (0 if zero_based_pages else 1)), max_page)
     return limit, page
 
diff --git a/Allura/allura/tests/functional/test_root.py b/Allura/allura/tests/functional/test_root.py
index cbc8649dd..765245c05 100644
--- a/Allura/allura/tests/functional/test_root.py
+++ b/Allura/allura/tests/functional/test_root.py
@@ -187,6 +187,18 @@ def test_neighborhood_project_browse(self):
         assert len(
             response.html.find_all('a', {'href': '/adobe/adobe-2/'})) == 1
 
+    def test_page_offset_max_redirects_browser(self):
+        r = self.app.get('/adobe/browse?limit=500&page=200', status=302)
+        assert r.location.endswith('/adobe/browse?limit=500&page=100')
+
+    def test_page_offset_max_rejects_ajax(self):
+        r = self.app.get('/adobe/browse?limit=500&page=200', status=400,
+                         headers={'X-Requested-With': 'XMLHttpRequest'})
+        assert r.json == {'error': 'page must be 100 or less when limit is 500'}
+
+    def test_page_offset_max_allows_last_page(self):
+        self.app.get('/adobe/browse?limit=500&page=100', status=200)
+
     def test_slash_redirect(self):
         self.app.get('/p', status=301)
         self.app.get('/p/', status=302)
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index 64900e261..68e65eb66 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -1087,6 +1087,7 @@ def test_markdown_user_mention_underscores(self, NeighborhoodProjectShortNameVal
 class TestHandlePaging:
 
     def setup_method(self, method):
+        setup_basic_test()
         prefs = {}
         c.user = Mock()
 
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 3a5a98132..4e0b53c5b 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -16,6 +16,7 @@
 #       under the License.
 
 import io
+import sys
 import urllib.request
 from unittest import skipIf
 from os import path
@@ -257,6 +258,8 @@ def test_paging_sanitizer():
         (10, None, 0): (10, 0),
         (10, 0): (10, 0),
         ('junk', 'more junk'): (25, 0),
+        # page_offset_max allows limit * page up to 50000
+        (500, 100, sys.maxsize): (500, 100),
     }
     for input, output in test_data.items():
         assert (h.paging_sanitizer(*input)) == output
diff --git a/Allura/development.ini b/Allura/development.ini
index e3307ab78..81288b246 100644
--- a/Allura/development.ini
+++ b/Allura/development.ini
@@ -714,6 +714,11 @@ lcd_timeout = 60
 ; Allowing exceedingly high values may have a performance impact
 limit_param_max = 500
 
+; Many URLs support a param like page=50  This setting controls the max value allowed for limit * page multiplied together
+; Allowing exceedingly high values may have a performance impact.  Set to 0 for no max.
+; Beyond the max, /rest/ and ajax requests get a 400 and browsers are redirected to the last allowed page.
+page_offset_max = 50000
+
 ; Hide private info (email addresses) in places like commit messages, since they cannot be edited.
 hide_private_info = true
 
diff --git a/ForgeTracker/forgetracker/tests/functional/test_root.py b/ForgeTracker/forgetracker/tests/functional/test_root.py
index 6278e016d..a20649cc3 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_root.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_root.py
@@ -2563,6 +2563,10 @@ def test_rest_tickets(self):
         assert (r['ticket']['attachments'][0]['url'] ==
                 'http://localhost/p/test/bugs/1/attachment/test_root.py')
 
+    def test_rest_tickets_page_offset_max(self):
+        r = self.app.get('/rest/p/test/bugs/?limit=500&page=200', status=400)
+        assert r.json == {'error': 'page must be 100 or less when limit is 500'}
+
     def test_html_escaping(self):
         with mock.patch.object(mail_tasks.smtp_client, '_client') as _client:
             self.new_ticket(summary='test <h2> ticket',