Make a bind failure fatal again, and undo four contract tightenings

An adversarial audit over the three merged migration PRs. Sixteen findings
survived triple refutation; these are the ones with a defect behind them.

The serious one is mine, from the gateway move. ws registers
`error: this.emit.bind(this, 'error')` on the http server it attaches to, and
the adapter now runs inside buildApp, before listen(). So from that moment the
http server HAS an error listener and Node stops throwing on one. The adapter's
handler was `() => {}`, which meant EADDRINUSE was forwarded there and dropped:
the process survived, the listen callback never ran, so the banner never
printed and the scheduler never started. A container that looks healthy and
serves nothing. Reproduced, then fixed at both ends: index.ts owns the fatal
decision on the http server, and the adapter logs instead of swallowing.

Four DTOs were tightening contracts rather than describing them, which is the
thing the pipe-before-handler ordering makes easy to do by accident:

- OauthClientCreateDto replaced 'Name is required' with a Zod field report, and
  pre-empted the handler's 403 on an instance with the MCP addon off.
- BookingImportConfirmDto demanded per-item fields the service never reads, so
  a caller assembling items itself got a 400 about `source`.
- TagCreateDto/TagUpdateDto typed name and color as strings; the legacy route
  bound whatever arrived, so a numeric name used to create a tag.
- PluginUserSettingsUpdateDto typed config as a record, where the handler
  answers 200 for anything it cannot use.

Two of my own tests were false-green, which the audit demonstrated by mutation
rather than by reading. WSGW-008 asserted on frames that never carry the user
object at all, so it passed with the password_version strip deleted; it reads
the retained identity now. GEO-009 made the same assertion before and after the
write; it distinguishes a cached negative from a miss now, through a hasCached
seam that says what getCached cannot. Both were re-run against the mutation
they exist to catch, and both fail on it.

booking-import geocodes every venue and endpoint in one request loop, up to
thirty sequential calls. Folding maps onto the shared Nominatim cursor put that
loop on the interactive lane, so an import starved every concurrent keystroke
for half a minute. It takes the background lane now, which is what the two-lane
split was built for.

Also: the README's in-container bridge list had outlived one entry and was
missing another, and the journey e2e still mocked a module this branch deleted.
This commit is contained in:
Maurice
2026-08-10 22:58:45 +02:00
committed by Maurice
parent b82651bf93
commit 3a7c8e1d26
18 changed files with 249 additions and 66 deletions
+10 -4
View File
@@ -15,6 +15,7 @@ import {
cacheKeyFor,
clearGeoCache,
getCached,
hasCached,
nominatimFetch,
setCached,
setGeoThrottleInterval,
@@ -153,12 +154,17 @@ describe('geo cache', () => {
expect(cacheKeyFor(52.53, 13.405, 'zoom18')).not.toBe(cacheKeyFor(52.52, 13.405, 'zoom18'));
});
it('GEO-009: distinguishes a cached undefined from a miss', () => {
it('GEO-009: a cached negative answer is a HIT, not a miss', () => {
// The earlier version asserted `undefined` before and after the write, which
// is the same assertion twice and held with the `has` guard removed. What
// the guard buys is that the second lookup does not go back to the network:
// the ocean does not become a country, so a null answer is worth keeping.
const key = cacheKeyFor(0, 0, 'zoom3');
expect(getCached(key)).toBeUndefined();
expect(hasCached(key)).toBe(false);
setCached(key, undefined);
// A negative answer is worth caching: the ocean does not become a country.
// `has` rather than a truthy check is what keeps it from being re-fetched.
expect(hasCached(key)).toBe(true);
expect(getCached(key)).toBeUndefined();
});
@@ -20,6 +20,7 @@ import {
joinRoom,
registerSocket,
setServer,
userOf,
type TrekWebSocket,
} from '../../../src/nest/realtime/ws-state';
import { emitPluginEvent } from '../../../src/plugin-event-sink';
@@ -134,10 +135,16 @@ describe('RealtimeGateway handshake', () => {
});
it('WSGW-008: never leaks password_version past the handshake', () => {
const { gw, ws } = connect('/ws?token=x');
gw.handleJoin({ tripId: 7 }, ws);
broadcast(7, 'trip:updated' as never, {} as never);
expect(ws.sent.join('')).not.toContain('password_version');
// Asserted on what the socket registry HOLDS, not on the frames it sends.
// The frames never carry the user object at all, so the earlier version of
// this case passed with the strip deleted: it proved nothing. What matters
// is that the retained identity is clean, because that object is what
// getOnlineUserIds and the onlyUserId filter read.
const { ws } = connect('/ws?token=x');
const held = userOf(ws) as unknown as Record<string, unknown> | undefined;
expect(held).toBeDefined();
expect(held).not.toHaveProperty('password_version');
expect(held).toMatchObject({ id: 3, email: 'm@x.test' });
});
});
+16 -3
View File
@@ -12,7 +12,11 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
// The origin allowlist is env-driven and empty in the test environment, so the
// branch that builds verifyClient would never run. Driving readEnv lets both
// sides of it be asserted rather than assumed.
const { wsOrigins } = vi.hoisted(() => ({ wsOrigins: { value: null as string[] | null } }));
const { wsOrigins, logError } = vi.hoisted(() => ({
wsOrigins: { value: null as string[] | null },
logError: vi.fn(),
}));
vi.mock('../../../src/nest/audit/audit-log.logger', () => ({ logError, logInfo: vi.fn(), logDebug: vi.fn(), logWarn: vi.fn() }));
vi.mock('../../../src/app-config', async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>;
return {
@@ -243,12 +247,21 @@ describe('TrekWsAdapter server creation', () => {
}
});
it('WSAD-031c: a server-level error is absorbed, not rethrown at the process', () => {
it('WSAD-031c: a server-level error is LOGGED, never swallowed', () => {
// ws forwards the http server's errors here, so an empty handler eats the
// bind failure too and the process survives EADDRINUSE serving nothing.
// index.ts owns the fatal decision; this handler owes a log line.
wsOrigins.value = null;
const ad = new TrekWsAdapter(httpServer);
const server = ad.create(0, { path: '/ws' }) as { emit: (e: string, err: Error) => boolean };
const server = ad.create(0, { path: '/ws' }) as { emit: (e: string, err: unknown) => boolean };
try {
expect(() => server.emit('error', new Error('upstream reset'))).not.toThrow();
expect(logError).toHaveBeenCalledWith(expect.stringContaining('upstream reset'));
// A non-Error rejection reads as itself rather than as [object Object].
logError.mockClear();
server.emit('error', 'ECONNRESET');
expect(logError).toHaveBeenCalledWith(expect.stringContaining('ECONNRESET'));
} finally {
void ad.close(server as never);
}
@@ -0,0 +1,79 @@
import { describe, it, expect, vi } from 'vitest';
import http from 'node:http';
import { WebSocketServer } from 'ws';
/**
* A bind failure must stay loud after the ws server moved in front of listen().
*
* `ws` registers `error: this.emit.bind(this, 'error')` on the http server it
* attaches to (websocket-server.js). Since buildApp now creates the ws server
* BEFORE index.ts calls listen(), the http server already has an error listener
* by the time the bind is attempted — which is exactly the condition under which
* Node stops throwing on an unhandled one.
*
* The failure that hides behind that is not subtle: EADDRINUSE gets forwarded to
* the WebSocketServer, and if its handler is a no-op the process survives, the
* listen callback never runs, so the startup banner never prints and the
* scheduler never starts. A container that looks healthy and serves nothing.
*
* These cases pin the mechanism rather than index.ts itself, because the module
* calls process.exit and boots the whole app on import.
*/
async function occupyAPort(): Promise<{ port: number; release: () => void }> {
const blocker = http.createServer();
await new Promise<void>((resolve) => blocker.listen(0, '127.0.0.1', resolve));
const { port } = blocker.address() as { port: number };
return { port, release: () => blocker.close() };
}
describe('ws attached before listen()', () => {
it('WSBIND-001: ws silences the http server, so a no-op handler loses EADDRINUSE entirely', async () => {
const { port, release } = await occupyAPort();
try {
const server = http.createServer();
const wss = new WebSocketServer({ server, path: '/ws' });
const swallowed = vi.fn();
wss.on('error', swallowed); // the shape this used to have
let listenCallbackRan = false;
server.listen(port, '127.0.0.1', () => { listenCallbackRan = true; });
await new Promise((r) => setTimeout(r, 300));
// The bind failed, and the only thing that heard about it was the ws
// handler. Nothing threw, nothing exited.
expect(swallowed).toHaveBeenCalledTimes(1);
expect((swallowed.mock.calls[0][0] as NodeJS.ErrnoException).code).toBe('EADDRINUSE');
expect(listenCallbackRan).toBe(false);
wss.close();
server.close();
} finally {
release();
}
});
it('WSBIND-002: an explicit http-server error handler is what makes it fatal again', async () => {
const { port, release } = await occupyAPort();
try {
const server = http.createServer();
const wss = new WebSocketServer({ server, path: '/ws' });
wss.on('error', () => {});
// What index.ts registers. It has to sit on the HTTP server: by the time
// ws re-emits, the process has already declined to throw.
const fatal = vi.fn();
server.on('error', fatal);
server.listen(port, '127.0.0.1', () => {});
await new Promise((r) => setTimeout(r, 300));
expect(fatal).toHaveBeenCalledTimes(1);
expect((fatal.mock.calls[0][0] as NodeJS.ErrnoException).code).toBe('EADDRINUSE');
wss.close();
server.close();
} finally {
release();
}
});
});