Skip to content

🐛 bug: copy FullURL string before returning pooled buffer#4275

Merged
ReneWerner87 merged 3 commits into
mainfrom
fix-fullurl-unsafe-string-aliasing
May 11, 2026
Merged

🐛 bug: copy FullURL string before returning pooled buffer#4275
ReneWerner87 merged 3 commits into
mainfrom
fix-fullurl-unsafe-string-aliasing

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented May 11, 2026

Motivation

  • Fix a use-after-pool/aliasing vulnerability in DefaultCtx.FullURL() where the method returned a string that could alias memory from a pooled bytebufferpool buffer that was returned to the pool before the caller could safely use the string.

Description

  • Change DefaultCtx.FullURL() to return a safe copy using buf.String() instead of returning c.app.toString(buf.Bytes()), preventing returned strings from referencing pooled memory that is recycled immediately. (see req.go).
  • Regenerate related generated interface files that updated method comments/signatures formatting (ctx_interface_gen.go, req_interface_gen.go).

Copilot AI review requested due to automatic review settings May 11, 2026 02:47
@gaby gaby added the codex label May 11, 2026
@gaby gaby requested a review from a team as a code owner May 11, 2026 02:47
@gaby gaby added the aardvark label May 11, 2026
@gaby gaby requested review from ReneWerner87, efectn and sixcolors May 11, 2026 02:47
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6527bb8b-e616-441a-aa36-9c2d0e5807a9

📥 Commits

Reviewing files that changed from the base of the PR and between f9cd6ba and 5227db4.

📒 Files selected for processing (1)
  • ctx_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • ctx_test.go

Walkthrough

FullURL() now returns the buffer's string via buf.String(); a test was added to ensure FullURL does not alias pooled buffer memory. Generated Ctx/Req interfaces’ IsFromLocal() comments were clarified to specify loopback-IP semantics.

Changes

Request and Context Interface Updates

Layer / File(s) Summary
FullURL Implementation & Test
req.go, ctx_test.go
(*DefaultCtx).FullURL() now returns buf.String() instead of c.app.toString(buf.Bytes()). Added Test_Ctx_FullURL_DoesNotAliasPooledBuffer to verify the returned string isn't affected by later pooled-buffer mutations.
IsFromLocal Interface Documentation
ctx_interface_gen.go, req_interface_gen.go
Updated IsFromLocal() documentation in generated interfaces to state it returns true when the request originates from a loopback IP; method signatures unchanged.

Sequence Diagram(s)

sequenceDiagram
  participant DefaultCtx
  participant bytebufferpool
  participant Test
  DefaultCtx->>bytebufferpool: acquire buffer and build URL
  DefaultCtx->>DefaultCtx: return buf.String()
  Test->>bytebufferpool: mutate pooled buffers repeatedly
  Test->>DefaultCtx: assert returned fullURL remains unchanged
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • gofiber/fiber#4007: Also modifies FullURL implementation and touches generated context interfaces.
  • gofiber/fiber#3835: Related work addressing pooled-buffer aliasing and safe byte-to-string conversions.
  • gofiber/fiber#3533: Also touches IsFromLocal docs/signatures and FullURL string handling.

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐰 I nibble buffers, tidy strings with care,
Loopback notes tucked in docs so fair,
A test hops in to guard what's true,
No pooled-alias ghost will haunt you,
Small fixes, big peace — thump, thump, hooray!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description lacks required template sections like Changes introduced checklist items, Type of change, and Checklist verification items that are specified in the repository template. Complete the template by adding the Changes introduced section with relevant checkboxes (Performance improvement, Code consistency), Type of change selection, and Checklist items to verify compliance.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main bug fix: preventing FullURL from aliasing pooled buffer memory. It directly relates to the core change in req.go.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-fullurl-unsafe-string-aliasing

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ReneWerner87 ReneWerner87 added this to v3 May 11, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 11, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the documentation for the IsFromLocal method in the Ctx and Req interfaces to clarify that it checks for loopback IPs. Additionally, the FullURL method in req.go was refactored to use buf.String() directly, which is a more standard and efficient way to retrieve the string from a buffer. I have no feedback to provide as there were no review comments.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.26%. Comparing base (c48bbc2) to head (5227db4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4275      +/-   ##
==========================================
+ 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     
Flag Coverage Δ
unittests 91.26% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a pooled-buffer aliasing risk in DefaultCtx.FullURL() by ensuring the returned string cannot reference memory that is returned to bytebufferpool before the caller uses it. It also updates regenerated interface files to reflect updated method documentation.

Changes:

  • Make DefaultCtx.FullURL() return a safe, detached string copy (buf.String()) instead of using the app’s unsafe byte-slice-to-string conversion.
  • Regenerate ctx_interface_gen.go and req_interface_gen.go, updating the IsFromLocal doc comment to clarify it refers to loopback IPs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
req.go Fix FullURL() to return a non-aliased string (safe vs pooled buffer reuse).
req_interface_gen.go Regenerated interface comment update for IsFromLocal.
ctx_interface_gen.go Regenerated interface comment update for IsFromLocal.

Comment thread req.go
@gaby
Copy link
Copy Markdown
Member Author

gaby commented May 11, 2026

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

@copilot apply changes based on the comments in this thread

Addressed in 5227db4. I added a focused regression test in ctx_test.go that captures FullURL(), exercises pooled buffer reuse, and verifies the returned string stays unchanged.

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 456afdd Previous: c48bbc2 Ratio
Benchmark_Ctx_FullURL (github.com/gofiber/fiber/v3) - B/op 48 B/op 0 B/op +∞
Benchmark_Ctx_FullURL (github.com/gofiber/fiber/v3) - allocs/op 1 allocs/op 0 allocs/op +∞

This comment was automatically generated by workflow using github-action-benchmark.

Copilot finished work on behalf of gaby May 11, 2026 03:00
@gaby
Copy link
Copy Markdown
Member Author

gaby commented May 11, 2026

Performance notice is expected since we are creating a string. Previous implementation was returning a buffer that was already deferred.

@ReneWerner87 ReneWerner87 merged commit 69bd5b3 into main May 11, 2026
21 checks passed
@ReneWerner87 ReneWerner87 deleted the fix-fullurl-unsafe-string-aliasing branch May 11, 2026 06:45
@github-project-automation github-project-automation Bot moved this to Done in v3 May 11, 2026
@ReneWerner87 ReneWerner87 modified the milestones: v3, v3.3.0 May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants