Skip to content

Commit 770f1de

Browse files
committed
net/http: remove test-only private key from production binaries
The net/http/internal package contains a PEM-encoded private key used in tests. This key is initialized at init time, which prevents it from being stripped by the linker in non-test binaries. Move the certificate and key to a new net/http/internal/testcert package to ensure it is only included in binaries that reference it. Fixes #46677. Change-Id: Ie98bda529169314cc791063e7ce4d99ef99113c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/326771 Trust: Damien Neil <[email protected]> Run-TryBot: Damien Neil <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 8d11b1d commit 770f1de

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

src/go/build/deps_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ var depsRules = `
440440
# HTTP, King of Dependencies.
441441
442442
FMT
443-
< golang.org/x/net/http2/hpack, net/http/internal, net/http/internal/ascii;
443+
< golang.org/x/net/http2/hpack
444+
< net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
444445
445446
FMT, NET, container/list, encoding/binary, log
446447
< golang.org/x/text/transform
@@ -459,6 +460,7 @@ var depsRules = `
459460
golang.org/x/net/http2/hpack,
460461
net/http/internal,
461462
net/http/internal/ascii,
463+
net/http/internal/testcert,
462464
net/http/httptrace,
463465
mime/multipart,
464466
log

src/net/http/httptest/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"log"
1515
"net"
1616
"net/http"
17-
"net/http/internal"
17+
"net/http/internal/testcert"
1818
"os"
1919
"strings"
2020
"sync"
@@ -144,7 +144,7 @@ func (s *Server) StartTLS() {
144144
if s.client == nil {
145145
s.client = &http.Client{Transport: &http.Transport{}}
146146
}
147-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
147+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
148148
if err != nil {
149149
panic(fmt.Sprintf("httptest: NewTLSServer: %v", err))
150150
}

src/net/http/internal/testcert.go renamed to src/net/http/internal/testcert/testcert.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package internal
5+
// Package testcert contains a test-only localhost certificate.
6+
package testcert
67

78
import "strings"
89

@@ -25,7 +26,7 @@ h1fIw3cSS2OolhloGw/XM6RWPWtPAlGykKLciQrBru5NAPvCMsb/I1DAceTiotQM
2526
fblo6RBxUQ==
2627
-----END CERTIFICATE-----`)
2728

28-
// LocalhostKey is the private key for localhostCert.
29+
// LocalhostKey is the private key for LocalhostCert.
2930
var LocalhostKey = []byte(testingKey(`-----BEGIN RSA TESTING KEY-----
3031
MIICXgIBAAKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9
3132
SjY1bIw4iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZB

src/net/http/serve_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/http/httptest"
2626
"net/http/httputil"
2727
"net/http/internal"
28+
"net/http/internal/testcert"
2829
"net/url"
2930
"os"
3031
"os/exec"
@@ -1475,7 +1476,7 @@ func TestServeTLS(t *testing.T) {
14751476
defer afterTest(t)
14761477
defer SetTestHookServerServe(nil)
14771478

1478-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
1479+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
14791480
if err != nil {
14801481
t.Fatal(err)
14811482
}
@@ -1599,7 +1600,7 @@ func TestAutomaticHTTP2_Serve_WithTLSConfig(t *testing.T) {
15991600
}
16001601

16011602
func TestAutomaticHTTP2_ListenAndServe(t *testing.T) {
1602-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
1603+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
16031604
if err != nil {
16041605
t.Fatal(err)
16051606
}
@@ -1609,7 +1610,7 @@ func TestAutomaticHTTP2_ListenAndServe(t *testing.T) {
16091610
}
16101611

16111612
func TestAutomaticHTTP2_ListenAndServe_GetCertificate(t *testing.T) {
1612-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
1613+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
16131614
if err != nil {
16141615
t.Fatal(err)
16151616
}

src/net/http/transport_internal_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"errors"
1313
"io"
1414
"net"
15-
"net/http/internal"
15+
"net/http/internal/testcert"
1616
"strings"
1717
"testing"
1818
)
@@ -191,7 +191,7 @@ func (f roundTripFunc) RoundTrip(r *Request) (*Response, error) {
191191

192192
// Issue 25009
193193
func TestTransportBodyAltRewind(t *testing.T) {
194-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
194+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
195195
if err != nil {
196196
t.Fatal(err)
197197
}

src/net/http/transport_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"net/http/httptest"
3131
"net/http/httptrace"
3232
"net/http/httputil"
33-
"net/http/internal"
33+
"net/http/internal/testcert"
3434
"net/textproto"
3535
"net/url"
3636
"os"
@@ -4299,7 +4299,7 @@ func TestTransportReuseConnEmptyResponseBody(t *testing.T) {
42994299

43004300
// Issue 13839
43014301
func TestNoCrashReturningTransportAltConn(t *testing.T) {
4302-
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
4302+
cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
43034303
if err != nil {
43044304
t.Fatal(err)
43054305
}

0 commit comments

Comments
 (0)