Skip to content
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

bug: unix_addr check is useless #1348

Open
aMOPel opened this issue Dec 18, 2024 · 1 comment
Open

bug: unix_addr check is useless #1348

aMOPel opened this issue Dec 18, 2024 · 1 comment

Comments

@aMOPel
Copy link

aMOPel commented Dec 18, 2024

  • [ x] I have looked at the documentation here first?
  • [ x] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10.23.0

Issue, Question or Enhancement:

The unix_addr check is useless. I looked into the src:

https://github.com/go-playground/validator/blob/6c3307e6c64040ebc0efffe9366927c68146ffba/baked_in.go#L2564C16-L2564C31

func isUnixAddrResolvable(fl FieldLevel) bool {
	_, err := net.ResolveUnixAddr("unix", fl.Field().String())

	return err == nil
}

and in stdlib net:

https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/net/unixsock.go;l=57

func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
	switch network {
	case "unix", "unixgram", "unixpacket":
		return &UnixAddr{Name: address, Net: network}, nil
	default:
		return nil, UnknownNetworkError(network)
	}
}

effectivly this will never throw an error since you're always passing "unix".

If fact, ResolveUnixAddr is itself useless to check if the socket actually exists.

You need something that checks if the file exists and if that file is a socket:

func IsSocket(filePath string) (err error) {
	stats, err := os.Stat(filePath)
	if err != nil {
		err = fmt.Errorf("check file existence: %w", err)
		return
	}
	if stats.Mode().Type() != fs.ModeSocket {
		err = fmt.Errorf("not socket file: %v", filePath)
		return
	}
	return
}
@nodivbyzero
Copy link
Contributor

I'm not entirely sure about the original design intention for this validator. It might have been intended to validate input values exclusively for the 'Unix' network. However, it would be beneficial to extend its functionality to also handle unixgram and unixpacket networks.

We always welcome pull requests with improvements or fixes.

I suggest creating a new socket validator and implementing it with the approach you provided.

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

No branches or pull requests

2 participants