net: reject non-IPv6 addresses in brackets in SplitHostPort
RFC 3986 specifies that brackets in URL host components are only valid for IPv6 addresses. SplitHostPort was accepting [hostname]:port and treating 'hostname' as the host, which can produce technically invalid results when used on URL host components.
This change validates that bracketed content is a valid IPv6 address using netip.ParseAddr, returning an 'invalid brackets' error otherwise.
Fixes golang/go#78945
diff --git a/src/net/ipsock.go b/src/net/ipsock.go
index 496faf3..a71bc03 100644
--- a/src/net/ipsock.go
+++ b/src/net/ipsock.go
@@ -6,6 +6,7 @@
import (
"context"
+ "net/netip"
"internal/bytealg"
"runtime"
"sync"
@@ -200,6 +201,10 @@
}
host = hostport[1:end]
j, k = 1, end+1 // there can't be a '[' resp. ']' before these positions
+ // Brackets are only valid for IPv6 addresses.
+ if _, err := netip.ParseAddr(host); err != nil {
+ return addrErr(hostport, "invalid brackets")
+ }
} else {
host = hostport[:i]
if bytealg.IndexByteString(host, ':') >= 0 {
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You have a long 251 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
2. Do you have the right bug reference format? For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message.
Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
Thanks, but changes like these should not be made without the appropriate accompanying GODEBUG and tests.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Gopher Robot abandoned this change.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
net: reject non-IPv6 addresses in brackets in SplitHostPort
RFC 3986 specifies that brackets in URL host components are only valid for IPv6 addresses. SplitHostPort was accepting [hostname]:port and treating 'hostname' as the host, which can produce technically invalid results when used on URL host components.
This change validates that bracketed content is a valid IPv6 address using netip.ParseAddr, returning an 'invalid brackets' error otherwise.
Fixes golang/go#78945
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You have a long 251 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
2. Do you have the right bug reference format? For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message.
Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Thanks. A change like this should have a test for the new behavior.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |