Skip to content

Adding forwarder headers so end server can resolve caller#258

Open
hansr wants to merge 4 commits into
masterfrom
hans/add-forwarded-header
Open

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

Conversation

@hansr
Copy link
Copy Markdown
Member

@hansr hansr commented Mar 12, 2026

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
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
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 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.

Comment thread rpc/proxy_listener.go

// 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
Copy Markdown
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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
		}

Copy link
Copy Markdown

@cursor cursor Bot Mar 18, 2026

Choose a reason for hiding this comment

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

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

  1. Pass loop variables into the goroutine
    go func(conn net.Conn, tlsConn *tls.Conn) { ... }(conn, tlsConn) so each goroutine gets its own copies.

  2. Use a separate variable for the wrapped connection
    forwardedConn := newForwardedHeaderConn(conn, clientAddr) instead of reassigning conn, to avoid confusion and similar issues.

  3. Scoped err in the handshake
    if err := tlsConn.Handshake(); err != nil { so the handshake error does not shadow the outer err.

Changes are committed and pushed to hans/add-forwarded-header; PR #258 is updated.

Open in Web Open in Cursor 

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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

Copy link
Copy Markdown

@cursor cursor Bot Mar 18, 2026

Choose a reason for hiding this comment

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

The revert is already in place. The file currently uses:

  • go func() without parameters (lines 53–99)
  • conn = newForwardedHeaderConn(conn, clientAddr) (line 77)

That state was committed and pushed in the previous turn (ddc13e3 on hans/add-forwarded-header). No further changes are required.

Open in Web Open in Cursor 

@dominicletz
Copy link
Copy Markdown
Member

@hansr did you miss committing a file?

aich and others added 2 commits March 13, 2026 08:36
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>
@dominicletz
Copy link
Copy Markdown
Member

@hansr Apart from that AI confusion above the PR is fine - just two comments

  1. This only works with http1 requests which reduces it to very few clients (also connection re-use doesn't trigger the injection again), would be better to have a proper http server library here for detection and handling.
  2. In all other cases the client can just inject any X-* headers -- so you cannot trust these values to be true on the receiving side.

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>
@tuhalf
Copy link
Copy Markdown
Collaborator

tuhalf commented Apr 17, 2026

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.

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.

5 participants