Skip to content

Handle IP splitting more safely#4

Merged
robstradling merged 1 commit into
crtsh:mainfrom
aww-aww:patch-1
Jun 9, 2026
Merged

Handle IP splitting more safely#4
robstradling merged 1 commit into
crtsh:mainfrom
aww-aww:patch-1

Conversation

@aww-aww

@aww-aww aww-aww commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Update getRealClientIP to handle IPv4 and IPv6 addresses more safely.

Update getRealClientIP to handle IPv4 and IPv6 addresses more safely.
@aww-aww aww-aww requested a review from robstradling as a code owner June 4, 2026 19:21
Comment thread logger/logger.go
remoteAddr := strings.SplitN(fhctx.RemoteAddr().String(), ":", 2)
remoteAddr := fhctx.RemoteAddr().String()
// Split host and port - handling both IPv4 and IPv6
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {

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.

Good call on using SplitHostPort.

Comment thread logger/logger.go
ipAddress := strings.Split(utils.B2S(xff), ",")
remoteAddr[0] = strings.TrimSpace(ipAddress[len(ipAddress)-1])
// Index 0 represents the original client
return strings.TrimSpace(ipAddress[0])

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.

AIUI, index 0 represents the original client's claim, which might be spoofed.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For#security_and_privacy_concerns recommends only trusting values in X-Forwarded-For that have been added by a trusted proxy, which is more likely to be true of the len-1 index (hence the current code).

I intend to stick to len-1 in my deployments. If you need index 0, then let's add that as a configuration option in config.go (with the len-1 behaviour as the default behaviour for that configuration option).

In any case, please revert this portion of this particular PR. Thanks!

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.

If you need index 0, then let's add that as a configuration option in config.go (with the len-1 behaviour as the default behaviour for that configuration option).

In any case, please revert this portion of this particular PR. Thanks!

On second thoughts, I'll merge this PR and then implement that configuration option in another PR.

@robstradling robstradling merged commit 68e837b into crtsh:main Jun 9, 2026
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.

2 participants