Skip to content

Conversation

@1911860538
Copy link
Contributor

This PR addresses an issue where the port parsed from the endpoint using raw, err := url.Parse(endpoint) and raw.Port() was not being validated properly. Although raw.Port() extracts the port from the URL, it does not guarantee that the port is valid or within the legal range (0-65535). This fix ensures that the port is parsed and checked to fall within the valid range and is not zero, preventing invalid service registration.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 6, 2025
@kagaya85 kagaya85 requested a review from Copilot July 14, 2025 03:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds stricter validation for service registration endpoints by ensuring parsed ports are within the valid range (1–65535) and non-zero, preventing invalid Consul registrations.

  • Validate raw.Port() errors and reject zero or out-of-range ports
  • Change second port parsing to use strconv.ParseUint instead of ParseInt
Comments suppressed due to low confidence (2)

contrib/registry/consul/client.go:164

  • No tests cover the new port validation logic (invalid, zero, or missing port). Add unit tests for endpoints with missing or out-of-range ports to verify correct error handling.
		port, err := strconv.ParseUint(raw.Port(), 10, 16)

contrib/registry/consul/client.go:163

  • [nitpick] Port parsing and validation logic is repeated; extract into a shared helper function to reduce duplication and improve readability.
		addr := raw.Hostname()

if len(checkAddresses) > 0 {
host, portRaw, _ := net.SplitHostPort(checkAddresses[0])
port, _ := strconv.ParseInt(portRaw, 10, 32)
port, _ := strconv.ParseUint(portRaw, 10, 16)
Copy link

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error from strconv.ParseUint is ignored here; consider handling parse errors to avoid silently defaulting to port 0, similar to the validation added above.

Suggested change
port, _ := strconv.ParseUint(portRaw, 10, 16)
port, err := strconv.ParseUint(portRaw, 10, 16)
if err != nil || port == 0 {
return fmt.Errorf("invalid port in check address: %q", checkAddresses[0])
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant