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

net/mail: enhance address parser to include IPv6 address tags #69461

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/net/mail/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,12 @@ func (p *addrParser) consumeDomainLiteral() (string, error) {
}

// 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 net.ParseIP(dtext).To4() == nil {
return "", fmt.Errorf("mail: invalid IP address in domain-literal: %q", dtext)
}

Expand Down
38 changes: 38 additions & 0 deletions src/net/mail/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down