Skip to content

Adding forwarder headers so end server can resolve caller#258

Open
hansr wants to merge 2 commits intomasterfrom
hans/add-forwarded-header
Open

Adding forwarder headers so end server can resolve caller#258
hansr wants to merge 2 commits intomasterfrom
hans/add-forwarded-header

Conversation

@hansr
Copy link
Member

@hansr hansr commented Mar 12, 2026

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • Forwarding Headers Injection: Introduced logic to inject standard forwarding headers (X-Forwarded-For, X-Real-IP, and Forwarded) into the connection to ensure backend services can identify the original client's IP address.
  • Connection Wrapping: Wrapped the established connection with a new newForwardedHeaderConn to encapsulate the header injection mechanism.
  • Consistent Client Address Usage: Ensured that the client's remote address is consistently used when establishing the connection to the device via connectDeviceFrom.
Changelog
  • rpc/proxy_listener.go
    • Introduced logic to inject X-Forwarded-For, X-Real-IP, and Forwarded headers into the connection.
    • Modified the connectDeviceFrom function call to consistently use the client's remote address.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

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.

Suggested change
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)

@dominicletz
Copy link
Member

@hansr did you miss committing a file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants