Skip to content

ExtractIPFromRealIPHeader returns RemoteAddr instead of x-real-ip #1834

@georgekarrv

Description

@georgekarrv

Issue Description

ExtractIPFromRealIPHeader does not return what was found in x-real-ip

echo/ip.go

Line 98 in a97052e

func ExtractIPFromRealIPHeader(options ...TrustOption) IPExtractor {

func ExtractIPFromRealIPHeader(options ...TrustOption) IPExtractor {
	checker := newIPChecker(options)
	return func(req *http.Request) string {
		directIP := ExtractIPDirect()(req)
		realIP := req.Header.Get(HeaderXRealIP)
		if realIP != "" {
			if ip := net.ParseIP(directIP); ip != nil && checker.trust(ip) {
				return realIP
			}
		}
		return directIP
	}
}

if realIP != "" should be if realIP == ""
because ultimately the logic that exists now is when you have real and direct ip it will return direct and not real

Checklist

  • [ x] Dependencies installed
  • [ x] No typos
  • [ x] Searched existing issues and docs

Expected behaviour

when x-real-ip is present the IPExtractor should return that ip instead of the directIP from RemoteAddr

Actual behaviour

ExtractIPFromRealIPHeader returns RemoteAddr if both RemoteAddr and x-real-ip exist

Steps to reproduce

Working code to debug

package main

func main() {
}

Version/commit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions