🐛 bug: enforce paginate sort allowlist when AllowedSorts is unset#4276
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
WalkthroughThis PR tightens sort-field validation in the paginate middleware so that empty or nil ChangesPaginate Sort Query Validation
Request Interface Documentation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Code Review
This pull request clarifies the documentation for the IsFromLocal method across the context and request interfaces and modifies the pagination middleware's sorting logic. The AllowedSorts configuration now ignores requested sort fields if the whitelist is nil or empty, defaulting to the specified default sort instead of allowing all fields. Corresponding updates were made to the documentation and test suite to reflect this change in behavior. I have no feedback to provide as there were no review comments.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4276 +/- ##
==========================================
+ Coverage 91.21% 91.26% +0.04%
==========================================
Files 130 130
Lines 12760 12760
==========================================
+ Hits 11639 11645 +6
+ 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 restores safe pagination sorting defaults by requiring an explicit AllowedSorts allowlist for any client-supplied sort fields, preventing unvalidated sort text from propagating to downstream consumers (e.g., SQL ORDER BY).
Changes:
- Enforced allowlist-only behavior in
parseSortQuery(no “permit all when allowlist is empty” fallback). - Updated pagination tests to expect default sorting when
AllowedSortsis nil. - Clarified
AllowedSortssemantics in paginate docs and regenerated interface artifacts with updatedIsFromLocalwording.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
middleware/paginate/paginate.go |
Enforces that only explicitly allowed sort fields are accepted; otherwise falls back to DefaultSort. |
middleware/paginate/paginate_test.go |
Updates expectations so nil AllowedSorts ignores request sorts and uses the default. |
docs/middleware/paginate.md |
Documents that nil/empty AllowedSorts ignores request sort and uses the default sort. |
ctx_interface_gen.go |
Updates generated interface comment for IsFromLocal to reflect loopback semantics. |
req_interface_gen.go |
Updates generated interface comment for IsFromLocal to reflect loopback semantics. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Motivation
parseSortQuerybecame permissive whenAllowedSortswas nil or empty, which allowed attacker-controlled sort text to reach downstream consumers (e.g., SQL ORDER BY) and created an injection risk.DefaultSortwhen no whitelist is configured rather than implicitly permitting all client-supplied fields.Description
parseSortQueryinmiddleware/paginate/paginate.goto accept a sort field only when it is explicitly present inAllowedSorts(removed thelen(allowedSorts) == 0permit-all branch).middleware/paginate/paginate_test.goso nil/emptyAllowedSortsfalls back toDefaultSortand tests assert the safe behavior.AllowedSortssemantics indocs/middleware/paginate.mdto state that nil/empty allowlist causes request sort fields to be ignored and the default sort to be used.ctx_interface_gen.go,req_interface_gen.go) as part of repository checks.