Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(ws-do): harden auth and disable public access (#99)

Genered945e953449
diff --git a/src/ws-do/src/ws-durable.test.ts b/src/ws-do/src/ws-durable.test.ts
index 6b77a530d..6c7739f4b 100644
--- a/src/ws-do/src/ws-durable.test.ts
+++ b/src/ws-do/src/ws-durable.test.ts
@@ -223,6 +223,32 @@ describe("WebSocketDurableObject", () => {
expect(ws.send).not.toHaveBeenCalled()
})
+ it("closes with 1008 when auth message has no token", async () => {
+ const { durable } = createDO()
+
+ const ws = createMockWebSocket()
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
+
+ await durable.webSocketMessage(ws as any, JSON.stringify({ type: "auth" }))
+
+ expect(ws.close).toHaveBeenCalledWith(1008, "Unauthorized")
+ expect(ws.send).not.toHaveBeenCalled()
+ expect(mockGetValidSession).not.toHaveBeenCalled()
+ })
+
+ it("closes with 1008 when auth message has empty string token", async () => {
+ const { durable } = createDO()
+
+ const ws = createMockWebSocket()
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
+
+ await durable.webSocketMessage(ws as any, JSON.stringify({ type: "auth", token: "" }))
+
+ expect(ws.close).toHaveBeenCalledWith(1008, "Unauthorized")
+ expect(ws.send).not.toHaveBeenCalled()
+ expect(mockGetValidSession).not.toHaveBeenCalled()
+ })
+
it("closes unauthenticated connection sending non-auth message", async () => {
const { durable } = createDO()
diff --git a/src/ws-do/src/ws-durable.ts b/src/ws-do/src/ws-durable.ts
index 8b118aefa..a31ca0c23 100644
--- a/src/ws-do/src/ws-durable.ts
+++ b/src/ws-do/src/ws-durable.ts
@@ -63,7 +63,11 @@ export class WebSocketDurableObject extends DurableObject<Env> {
return
}
- const userId = await this.validateToken(msg.token!)
+ if (!msg.token) {
+ ws.close(1008, "Unauthorized")
+ return
+ }
+ const userId = await this.validateToken(msg.token)
if (!userId) {
log.warn("websocket auth failed")
ws.close(1008, "Unauthorized")
diff --git a/src/ws-do/wrangler.toml b/src/ws-do/wrangler.toml
index 0002e4a38..ece7bde41 100644
--- a/src/ws-do/wrangler.toml
+++ b/src/ws-do/wrangler.toml
@@ -2,6 +2,7 @@ name = "alook-ws-do"
main = "src/index.ts"
compatibility_date = "2025-04-01"
compatibility_flags = ["nodejs_compat"]
+workers_dev = false
[observability]
enabled = true