Skip to content

net/netip: ParseAddr should reject invalid zone #71362

Open

Description

@qinlonglong123

Proposal Details

I have the following use case:

package main

import (
    "fmt"
    "net/netip"
)

func main() {
    addr, err := netip.ParseAddr("2006:abcd::1%%")
    fmt.Printf("addr = %s,zone = %s,err = %v\n", addr.String(), addr.Zone(), err)
}

https://go.dev/play/p/soSl7hzPhJx
output:

addr = 2006:abcd::1%%,zone = %,err = <nil>

When running with Go 1.23, it considers the last % as the IPv6 zone ID. However, when parsing the same address in other languages, it considers the parameter as an invalid IP, for example:

1.C Language

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>

const char* parse_ipv6_with_zone_id(const char *input) {
    struct addrinfo hints, *res;
    int status;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;

    status = getaddrinfo(input, NULL, &hints, &res);

    if (status != 0) {
        return "Invalid";
    }

    freeaddrinfo(res);
    return "Valid";
}

int main() {
    const char *input1 = "2006:abcd::1%%";

    printf("Address 1: %s - %s\n", input1, parse_ipv6_with_zone_id(input1));

    return 0;
}

gcc version

gcc version 10.3.1 (GCC)

and output:

Address 1: 2006:abcd::1%% - Invalid

2. java

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;

public class IPv6ZoneIDParser {
 public static void main(String[] args) {
        final String ipv6WithZone = "2006:abcd::1%%";
        try {
            InetAddress inetAddress = InetAddress.getByName(ipv6WithZone);
            if (inetAddress instanceof Inet6Address) {
                System.out.println(ipv6WithZone + " It is an IPv6 address.");
            }

        } catch (java.net.UnknownHostException e) {
            System.out.println(ipv6WithZone + " It is a invalid ip address.");
        }
    }
}

java -version
openjdk version "1.8.0_392"

output:
2006:abcd::1%% It is a invalid ip address.

3. python

import ipaddress

def is_valid_ip(ip):
    try:
        ipaddress.ip_address(ip)
        return True
    except ValueError:
        return False

print(is_valid_ip("2006:abcd::1%%"))

python version
Python 3.9.11 (main, Jul 18 2024, 15:21:06)

output:
False

why Go language behaves differently when parsing IPv6 zone IDs compared to other languages as shown above? Thank you.

Activity

  1. added this to the Proposal milestone on Jan 21, 2025
  2. added
    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a tool
    on Jan 21, 2025
  3. changed the title [-]proposal: net/netip: netip.ParseAddr is inconsistent with certain parameters compared to other languages[/-] [+]net/netip: ParseAddr should reject invalid zone[/+] on Jan 21, 2025
  4. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    BugReportIssues describing a possible bug in the Go implementation.
    and removed
    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a tool
    on Jan 21, 2025
  5. seankhliao commented on Jan 21, 2025

    @seankhliao
    Member

    I think this should be a bug not a proposal.
    RFC6874 https://datatracker.ietf.org/doc/html/rfc6874 has

    ZoneID = 1*( unreserved / pct-encoded )
    IPv6addrz = IPv6address "%25" ZoneID

    referring to RFC3986 https://datatracker.ietf.org/doc/html/rfc3986#section-2.3

    unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

    a lone % should be considered invalid.

  6. gopherbot commented on Jan 21, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/642896 mentions this issue: net/netip: reject invalid zone identifiers in ParseAddr

  7. ianlancetaylor commented on Jan 21, 2025

    @ianlancetaylor
    Contributor

    Note that as far as I can see the C program does not accept any zone ID at all.

  8. qinlonglong123 commented on Jan 22, 2025

    @qinlonglong123
    ContributorAuthor

    Note that as far as I can see the C program does not accept any zone ID at all.

    Sorry, the above C program cannot be used as one of the validation programs, because even a normal IPv6 address with a zone ID cannot be resolved.Thank you for your correction.

  9. seankhliao commented on Jan 22, 2025

    @seankhliao
    Member

    From CL 642896, windows contains a builtin "Loopback Pseudo-Interface 1" (contains spaces)
    and linux accepts interface names like # or &.
    I'm not sure what sort of validation (if any) we can do on the zone.

  10. seankhliao commented on Jan 22, 2025

    @seankhliao
    Member

    The python docs point to RFC 4007.
    Section 11.2 includes the following: https://datatracker.ietf.org/doc/html/rfc4007.html#section-11.2

      An implementation MAY support other kinds of non-null strings as
       <zone_id>.  However, the strings must not conflict with the delimiter
       character.  The precise format and semantics of additional strings is
       implementation dependent.
    

    That points to % being the only disallowed character, which somewhat conflicts with RFC6874's pct-encoded.
    Given that we don't know if ParseAddr is going to operate on an encoded or decoded form, perhaps we just shouldn't try to validate it (keep things as is).

  11. qinlonglong123 commented on Jan 23, 2025

    @qinlonglong123
    ContributorAuthor

    The python docs point to RFC 4007. Section 11.2 includes the following: https://datatracker.ietf.org/doc/html/rfc4007.html#section-11.2

      An implementation MAY support other kinds of non-null strings as
       <zone_id>.  However, the strings must not conflict with the delimiter
       character.  The precise format and semantics of additional strings is
       implementation dependent.
    

    That points to % being the only disallowed character, which somewhat conflicts with RFC6874's pct-encoded. Given that we don't know if ParseAddr is going to operate on an encoded or decoded form, perhaps we just shouldn't try to validate it (keep things as is).

    I personally believe that the pct-encoding in RFC6874 refers to the special characters in the URI's host, which should be represented in pct-encoded form. If we only consider the ParseAddr function, it should not take encoding into account. Because url.Parse already decodes pct-encoded characters.

  12. modified the milestones: Proposal, Unplanned on Feb 1, 2025
  13. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Feb 1, 2025
  14. neild commented on Mar 10, 2026

    @neild
    Contributor

    As @fatihhcelik points out in #78034, you can get some surprising results from putting a newline in a zone identifier.

    addr, _ := netip.ParseAddr("::1%en0\nreally?")
    fmt.Println(addr)
    // :1%en0
    // really?

    On one hand, I suspect you can technically create an interface named "foo\nbar" on some platforms. On the other, do we really cause any problems if we limit zones to, say, characters in (0x20, 0x7e], (0x80-0xff]? (Printable, non-newline ASCII, plus non-ASCII Unicode.)

  15. seankhliao commented on Mar 11, 2026

    @seankhliao
    Member

    I don't think restricting ParseAddr would prevent that, there's also the WithZone method that doesn't return an error.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions