Skip to content
Open
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
8 changes: 7 additions & 1 deletion pkg/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"sync"
Expand Down Expand Up @@ -142,8 +143,13 @@ func (c *Client) connectAndServe(ctx context.Context) error {

c.logger.Info("connecting to relay", "url", c.relayURL)

conn, _, err := websocket.DefaultDialer.DialContext(ctx, c.relayURL, header)
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, c.relayURL, header)
if err != nil {
if resp != nil {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body.Close() // nolint:errcheck
return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body)
}
Comment on lines +148 to +152

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential nil pointer dereference panics, we should check if resp.Body is nil before attempting to read from or close it. Even if resp.Body is nil, we can still safely report the HTTP status code.

		if resp != nil {
			var body []byte
			if resp.Body != nil {
				body, _ = io.ReadAll(io.LimitReader(resp.Body, 1024))
				resp.Body.Close() // nolint:errcheck
			}
			return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body)
		}

return fmt.Errorf("dial failed: %w", err)
}

Expand Down
Loading