echo icon indicating copy to clipboard operation
echo copied to clipboard

ExtractIPFromXFFHeader ignores X-Forwarded-For when the peer address has an IPv6 zone

Open TheNetherWatcher opened this issue 1 month ago • 0 comments

Issue Description

When the direct peer is a link-local IPv6 address with a zone (e.g. [fe80::1%eth0]:8080), ExtractIPFromXFFHeader returns the proxy's own address instead of the client IP from X-Forwarded-For.

extractIP returns the host with its zone (fe80::1%eth0), and net.ParseIP rejects zoned addresses. The XFF loop treats the nil result as an unparseable record and returns directIP early. Link-local addresses are trusted by default, so the expected result is the address from the header.

isTrustedPeer in scheme.go already strips the zone before net.ParseIP for this reason.

Working code to debug

package main

import (
	"net/http/httptest"
	"testing"

	"github.com/labstack/echo/v5"
)

func TestXFFWithZonedPeer(t *testing.T) {
	extract := echo.ExtractIPFromXFFHeader()

	req := httptest.NewRequest("GET", "/", nil)
	req.RemoteAddr = "[fe80::1%eth0]:8080"
	req.Header.Set(echo.HeaderXForwardedFor, "203.0.113.5")

	if got := extract(req); got != "203.0.113.5" {
		t.Errorf("got %q, want %q", got, "203.0.113.5")
	}
	// got "fe80::1%eth0", want "203.0.113.5"
}

Version/commit

master @ 8fd0e18 (v5)

TheNetherWatcher avatar Oct 16 '26 14:10 TheNetherWatcher