* fix(incidents): add mapWebsocketPayload default to BaseIncidentPlugin
Default unwraps three WsClient-visible payload shapes:
- scheduler envelope { source, fetchedAt, items, totalCount } -> items
- GeoJSON FeatureCollection { features: [...] } -> normalised GeoEntity[]
- flat GeoEntity[] -> passthrough (with timestamp normalisation)
Resolves the silent-drop bug that left the earthquakes plugin showing an empty
layer when the scheduler's standard wrapping was used. Subclasses can still
override mapWebsocketPayload for domain-specific shapes (iranwarlive pattern
preserved by 2 override tests).
Test deltas:
- 2 it.fails tests flipped to passing (scheduler-wrap, GeoJSON unwrap).
- 2 'currently drops' companion tests deleted (bug is fixed, companions obsolete).
- 1 structural test updated: 'does NOT have mapWebsocketPayload' -> 'has mapWebsocketPayload defined'.
- Net: 12 tests -> 10 tests, all passing.
* test(ssrf): replace placeholder assertions with real tests for DNS pinning and size limits
The two expect(true).toBe(true) stubs in ssrf.spec.ts are replaced with
assertions that verify actual SSRF guard behavior:
- "enforces maxSize" — mocks undici.fetch to return an 11-byte body with a
10-byte limit; verifies response.text() rejects with "size exceeded"
- "does not follow redirects" — mocks dns.lookup + undici.fetch; verifies
redirect:manual is passed, fetch is called once, and the 301 is returned
without being followed; also asserts dispatcher is set (DNS pinning)
Uses vi.hoisted + vi.mock to patch dns/promises and undici at module level.
360/360 tests pass.
* fix(security): close Phase 3 audit blockers and unregistered flags
- WsClient.ts: guard subscribe() against sending frames while awaitingWelcome=true;
prevents pre-auth subscribe race when a second plugin subscribes mid-handshake
- connect/route.ts + callback/route.ts: set PKCE cookie path="/" to satisfy
RFC 6265bis __Host- prefix requirement (Chromium silently rejects __Host- + path!=/)
- pkce.spec.ts: update assertion to match corrected path="/"
- docs/plans/03-SECURITY.md: security audit report (13 closed, 1 fixed, 3 flags filed)
259 lines
10 KiB
TypeScript
259 lines
10 KiB
TypeScript
import type { WsStreamPayload, GeoEntity } from "@worldwideview/wwv-plugin-sdk";
|
|
import { dataBus } from "./DataBus";
|
|
import { pluginManager } from "../plugins/PluginManager";
|
|
import { useStore } from "../state/store";
|
|
import { ticketAuthEnabledForPlugin } from "../edition";
|
|
import type { PluginTicket } from "@worldwideview/wwv-plugin-sdk";
|
|
|
|
async function fetchPluginTicket(pluginId: string): Promise<PluginTicket> {
|
|
const res = await fetch(`/api/auth/ticket?pluginId=${encodeURIComponent(pluginId)}`);
|
|
if (!res.ok) throw new Error(`[WSClient] Ticket fetch failed (${res.status}) for ${pluginId}`);
|
|
const data = await res.json() as { token?: string };
|
|
if (!data.token) throw new Error(`[WSClient] Ticket response missing token for ${pluginId}`);
|
|
return data.token as PluginTicket;
|
|
}
|
|
|
|
interface EngineConnection {
|
|
ws: WebSocket | null;
|
|
reconnectTimer: NodeJS.Timeout | null;
|
|
subscriptions: Set<string>;
|
|
/** Grace period timer — closes the connection if no plugins remain subscribed */
|
|
cleanupTimer: NodeJS.Timeout | null;
|
|
/** Backoff attempt counter — resets after a stable connection (>5s open) */
|
|
reconnectAttempts: number;
|
|
/** Timer that resets the backoff counter once a connection has been stable */
|
|
stableConnectionTimer: NodeJS.Timeout | null;
|
|
/** True while waiting for the server's welcome after sending an auth message */
|
|
awaitingWelcome: boolean;
|
|
/** Closes the connection if the server doesn't send welcome within 3s */
|
|
authTimeoutTimer: NodeJS.Timeout | null;
|
|
}
|
|
|
|
const RECONNECT_BASE_MS = 5000;
|
|
const RECONNECT_MAX_MS = 60000; // Cap at 1 minute
|
|
const RECONNECT_JITTER_MS = 4000;
|
|
const STABLE_CONNECTION_MS = 5000; // Reset backoff after 5s of stable connection
|
|
const CLEANUP_GRACE_MS = 30000;
|
|
|
|
/** Normalizes underscore-based pluginIds to kebab-case (e.g. `my_plugin` → `my-plugin`). */
|
|
function normalizePluginId(id: string): string {
|
|
return id.replace(/_/g, "-");
|
|
}
|
|
|
|
class WebSocketClient {
|
|
private engines = new Map<string, EngineConnection>();
|
|
|
|
private getOrCreateEngine(engineUrl: string): EngineConnection {
|
|
let engine = this.engines.get(engineUrl);
|
|
if (!engine) {
|
|
engine = {
|
|
ws: null,
|
|
reconnectTimer: null,
|
|
subscriptions: new Set(),
|
|
cleanupTimer: null,
|
|
reconnectAttempts: 0,
|
|
stableConnectionTimer: null,
|
|
awaitingWelcome: false,
|
|
authTimeoutTimer: null,
|
|
};
|
|
this.engines.set(engineUrl, engine);
|
|
}
|
|
return engine;
|
|
}
|
|
|
|
private connectEngine(engineUrl: string) {
|
|
const engine = this.getOrCreateEngine(engineUrl);
|
|
|
|
if (engine.ws && (engine.ws.readyState === WebSocket.CONNECTING || engine.ws.readyState === WebSocket.OPEN)) {
|
|
return;
|
|
}
|
|
|
|
const wsStart = performance.now();
|
|
engine.ws = new WebSocket(engineUrl);
|
|
|
|
engine.ws.onopen = () => {
|
|
console.debug(`[WSClient] 🟢 Connected to ${engineUrl}. WS Handshake took ${(performance.now() - wsStart).toFixed(2)}ms`);
|
|
// Only reset backoff if the connection stays open for a non-trivial time —
|
|
// an immediate close (e.g. server-side rejection) shouldn't be treated as success.
|
|
if (engine.stableConnectionTimer) clearTimeout(engine.stableConnectionTimer);
|
|
engine.stableConnectionTimer = setTimeout(() => {
|
|
engine.reconnectAttempts = 0;
|
|
}, STABLE_CONNECTION_MS);
|
|
|
|
// Check whether any subscription on this engine requires ticket auth.
|
|
const ticketPlugin = [...engine.subscriptions].find((id) => ticketAuthEnabledForPlugin(id));
|
|
if (ticketPlugin) {
|
|
engine.awaitingWelcome = true;
|
|
fetchPluginTicket(ticketPlugin)
|
|
.then((ticket) => {
|
|
this.send(engine, { type: "auth", v: 1, token: ticket });
|
|
// 3s timeout — if the server doesn't send welcome, close and trigger reconnect.
|
|
engine.authTimeoutTimer = setTimeout(() => {
|
|
if (engine.awaitingWelcome) {
|
|
console.warn(`[WSClient] Auth timeout waiting for welcome from ${engineUrl}. Closing to reconnect.`);
|
|
engine.ws?.close();
|
|
}
|
|
}, 3000);
|
|
})
|
|
.catch((err: unknown) => {
|
|
console.error(`[WSClient] Failed to get ticket for ${ticketPlugin}:`, err instanceof Error ? err.message : err);
|
|
engine.ws?.close();
|
|
});
|
|
} else {
|
|
// No ticket auth required — subscribe immediately.
|
|
for (const pluginId of engine.subscriptions) {
|
|
this.send(engine, { action: "subscribe", pluginId });
|
|
}
|
|
}
|
|
};
|
|
|
|
engine.ws.onmessage = (event) => {
|
|
try {
|
|
const msgTime = performance.now();
|
|
console.debug(`[WSClient] 📥 Received raw message at +${(msgTime - wsStart).toFixed(2)}ms from start:`, event.data.substring(0, 150) + (event.data.length > 150 ? '...' : ''));
|
|
const data = JSON.parse(event.data);
|
|
|
|
if (data.type === "welcome") {
|
|
console.debug(`[WSClient] 👋 Engine ${engineUrl} serves: ${data.plugins?.join(", ")}`);
|
|
if (engine.awaitingWelcome) {
|
|
engine.awaitingWelcome = false;
|
|
if (engine.authTimeoutTimer) { clearTimeout(engine.authTimeoutTimer); engine.authTimeoutTimer = null; }
|
|
for (const pluginId of engine.subscriptions) {
|
|
this.send(engine, { action: "subscribe", pluginId });
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (data.type === "data" && data.pluginId && data.payload) {
|
|
this.handleDataMessage(data as WsStreamPayload);
|
|
}
|
|
} catch (err) {
|
|
console.error("[WSClient] Error parsing message:", err);
|
|
}
|
|
};
|
|
|
|
engine.ws.onerror = () => {
|
|
console.warn(`[WSClient] Connection to ${engineUrl} failed. Retrying in background...`);
|
|
};
|
|
|
|
engine.ws.onclose = () => {
|
|
engine.ws = null;
|
|
engine.awaitingWelcome = false;
|
|
if (engine.authTimeoutTimer) { clearTimeout(engine.authTimeoutTimer); engine.authTimeoutTimer = null; }
|
|
if (engine.stableConnectionTimer) {
|
|
clearTimeout(engine.stableConnectionTimer);
|
|
engine.stableConnectionTimer = null;
|
|
}
|
|
if (engine.reconnectTimer) clearTimeout(engine.reconnectTimer);
|
|
// Only reconnect if there are still active subscriptions
|
|
if (engine.subscriptions.size > 0) {
|
|
// Exponential backoff with jitter to prevent thundering herd on engine restart.
|
|
// 5s -> 10s -> 20s -> 40s -> 60s (cap), plus ±4s of jitter so simultaneous
|
|
// sessions don't all reconnect at the same instant.
|
|
const expDelay = Math.min(
|
|
RECONNECT_BASE_MS * Math.pow(2, engine.reconnectAttempts),
|
|
RECONNECT_MAX_MS
|
|
);
|
|
const delay = expDelay + Math.random() * RECONNECT_JITTER_MS;
|
|
engine.reconnectAttempts++;
|
|
console.warn(`[WSClient] Disconnected from ${engineUrl}. Reconnecting in ${Math.round(delay / 1000)}s (attempt ${engine.reconnectAttempts})...`);
|
|
engine.reconnectTimer = setTimeout(() => this.connectEngine(engineUrl), delay);
|
|
}
|
|
};
|
|
}
|
|
|
|
private handleDataMessage(data: WsStreamPayload) {
|
|
const pluginId = normalizePluginId(data.pluginId!);
|
|
const plugin = pluginManager.getPlugin(pluginId)?.plugin;
|
|
let finalEntities = data.payload as GeoEntity[];
|
|
const existingEntities = useStore.getState().entitiesByPlugin[pluginId] || [];
|
|
|
|
if (plugin && typeof (plugin as any).mapWebsocketPayload === "function") {
|
|
finalEntities = (plugin as any).mapWebsocketPayload(data.payload, existingEntities);
|
|
} else if (!Array.isArray(data.payload)) {
|
|
console.warn(`[WsClient] Payload for ${pluginId} is an object but no mapWebsocketPayload exists. Ignoring.`);
|
|
return;
|
|
} else {
|
|
finalEntities = finalEntities.map((e) => ({
|
|
...e,
|
|
timestamp: new Date(e.timestamp || Date.now()),
|
|
}));
|
|
}
|
|
|
|
console.debug(`[WSClient] 🔄 Dispatching ${finalEntities.length} entities for ${pluginId} to DataBus`);
|
|
|
|
dataBus.emit("dataUpdated", {
|
|
pluginId,
|
|
entities: finalEntities,
|
|
});
|
|
}
|
|
|
|
private send(engine: EngineConnection, msg: any) {
|
|
if (engine.ws && engine.ws.readyState === WebSocket.OPEN) {
|
|
engine.ws.send(JSON.stringify(msg));
|
|
}
|
|
}
|
|
|
|
public subscribe(pluginId: string, engineUrl: string) {
|
|
console.debug(`[WSClient] 📡 Subscribing to ${pluginId} at ${engineUrl}`);
|
|
const engine = this.getOrCreateEngine(engineUrl);
|
|
|
|
// Cancel any pending cleanup
|
|
if (engine.cleanupTimer) {
|
|
clearTimeout(engine.cleanupTimer);
|
|
engine.cleanupTimer = null;
|
|
}
|
|
|
|
engine.subscriptions.add(pluginId);
|
|
this.connectEngine(engineUrl);
|
|
// Only send immediately if auth is not in-flight; the welcome handler will
|
|
// replay all pending subscriptions once auth succeeds (see onmessage:121-124).
|
|
if (!engine.awaitingWelcome) {
|
|
this.send(engine, { action: "subscribe", pluginId });
|
|
}
|
|
}
|
|
|
|
public unsubscribe(pluginId: string, engineUrl: string) {
|
|
const engine = this.engines.get(engineUrl);
|
|
if (!engine) return;
|
|
|
|
engine.subscriptions.delete(pluginId);
|
|
this.send(engine, { action: "unsubscribe", pluginId });
|
|
|
|
// If no more subscriptions for this engine, schedule cleanup
|
|
if (engine.subscriptions.size === 0) {
|
|
engine.cleanupTimer = setTimeout(() => {
|
|
if (engine.subscriptions.size === 0) {
|
|
console.log(`[WSClient] No subscriptions remain for ${engineUrl}. Closing connection.`);
|
|
if (engine.reconnectTimer) clearTimeout(engine.reconnectTimer);
|
|
if (engine.stableConnectionTimer) clearTimeout(engine.stableConnectionTimer);
|
|
if (engine.authTimeoutTimer) { clearTimeout(engine.authTimeoutTimer); engine.authTimeoutTimer = null; }
|
|
engine.ws?.close();
|
|
this.engines.delete(engineUrl);
|
|
}
|
|
}, CLEANUP_GRACE_MS);
|
|
}
|
|
}
|
|
|
|
public printConnections() {
|
|
const table: any[] = [];
|
|
this.engines.forEach((engine, url) => {
|
|
table.push({
|
|
'Engine URL': url,
|
|
Status: engine.ws ? ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'][engine.ws.readyState] || 'UNKNOWN' : 'DISCONNECTED',
|
|
'Plugins Subscribed': Array.from(engine.subscriptions).join(", ") || "(None)",
|
|
});
|
|
});
|
|
console.groupCollapsed("[WSClient] Active Engine Connections Matrix");
|
|
console.table(table);
|
|
console.groupEnd();
|
|
}
|
|
}
|
|
|
|
export const wsClient = new WebSocketClient();
|
|
|
|
if (typeof window !== "undefined") {
|
|
(window as any).wwvDebugConnections = () => wsClient.printConnections();
|
|
}
|