Here are the potential issues found in the codebase:
-
Ignored Sync Error in Logging (cmd/signaling/main.go)
- File:
cmd/signaling/main.go
- Issue:
logger.Sync()
error is ignored with// nolint:errcheck
. In production, this could lead to lost log entries during shutdown. - Fix: Handle the error or log it.
- File:
-
Panic in Test Proxy (cmd/testproxy/main.go)
- File:
cmd/testproxy/main.go
- Issue: HTTP handlers use
panic(err)
which crashes the server on errors. Not suitable for production. - Fix: Return HTTP 500 errors instead of panicking.
- File:
-
Potential Sleep After Context Cancel (internal/cloudflare/credentials.go)
- File:
internal/cloudflare/credentials.go
- Issue: The loop sleeps without checking
ctx.Done()
, delaying shutdown. - Fix: Use
select
with<-ctx.Done()
and<-time.After(...)
.
- File:
-
Max Players Check Logic (internal/signaling/stores/postgres.go)
- File:
internal/signaling/stores/postgres.go
(JoinLobby) - Issue:
len(peerlist) >= maxPlayers
blocks ifmaxPlayers
is 0. Default is 64, but code should handle 0 as "unlimited". - Fix: Add
maxPlayers <= 0
check to bypass the condition.
- File:
-
SQL Injection Risk in Publish (internal/signaling/stores/postgres.go)
- File:
internal/signaling/stores/postgres.go
(Publish) - Issue: Using string interpolation for
NOTIFY
payload. Malicious data could break the query. - Fix: Validate
topic
anddata
length/format strictly.
- File:
-
Insecure CORS Configuration (internal/signaling/handler.go)
- File:
internal/signaling/handler.go
- Issue:
cors.Default()
allows any origin. In production, this should be restricted. - Fix: Configure CORS with allowed origins from environment variables.
- File:
-
Deprecated rand.Seed Usage (cmd/signaling/main.go)
- File:
cmd/signaling/main.go
- Issue:
rand.Seed
is deprecated in Go 1.20+. - Fix: Use
rand.New(rand.NewSource(...))
for local generators.
- File:
-
Test Proxy Arbitrary SQL Execution (cmd/testproxy/main.go)
- File:
cmd/testproxy/main.go
(/sql endpoint) - Issue: Executes arbitrary SQL, a security risk if exposed outside tests.
- Fix: Ensure the testproxy is only enabled in test environments.
- File:
-
Race Condition in Reconnection (lib/signaling.ts)
- File:
lib/signaling.ts
(reconnect method) - Issue: Backoff uses
Math.random() * 100 * attempt
, leading to unpredictable retries. - Fix: Use exponential backoff with jitter.
- File:
-
Unhandled Promise Rejection (example/main.ts)
- File:
example/main.ts
(create lobby) - Issue:
void n.create(...)
without error handling. - Fix: Add
.catch()
to handle errors.
- File:
-
Potential Deadlock in Leader Election (internal/signaling/stores/postgres.go)
- File:
internal/signaling/stores/postgres.go
(DoLeaderElection) - Issue: Locking the entire
peers
table could cause contention. - Fix: Optimize locking scope or use advisory locks.
- File:
-
Missing Error Handling in MarkPeerAsActive (internal/signaling/timeout_manager.go)
- File:
internal/signaling/timeout_manager.go
(MarkPeerAsActive) - Issue: Logs errors but doesn’t retry, possibly marking active peers as inactive.
- Fix: Implement retry logic or queue for retries.
- File:
-
WebSocket Origin Vulnerability (internal/signaling/handler.go)
- File:
internal/signaling/handler.go
- Issue:
InsecureSkipVerify: true
allows any WebSocket origin. - Fix: Validate the
Origin
header against allowed domains.
- File:
-
ICE Server Filtering (lib/peer.ts)
- File:
lib/peer.ts
(_addPeer) - Issue: Removes TURN servers if credentials are missing, potentially breaking NAT traversal.
- Fix: Ensure at least one STUN server remains if TURN is unavailable.
- File: