1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:57:58 -07:00

net/mail: IPv6 domain-literals address tag

This commit is contained in:
Cassondra Foesch 2024-09-14 01:28:04 +00:00
parent f117d1c9b5
commit 18675e7046
2 changed files with 45 additions and 2 deletions

View File

@ -751,8 +751,13 @@ func (p *addrParser) consumeDomainLiteral() (string, error) {
return "", errors.New("mail: unclosed domain-literal")
}
// Check if the domain literal is an IP address
if net.ParseIP(dtext) == nil {
if addr, ok := strings.CutPrefix(dtext, "IPv6:"); ok {
if len(net.ParseIP(addr)) != net.IPv6len {
return "", fmt.Errorf("mail: invalid IPv6 address in domain-literal: %q", dtext)
}
} else if len(net.ParseIP(dtext).To4()) != net.IPv4len {
// Check if the domain literal is an IP address
return "", fmt.Errorf("mail: invalid IP address in domain-literal: %q", dtext)
}

View File

@ -395,6 +395,7 @@ func TestAddressParsingError(t *testing.T) {
22: {"<jdoe@[[192.168.0.1]>", "bad character in domain-literal"},
23: {"<jdoe@[192.168.0.1>", "unclosed domain-literal"},
24: {"<jdoe@[256.0.0.1]>", "invalid IP address in domain-literal"},
25: {"<jdoe@[fd42::de:ad:be:ef]>", "invalid IP address in domain-literal"},
}
for i, tc := range mustErrTestCases {
@ -825,6 +826,20 @@ func TestAddressParsing(t *testing.T) {
Address: "jdoe@[192.168.0.1]",
}},
},
// IPv6 Domain-literal
{
`jdoe@[IPv6:fd42::dead:beef:1234]`,
[]*Address{{
Address: "jdoe@[IPv6:fd42::dead:beef:1234]",
}},
},
{
`John Doe <jdoe@[IPv6:fd42::dead:beef:1234]>`,
[]*Address{{
Name: "John Doe",
Address: "jdoe@[IPv6:fd42::dead:beef:1234]",
}},
},
}
for _, test := range tests {
if len(test.exp) == 1 {
@ -989,6 +1004,20 @@ func TestAddressParser(t *testing.T) {
Address: "jdoe@[192.168.0.1]",
}},
},
// IPv6 Domain-literal
{
`jdoe@[IPv6:fd42::dead:beef:1234]`,
[]*Address{{
Address: "jdoe@[IPv6:fd42::dead:beef:1234]",
}},
},
{
`John Doe <jdoe@[IPv6:fd42::dead:beef:1234]>`,
[]*Address{{
Name: "John Doe",
Address: "jdoe@[IPv6:fd42::dead:beef:1234]",
}},
},
}
ap := AddressParser{WordDecoder: &mime.WordDecoder{
@ -1104,6 +1133,15 @@ func TestAddressString(t *testing.T) {
&Address{Name: "Bob", Address: "bob@[192.168.0.1]"},
`"Bob" <bob@[192.168.0.1]>`,
},
// IPv6 Domain-literal
{
&Address{Address: "bob@[IPv6:fd42::dead:beef:1234]"},
"<bob@[IPv6:fd42::dead:beef:1234]>",
},
{
&Address{Name: "Bob", Address: "bob@[IPv6:fd42::dead:beef:1234]"},
`"Bob" <bob@[IPv6:fd42::dead:beef:1234]>`,
},
}
for _, test := range tests {
s := test.addr.String()