Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .github/workflows/test-websocket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
matrix:
go-version:
- 1.25.x
- 1.26.x
steps:
- name: Fetch Repository
uses: actions/checkout@v6
Expand Down
2 changes: 1 addition & 1 deletion v3/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func New(handler func(*Conn), config ...Config) fiber.Handler {
}

// ip address
conn.ip = c.IP()
conn.ip = utils.CopyString(c.IP())

if err := upgrader.Upgrade(c.RequestCtx(), func(fconn *websocket.Conn) {
conn.Conn = fconn
Expand Down
71 changes: 71 additions & 0 deletions v3/websocket/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,77 @@ func TestWebSocketConnIP(t *testing.T) {
assert.Equal(t, "hello websocket", msg["message"])
}

func TestWebSocketConnIPStaysStableAcrossRequests(t *testing.T) {
trigger := make(chan struct{})

app := fiber.New(fiber.Config{
ProxyHeader: "X-Real-IP",
TrustProxy: true,
TrustProxyConfig: fiber.TrustProxyConfig{
Loopback: true,
},
})

app.Get("/ws/hold", New(func(c *Conn) {
<-trigger
c.WriteJSON(fiber.Map{
"ip": c.IP(),
})
}))

app.Get("/ws/churn", New(func(c *Conn) {
c.WriteJSON(fiber.Map{
"message": "ok",
})
}))

go app.Listen(":3000", fiber.ListenConfig{DisableStartupMessage: true})
defer app.Shutdown()

readyCh := make(chan struct{})
go func() {
for {
conn, err := net.Dial("tcp", "localhost:3000")
if err != nil {
continue
}
conn.Close()
readyCh <- struct{}{}
return
}
}()
<-readyCh
Comment on lines +377 to +392

Copilot AI Apr 11, 2026

Copy link

Choose a reason for hiding this comment

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

This test starts a real server on a hard-coded ":3000" and waits for readiness via an infinite busy-spin dial loop. If the port is already in use or app.Listen fails, the test can hang indefinitely and burn CPU. Consider using an ephemeral/in-memory listener (see v3/socketio/socketio_test.go using fasthttputil.NewInmemoryListener() + app.Listener) or, at minimum, capture/handle the Listen error and add a bounded timeout + small backoff in the readiness loop.

Copilot uses AI. Check for mistakes.

const firstIP = "165.227.7.228"

conn, resp, err := websocket.DefaultDialer.Dial("ws://localhost:3000/ws/hold", http.Header{
"X-Real-IP": []string{firstIP},
})
if !assert.NoError(t, err) {
return
}
defer conn.Close()
assert.Equal(t, 101, resp.StatusCode)

for i := 0; i < 10; i++ {
churnConn, _, churnErr := websocket.DefaultDialer.Dial("ws://localhost:3000/ws/churn", http.Header{
"X-Real-IP": []string{"89.125.209.209"},
})
if !assert.NoError(t, churnErr) {
close(trigger)
return
}
churnConn.Close()
}

close(trigger)

var msg fiber.Map
err = conn.ReadJSON(&msg)
assert.NoError(t, err)
assert.Equal(t, firstIP, msg["ip"])
}

func setupTestApp(cfg Config, h func(c *Conn)) *fiber.App {
var handler fiber.Handler
if h == nil {
Expand Down
Loading