🐛 bug: avoid reading omitted request bodies in Bind.All - #4565
Conversation
Walkthrough
ChangesBody binder precedence guard
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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.2)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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4565 +/- ##
==========================================
- Coverage 93.29% 93.24% -0.06%
==========================================
Files 140 140
Lines 14854 14853 -1
==========================================
- Hits 13858 13849 -9
- Misses 620 627 +7
- Partials 376 377 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
bind_test.go (1)
3044-3063: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd coverage for the no-
Content-Typeand default-precedence paths.This test only exercises custom
queryprecedence withContent-Typepresent, so it cannot catch the new condition reading a streamed body before checking the header or regressions in the default branch.🤖 Prompt for 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. In `@bind_test.go` around lines 3044 - 3063, Extend Test_Bind_All_CustomPrecedence_DoesNotReadOmittedBody with coverage for a streamed request that has no Content-Type, verifying query-only binding still uses the query value without consuming the body. Add a separate assertion for the default-precedence binding path to confirm omitted-body sources are not read and existing precedence behavior remains intact.
🤖 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 `@bind.go`:
- Around line 527-528: In both custom-precedence and default-precedence
body-binding branches, reorder the conditions so ContentType() is checked before
Request().Body(). Update the checks at bind.go lines 527-528 and 543-544;
preserve the existing body-binding behavior after the content type is confirmed.
---
Nitpick comments:
In `@bind_test.go`:
- Around line 3044-3063: Extend
Test_Bind_All_CustomPrecedence_DoesNotReadOmittedBody with coverage for a
streamed request that has no Content-Type, verifying query-only binding still
uses the query value without consuming the body. Add a separate assertion for
the default-precedence binding path to confirm omitted-body sources are not read
and existing precedence behavior remains intact.
🪄 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 Plus
Run ID: 8436ac3c-8460-43de-a73a-4adf58e26b1b
📒 Files selected for processing (2)
bind.gobind_test.go
Motivation
Bind.Alleagerly inspectedRequest().Body()before honoring a struct'sbinding_sourceprecedence, which can force buffering of streamed request bodies and undermineStreamRequestBodystreaming/DoS protections.Description
hasBodyprecomputation and defer callingRequest().Body()until thebodysource is actually selected in the custom-precedence loop or the default precedence path.bodyis included in precedence (default or custom) so body-binding still occurs when intended.Test_Bind_All_CustomPrecedence_DoesNotReadOmittedBodythat usesSetBodyStreamand asserts a query-onlybinding_source:"query"does not consume the request body stream.fix(bind): avoid reading omitted request bodiesand PR metadata created with the same summary title.Testing
make generate,make betteralign,make format, andmake lint, all of which completed successfully (lint reported0 issues).make test, which completed successfully: the test suite reportedPASSfor all tests (4191 tests, 1 skipped) and the new regression test passed.make audit;go mod verifyandgo vetcompleted, butgovulncheckin the execution environment reported pre-existing standard-library vulnerabilities (Go 1.25.1), causingmake auditto exit non-zero; this is an environmental/toolchain issue and not caused by these code changes.Codex Task