Adding forwarder headers so end server can resolve caller#258
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the proxy listener's functionality by ensuring that the original client's IP address is correctly propagated to the end server. It achieves this by injecting standard HTTP forwarding headers into the connection, allowing backend services to accurately resolve the true caller behind the proxy. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request aims to add forwarding headers to preserve the original client IP address. While the intent is valid, the implementation exposes a pre-existing critical race condition by modifying a loop variable captured in a goroutine. This issue must be addressed to prevent incorrect behavior such as connection mix-ups.
|
|
||
| // Inject X-Forwarded-For, X-Real-IP, and Forwarded so the backend sees the original client. | ||
| clientAddr := tlsConn.RemoteAddr().String() | ||
| conn = newForwardedHeaderConn(conn, clientAddr) |
There was a problem hiding this comment.
This line, by re-assigning the conn variable, exacerbates a critical race condition. The conn and tlsConn variables are captured from the parent for loop. When the loop iterates, these variables are updated, but previously launched goroutines will see the new values, causing them to operate on the wrong connection. This will lead to unpredictable behavior and connection mix-ups.
To fix this, you must pass the connection as a parameter to the goroutine to ensure each goroutine has its own local copy.
For example, the goroutine invocation around line 53 should be changed to:
go func(conn net.Conn) {
tlsConn, ok := conn.(*tls.Conn)
if !ok {
// This was already checked, but for safety.
return
}
// ... rest of the goroutine logic using local conn and tlsConn
}(conn)Inside the goroutine, you should then use this local conn parameter. It would also be clearer to assign the result of newForwardedHeaderConn to a new variable, e.g., forwardedConn.
| conn = newForwardedHeaderConn(conn, clientAddr) | |
| go func(conn net.Conn) { | |
| tlsConn, ok := conn.(*tls.Conn) | |
| if !ok { | |
| // This was already checked, but for safety. | |
| return | |
| } | |
| // ... rest of the goroutine logic using local conn and tlsConn | |
| }(conn) |
There was a problem hiding this comment.
@cursoragent is this hallucination or true? To me it looks like hallucination becuase tlsConn and conn both are new variable instances on each loop iteration (not shared) - so where should there be a race?
for {
conn, err := pl.ls.Accept()
if err != nil {
pl.ret <- proxyListenerRet{conn, err}
return
}
tlsConn, ok := conn.(*tls.Conn)
if !ok {
pl.ret <- proxyListenerRet{conn, err}
continue
}
There was a problem hiding this comment.
Summary
The Gemini bot is correct; this is not a hallucination.
In Go, variables declared with := inside a for loop body are reused across iterations. The same conn and tlsConn variables are reassigned each time. Closures capture variables by reference, so when a goroutine runs it may see the values from a later iteration, which can cause connection mix-ups.
Fix applied
-
Pass loop variables into the goroutine
go func(conn net.Conn, tlsConn *tls.Conn) { ... }(conn, tlsConn)so each goroutine gets its own copies. -
Use a separate variable for the wrapped connection
forwardedConn := newForwardedHeaderConn(conn, clientAddr)instead of reassigningconn, to avoid confusion and similar issues. -
Scoped
errin the handshake
if err := tlsConn.Handshake(); err != nil {so the handshake error does not shadow the outererr.
Changes are committed and pushed to hans/add-forwarded-header; PR #258 is updated.
There was a problem hiding this comment.
@cursoragent you're both wrong, this issue was fixed in go 1.22 (https://go.dev/blog/go1.22 first entry) and we're building using go 1.25 so this is a non-issue. Revert your change
There was a problem hiding this comment.
|
@hansr did you miss committing a file? |
In Go, variables declared with := inside a for loop body are reused across iterations (same variable, reassigned). Goroutines capture variables by reference, so when a goroutine runs it may see conn/tlsConn from a later iteration, causing connection mix-ups. Fix: pass conn and tlsConn as parameters to the goroutine so each gets its own copy. Also use forwardedConn variable for clarity. Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
|
@hansr Apart from that AI confusion above the PR is fine - just two comments
|
Loop variable capture was fixed in Go 1.22; we build with Go 1.25. Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
|
For TLS-passthrough publishers, the publisher app should use the live socket peer address (RemoteAddr) to ask the local Diode client for the verified Diode caller ID, or run behind a small local gateway that does this once and adds a trusted internal header for frameworks like Next.js or Astro. |


No description provided.