Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
jubnl
2026-08-10 22:32:10 +02:00
18 changed files with 1190 additions and 270 deletions
+25
View File
@@ -1,3 +1,4 @@
import nodeHttp from 'node:http';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter, type NestExpressApplication } from '@nestjs/platform-express';
import type { INestApplication } from '@nestjs/common';
@@ -12,6 +13,7 @@ import { McpRegistryService } from '@trek/nest-mcp';
import { setMcpRegistry } from './mcp/registry-handoff';
import { validateBodyContracts } from './nest/common/validate-body-contracts';
import { validateRouteGuards } from './nest/common/validate-route-guards';
import { TrekWsAdapter } from './nest/realtime/trek-ws.adapter';
/**
* Builds the unified TREK NestJS application that serves the ENTIRE surface — the
@@ -43,12 +45,35 @@ import { validateRouteGuards } from './nest/common/validate-route-guards';
* SpaFallbackFilter (APP_FILTER in AppModule); the global error envelope is the
* TrekExceptionFilter (also APP_FILTER).
*/
let boundHttpServer: nodeHttp.Server | null = null;
/**
* The http server buildApp created and bound the ws gateway to.
*
* index.ts listens on it; the websocket suites connect to it. Anyone creating a
* second server around the same express instance would get a working REST API
* and a /ws that no client can reach.
*/
export function getHttpServer(): nodeHttp.Server {
if (!boundHttpServer) throw new Error('buildApp() has not run yet');
return boundHttpServer;
}
export async function buildApp(): Promise<INestApplication> {
// rawBody keeps the unparsed request bytes on req.rawBody so a plugin webhook
// route can verify a provider's HMAC signature over the exact payload (the
// parsed JSON alone can't be re-serialised byte-for-byte).
const app = await NestFactory.create(AppModule, new ExpressAdapter(), { rawBody: true });
const instance = app.getHttpAdapter().getInstance();
// The http server is created HERE, not by the caller after buildApp returns,
// and that ordering is the whole point: Nest binds gateways during app.init(),
// so the ws adapter has to exist before it and has to already know which
// server to attach to. Handing it the app instead would bind the ws server to
// Nest's own internal http server, which this process never listens on: the
// boot succeeds, the gateway logs as registered, every test passes, and no
// browser can connect. Callers take the server from getHttpServer() below.
boundHttpServer = nodeHttp.createServer(instance);
app.useWebSocketAdapter(new TrekWsAdapter(boundHttpServer));
// ConfigModule.forRoot's load factories already ran inside NestFactory.create,
// so the boot-stable snapshot is resolvable here, BEFORE app.init() — this is
// the one bridge that lets the pre-init Express layer consume the validated
+7 -5
View File
@@ -8,7 +8,7 @@ import path from 'node:path';
import fs from 'node:fs';
import http from 'node:http';
import type { INestApplication } from '@nestjs/common';
import { buildApp } from './bootstrap';
import { buildApp, getHttpServer } from './bootstrap';
// Create upload and data directories on startup.
// Every uploads subdir the app writes to must be listed here (#1762): a dir
@@ -86,9 +86,8 @@ const onListen = () => {
if (env.demo.enabled && env.app.isProduction) {
sLogWarn('SECURITY WARNING: DEMO_MODE is enabled in production!');
}
import('./websocket').then(({ setupWebSocket }) => {
setupWebSocket(server);
});
// Nothing else to boot by hand: the crons are domain providers on the
// scheduling registrar, and /ws is the Nest gateway buildApp already bound.
};
let server: http.Server;
@@ -101,7 +100,10 @@ async function bootstrap(): Promise<void> {
// (/mcp, /.well-known, OAuth SDK, SPA catch-all). buildApp() owns the composition
// order; it is shared with the integration-test harness so they can't drift.
nestApp = await buildApp();
server = http.createServer(nestApp.getHttpAdapter().getInstance());
// The server buildApp created and bound /ws to. Creating a second one here
// would serve the REST API fine and leave the gateway attached to a socket
// nobody listens on.
server = getHttpServer();
if (HOST) server.listen(PORT, HOST, onListen);
else server.listen(PORT, onListen);
}
+38
View File
@@ -146,6 +146,44 @@ tools, which never pass through an HTTP guard. In the five domains piloted for
files of import churn, and `scripts/coverage-thresholds.mjs` buckets on one
path segment, so merging `photos/` into it would collapse two ratchet
entries into one and drop the stricter of the two.
- `realtime/` — the /ws transport, as a Nest gateway.
- `realtime.gateway.ts` owns the connection lifecycle and the handshake, with
`DatabaseService` and `EphemeralTokenService` injected instead of imported.
It replaced `setupWebSocket(server)`, which index.ts kicked off with a
dynamic import after listen().
- `ws-state.ts` holds the socket registry, and that is **module state on
purpose**. Seventeen `*.bridge.ts` files and `notifications.instance.ts`
build `new RealtimeService()` outside the container; rooms on a provider
instance would leave their broadcasts going to an empty map, with no error
and no log. Same reasoning as the geo throttle cursor.
- `trek-ws.adapter.ts` exists because the stock `WsAdapter` dispatches on
`{ event, data }` and every deployed client sends `{ type, tripId }`. It
also builds the ws server itself rather than delegating: the base's port-0
branch uses `noServer: true` and performs the upgrade from its own registry,
which never consults `verifyClient`, so delegating would silently drop the
origin check. The per-socket `error` listener is attached FIRST, because an
unhandled one kills the process (#1576), and the flood guard counts every
inbound frame before parsing, because malformed frames are the ones worth
limiting.
- The socket id stays a **monotonic integer**. The client echoes it as
`X-Socket-Id` and `broadcast` excludes the originator with
`Number(excludeSid)`; a uuid is `NaN`, `NaN === NaN` is false, and every
client would receive its own writes back. It shows up as drag-and-drop that
jumps, not as an error.
- `RealtimeGatewayModule` is imported by AppModule alone. RealtimeModule is
@Global, so a gateway there follows into every hand-assembled TestingModule,
and Nest's SocketModule then loads an adapter none of them set: it reaches
for `@nestjs/platform-socket.io` and calls `process.exit(1)` when it is
absent.
- `buildApp()` creates the http server and binds the adapter to it BEFORE
`app.init()`, and callers take it from `getHttpServer()`. Nest binds gateways
during init, so an adapter registered later binds nothing, and one given the
app instead of the server binds to Nest's internal server, which this process
never listens on. Either way the boot is green and no browser can connect.
- `src/websocket.ts` survives as a re-export. 115 test files `vi.mock` that
path to assert on broadcasts; repointing them all would be a large diff that
proves nothing.
- `geo/` — the ONE Nominatim client (#576). There used to be two: atlas
throttled to the published 1.1s and cached its answers, maps did neither, so
five interactive routes fired straight at the service while the atlas loops
+2 -1
View File
@@ -61,6 +61,7 @@ import { TrekExceptionFilter } from './common/trek-exception.filter';
import { SpaFallbackFilter } from './platform/spa-fallback.filter';
import { IdempotencyInterceptor } from './common/idempotency.interceptor';
import { IdempotencyCleanupJob } from './common/idempotency-cleanup.job';
import { RealtimeGatewayModule } from './realtime/realtime-gateway.module';
/**
* Root NestJS module for the incremental migration. Domain modules
@@ -68,7 +69,7 @@ import { IdempotencyCleanupJob } from './common/idempotency-cleanup.job';
* migrated.
*/
@Module({
imports: [AppConfigModule, DatabaseModule, RealtimeModule, SchedulingModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), HealthModule, WeatherModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, GeoModule, MapsModule, PlaceEnrichmentModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, DayNotesModule, AccommodationsModule, AssignmentsModule, PlacesModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, ReservationImportModule, LlmParseModule],
imports: [AppConfigModule, DatabaseModule, RealtimeModule, RealtimeGatewayModule, SchedulingModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), HealthModule, WeatherModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, GeoModule, MapsModule, PlaceEnrichmentModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, DayNotesModule, AccommodationsModule, AssignmentsModule, PlacesModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, ReservationImportModule, LlmParseModule],
providers: [
// Default-deny: a route is authenticated unless it carries @Public() or
// @OptionalAuth(), or declares its own @UseGuards chain. Protection used to
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { RealtimeGateway } from './realtime.gateway';
import { EphemeralTokenModule } from '../auth/ephemeral-token.module';
/**
* The transport, kept out of RealtimeModule on purpose.
*
* RealtimeModule is @Global and every domain pulls it in for the broadcast
* facade. A gateway there would follow into every e2e TestingModule, and Nest's
* SocketModule loads a websocket adapter for any app that has one: with none
* set, it reaches for @nestjs/platform-socket.io and calls process.exit(1) when
* it is absent. Eighteen harnesses died that way.
*
* So this module is imported by AppModule alone. Test apps that assemble a few
* domain modules by hand get the facade without the transport, which is what
* they had before; the ones that boot the real buildApp() get both, with
* TrekWsAdapter already registered.
*/
@Module({
imports: [EphemeralTokenModule],
providers: [RealtimeGateway],
})
export class RealtimeGatewayModule {}
@@ -0,0 +1,161 @@
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import {
ConnectedSocket,
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
} from '@nestjs/websockets';
import type { IncomingMessage } from 'node:http';
import type { WebSocketServer } from 'ws';
import { DatabaseService } from '../database/database.service';
import { EphemeralTokenService } from '../auth/ephemeral-token.service';
import { User } from '../../types';
import {
joinRoom,
leaveAllRooms,
leaveRoom,
registerSocket,
userOf,
type TrekWebSocket,
} from './ws-state';
const HEARTBEAT_INTERVAL = 30_000;
/**
* The /ws transport, as a Nest gateway.
*
* It replaces the hand-rolled `setupWebSocket(server)` that index.ts kicked off
* with a dynamic import after listen(). What moved is ownership: the handshake
* reads its collaborators from the container instead of importing the db
* singleton and the token store, and the heartbeat is a lifecycle hook rather
* than an interval nobody stops.
*
* What did NOT move is the socket registry. It stays module-scoped in
* ws-state.ts because seventeen bridges construct `new RealtimeService()`
* outside the container and must see the same rooms; see the note there.
*
* The wire protocol is unchanged, down to the frame names. TrekWsAdapter maps
* `{ type }` onto @SubscribeMessage, because the stock adapter dispatches on
* `{ event, data }` and every deployed client speaks the former.
*/
@Injectable()
@WebSocketGateway({ path: '/ws' })
export class RealtimeGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, OnModuleDestroy
{
private heartbeat: ReturnType<typeof setInterval> | null = null;
constructor(
private readonly db: DatabaseService,
private readonly tokens: EphemeralTokenService,
) {}
afterInit(server: WebSocketServer): void {
this.heartbeat = setInterval(() => {
server.clients.forEach((ws) => {
const tws = ws as TrekWebSocket;
if (tws.isAlive === false) return tws.terminate();
tws.isAlive = false;
tws.ping();
});
}, HEARTBEAT_INTERVAL);
this.heartbeat.unref?.();
}
onModuleDestroy(): void {
if (this.heartbeat) clearInterval(this.heartbeat);
this.heartbeat = null;
}
/**
* Authenticate the upgrade, then admit the socket.
*
* Every rejection closes with the code the client already handles: 4001 for
* anything about identity, 4403 for the MFA policy. The order matters and is
* unchanged — a missing token is refused before the store is touched, and the
* password-version gate runs before the MFA one.
*/
handleConnection(socket: TrekWebSocket, request: IncomingMessage): void {
const url = new URL(request.url ?? '/', 'http://localhost');
const token = url.searchParams.get('token');
if (!token) {
socket.close(4001, 'Authentication required');
return;
}
const consumed = this.tokens.consumeWithMeta(token, 'ws');
if (!consumed) {
socket.close(4001, 'Invalid or expired token');
return;
}
const row = this.db.get<User & { password_version?: number }>(
'SELECT id, username, email, role, mfa_enabled, password_version FROM users WHERE id = ?',
consumed.userId,
);
if (!row) {
socket.close(4001, 'User not found');
return;
}
// Session gate (defence-in-depth): reject a ws-token minted before a
// password change. Tokens carry the pv they were issued with; tokens minted
// without a pv (legacy) are treated as version 0, matching the JWT `pv`
// claim semantics in verifyJwtAndLoadUser.
const tokenPv = typeof consumed.pv === 'number' ? consumed.pv : 0;
const currentPv = typeof row.password_version === 'number' ? row.password_version : 0;
if (tokenPv !== currentPv) {
socket.close(4001, 'Invalid or expired token');
return;
}
// Don't leak password_version beyond the handshake.
const { password_version: _pv, ...user } = row;
const requireMfa =
this.db.get<{ value: string }>("SELECT value FROM app_settings WHERE key = 'require_mfa'")?.value === 'true';
const mfaOk = user.mfa_enabled === 1 || user.mfa_enabled === true;
if (requireMfa && !mfaOk) {
socket.close(4403, 'MFA required');
return;
}
socket.isAlive = true;
const sid = registerSocket(socket, user as User);
socket.send(JSON.stringify({ type: 'welcome', socketId: sid }));
socket.on('pong', () => { socket.isAlive = true; });
}
handleDisconnect(socket: TrekWebSocket): void {
leaveAllRooms(socket);
}
@SubscribeMessage('join')
handleJoin(
@MessageBody() message: { tripId?: number | string },
@ConnectedSocket() socket: TrekWebSocket,
): { type: string; tripId?: number; message?: string } | undefined {
const user = userOf(socket);
if (!user || !message?.tripId) return undefined;
const tripId = Number(message.tripId);
if (!this.db.canAccessTrip(tripId, user.id)) {
return { type: 'error', message: 'Access denied' };
}
joinRoom(socket, tripId);
return { type: 'joined', tripId };
}
@SubscribeMessage('leave')
handleLeave(
@MessageBody() message: { tripId?: number | string },
@ConnectedSocket() socket: TrekWebSocket,
): { type: string; tripId?: number; message?: string } | undefined {
if (!message?.tripId) return undefined;
const tripId = Number(message.tripId);
leaveRoom(socket, tripId);
return { type: 'left', tripId };
}
}
+8 -4
View File
@@ -3,10 +3,14 @@ import { RealtimeService } from './realtime.service';
/**
* Global so every migrated module can inject RealtimeService without
* re-importing (same rationale as DatabaseModule). The service is
* stateless and dependency-free — the ws server itself is still wired
* in index.ts via setupWebSocket(); this module only provides the
* injectable broadcast seam.
* re-importing (same rationale as DatabaseModule). The service stays
* stateless and dependency-free so the out-of-container bridges can build it
* with a bare `new`.
*
* The transport itself lives in RealtimeGatewayModule, which AppModule imports
* directly. It cannot live here: this module is @Global, so a gateway would
* follow into every hand-assembled TestingModule and make Nest look for a
* websocket adapter none of them set.
*/
@Global()
@Module({
+161
View File
@@ -0,0 +1,161 @@
import { WsAdapter } from '@nestjs/platform-ws';
import type { INestApplicationContext } from '@nestjs/common';
import type { Server as HttpServer } from 'node:http';
import type { MessageMappingProperties } from '@nestjs/websockets';
import { WebSocketServer } from 'ws';
import type { Observable } from 'rxjs';
import { readEnv } from '../../app-config';
import { setServer, type TrekWebSocket } from './ws-state';
// Per-connection message rate limiting. It lives in the adapter, not in the
// gateway's handlers, because the original counted EVERY inbound frame before
// parsing it: malformed JSON and unknown types count too. Counting only the
// frames that reach a @SubscribeMessage handler would leave a client free to
// flood the process with garbage.
const WS_MSG_LIMIT = 30; // max messages
const WS_MSG_WINDOW = 10_000; // per 10 seconds
const rates = new WeakMap<TrekWebSocket, { count: number; windowStart: number }>();
function withinRate(socket: TrekWebSocket): boolean {
let rate = rates.get(socket);
if (!rate) {
rate = { count: 0, windowStart: Date.now() };
rates.set(socket, rate);
}
const now = Date.now();
if (now - rate.windowStart > WS_MSG_WINDOW) {
rate.count = 1;
rate.windowStart = now;
return true;
}
rate.count++;
return rate.count <= WS_MSG_LIMIT;
}
/**
* TREK's wire protocol, on Nest's gateway plumbing.
*
* The stock WsAdapter dispatches on `{ event, data }`. TREK's client has always
* sent `{ type: 'join', tripId }` and expects `{ type: 'joined' }` back, and
* changing that would break every deployed client and every offline queue that
* has a frame parked in it. So the adapter translates: `type` names the handler,
* and the whole message is the payload.
*
* It also owns two things the stock adapter has no concept of, both of which
* predate this migration and neither of which may be dropped:
*
* - The origin check. `verifyClient` rejects a cross-origin upgrade with 403
* before a socket exists, which is cheaper and stricter than checking after.
* - The per-socket `error` listener, attached FIRST. `ws` surfaces protocol
* violations (malformed frames, reserved close codes such as 1006) as an
* `error` event on the socket; unhandled, Node rethrows it and the process
* dies. That is #1576, and it only reproduces against a hostile client, so
* nothing in CI would catch its removal.
*/
export class TrekWsAdapter extends WsAdapter {
private readonly httpServerRef: HttpServer;
/**
* Takes the http.Server directly, not the app.
*
* The base adapter accepts either, but only the server form guarantees the ws
* server lands on the socket this process actually listens on. Given the app,
* it would reach for Nest's own internal server, which buildApp never uses.
*/
constructor(httpServer: HttpServer) {
super(httpServer as unknown as INestApplicationContext);
this.httpServerRef = httpServer;
}
/**
* Builds the ws server directly rather than delegating to the base.
*
* The base's port-0 branch creates it with `noServer: true` and performs the
* upgrade itself from its own registry. That path never consults
* `verifyClient`, because ws only calls it when ws owns the upgrade — so
* delegating would silently drop the origin check and answer 101 to any
* origin. Attaching to the http server keeps ws in charge of the handshake,
* which is where the 403 has always come from.
*/
create(_port: number, options?: Record<string, unknown>): unknown {
const allowedOrigins = readEnv().http.wsOrigins;
const server = new WebSocketServer({
server: this.httpServerRef,
path: (options?.path as string) ?? '/ws',
maxPayload: 64 * 1024, // 64 KB max message size
...(allowedOrigins
? {
verifyClient: (
{ origin }: { origin: string },
cb: (ok: boolean, code?: number, msg?: string) => void,
) => {
if (!origin || allowedOrigins.includes(origin)) cb(true);
else cb(false, 403, 'Origin not allowed');
},
}
: {}),
});
// A server-level error must not reach the process. The base binds one on the
// instances it creates; this one is ours, so it needs its own.
server.on('error', () => {});
// The fan-out primitives reach the live server through here, and so do the
// out-of-container bridges.
setServer(server);
return server;
}
bindClientConnect(server: WebSocketServer, callback: (...args: unknown[]) => void): void {
server.on('connection', (socket: TrekWebSocket, request: unknown) => {
// Must stay above anything that can close early: a socket that never gets
// this listener can still crash the process while it finishes closing.
socket.on('error', () => socket.terminate());
// The request rides along: the handshake reads the ws token off its query
// string, so dropping it here would leave handleConnection with nothing
// to authenticate.
callback(socket, request);
});
}
bindMessageHandlers(
socket: TrekWebSocket,
handlers: MessageMappingProperties[],
transform: (data: unknown) => Observable<unknown>,
): void {
socket.on('message', (buffer: Buffer) => {
if (!withinRate(socket)) {
if (socket.readyState === 1) {
socket.send(JSON.stringify({ type: 'error', message: 'Rate limit exceeded' }));
}
return;
}
let message: { type?: unknown };
try {
message = JSON.parse(buffer.toString());
} catch {
return; // Malformed JSON, ignored exactly as before.
}
if (!message || typeof message !== 'object' || typeof message.type !== 'string') return;
const handler = handlers.find((h) => h.message === message.type);
if (!handler) return; // An unknown type is ignored, not an error frame.
// The whole message is the payload: TREK's frames are flat, so `tripId`
// sits beside `type` rather than under a `data` key.
transform(handler.callback(message, socket)).subscribe({
next: (response) => {
if (response !== undefined && socket.readyState === 1) {
socket.send(JSON.stringify(response));
}
},
});
});
}
async close(server: WebSocketServer): Promise<void> {
setServer(null);
await super.close(server);
}
}
+143
View File
@@ -0,0 +1,143 @@
import { WebSocketServer, WebSocket } from 'ws';
import { emitPluginEvent, pluginEventMeta } from '../../plugin-event-sink';
import { User } from '../../types';
/**
* The socket registry: rooms, per-socket identity, and the three fan-out
* primitives.
*
* MODULE state, not provider state, and that is load-bearing. Seventeen
* `*.bridge.ts` files and notifications.instance.ts build `new
* RealtimeService()` by hand, outside the container, because they run outside
* it. If the rooms lived on a provider instance, those broadcasts would go to
* an empty map: no error, no log, just a client that stops updating. Same
* reasoning as the geo throttle cursor and the permissions cache.
*
* The gateway next door owns the connection lifecycle and the handshake and
* writes into here. This file deliberately knows nothing about auth.
*/
export interface TrekWebSocket extends WebSocket {
isAlive: boolean;
}
const rooms = new Map<number, Set<TrekWebSocket>>();
const socketRooms = new WeakMap<TrekWebSocket, Set<number>>();
const socketUser = new WeakMap<TrekWebSocket, User>();
const socketId = new WeakMap<TrekWebSocket, number>();
/**
* A monotonic integer, NOT a uuid.
*
* The client echoes it back as the X-Socket-Id header and `broadcast` excludes
* the originator with `Number(excludeSid)`. `Number(<uuid>)` is NaN, and
* `NaN === NaN` is false, so every client would receive its own writes back.
* Nothing throws; it shows up as drag-and-drop that jumps back under your
* cursor.
*/
let nextSocketId = 1;
let wss: WebSocketServer | null = null;
export function setServer(server: WebSocketServer | null): void {
wss = server;
}
export function getServer(): WebSocketServer | null {
return wss;
}
export function registerSocket(ws: TrekWebSocket, user: User): number {
const sid = nextSocketId++;
socketId.set(ws, sid);
socketUser.set(ws, user);
socketRooms.set(ws, new Set());
return sid;
}
export function userOf(ws: TrekWebSocket): User | undefined {
return socketUser.get(ws);
}
export function joinRoom(ws: TrekWebSocket, tripId: number): void {
if (!rooms.has(tripId)) rooms.set(tripId, new Set());
rooms.get(tripId)!.add(ws);
socketRooms.get(ws)?.add(tripId);
}
export function leaveRoom(ws: TrekWebSocket, tripId: number): void {
const room = rooms.get(tripId);
if (room) {
room.delete(ws);
if (room.size === 0) rooms.delete(tripId);
}
socketRooms.get(ws)?.delete(tripId);
}
export function leaveAllRooms(ws: TrekWebSocket): void {
const mine = socketRooms.get(ws);
if (!mine) return;
for (const tripId of mine) leaveRoom(ws, tripId);
}
/**
* Broadcast an event to all sockets in a trip room, optionally excluding a
* socket.
*
* When `onlyUserId` is given the event is delivered only to that user's sockets
* in the room — used to keep private packing items (#858) off other members'
* screens while still syncing the owner's own tabs.
*/
export function broadcast(
tripId: number | string,
eventType: string,
payload: Record<string, unknown>,
excludeSid?: number | string,
onlyUserId?: number,
): void {
tripId = Number(tripId);
// Announce every CORE trip event (name only, never the payload) to subscribed
// plugins — before the room check so it fires even with no connected viewers
// and with no ws server at all, and skipping plugin:* re-broadcasts so a
// plugin's own events can't loop back.
if (!eventType.startsWith('plugin:')) emitPluginEvent(tripId, eventType, pluginEventMeta(eventType, payload));
const room = rooms.get(tripId);
if (!room || room.size === 0) return;
const excludeNum = excludeSid ? Number(excludeSid) : null;
for (const ws of room) {
if (ws.readyState !== 1) continue; // WebSocket.OPEN === 1
if (excludeNum && socketId.get(ws) === excludeNum) continue;
if (onlyUserId != null && socketUser.get(ws)?.id !== onlyUserId) continue;
ws.send(JSON.stringify({ type: eventType, tripId, ...payload }));
}
}
/** Send a message to all sockets belonging to a specific user (e.g. trip invitations). */
export function broadcastToUser(
userId: number,
payload: Record<string, unknown>,
excludeSid?: number | string,
): void {
if (!wss) return;
const excludeNum = excludeSid ? Number(excludeSid) : null;
for (const ws of wss.clients) {
const tws = ws as TrekWebSocket;
if (tws.readyState !== 1) continue;
if (excludeNum && socketId.get(tws) === excludeNum) continue;
if (socketUser.get(tws)?.id === userId) tws.send(JSON.stringify(payload));
}
}
export function getOnlineUserIds(): Set<number> {
const ids = new Set<number>();
if (!wss) return ids;
for (const ws of wss.clients) {
const tws = ws as TrekWebSocket;
if (tws.readyState !== 1) continue;
const user = socketUser.get(tws);
if (user) ids.add(user.id);
}
return ids;
}
+12 -245
View File
@@ -1,247 +1,14 @@
import { WebSocketServer, WebSocket } from 'ws';
import { readEnv } from './app-config';
import { db, canAccessTrip } from './db/database';
import { consumeEphemeralTokenWithMeta } from './nest/auth/ephemeral-tokens';
import { emitPluginEvent, pluginEventMeta } from './plugin-event-sink';
import { User } from './types';
import http from 'node:http';
interface NomadWebSocket extends WebSocket {
isAlive: boolean;
}
// Room management: tripId -> Set<WebSocket>
const rooms = new Map<number, Set<NomadWebSocket>>();
// Track which rooms each socket is in
const socketRooms = new WeakMap<NomadWebSocket, Set<number>>();
// Track user info per socket
const socketUser = new WeakMap<NomadWebSocket, User>();
// Track unique socket ID
const socketId = new WeakMap<NomadWebSocket, number>();
let nextSocketId = 1;
let wss: WebSocketServer | null = null;
// Per-connection message rate limiting
const WS_MSG_LIMIT = 30; // max messages
const WS_MSG_WINDOW = 10_000; // per 10 seconds
const socketMsgCounts = new WeakMap<NomadWebSocket, { count: number; windowStart: number }>();
/** Attaches a WebSocket server with JWT auth, room-based trip channels, and heartbeat keep-alive. */
function setupWebSocket(server: http.Server): void {
const allowedOrigins = readEnv().http.wsOrigins;
wss = new WebSocketServer({
server,
path: '/ws',
maxPayload: 64 * 1024, // 64 KB max message size
verifyClient: allowedOrigins
? ({ origin }, cb) => {
if (!origin || allowedOrigins.includes(origin)) cb(true);
else cb(false, 403, 'Origin not allowed');
}
: undefined,
});
const HEARTBEAT_INTERVAL = 30000; // 30 seconds
const heartbeat = setInterval(() => {
wss.clients.forEach((ws) => {
const nws = ws as NomadWebSocket;
if (nws.isAlive === false) return nws.terminate();
nws.isAlive = false;
nws.ping();
});
}, HEARTBEAT_INTERVAL);
wss.on('close', () => clearInterval(heartbeat));
wss.on('connection', (ws: WebSocket, req: http.IncomingMessage) => {
const nws = ws as NomadWebSocket;
// ws emits protocol violations (malformed frames, reserved close codes such as 1006)
// as an 'error' event on the socket; unhandled, Node rethrows it and the process dies.
// Must stay above the auth guards below: they close and return early, and a socket that
// never gets this listener can still crash the server before it finishes closing.
nws.on('error', () => nws.terminate());
// Extract token from query param
const url = new URL(req.url, 'http://localhost');
const token = url.searchParams.get('token');
if (!token) {
nws.close(4001, 'Authentication required');
return;
}
const consumed = consumeEphemeralTokenWithMeta(token, 'ws');
if (!consumed) {
nws.close(4001, 'Invalid or expired token');
return;
}
const { userId } = consumed;
let row: (User & { password_version?: number }) | undefined;
row = db.prepare(
'SELECT id, username, email, role, mfa_enabled, password_version FROM users WHERE id = ?'
).get(userId) as (User & { password_version?: number }) | undefined;
if (!row) {
nws.close(4001, 'User not found');
return;
}
// Session gate (defence-in-depth): reject a ws-token minted before a
// password change. Tokens carry the pv they were issued with; tokens
// minted without a pv (legacy) are treated as version 0, matching the
// JWT `pv` claim semantics in verifyJwtAndLoadUser.
const tokenPv = typeof consumed.pv === 'number' ? consumed.pv : 0;
const currentPv = typeof row.password_version === 'number' ? row.password_version : 0;
if (tokenPv !== currentPv) {
nws.close(4001, 'Invalid or expired token');
return;
}
// Don't leak password_version beyond the handshake.
const { password_version: _pv, ...user } = row;
const requireMfa = (db.prepare("SELECT value FROM app_settings WHERE key = 'require_mfa'").get() as { value: string } | undefined)?.value === 'true';
const mfaOk = user.mfa_enabled === 1 || user.mfa_enabled === true;
if (requireMfa && !mfaOk) {
nws.close(4403, 'MFA required');
return;
}
nws.isAlive = true;
const sid = nextSocketId++;
socketId.set(nws, sid);
socketUser.set(nws, user);
socketRooms.set(nws, new Set());
nws.send(JSON.stringify({ type: 'welcome', socketId: sid }));
nws.on('pong', () => { nws.isAlive = true; });
socketMsgCounts.set(nws, { count: 0, windowStart: Date.now() });
nws.on('message', (data) => {
// Rate limiting
const rate = socketMsgCounts.get(nws);
const now = Date.now();
if (now - rate.windowStart > WS_MSG_WINDOW) {
rate.count = 1;
rate.windowStart = now;
} else {
rate.count++;
if (rate.count > WS_MSG_LIMIT) {
nws.send(JSON.stringify({ type: 'error', message: 'Rate limit exceeded' }));
return;
}
}
let msg: { type: string; tripId?: number | string };
try {
msg = JSON.parse(data.toString());
} catch {
return; // Malformed JSON, ignore
}
// Basic validation
if (!msg || typeof msg !== 'object' || typeof msg.type !== 'string') return;
if (msg.type === 'join' && msg.tripId) {
const tripId = Number(msg.tripId);
// Verify the user has access to this trip
if (!canAccessTrip(tripId, user.id)) {
nws.send(JSON.stringify({ type: 'error', message: 'Access denied' }));
return;
}
// Add to room
if (!rooms.has(tripId)) rooms.set(tripId, new Set());
rooms.get(tripId).add(nws);
socketRooms.get(nws).add(tripId);
nws.send(JSON.stringify({ type: 'joined', tripId }));
}
if (msg.type === 'leave' && msg.tripId) {
const tripId = Number(msg.tripId);
leaveRoom(nws, tripId);
nws.send(JSON.stringify({ type: 'left', tripId }));
}
});
nws.on('close', () => {
// Clean up all rooms this socket was in
const myRooms = socketRooms.get(nws);
if (myRooms) {
for (const tripId of myRooms) {
leaveRoom(nws, tripId);
}
}
});
});
console.log('WebSocket server attached at /ws');
}
function leaveRoom(ws: NomadWebSocket, tripId: number): void {
const room = rooms.get(tripId);
if (room) {
room.delete(ws);
if (room.size === 0) rooms.delete(tripId);
}
const myRooms = socketRooms.get(ws);
if (myRooms) myRooms.delete(tripId);
}
/**
* Broadcast an event to all sockets in a trip room, optionally excluding a socket.
* When `onlyUserId` is given the event is delivered only to that user's sockets in
* the room — used to keep private packing items (#858) off other members' screens
* while still syncing the owner's own tabs.
* The transport's public surface, for code outside the Nest container.
*
* The implementation moved to nest/realtime: the connection lifecycle and the
* handshake are RealtimeGateway, the wire protocol is TrekWsAdapter, and the
* socket registry is ws-state.ts. This file stays because it is the import path
* the rest of the tree knows, including 115 test files that `vi.mock` it to
* assert on broadcasts. Repointing them all would be a large diff that proves
* nothing, and this re-export keeps every one of those mocks working.
*
* `setupWebSocket` is gone: the server is created by the adapter now, when Nest
* initialises the gateway, rather than by index.ts after listen().
*/
function broadcast(tripId: number | string, eventType: string, payload: Record<string, unknown>, excludeSid?: number | string, onlyUserId?: number): void {
tripId = Number(tripId);
// Announce every CORE trip event (name only, never the payload) to subscribed
// plugins — before the room check so it fires even with no connected viewers, and
// skipping plugin:* re-broadcasts so a plugin's own events can't loop back.
if (!eventType.startsWith('plugin:')) emitPluginEvent(tripId, eventType, pluginEventMeta(eventType, payload));
const room = rooms.get(tripId);
if (!room || room.size === 0) return;
const excludeNum = excludeSid ? Number(excludeSid) : null;
for (const ws of room) {
if (ws.readyState !== 1) continue; // WebSocket.OPEN === 1
// Exclude the specific socket that triggered the change
if (excludeNum && socketId.get(ws) === excludeNum) continue;
if (onlyUserId != null && socketUser.get(ws)?.id !== onlyUserId) continue;
ws.send(JSON.stringify({ type: eventType, tripId, ...payload }));
}
}
/** Send a message to all sockets belonging to a specific user (e.g., for trip invitations). */
function broadcastToUser(userId: number, payload: Record<string, unknown>, excludeSid?: number | string): void {
if (!wss) return;
const excludeNum = excludeSid ? Number(excludeSid) : null;
for (const ws of wss.clients) {
const nws = ws as NomadWebSocket;
if (nws.readyState !== 1) continue;
if (excludeNum && socketId.get(nws) === excludeNum) continue;
const user = socketUser.get(nws);
if (user?.id === userId) {
nws.send(JSON.stringify(payload));
}
}
}
function getOnlineUserIds(): Set<number> {
const ids = new Set<number>();
if (!wss) return ids;
for (const ws of wss.clients) {
const nws = ws as NomadWebSocket;
if (nws.readyState !== 1) continue;
const user = socketUser.get(nws);
if (user) ids.add(user.id);
}
return ids;
}
export { setupWebSocket, broadcast, broadcastToUser, getOnlineUserIds };
export { broadcast, broadcastToUser, getOnlineUserIds } from './nest/realtime/ws-state';