diff --git a/internal/server/app_notifications.go b/internal/server/app_notifications.go index 9a3b6e86..999ccea0 100644 --- a/internal/server/app_notifications.go +++ b/internal/server/app_notifications.go @@ -39,25 +39,37 @@ type appNotifier struct { } type appNotificationSub struct { - queue chan protocol.Message - done chan struct{} - once sync.Once + ctx context.Context + requestID string + started time.Time + queue chan protocol.Message + done chan struct{} + once sync.Once + ended sync.Once } func newAppNotifier(store appNotificationStore) *appNotifier { return &appNotifier{store: store, now: time.Now, subs: map[string]*appNotificationSub{}} } -func (s *appNotificationSub) stop() { s.once.Do(func() { close(s.done) }) } +func (s *appNotificationSub) stop(reason string) { + s.once.Do(func() { close(s.done) }) + s.ended.Do(func() { + log.Printf("notification subscription ended request_id=%q duration=%s reason=%s", s.requestID, time.Since(s.started), reason) + }) +} func (s *appNotificationSub) run(write func(protocol.Message) error) { for { select { case <-s.done: return + case <-s.ctx.Done(): + s.stop("server stopping") + return case msg := <-s.queue: if write(msg) != nil { - s.stop() + s.stop("write error") return } } @@ -77,7 +89,7 @@ func (a *appNotifier) enqueueLocked(msg protocol.Message, exclude string) { delete(a.subs, id) default: log.Printf("app notifications: dropping slow subscriber %s", id) - sub.stop() + sub.stop("write error") delete(a.subs, id) } } @@ -93,11 +105,15 @@ func (a *appNotifier) subscribe(ctx context.Context, clientID, requestID, after return err } if old := a.subs[clientID]; old != nil { - old.stop() + old.stop("client closed") + } + sub := &appNotificationSub{ + ctx: ctx, requestID: requestID, started: time.Now(), + queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{}), } - sub := &appNotificationSub{queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{})} sub.queue <- protocol.Message{Type: protocol.NotificationsSubscribed, ID: requestID, Notifications: active, IDs: cleared} a.subs[clientID] = sub + log.Printf("notification subscription started request_id=%q cursor=%q", requestID, after) go sub.run(write) return nil } @@ -106,7 +122,11 @@ func (a *appNotifier) detach(clientID string) { a.mu.Lock() defer a.mu.Unlock() if sub := a.subs[clientID]; sub != nil { - sub.stop() + reason := "client closed" + if sub.ctx.Err() != nil { + reason = "server stopping" + } + sub.stop(reason) delete(a.subs, clientID) } } diff --git a/internal/server/app_notifications_test.go b/internal/server/app_notifications_test.go index e4d5d2be..22ecadad 100644 --- a/internal/server/app_notifications_test.go +++ b/internal/server/app_notifications_test.go @@ -1,10 +1,13 @@ package server import ( + "bytes" "context" "encoding/json" "fmt" + "log" "net/http/httptest" + "strings" "testing" "time" @@ -17,6 +20,30 @@ import ( "git.a-wan.net/alan/pipelines/internal/summarise" ) +func TestAppNotificationSubscriptionLogsLifecycle(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + f := newAppNotificationFixture(t, ctx) + var output bytes.Buffer + previous := log.Writer() + log.SetOutput(&output) + t.Cleanup(func() { log.SetOutput(previous) }) + + phone := dialAckClient(t, ctx, f.url) + subscribeNotifications(t, ctx, phone, "cursor-1") + if !strings.Contains(output.String(), `notification subscription started request_id="sub" cursor="cursor-1"`) { + t.Fatalf("subscription start log = %q", output.String()) + } + phone.CloseNow() + deadline := time.Now().Add(2 * time.Second) + for !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + } + if !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) || !strings.Contains(output.String(), "reason=client closed") { + t.Fatalf("subscription end log = %q", output.String()) + } +} + // appNotificationFixture is a handler over a temp store with one pipeline, // plus helpers to make a run end with a durable notification intent. type appNotificationFixture struct { diff --git a/web/serve.mjs b/web/serve.mjs index dbae20fc..1055c51b 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -705,6 +705,12 @@ function proxyUpgrade(req, clientSocket, clientHead) { clientSocket.on("error", () => upstream.destroy()); } +function logRejectedUpgrade(status, pathname, reason) { + // pathname excludes the query string so cookies/tokens can never enter this + // diagnostic line. + console.warn(`websocket upgrade rejected status=${status} path=${pathname} reason=${reason}`); +} + server.on("upgrade", (req, clientSocket, clientHead) => { try { refreshAuth(); } catch { clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n"); @@ -712,11 +718,13 @@ server.on("upgrade", (req, clientSocket, clientHead) => { } const pathname = (req.url ?? "").split("?")[0]; if (access.plainLanBlocked(req) || !sameOriginUpgrade(req.headers.origin, req.headers.host)) { + logRejectedUpgrade(403, pathname, access.plainLanBlocked(req) ? "LAN access refused" : "origin mismatch"); clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); clientSocket.destroy(); return; } if (auth && !isAuthed(req)) { + logRejectedUpgrade(401, pathname, "not authenticated"); clientSocket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); clientSocket.destroy(); return; diff --git a/web/test/serve.test.mjs b/web/test/serve.test.mjs index b85b4966..7a2d9ea9 100644 --- a/web/test/serve.test.mjs +++ b/web/test/serve.test.mjs @@ -30,6 +30,46 @@ async function start(args) { return { base: `http://127.0.0.1:${port}`, stop: () => child.kill() }; } +async function rawUpgrade(port, path, headers = {}) { + const socket = net.createConnection(port, "127.0.0.1"); + await once(socket, "connect"); + const request = [ + `GET ${path} HTTP/1.1`, + `Host: 127.0.0.1:${port}`, + "Connection: Upgrade", + "Upgrade: websocket", + "Sec-WebSocket-Version: 13", + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==", + ...Object.entries(headers).map(([name, value]) => `${name}: ${value}`), + "", + "", + ].join("\r\n"); + const reply = new Promise((resolve) => { + const done = (data = "") => { + socket.destroy(); + resolve(data.toString()); + }; + socket.once("data", done); + socket.once("close", () => done()); + socket.once("error", () => done()); + }); + socket.end(request); + return reply; +} + +function nextLog(stderr, predicate) { + return new Promise((resolve) => { + let output = ""; + const onData = (chunk) => { + output += chunk.toString(); + if (!predicate(output)) return; + stderr.off("data", onData); + resolve(output); + }; + stderr.on("data", onData); + }); +} + test("serves the app root, deep routes and the update feed", async (t) => { const dir = await mkdtemp(join(tmpdir(), "serve-")); const root = join(dir, "app"); @@ -92,6 +132,40 @@ test("icons and the manifest need no sign-in; the app does", async (t) => { assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401); }); +test("rejected websocket upgrades log status and a redacted reason", async (t) => { + const dir = await mkdtemp(join(tmpdir(), "serve-upgrade-log-")); + const auth = join(dir, "auth.json"); + await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) })); + const port = await freePort(); + const child = spawn(process.execPath, ["serve.mjs", "--port", String(port)], { + cwd: here, + env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, + stdio: ["ignore", "pipe", "pipe"], + }); + t.after(async () => { + child.kill(); + await once(child, "exit").catch(() => {}); + }); + await once(child.stdout, "data"); + + const forbiddenLog = nextLog(child.stderr, (output) => output.includes("status=403")); + const forbidden = await rawUpgrade(port, "/connect?token=forbidden-secret", { + Origin: "http://evil.example", + Cookie: "session=private-cookie", + }); + assert.match(forbidden, /^HTTP\/1\.1 403 Forbidden/m); + const forbiddenOutput = await forbiddenLog; + assert.match(forbiddenOutput, /websocket upgrade rejected status=403 path=\/connect reason=origin mismatch/); + assert.doesNotMatch(forbiddenOutput, /forbidden-secret|private-cookie/); + + const unauthorizedLog = nextLog(child.stderr, (output) => output.includes("status=401")); + const unauthorized = await rawUpgrade(port, "/private?token=unauthorized-secret"); + assert.match(unauthorized, /^HTTP\/1\.1 401 Unauthorized/m); + const unauthorizedOutput = await unauthorizedLog; + assert.match(unauthorizedOutput, /websocket upgrade rejected status=401 path=\/private reason=not authenticated/); + assert.doesNotMatch(unauthorizedOutput, /unauthorized-secret/); +}); + test("files require sign-in and proxy query, ranges and attachment headers", async (t) => { const requests = []; const upstream = createServer((req, res) => {