feat: resolve client IP from X-Forwarded-For/X-Real-IP headers and log error response body on 4xx/5xx - #3985
Conversation
📝 WalkthroughWalkthroughAdds an unexported helper that returns the leftmost X-Forwarded-For or X-Real-IP (or "") and updates the CORS middleware completion log to emit http.forwarded_for, include trace_id when present, and attach http.error from non-streaming response bodies for status >= 400. ChangesReverse-proxy-aware request logging
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 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)
Comment |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
X-Forwarded-For/X-Real-IP headers and log error response body on 4xx/5xx
Confidence Score: 5/5The change is safe to merge; it adds logging-only fields with no effect on request routing, auth, or response semantics. Both additions are isolated to the access-log defer block. The IsBodyStream() guard correctly mirrors the existing deferred-trace pattern, preventing any race with streaming responses. No control flow, auth, or data-path logic is altered. No files require special attention. Important Files Changed
Reviews (2): Last reviewed commit: "feat: log request body in case of 4xx+" | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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 `@transports/bifrost-http/handlers/middlewares.go`:
- Around line 52-63: The clientIP helper (clientIP(ctx *fasthttp.RequestCtx))
currently trusts X-Forwarded-For/X-Real-IP unconditionally; add a new config
field in transports/config.schema.json (e.g., trusted_proxies: array of CIDR/IP)
and change clientIP to only honor X-Forwarded-For / X-Real-IP when
ctx.RemoteAddr() is in that trusted_proxies list; otherwise return
ctx.RemoteAddr().String(). Ensure the code that loads transport config exposes
the trusted_proxies (parsed as net.IPNet/CIDRs) and reference that configuration
from where clientIP is called or make it accept the config/context so the
header-trusting behavior is gated by the trusted_proxies setting.
- Around line 92-95: The current error-body logging in middlewares.go
unconditionally attaches ctx.Response.Body() for statusCode >= 400; change it to
first consult the content-logging controls (the global disable_content_logging
flag and the per-request content-storage overrides) and only call
logBuilder.Str("http.error", string(body)) when content logging is permitted.
Locate the request/response metadata (e.g., the per-request overrides and
disable_content_logging flag available on the request context or request
metadata object used in this middleware), evaluate whether content logging is
allowed for this request, and gate the ctx.Response.Body() read and
logBuilder.Str call behind that check so error bodies are logged only when
allowed.
- Around line 92-95: The CorsMiddleware log path currently calls
ctx.Response.Body() for statusCode >= 400 which can drain/destructively consume
fasthttp response bodies; update the branch that sets
logBuilder.Str("http.error", ...) so it first reuses the same guards used in
fasthttpResponseToHTTPResponse (deferred trace completion,
large-payload/large-response mode checks and the large-response threshold) and
also respects the disable_content_logging flag / x-bf-disable-content-logging
header; only if all those guards allow materializing the body should you call
ctx.Response.Body() and set http.error, otherwise skip adding http.error to the
log to avoid corrupting chunked/SSE/deferred responses.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 707b1031-48ef-4770-9019-12696a056111
📒 Files selected for processing (1)
transports/bifrost-http/handlers/middlewares.go
23011cc to
8a9dacb
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Merge activity
|
…d log error response body on 4xx/5xx (#3985) ## Summary Improves HTTP request logging by resolving the true client IP from reverse-proxy headers and including error response bodies in logs for failed requests. ## Issues Closes #3904 ## Changes - Added a `clientIP` helper that extracts the originating client IP by checking `X-Forwarded-For` (taking the leftmost entry in comma-separated lists), then `X-Real-IP`, and finally falling back to the direct peer address. This ensures accurate client identification when Bifrost sits behind a reverse proxy. - Replaced the direct `ctx.RemoteAddr().String()` call in the `http.remote_addr` log field with `clientIP(ctx)` so logs reflect the real client rather than the proxy. - Added logging of the response body as `http.error` for any request that results in a 4xx or 5xx status code, making it easier to diagnose failures from logs alone. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor - [ ] Documentation - [ ] Chore/CI ## Affected areas - [ ] Core (Go) - [x] Transports (HTTP) - [ ] Providers/Integrations - [ ] Plugins - [ ] UI (React) - [ ] Docs ## How to test ```sh go test ./... ``` - Deploy Bifrost behind a reverse proxy that sets `X-Forwarded-For` or `X-Real-IP` headers and verify that `http.remote_addr` in logs reflects the originating client IP rather than the proxy address. - Trigger a request that returns a 4xx or 5xx response and confirm the `http.error` field appears in the log output with the response body. ## Screenshots/Recordings N/A ## Breaking changes - [ ] Yes - [x] No ## Related issues N/A ## Security considerations The `clientIP` helper trusts `X-Forwarded-For` and `X-Real-IP` headers as provided by upstream proxies. If Bifrost is exposed directly to the internet without a trusted reverse proxy, these headers could be spoofed by clients, resulting in inaccurate IP logging. Ensure Bifrost is always deployed behind a trusted proxy when relying on these values. ## Checklist - [ ] I read `docs/contributing/README.md` and followed the guidelines - [ ] I added/updated tests where appropriate - [ ] I updated documentation where needed - [ ] I verified builds succeed (Go and UI) - [ ] I verified the CI pipeline passes locally if applicablecs
…d log error response body on 4xx/5xx (#3985) ## Summary Improves HTTP request logging by resolving the true client IP from reverse-proxy headers and including error response bodies in logs for failed requests. ## Issues Closes #3904 ## Changes - Added a `clientIP` helper that extracts the originating client IP by checking `X-Forwarded-For` (taking the leftmost entry in comma-separated lists), then `X-Real-IP`, and finally falling back to the direct peer address. This ensures accurate client identification when Bifrost sits behind a reverse proxy. - Replaced the direct `ctx.RemoteAddr().String()` call in the `http.remote_addr` log field with `clientIP(ctx)` so logs reflect the real client rather than the proxy. - Added logging of the response body as `http.error` for any request that results in a 4xx or 5xx status code, making it easier to diagnose failures from logs alone. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor - [ ] Documentation - [ ] Chore/CI ## Affected areas - [ ] Core (Go) - [x] Transports (HTTP) - [ ] Providers/Integrations - [ ] Plugins - [ ] UI (React) - [ ] Docs ## How to test ```sh go test ./... ``` - Deploy Bifrost behind a reverse proxy that sets `X-Forwarded-For` or `X-Real-IP` headers and verify that `http.remote_addr` in logs reflects the originating client IP rather than the proxy address. - Trigger a request that returns a 4xx or 5xx response and confirm the `http.error` field appears in the log output with the response body. ## Screenshots/Recordings N/A ## Breaking changes - [ ] Yes - [x] No ## Related issues N/A ## Security considerations The `clientIP` helper trusts `X-Forwarded-For` and `X-Real-IP` headers as provided by upstream proxies. If Bifrost is exposed directly to the internet without a trusted reverse proxy, these headers could be spoofed by clients, resulting in inaccurate IP logging. Ensure Bifrost is always deployed behind a trusted proxy when relying on these values. ## Checklist - [ ] I read `docs/contributing/README.md` and followed the guidelines - [ ] I added/updated tests where appropriate - [ ] I updated documentation where needed - [ ] I verified builds succeed (Go and UI) - [ ] I verified the CI pipeline passes locally if applicablecs
…d log error response body on 4xx/5xx (#3985) ## Summary Improves HTTP request logging by resolving the true client IP from reverse-proxy headers and including error response bodies in logs for failed requests. ## Issues Closes #3904 ## Changes - Added a `clientIP` helper that extracts the originating client IP by checking `X-Forwarded-For` (taking the leftmost entry in comma-separated lists), then `X-Real-IP`, and finally falling back to the direct peer address. This ensures accurate client identification when Bifrost sits behind a reverse proxy. - Replaced the direct `ctx.RemoteAddr().String()` call in the `http.remote_addr` log field with `clientIP(ctx)` so logs reflect the real client rather than the proxy. - Added logging of the response body as `http.error` for any request that results in a 4xx or 5xx status code, making it easier to diagnose failures from logs alone. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor - [ ] Documentation - [ ] Chore/CI ## Affected areas - [ ] Core (Go) - [x] Transports (HTTP) - [ ] Providers/Integrations - [ ] Plugins - [ ] UI (React) - [ ] Docs ## How to test ```sh go test ./... ``` - Deploy Bifrost behind a reverse proxy that sets `X-Forwarded-For` or `X-Real-IP` headers and verify that `http.remote_addr` in logs reflects the originating client IP rather than the proxy address. - Trigger a request that returns a 4xx or 5xx response and confirm the `http.error` field appears in the log output with the response body. ## Screenshots/Recordings N/A ## Breaking changes - [ ] Yes - [x] No ## Related issues N/A ## Security considerations The `clientIP` helper trusts `X-Forwarded-For` and `X-Real-IP` headers as provided by upstream proxies. If Bifrost is exposed directly to the internet without a trusted reverse proxy, these headers could be spoofed by clients, resulting in inaccurate IP logging. Ensure Bifrost is always deployed behind a trusted proxy when relying on these values. ## Checklist - [ ] I read `docs/contributing/README.md` and followed the guidelines - [ ] I added/updated tests where appropriate - [ ] I updated documentation where needed - [ ] I verified builds succeed (Go and UI) - [ ] I verified the CI pipeline passes locally if applicablecs
…d log error response body on 4xx/5xx (maximhq#3985) ## Summary Improves HTTP request logging by resolving the true client IP from reverse-proxy headers and including error response bodies in logs for failed requests. ## Issues Closes maximhq#3904 ## Changes - Added a `clientIP` helper that extracts the originating client IP by checking `X-Forwarded-For` (taking the leftmost entry in comma-separated lists), then `X-Real-IP`, and finally falling back to the direct peer address. This ensures accurate client identification when Bifrost sits behind a reverse proxy. - Replaced the direct `ctx.RemoteAddr().String()` call in the `http.remote_addr` log field with `clientIP(ctx)` so logs reflect the real client rather than the proxy. - Added logging of the response body as `http.error` for any request that results in a 4xx or 5xx status code, making it easier to diagnose failures from logs alone. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor - [ ] Documentation - [ ] Chore/CI ## Affected areas - [ ] Core (Go) - [x] Transports (HTTP) - [ ] Providers/Integrations - [ ] Plugins - [ ] UI (React) - [ ] Docs ## How to test ```sh go test ./... ``` - Deploy Bifrost behind a reverse proxy that sets `X-Forwarded-For` or `X-Real-IP` headers and verify that `http.remote_addr` in logs reflects the originating client IP rather than the proxy address. - Trigger a request that returns a 4xx or 5xx response and confirm the `http.error` field appears in the log output with the response body. ## Screenshots/Recordings N/A ## Breaking changes - [ ] Yes - [x] No ## Related issues N/A ## Security considerations The `clientIP` helper trusts `X-Forwarded-For` and `X-Real-IP` headers as provided by upstream proxies. If Bifrost is exposed directly to the internet without a trusted reverse proxy, these headers could be spoofed by clients, resulting in inaccurate IP logging. Ensure Bifrost is always deployed behind a trusted proxy when relying on these values. ## Checklist - [ ] I read `docs/contributing/README.md` and followed the guidelines - [ ] I added/updated tests where appropriate - [ ] I updated documentation where needed - [ ] I verified builds succeed (Go and UI) - [ ] I verified the CI pipeline passes locally if applicablecs

Summary
Improves HTTP request logging by resolving the true client IP from reverse-proxy headers and including error response bodies in logs for failed requests.
Issues
Closes #3904
Changes
clientIPhelper that extracts the originating client IP by checkingX-Forwarded-For(taking the leftmost entry in comma-separated lists), thenX-Real-IP, and finally falling back to the direct peer address. This ensures accurate client identification when Bifrost sits behind a reverse proxy.ctx.RemoteAddr().String()call in thehttp.remote_addrlog field withclientIP(ctx)so logs reflect the real client rather than the proxy.http.errorfor any request that results in a 4xx or 5xx status code, making it easier to diagnose failures from logs alone.Type of change
Affected areas
How to test
go test ./...X-Forwarded-FororX-Real-IPheaders and verify thathttp.remote_addrin logs reflects the originating client IP rather than the proxy address.http.errorfield appears in the log output with the response body.Screenshots/Recordings
N/A
Breaking changes
Related issues
N/A
Security considerations
The
clientIPhelper trustsX-Forwarded-ForandX-Real-IPheaders as provided by upstream proxies. If Bifrost is exposed directly to the internet without a trusted reverse proxy, these headers could be spoofed by clients, resulting in inaccurate IP logging. Ensure Bifrost is always deployed behind a trusted proxy when relying on these values.Checklist
docs/contributing/README.mdand followed the guidelines