Skip to content

Commit d2f2ed2

Browse files
Infinoidmnencia
andauthored
fix: don't generate invalid IPv6 URLs (#10682)
When connecting to an IPv6 address via URL, the address must be in square brackets. Use standard library functions that do the right thing. Closes #10679 Signed-off-by: Mark Glines <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
1 parent a6b6c28 commit d2f2ed2

4 files changed

Lines changed: 136 additions & 13 deletions

File tree

‎pkg/management/postgres/webserver/probes/pinger.go‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ import (
2525
"fmt"
2626
"net"
2727
"net/http"
28-
"net/url"
2928
"os"
3029
"path/filepath"
3130
"time"
3231

3332
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
3433
"github.com/cloudnative-pg/cloudnative-pg/pkg/certs"
35-
cnpgUrl "github.com/cloudnative-pg/cloudnative-pg/pkg/management/url"
34+
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/url"
3635
postgresSpec "github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
3736
)
3837

@@ -84,15 +83,11 @@ func buildInstanceReachabilityChecker(cfg *apiv1.IsolationCheckConfiguration) (*
8483
// ping checks if the instance with the passed coordinates is reachable
8584
// by calling the failsafe endpoint.
8685
func (e *pinger) ping(host, ip string) error {
87-
failsafeURL := url.URL{
88-
Scheme: "https",
89-
Host: fmt.Sprintf("%s:%d", ip, cnpgUrl.StatusPort),
90-
Path: cnpgUrl.PathFailSafe,
91-
}
86+
failsafeURL := url.Build("https", ip, url.PathFailSafe, url.StatusPort)
9287

9388
var res *http.Response
9489
var err error
95-
if res, err = e.client.Get(failsafeURL.String()); err != nil {
90+
if res, err = e.client.Get(failsafeURL); err != nil {
9691
return &pingError{
9792
host: host,
9893
ip: ip,

‎pkg/management/url/suite_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package url
21+
22+
import (
23+
"testing"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
func TestUrl(t *testing.T) {
30+
RegisterFailHandler(Fail)
31+
RunSpecs(t, "Url Suite")
32+
}

‎pkg/management/url/url.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ SPDX-License-Identifier: Apache-2.0
2121
package url
2222

2323
import (
24-
"fmt"
24+
"net"
25+
neturl "net/url"
26+
"strconv"
2527
)
2628

2729
const (
@@ -84,9 +86,10 @@ func Local(path string, port int32) string {
8486

8587
// Build builds an url given the hostname and the path, pointing to the status web server
8688
func Build(scheme, hostname, path string, port int32) string {
87-
// If path already starts with '/' we remove it
88-
if path[0] == '/' {
89-
path = path[1:]
89+
url := neturl.URL{
90+
Scheme: scheme,
91+
Host: net.JoinHostPort(hostname, strconv.Itoa(int(port))),
92+
Path: path,
9093
}
91-
return fmt.Sprintf("%s://%s:%d/%s", scheme, hostname, port, path)
94+
return url.String()
9295
}

‎pkg/management/url/url_test.go‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package url
21+
22+
import (
23+
"net"
24+
neturl "net/url"
25+
"strconv"
26+
"strings"
27+
28+
. "github.com/onsi/ginkgo/v2"
29+
. "github.com/onsi/gomega"
30+
)
31+
32+
var _ = Describe("URL", func() {
33+
cases := []struct {
34+
scheme string
35+
host string
36+
port int32
37+
path string
38+
expect string
39+
}{
40+
{
41+
// path has leading slash
42+
scheme: "http",
43+
host: "localhost",
44+
port: 1234,
45+
path: "/foo",
46+
expect: "http://localhost:1234/foo",
47+
},
48+
{
49+
// path has no leading slash
50+
scheme: "http",
51+
host: "localhost",
52+
port: 1234,
53+
path: "foo",
54+
expect: "http://localhost:1234/foo",
55+
},
56+
{
57+
// ipv4 address
58+
scheme: "http",
59+
host: "1.2.3.4",
60+
port: 1234,
61+
path: "foo",
62+
expect: "http://1.2.3.4:1234/foo",
63+
},
64+
{
65+
// ipv6 address
66+
scheme: "http",
67+
host: "1:2:3::4",
68+
port: 1234,
69+
path: "foo",
70+
expect: "http://[1:2:3::4]:1234/foo",
71+
},
72+
}
73+
74+
It("generates the expected url", func() {
75+
for _, unit := range cases {
76+
Expect(Build(unit.scheme, unit.host, unit.path, unit.port)).To(Equal(unit.expect))
77+
}
78+
})
79+
80+
It("roundtrips", func() {
81+
for _, unit := range cases {
82+
url := Build(unit.scheme, unit.host, unit.path, unit.port)
83+
parsed, err := neturl.Parse(url)
84+
Expect(err).ToNot(HaveOccurred())
85+
Expect(parsed.Scheme).To(Equal(unit.scheme))
86+
port := strconv.Itoa(int(unit.port))
87+
Expect(parsed.Host).To(Equal(net.JoinHostPort(unit.host, port)))
88+
Expect(parsed.Port()).To(Equal(port))
89+
// parsed.Path always has a leading slash
90+
Expect(parsed.Path).To(Equal("/" + strings.TrimPrefix(unit.path, "/")))
91+
}
92+
})
93+
})

0 commit comments

Comments
 (0)