Skip to content

bug: unix_addr check is useless #1348

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
aMOPel opened this issue Dec 18, 2024 · 2 comments
Open

bug: unix_addr check is useless #1348

aMOPel opened this issue Dec 18, 2024 · 2 comments

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)
	}
}

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

In 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.

@nf-brentsaner
Copy link

A "socket" does not inherently mean a UDS/IPC socket. tcp,127.0.0.1:1234 is a "socket".

If anything, it explicitly should be called "ipc" ("Inter-Process Communication") or "uds" (UNIX Domain Socket).

socket as a validator name should be applicable to all of the above cases (see also #1327).

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

3 participants