A modern and elegant dashboard for network traffic visualization and analysis.
fix(websocket): decode the session cookie before verifying it (#92)
luoleidaaaaf278576
diff --git a/apps/collector/src/modules/websocket/websocket.server.ts b/apps/collector/src/modules/websocket/websocket.server.tsindex 56f2ed4..8ea282d 100644--- a/apps/collector/src/modules/websocket/websocket.server.ts+++ b/apps/collector/src/modules/websocket/websocket.server.ts@@ -6,6 +6,7 @@ import type { SummaryFieldKey, SummaryFieldMask } from './websocket.types.js';import { AuthService } from '../auth/auth.service.js';import { IncomingMessage } from 'http';import { URL } from 'url';+import { readCookie } from '../../shared/utils/cookie-header.js';export interface WebSocketMessage {type: 'stats' | 'ping' | 'pong' | 'subscribe';@@ -230,16 +231,8 @@ export class StatsWebSocketServer {let token = url.searchParams.get('token');// Try getting token from cookie if not in URL- if (!token && req.headers.cookie) {- const cookies = req.headers.cookie.split(';').reduce((acc, cookie) => {- const [key, value] = cookie.trim().split('=');- acc[key] = value;- return acc;- }, {} as Record<string, string>);-- if (cookies['neko-session']) {- token = cookies['neko-session'];- }+ if (!token) {+ token = readCookie(req.headers.cookie, 'neko-session');}// Check if auth is required and verify tokendiff --git a/apps/collector/src/shared/utils/cookie-header.test.ts b/apps/collector/src/shared/utils/cookie-header.test.tsnew file mode 100644index 0000000..9774598--- /dev/null+++ b/apps/collector/src/shared/utils/cookie-header.test.ts@@ -0,0 +1,22 @@+import { describe, expect, it } from "vitest";+import { readCookie } from "./cookie-header.js";++describe("readCookie", () => {+ it("decodes percent-encoded values set by @fastify/cookie", () => {+ const header = `theme=dark; neko-session=${encodeURIComponent("abc1+/=")}`;+ expect(readCookie(header, "neko-session")).toBe("abc1+/=");+ });++ it("keeps everything after the first '=' as the value", () => {+ expect(readCookie("neko-session=YWJj==; x=1", "neko-session")).toBe("YWJj==");+ });++ it("returns the raw value when percent-encoding is malformed", () => {+ expect(readCookie("neko-session=%E0%A4%A", "neko-session")).toBe("%E0%A4%A");+ });++ it("returns null when the cookie is missing", () => {+ expect(readCookie("a=1; b=2", "neko-session")).toBeNull();+ expect(readCookie(undefined, "neko-session")).toBeNull();+ });+});diff --git a/apps/collector/src/shared/utils/cookie-header.ts b/apps/collector/src/shared/utils/cookie-header.tsnew file mode 100644index 0000000..955ed34--- /dev/null+++ b/apps/collector/src/shared/utils/cookie-header.ts@@ -0,0 +1,21 @@+/**+ * Read one cookie from a raw `Cookie` header. The WebSocket server sees the+ * header before any cookie plugin runs, and @fastify/cookie percent-encodes+ * values when it sets them, so values are decoded here (issue #92). Only the+ * first `=` separates name and value; base64 tokens may contain `=`.+ */+export function readCookie(header: string | undefined, name: string): string | null {+ if (!header) return null;+ for (const part of header.split(';')) {+ const index = part.indexOf('=');+ if (index === -1) continue;+ if (part.slice(0, index).trim() !== name) continue;+ const raw = part.slice(index + 1).trim();+ try {+ return decodeURIComponent(raw);+ } catch {+ return raw;+ }+ }+ return null;+}