Repository: libp2p/go-libp2p
Pull request #1770 (2022-09-21 00:04:32)

websocket: fix nil pointer in tlsClientConf
It's possible (and common) to have a WebSocket transport without a tls client config. This leads to a nil pointer deref error when we set the ServerName.

This sets a default empty `tlsClientConf` and adds a test case.

Fixes https://github.com/libp2p/go-libp2p/issues/1769

github-actions[bot]: Suggested version: `v0.23.1`
Comparing to: [`v0.23.0`](https://github.com/libp2p/go-libp2p/releases/tag/v0.23.0) ([diff](https://github.com/libp2p/go-libp2p/compare/v0.23.0..libp2p:marco/fix-nil-pointer))

Changes in `go.mod` file(s):
```diff
(empty)
```

`gorelease` says:
```
# diagnostics
go.mod: the following requirements are needed
	github.com/libp2p/go-yamux/v3@v3.1.2
Run go mod tidy to add missing requirements.
required module github.com/microcosm-cc/bluemonday@v1.0.1 retracted by module author: Retract older versions as only latest is to be depended upon

# summary
Suggested version: v0.23.1
```

`gocompat` says:
```
(empty)
```
<!-- Sticky Pull Request Commentrelease-check -->

Patch:
diff --git a/.github/workflows/go-check.yml b/.github/workflows/go-check.yml
index 251f7faa..3f982c1d 100644
--- a/.github/workflows/go-check.yml
+++ b/.github/workflows/go-check.yml
@@ -11,10 +11,10 @@ jobs:
     env:
       RUNGOGENERATE: false
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v2
         with:
           submodules: recursive
-      - uses: actions/setup-go@v3
+      - uses: actions/setup-go@v2
         with:
           go-version: "1.19.x"
       - name: Run repo-specific setup
diff --git a/p2p/transport/websocket/websocket.go b/p2p/transport/websocket/websocket.go
index 04af9b8c..2e9fc0b0 100644
--- a/p2p/transport/websocket/websocket.go
+++ b/p2p/transport/websocket/websocket.go
@@ -95,8 +95,9 @@ func New(u transport.Upgrader, rcmgr network.ResourceManager, opts ...Option) (*
 		rcmgr = network.NullResourceManager
 	}
 	t := &WebsocketTransport{
-		upgrader: u,
-		rcmgr:    rcmgr,
+		upgrader:      u,
+		rcmgr:         rcmgr,
+		tlsClientConf: &tls.Config{},
 	}
 	for _, opt := range opts {
 		if err := opt(t); err != nil {


Test patch:
diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml
index 8a1697b2..360f0b6e 100644
--- a/.github/workflows/go-test.yml
+++ b/.github/workflows/go-test.yml
@@ -16,10 +16,10 @@ jobs:
     runs-on: ${{ format('{0}-latest', matrix.os) }}
     name: ${{ matrix.os }} (go ${{ matrix.go }})
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v2
         with:
           submodules: recursive
-      - uses: actions/setup-go@v3
+      - uses: actions/setup-go@v2
         with:
           go-version: ${{ matrix.go }}
       - name: Go information
diff --git a/p2p/transport/websocket/websocket_test.go b/p2p/transport/websocket/websocket_test.go
index 9ac71a77..1961e9ce 100644
--- a/p2p/transport/websocket/websocket_test.go
+++ b/p2p/transport/websocket/websocket_test.go
@@ -165,10 +165,7 @@ func testWSSServer(t *testing.T, listenAddr ma.Multiaddr) (ma.Multiaddr, peer.ID
 		t.Fatal(err)
 	}
 
-	// l, err := tpt.Listen(ma.StringCast("/ip4/127.0.0.1/tcp/0/wss"))
-	// l, err := tpt.Listen(ma.StringCast("/ip4/127.0.0.1/tcp/0/tls/ws"))
 	l, err := tpt.Listen(listenAddr)
-	fmt.Println("here", listenAddr)
 	require.NoError(t, err)
 	t.Cleanup(func() {
 		l.Close()
@@ -245,6 +242,24 @@ func TestDialWss(t *testing.T) {
 	require.NoError(t, err)
 }
 
+func TestDialWssNoClientCert(t *testing.T) {
+	serverMA, rid, _ := testWSSServer(t, ma.StringCast("/ip4/127.0.0.1/tcp/0/tls/sni/example.com/ws"))
+	require.Contains(t, serverMA.String(), "tls")
+
+	_, u := newSecureUpgrader(t)
+	tpt, err := New(u, network.NullResourceManager)
+	require.NoError(t, err)
+
+	masToDial, err := tpt.Resolve(context.Background(), serverMA)
+	require.NoError(t, err)
+
+	_, err = tpt.Dial(context.Background(), masToDial[0], rid)
+	require.Error(t, err)
+
+	// The server doesn't have a signed certificate
+	require.Contains(t, err.Error(), "x509")
+}
+
 func TestWebsocketTransport(t *testing.T) {
 	t.Skip("This test is failing, see https://github.com/libp2p/go-ws-transport/issues/99")
 	_, ua := newUpgrader(t)