Adding forwarder headers so end server can resolve caller#258
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) |
|
@hansr did you miss committing a file? |
No description provided.