Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add optional IP anonymization #2798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aine-etke wants to merge 2 commits into labstack:master
base: master
Choose a base branch
Loading
from aine-etke:add-anonymizedip

Conversation

@aine-etke
Copy link

@aine-etke aine-etke commented Jun 24, 2025

This PR provides an option to anonymize IP:

  • in context via Context.AnonymizedIP()
  • in (old) logger middleware via remote_ip_anon token
  • in (new) request logger middleware via AnonymizeRemoteIP option

Copy link
Contributor

aldas commented Jun 24, 2025
edited
Loading

Hi,

I we can not accept this as adding new method to Context interface would be API breaking change as that type is an Interface.

I think you would be totally fine if you add utility public function in your code base like that. There is no need to add it to the Context interface.

func AnonymizedIP(c echo.Context) string {
	ip := c.RealIP()
	if ip == "" {
		return "" // safeguard
	}
	parsedIP := net.ParseIP(ip)
	if parsedIP == nil {
		return ip // safeguard, not an ip
	}
	// IPv4
	if parsedIP.To4() != nil {
		ipParts := strings.Split(parsedIP.String(), ".")
		if len(ipParts) == 4 {
			ipParts[3] = "0"
			return strings.Join(ipParts, ".")
		}
	}
	// IPv6
	ipParts := strings.Split(parsedIP.String(), ":")
	ipParts[len(ipParts)-1] = "0"
	return strings.Join(ipParts, ":")
}

Copy link
Author

Hello,
I already did a public utility function in https://github.com/etkecc/go-kit/blob/main/ip.go but just wanted to provide a "frictionless" version as part of the echo.

Would you mind telling if you are against Context.AnonymizedIP() method specifically and will accept middleware changes (logger and request logger), or you don't like the proposed idea at all, please?

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /