🐛 bug: prevent negative paginate start overflow#4272
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughRewords IsFromLocal() docs to specify loopback IPs; PageInfo.Start() treats Limit < 1 as invalid and adds an int overflow guard returning maxInt; tests updated with additional Start() edge-case assertions. ChangesSafety and Documentation Improvements
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.1)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@middleware/paginate/page_info.go`:
- Around line 72-73: The overflow guard in the pagination calculation using
p.Page and p.Limit is incorrect: replace the current check that compares p.Page
to (maxInt/p.Limit)+1 with a non-overflowing comparison using (p.Page - 1) >
(maxInt / p.Limit) so the logic in the function/method that computes page end
(referencing p.Page, p.Limit and maxInt in page_info.go) correctly detects
overflow without causing (maxInt/p.Limit)+1 to wrap when Limit == 1; update the
conditional and keep returning maxInt when the new condition is true.
In `@middleware/paginate/paginate_test.go`:
- Around line 127-128: Add a new unit test case to paginate_test.go to cover the
Limit==1 boundary: in the table of test cases that uses PageInfo (e.g., the
slice containing {"Zero page", PageInfo{...}, ...} and the overflow case
PageInfo{Page: int(^uint(0) >> 1), Limit: 100}), add a case with PageInfo{Page:
2, Limit: 1} and an expected result of 1; this ensures the pagination
overflow-guard math in the code that consumes PageInfo is exercised for the
fragile Limit==1 boundary.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 23c2e965-16fe-45c3-bc6c-cbf5f31422ce
📒 Files selected for processing (4)
ctx_interface_gen.gomiddleware/paginate/page_info.gomiddleware/paginate/paginate_test.goreq_interface_gen.go
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4272 +/- ##
==========================================
+ Coverage 91.21% 91.26% +0.04%
==========================================
Files 130 130
Lines 12760 12763 +3
==========================================
+ Hits 11639 11648 +9
+ Misses 709 704 -5
+ Partials 412 411 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the paginate middleware’s PageInfo.Start() calculation to prevent negative/wrapped offsets caused by invalid inputs or integer overflow, and updates tests and regenerated interface files accordingly.
Changes:
- Add input validation and overflow clamping logic to
PageInfo.Start()for safer pagination start computation. - Extend
Test_PageInfoStartwith cases for cursor-modePage=0and very largePagevalues. - Update generated interface comments for
IsFromLocalto clarify it checks for loopback IPs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| middleware/paginate/page_info.go | Adds Start() validation + overflow clamp logic for safer offsets. |
| middleware/paginate/paginate_test.go | Adds regression tests for cursor-mode and overflow-scale inputs. |
| ctx_interface_gen.go | Regenerated interface comment clarifying loopback semantics of IsFromLocal. |
| req_interface_gen.go | Regenerated interface comment clarifying loopback semantics of IsFromLocal. |
|
@copilot apply changes based on the comments in this thread |
There was a problem hiding this comment.
Code Review
This pull request updates the documentation for the IsFromLocal method in the context and request interfaces and introduces overflow protection in the pagination middleware's Start method. A review comment correctly identified a logic error in the new overflow check where a limit of 1 would cause an integer overflow during the comparison, and a safer code suggestion was provided to fix this issue.
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/a567be06-f05a-4b3e-94fa-dd75f292109e Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Addressed in 3d60b0a. The overflow guard now compares |
Motivation
Pageunset or when extremely largepagevalues overflow the multiplication.limitwhilepageandoffsetcould be attacker-controlled and produce negative or wrapped results used as slice/SQL offsets.cursorcould trigger the bug in handlers that follow the documentedpageInfo.Start()usage.Description
PageInfo.Start()to return0ifPage < 1orLimit < 1and to clamp extremely large page-based starts toMaxIntto avoid integer overflow.Test_PageInfoStartcoveringPage = 0(cursor mode) and an overflow-scalePagevalue.ctx_interface_gen.goandreq_interface_gen.go) produced by repository tooling.