The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
test(api): hold every request row id to toRowId on paths, bodies, queries, MCP URIs and plugin RPC
Mauricecc0fe6ce2b1f
diff --git a/server/tests/e2e/body-row-ids.e2e.test.ts b/server/tests/e2e/body-row-ids.e2e.test.tsnew file mode 100644index 000000000..3925817e9--- /dev/null+++ b/server/tests/e2e/body-row-ids.e2e.test.ts@@ -0,0 +1,250 @@+/**+ * BODYID, JRNID and QUERYID: a row id or a count in a request body or query+ * that toRowId refuses is a 400, or the value left out, on every engine, and+ * reaches no row.+ *+ * ROUTEID-002 (route-row-ids.e2e.test.ts) holds every typed request field to+ * that rule at the schema. These suites cover what the schema cannot see: the+ * open accommodation update body, whose refs the service reads itself, the+ * journey fields that answer the handler's own error, and the collab reply,+ * whose multipart form spells "no reply" as ''. Bound as sent, '0x1' matched+ * nothing on SQLite, was refused with a 500 by Postgres 14 and 15 and read as+ * row 1 by 16 and later; '1e0' reached row 1 on SQLite. Each suite sends the+ * spellings of a real row's id, so a reach shows as a write.+ */+import { buildApp } from '../../src/bootstrap';+import { Addons } from '../../src/db/entities/Addons.entity';+import { CollabMessages } from '../../src/db/entities/CollabMessages.entity';+import { DayAccommodations } from '../../src/db/entities/DayAccommodations.entity';+import { JourneyContributors } from '../../src/db/entities/JourneyContributors.entity';+import { JourneyTrips } from '../../src/db/entities/JourneyTrips.entity';+import { RateLimitService } from '../../src/nest/common/rate-limit.service';+import { makeCollabMessage } from '../helpers/factories/collab';+import { makeJourney, makeJourneyEntry } from '../helpers/factories/journeys';+import { makeNotification } from '../helpers/factories/notifications';+import { makePlace } from '../helpers/factories/places';+import { countRows, updateRows } from '../helpers/factories/rows';+import { makeTrip, readTripDays } from '../helpers/factories/trips';+import { makeUser } from '../helpers/factories/users';+import { oneByOne } from '../helpers/one-by-one';+import { sessionCookie } from './harness';+import { MikroORM } from '@mikro-orm/core';+import type { INestApplication } from '@nestjs/common';++import request from 'supertest';+import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';++let app: INestApplication;+let orm: MikroORM;++/** The spellings of `id` toRowId refuses, and two values no id is, as a JSON body carries them. */+function malformed(id: number): unknown[] {+ return [`0x${id.toString(16)}`, `${id}e0`, `${id}.0`, ` ${id}`, 'abc', '99999999999999999999', id + 0.5, 1e20];+}++/** Every malformed value of each field, as `[field, value]` pairs. */+function malformedFields<F extends string>(ids: Record<F, number>): Array<[F, unknown]> {+ return (Object.keys(ids) as F[]).flatMap((field) =>+ malformed(ids[field]).map((value): [F, unknown] => [field, value]),+ );+}++interface Answer {+ sent: string;+ status: number;+ body: unknown;+}++/** The status and body of each request `send` makes, one at a time, labelled with what was sent. */+function answers<T>(items: readonly T[], send: (item: T) => PromiseLike<request.Response>): Promise<Answer[]> {+ return oneByOne(items, async (item) => {+ const res = await send(item);+ return { sent: JSON.stringify(item), status: res.status, body: res.body as unknown };+ });+}++/** The answers that are not the 400 carrying `error`. */+function not400(got: Answer[], error: string): Answer[] {+ return got.filter((a) => a.status !== 400 || JSON.stringify(a.body) !== JSON.stringify({ error }));+}++beforeAll(async () => {+ app = await buildApp();+ orm = app.get(MikroORM);+ await updateRows(orm, Addons, {}, { enabled: 1 });+}, 60_000);++afterAll(async () => {+ await app?.close();+});++beforeEach(async () => {+ await app.get(RateLimitService).reset();+});++async function tripWorld() {+ const { user } = await makeUser(orm);+ const trip = await makeTrip(orm, user.id, { start_date: '2026-03-01', end_date: '2026-03-03' });+ const [day1, day2] = await readTripDays(orm, trip.id);+ const place = await makePlace(orm, trip.id);+ const other = await makePlace(orm, trip.id);+ return { cookie: sessionCookie(user.id), user, trip, day1, day2, place, other };+}++describe('BODYID: accommodation refs', () => {+ it('BODYID-001: POST answers 400 for a malformed place or day id and books nothing', async () => {+ const { cookie, trip, day1, place } = await tripWorld();+ const valid = { place_id: place.id, start_day_id: day1.id, end_day_id: day1.id };+ const got = await answers(malformedFields(valid), ([field, value]) =>+ request(app.getHttpServer())+ .post(`/api/trips/${trip.id}/accommodations`)+ .set('Cookie', cookie)+ .send({ ...valid, [field]: value }),+ );+ expect(got.filter((a) => a.status !== 400)).toEqual([]);+ expect(await countRows(orm, DayAccommodations, { trip: trip.id })).toBe(0);++ const ok = await request(app.getHttpServer())+ .post(`/api/trips/${trip.id}/accommodations`)+ .set('Cookie', cookie)+ .send({ ...valid, place_id: String(place.id) });+ expect(ok.status).toBe(201);+ });++ it('BODYID-002: PUT, whose body is open, answers 400 for a malformed ref and leaves the stay as it was', async () => {+ const { cookie, trip, day1, day2, place, other } = await tripWorld();+ const created = await request(app.getHttpServer())+ .post(`/api/trips/${trip.id}/accommodations`)+ .set('Cookie', cookie)+ .send({ place_id: place.id, start_day_id: day1.id, end_day_id: day1.id });+ const stayId = (created.body as { accommodation: { id: number } }).accommodation.id;++ const pairs = malformedFields({ place_id: other.id, start_day_id: day2.id, end_day_id: day2.id });+ const got = await answers(pairs, ([field, value]) =>+ request(app.getHttpServer())+ .put(`/api/trips/${trip.id}/accommodations/${stayId}`)+ .set('Cookie', cookie)+ .send({ [field]: value }),+ );+ expect(got.map((a) => [a.sent, a.status, a.body])).toEqual(+ pairs.map((pair) => [JSON.stringify(pair), 400, { error: `${pair[0]}: Invalid id` }]),+ );+ expect(+ await countRows(orm, DayAccommodations, { id: stayId, place: place.id, startDay: day1.id, endDay: day1.id }),+ ).toBe(1);++ const ok = await request(app.getHttpServer())+ .put(`/api/trips/${trip.id}/accommodations/${stayId}`)+ .set('Cookie', cookie)+ .send({ place_id: String(other.id) });+ expect(ok.status).toBe(200);+ expect(await countRows(orm, DayAccommodations, { id: stayId, place: other.id })).toBe(1);+ });+});++describe('BODYID: collab reply', () => {+ it('BODYID-003: a malformed reply_to is a 400 that posts nothing; "" and null are no reply', async () => {+ const { cookie, user, trip } = await tripWorld();+ const target = await makeCollabMessage(orm, trip.id, user.id);+ const post = (reply_to: unknown) =>+ request(app.getHttpServer())+ .post(`/api/trips/${trip.id}/collab/messages`)+ .set('Cookie', cookie)+ .send({ text: 'hi', reply_to });++ expect((await answers(malformed(target.id), post)).filter((a) => a.status !== 400)).toEqual([]);+ expect(await countRows(orm, CollabMessages, { trip: trip.id })).toBe(1);++ const none = await answers(['', null], post);+ expect(none.map((a) => [a.status, (a.body as { message?: { reply_to: unknown } }).message?.reply_to])).toEqual([+ [201, null],+ [201, null],+ ]);+ const reply = await post(String(target.id));+ expect(reply.status).toBe(201);+ expect(await countRows(orm, CollabMessages, { trip: trip.id, reply_to: target.id })).toBe(1);+ });+});++describe('JRNID: journey ids the handler reads itself', () => {+ async function journeyWorld() {+ const world = await tripWorld();+ const journey = await makeJourney(orm, world.user.id);+ return { ...world, journey };+ }++ it('JRNID-001: POST /journeys links no trip for a malformed trip_ids entry', async () => {+ const { cookie, trip } = await journeyWorld();+ const res = await request(app.getHttpServer())+ .post('/api/journeys')+ .set('Cookie', cookie)+ .send({ title: 'J', trip_ids: malformed(trip.id) });+ expect(res.status).toBe(201);+ expect(await countRows(orm, JourneyTrips, { journey: (res.body as { id: number }).id })).toBe(0);+ });++ it("JRNID-002: POST /journeys/:id/trips answers 400 'trip_id required' for a malformed trip_id", async () => {+ const { cookie, trip, journey } = await journeyWorld();+ const got = await answers(malformed(trip.id), (value) =>+ request(app.getHttpServer())+ .post(`/api/journeys/${journey.id}/trips`)+ .set('Cookie', cookie)+ .send({ trip_id: value }),+ );+ expect(not400(got, 'trip_id required')).toEqual([]);+ expect(await countRows(orm, JourneyTrips, { journey: journey.id })).toBe(0);+ });++ it('JRNID-003: PUT /journeys/:id/entries/reorder answers 400 for a malformed entry id', async () => {+ const { cookie, user, journey } = await journeyWorld();+ const entry = await makeJourneyEntry(orm, journey.id, user.id);+ const got = await answers(malformed(entry.id), (value) =>+ request(app.getHttpServer())+ .put(`/api/journeys/${journey.id}/entries/reorder`)+ .set('Cookie', cookie)+ .send({ orderedIds: [value] }),+ );+ expect(not400(got, 'orderedIds must be an array of numbers')).toEqual([]);+ });++ it("JRNID-004: POST /journeys/:id/contributors answers 400 'user_id required' for a malformed user_id", async () => {+ const { cookie, journey } = await journeyWorld();+ const { user: friend } = await makeUser(orm);+ const got = await answers(malformed(friend.id), (value) =>+ request(app.getHttpServer())+ .post(`/api/journeys/${journey.id}/contributors`)+ .set('Cookie', cookie)+ .send({ user_id: value }),+ );+ expect(not400(got, 'user_id required')).toEqual([]);+ expect(await countRows(orm, JourneyContributors, { journey: journey.id, user: friend.id })).toBe(0);+ });++ it("JRNID-005: POST /journeys/entries/:entryId/link-photo answers 400 'journey_photo_id required' for a malformed id", async () => {+ const { cookie, user, journey } = await journeyWorld();+ const entry = await makeJourneyEntry(orm, journey.id, user.id);+ const got = await answers(malformedFields({ journey_photo_id: 1, photo_id: 1 }), ([field, value]) =>+ request(app.getHttpServer())+ .post(`/api/journeys/entries/${entry.id}/link-photo`)+ .set('Cookie', cookie)+ .send({ [field]: value }),+ );+ expect(not400(got, 'journey_photo_id required')).toEqual([]);+ });+});++describe('QUERYID: whole numbers in a query', () => {+ it('QUERYID-001: a malformed in-app limit or offset reads as left out, never as a number', async () => {+ const { user } = await makeUser(orm);+ const cookie = sessionCookie(user.id);+ await oneByOne([1, 2, 3], () => makeNotification(orm, user.id));+ // parseInt read '1e0' as 1 and '-1' as -1, which Postgres refuses in LIMIT and OFFSET.+ const queries = ['limit=-1', 'limit=1e0', 'limit=0x1', 'offset=-1', 'offset=1e0', 'offset=0x1'];+ const got = await answers(queries, (query) =>+ request(app.getHttpServer()).get(`/api/notifications/in-app?${query}`).set('Cookie', cookie),+ );+ expect(got.map((a) => [a.sent, a.status, (a.body as { notifications?: unknown[] }).notifications?.length])).toEqual(+ queries.map((query) => [JSON.stringify(query), 200, 3]),+ );+ });+});diff --git a/server/tests/e2e/mcp-resource-row-ids.e2e.test.ts b/server/tests/e2e/mcp-resource-row-ids.e2e.test.tsnew file mode 100644index 000000000..71769bd08--- /dev/null+++ b/server/tests/e2e/mcp-resource-row-ids.e2e.test.ts@@ -0,0 +1,99 @@+/**+ * MCPROWID: every MCP resource whose URI holds a row id answers a spelling of+ * that id toRowId refuses exactly as it answers 'abc', and hands neither to SQL.+ *+ * The resources read their URI variables themselves (`uriRowId` in+ * src/nest/common/row-id.ts). A local Number() read '0x1' and '1e0' as row 1+ * and passed 1e20 to Postgres, which refuses it as out of range for bigint.+ * The suite lists the templates the server registers, so a new resource is+ * walked without anyone adding it here. It boots the app on the real database+ * module, so it runs in the SQLite suite and in the Postgres lane.+ */+import { makeJourney } from '../helpers/factories/journeys';+import { makeTrip, readTripDays } from '../helpers/factories/trips';+import { makeUser } from '../helpers/factories/users';+import { createMcpHarness, type McpHarness } from '../helpers/mcp-harness';+import { oneByOne } from '../helpers/one-by-one';+import { malformedIdSpellings, ROW_ID_NAME } from '../helpers/row-id-fields';+import { carriedTo, installSqlCapture, recordSql, uninstallSqlCapture } from '../helpers/sql-capture';+import { bootTestApp } from '../helpers/test-app';+import { MikroORM } from '@mikro-orm/core';++import { afterAll, beforeAll, describe, expect, it } from 'vitest';++/** An id no row has: a resource that reads '0x3ade68b1' with Number() binds it. */+const UNUSED_ID = 987654321;++let orm: MikroORM;+let harness: McpHarness;+const ids: Record<string, number> = {};++beforeAll(async () => {+ installSqlCapture();+ orm = (await bootTestApp()).get(MikroORM);+ const { user } = await makeUser(orm);+ const trip = await makeTrip(orm, user.id, { start_date: '2026-03-01', end_date: '2026-03-02' });+ const [day] = await readTripDays(orm, trip.id);+ const journey = await makeJourney(orm, user.id);+ Object.assign(ids, { tripId: trip.id, dayId: day.id, journeyId: journey.id });+ harness = await createMcpHarness({ userId: user.id });+}, 60_000);++afterAll(async () => {+ uninstallSqlCapture();+ await harness?.cleanup();+});++describe('MCPROWID: MCP resource URIs read their ids through toRowId', () => {+ it('MCPROWID-001: a malformed id in any resource URI answers like "abc" and never reaches SQL', async () => {+ const { resourceTemplates } = await harness.client.listResourceTemplates();+ const cases = resourceTemplates.flatMap(({ uriTemplate }) =>+ [...uriTemplate.matchAll(/\{(\w+)\}/g)]+ .map((m) => m[1])+ .filter((name) => ROW_ID_NAME.test(name))+ .map((variable) => ({ uriTemplate, variable })),+ );++ // One read at a time: they share one harness and one seeded world.+ const failures = (+ await oneByOne(cases, async ({ uriTemplate, variable }) => {+ const label = `${uriTemplate} {${variable}}`;+ const uriWith = (value: string) =>+ uriTemplate.replace(/\{(\w+)\}/g, (_m, name: string) =>+ name === variable ? encodeURIComponent(value) : String(ids[name] ?? 2026),+ );+ // The answer quotes the URI it was asked for; compare it as if 'abc' had been asked.+ const read = async (value: string): Promise<string> => {+ let answer: string;+ try {+ answer = JSON.stringify(await harness.client.readResource({ uri: uriWith(value) }));+ } catch (err) {+ answer = `error: ${(err as Error).message}`;+ }+ return answer.split(uriWith(value)).join(uriWith('abc'));+ };+ const found = ids[variable] === undefined ? [`${label}: seed a row for it above`] : [];+ const unknown = await read('abc');+ const results = await oneByOne(['abc', ...malformedIdSpellings(UNUSED_ID)], async (spelling) => {+ let answer = '';+ const sent = await recordSql(async () => {+ answer = await read(spelling);+ });+ return { spelling, answer, sql: carriedTo(sent, spelling, UNUSED_ID) };+ });+ for (const { spelling, answer, sql } of results) {+ if (sql) found.push(`${label}='${spelling}' reached SQL: ${sql.slice(0, 160)}`);+ if (answer !== unknown) found.push(`${label}='${spelling}' answered ${answer.slice(0, 160)}`);+ }+ return found;+ })+ ).flat();++ expect(cases.length, 'no resource template holds an id; the listing broke').toBeGreaterThan(15);+ expect(+ failures,+ 'An MCP resource reads a URI id some other way than toRowId. Read it with uriRowId (src/nest/common/row-id.ts) ' ++ 'and answer a null the way the resource answers an id that names no row.',+ ).toEqual([]);+ }, 120_000);+});diff --git a/server/tests/e2e/route-row-ids.e2e.test.ts b/server/tests/e2e/route-row-ids.e2e.test.tsnew file mode 100644index 000000000..b30fa0bad--- /dev/null+++ b/server/tests/e2e/route-row-ids.e2e.test.ts@@ -0,0 +1,395 @@+/**+ * ROUTEID: no row id a request carries reaches SQL in a spelling toRowId+ * refuses, on any route, on any engine.+ *+ * An id bound to SQL as the string it arrived as is three answers: SQLite+ * compares it by affinity and matches nothing (or, for '5e0', row 5),+ * Postgres 14 and 15 refuse the cast with an error (a 500), and Postgres 16+ * and later read hex and underscores as a number and reach a real row.+ * `Number()`, `parseInt()` and Nest's `ParseIntPipe` read '0x5', '5e0', '5.0'+ * or '+5' as row 5 on every engine, and hand Postgres a NaN or a number past+ * the bigint range. The one rule is `toRowId` (src/db/row-id.ts): only the+ * canonical decimal spelling is an id, and everything else names no row.+ *+ * ROUTEID-001 walks every route the app registers. For each path param named+ * like an id it sends 'abc' and the non-canonical spellings of an id that+ * exists (seeded below), with every other param holding a real id and a body+ * the route's DTO accepts, and requires no 5xx and the same status and body as+ * 'abc' for each. It then sends the spellings of an id no row has and records+ * what the database drivers are handed (tests/helpers/sql-capture.ts): none+ * may carry the spelling, or the number Number() makes of it, so a route over+ * a table this suite seeds nothing in is held as well.+ *+ * ROUTEID-002 walks every request schema (the @trek/shared request, body and+ * query schemas and every route's body and query DTO) for fields named like a+ * row id and requires each to refuse those spellings, so the Zod pipe answers+ * 400 before a handler sees one. MCPROWID (mcp-resource-row-ids.e2e.test.ts)+ * does the same for MCP resource URIs, RPCROWID+ * (tests/unit/nest-rpc/rpc-row-id-params.test.ts) for plugin RPC params.+ *+ * It boots the app on the real database module, so it runs in the SQLite+ * suite and in the Postgres lane on 14 and 18.+ */+import { buildApp } from '../../src/bootstrap';+import { Addons } from '../../src/db/entities/Addons.entity';+import { RateLimitService } from '../../src/nest/common/rate-limit.service';+import { makeBucketListItem } from '../helpers/factories/atlas';+import { makeBudgetItem } from '../helpers/factories/budget';+import { makeCollabNote } from '../helpers/factories/collab';+import { makeCollection, makeCollectionPlace } from '../helpers/factories/collections';+import { makeDayAssignment, makeDayNote } from '../helpers/factories/itinerary';+import { makeJourney, makeJourneyEntry } from '../helpers/factories/journeys';+import { makeNotification } from '../helpers/factories/notifications';+import { makePackingBag, makePackingItem } from '../helpers/factories/packing';+import { makePlace } from '../helpers/factories/places';+import { makeReservation } from '../helpers/factories/reservations';+import { updateRows } from '../helpers/factories/rows';+import { makeTodoItem } from '../helpers/factories/todos';+import { makeTrip, readTripDays } from '../helpers/factories/trips';+import { makeAdmin, makeUser } from '../helpers/factories/users';+import { oneByOne } from '../helpers/one-by-one';+import { routeBodySchemas, routeRequestDtos, sampleBody } from '../helpers/route-bodies';+import { malformedAccepted, malformedIdSpellings, rowIdFields } from '../helpers/row-id-fields';+import { carriedTo, installSqlCapture, recordSql, uninstallSqlCapture } from '../helpers/sql-capture';+import { sessionCookie } from './harness';+import { MikroORM } from '@mikro-orm/core';+import type { INestApplication } from '@nestjs/common';+import * as contracts from '@trek/shared';++import http from 'node:http';+import type { AddressInfo } from 'node:net';+import request from 'supertest';+import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import type { z } from 'zod';++/** A path param is taken for a row id when it is named like one. */+const ID_PARAM = /^(id|pid|lid|[a-z][A-Za-z]*Id)$/;++/**+ * Params named like an id that do not hold an integer row id, keyed+ * `METHOD path :param` or `* path-prefix :param`, each with its reason.+ */+const NOT_ROW_IDS: Record<string, string> = {+ '* /api/plugins/ :pluginId': 'a plugin id is its manifest slug',+ '* /api/plugin-oauth/ :id': 'a plugin id is its manifest slug',+ '* /api/admin/plugins/ :id': 'a plugin id is its manifest slug',+ '* /api/integrations/memories/immich/ :assetId': 'an Immich asset id is a provider string',+ '* /api/integrations/memories/synologyphotos/ :photoId': 'a Synology photo id is a provider string',+ '* /api/public/journey/ :assetId': 'a provider asset id is a provider string',+ '* /api/plugin-settings/ :id': 'a plugin id is its manifest slug',+ '* /api/oauth/clients/ :id': 'an OAuth client row id is a generated text key',+ 'PUT /api/admin/addons/:id :id': 'an addon id is its key',+ 'GET /api/maps/place-photo/:placeId :placeId': 'a maps provider place id is a provider string',+ '* /api/ :toolCallId': "an assistant tool call id is the model's string",+};++/**+ * Request fields named like a row id that do not hold an integer row id, by+ * field name, or by `<schema> <path>` where the name means a row id elsewhere.+ * The schema is the @trek/shared export, or the DTO class for a schema the+ * server declares itself.+ */+const NOT_ROW_ID_FIELDS: Record<string, string> = {+ spreadId: 'a photo book spread id is a generated string',+ spreadIds: 'a photo book spread id is a generated string',+ afterSpreadId: 'a photo book spread id is a generated string',+ elementId: 'a photo book element id is a generated string',+ elementIds: 'a photo book element id is a generated string',+ photoElementId: 'a photo book element id is a generated string',+ groupId: 'a photo book group id is a generated string',+ designId: 'a photo book design id is a generated string',+ layoutId: 'a photo book layout id is a generated string',+ stickerId: 'a sticker id is a catalog key',+ toolCallId: "an assistant tool call id is the model's string",+ client_id: 'an OAuth or OIDC client id is a string',+ accessKeyId: 'an S3 access key id is a credential',+ google_place_id: 'a Google place id is a provider string',+ osm_id: 'an OpenStreetMap id is a provider string such as "node/123"',+ placeId: 'a maps provider place id is a provider string',+ pickedPlaceId: 'a maps provider place id is a provider string',+ trailId: 'a Wanderer trail id is a provider string',+ pluginId: 'a plugin id is its manifest slug',+ asset_id: 'a photo provider asset id is a provider string',+ asset_ids: 'a photo provider asset id is a provider string',+ album_id: 'a photo provider album id is a provider string',+ remoteRootId: 'a document provider folder id is a provider string',+ flightIds: 'an AirTrail flight id is a provider string',+ 'bookSaveRequestSchema $.document.spreads[].id': 'a photo book spread id is a generated string',+ 'bookSaveRequestSchema $.document.spreads[].elements[].id': 'a photo book element id is a generated string',+ 'bookSaveRequestSchema $.document.spreads[].parked[].id': 'a photo book element id is a generated string',+ 'bookSaveRequestSchema $.document.spreads[].groups[].id': 'a photo book group id is a generated string',+ 'bookSaveRequestSchema $.document.layouts[].id': 'a photo book layout id is a generated string',+ 'bookSaveRequestSchema $.document.layouts[].elements[].id': 'a photo book element id is a generated string',+ 'bookSaveRequestSchema $.document.theme.id': 'a photo book theme id is a catalog key',+ 'restoreBookPageRequestSchema $.pages[].spread.id': 'a photo book spread id is a generated string',+ 'restoreBookPageRequestSchema $.pages[].spread.elements[].id': 'a photo book element id is a generated string',+ 'restoreBookPageRequestSchema $.pages[].spread.parked[].id': 'a photo book element id is a generated string',+ 'restoreBookPageRequestSchema $.pages[].spread.groups[].id': 'a photo book group id is a generated string',+ 'editBookPageRequestSchema $.ops[].id': 'a photo book element or group id is a generated string',+ 'previewBookPageRequestSchema $.ops[].id': 'a photo book element or group id is a generated string',+ 'reviseBookDesignRequestSchema $.ops[].id': 'a photo book element or group id is a generated string',+ 'paddleRegionRequestSchema $.id': 'a paddle region id is a catalog key',+ 'pluginInstallRequestSchema $.id': 'a plugin id is its manifest slug',+};++/**+ * Open fields (`z.unknown()`) named like a row id, whose handler answers its+ * own error for a bad value and so reads the id itself with toRowId, keyed+ * `<schema> <path>`, each naming where. contracts:open only lets these shrink.+ */+const READ_BY_HANDLER: Record<string, string> = {+ 'journeyCreateRequestSchema $.trip_ids': 'JourneyController.create keeps the entries toRowId reads (JRNID-001)',+ 'journeyAddTripRequestSchema $.trip_id': "JourneyController.addTrip, 400 'trip_id required' (JRNID-002)",+ 'journeyReorderEntriesRequestSchema $.orderedIds':+ "JourneyController.reorderEntries, 400 'orderedIds must be...' (JRNID-003)",+ 'journeyContributorRequestSchema $.user_id': "JourneyController.addContributor, 400 'user_id required' (JRNID-004)",+ 'journeyLinkPhotoRequestSchema $.journey_photo_id':+ "JourneyController.linkPhoto, 400 'journey_photo_id required' (JRNID-005)",+ 'journeyLinkPhotoRequestSchema $.photo_id':+ "JourneyController.linkPhoto, 400 'journey_photo_id required' (JRNID-005)",+};++const METHODS = ['get', 'post', 'put', 'patch', 'delete'] as const;+type Method = (typeof METHODS)[number];++interface Route {+ method: Method;+ path: string;+}++interface Answer {+ status: number;+ body: string;+}++let app: INestApplication;+let orm: MikroORM;++function isZodSchema(value: unknown): value is z.ZodType {+ return typeof value === 'object' && value !== null && '_zod' in value;+}++/** Every route Express holds once Nest is initialised, in registration order. */+function registeredRoutes(): Route[] {+ type Layer = { route?: { path: unknown; methods: Record<string, boolean> } };+ const express = app.getHttpAdapter().getInstance() as { router: { stack: Layer[] } };+ const routes: Route[] = [];+ for (const layer of express.router.stack) {+ if (!layer.route || typeof layer.route.path !== 'string') continue;+ for (const method of METHODS) {+ if (layer.route.methods[method]) routes.push({ method, path: layer.route.path });+ }+ }+ return routes;+}++function exempts(key: string, route: Route, param: string): boolean {+ const [method, path, name] = key.split(' ');+ if (name !== `:${param}`) return false;+ if (method === '*') return route.path.startsWith(path);+ return method === route.method.toUpperCase() && path === route.path;+}++function exempt(route: Route, param: string): boolean {+ return Object.keys(NOT_ROW_IDS).some((key) => exempts(key, route, param));+}++function pathParams(route: Route): string[] {+ return [...route.path.matchAll(/:([A-Za-z_]+)/g)].map((m) => m[1]);+}++/**+ * An id no seeded row has. A route that reads '0x3ade68b1' or '987654321e0' with+ * Number() binds this number although no answer shows it, on a table this+ * suite seeds nothing in as well as on one it does.+ */+const UNUSED_ID = 987654321;++describe('ROUTEID: every route answers a malformed path id like an unknown one', () => {+ let cookie: string;+ /**+ * One listening server and one keep-alive connection pool for the whole walk:+ * supertest on an unbound server listens anew per request, and a few thousand+ * requests run the machine out of ephemeral ports.+ */+ let baseUrl: string;+ const keepAlive = new http.Agent({ keepAlive: true, maxSockets: 1 });+ /** The id each param names in the seeded world, by param name or by the path segment before `:id`. */+ const ids: Record<string, number> = {};++ beforeAll(async () => {+ installSqlCapture();+ app = await buildApp();+ orm = app.get(MikroORM);+ await app.listen(0, '127.0.0.1');+ baseUrl = `http://127.0.0.1:${(app.getHttpServer().address() as AddressInfo).port}`;+ await updateRows(orm, Addons, {}, { enabled: 1 });++ const { user: admin } = await makeAdmin(orm);+ const { user: other } = await makeUser(orm);+ cookie = sessionCookie(admin.id);+ const trip = await makeTrip(orm, admin.id, { start_date: '2026-03-01', end_date: '2026-03-03' });+ const [day] = await readTripDays(orm, trip.id);+ const place = await makePlace(orm, trip.id);+ const assignment = await makeDayAssignment(orm, day.id, place.id);+ const note = await makeDayNote(orm, day.id, trip.id);+ const journey = await makeJourney(orm, admin.id);+ const entry = await makeJourneyEntry(orm, journey.id, admin.id);+ const collection = await makeCollection(orm, admin.id);+ const collectionPlace = await makeCollectionPlace(orm, collection.id, admin.id);+ Object.assign(ids, {+ tripId: trip.id,+ trips: trip.id,+ dayId: day.id,+ days: day.id,+ placeId: place.id,+ places: place.id,+ assignments: assignment.id,+ notes: note.id,+ journeyId: journey.id,+ journeys: journey.id,+ entryId: entry.id,+ entries: entry.id,+ collections: collection.id,+ pid: collectionPlace.id,+ userId: other.id,+ users: other.id,+ ownerId: other.id,+ reservations: (await makeReservation(orm, trip.id)).id,+ budget: (await makeBudgetItem(orm, trip.id)).id,+ bags: (await makePackingBag(orm, trip.id)).id,+ packing: (await makePackingItem(orm, trip.id)).id,+ todos: (await makeTodoItem(orm, trip.id)).id,+ 'bucket-list': (await makeBucketListItem(orm, admin.id)).id,+ 'in-app': (await makeNotification(orm, admin.id)).id,+ });+ ids.collab = (await makeCollabNote(orm, trip.id, admin.id)).id;+ }, 60_000);++ afterAll(async () => {+ uninstallSqlCapture();+ keepAlive.destroy();+ await app?.close();+ });++ /** The id a param names: by its own name, else by the path segment before it, else the trip's. */+ function idFor(path: string, param: string): number {+ if (ids[param] !== undefined) return ids[param];+ const before = path.split(`/:${param}`)[0].split('/').pop() ?? '';+ return ids[before] ?? ids.tripId;+ }++ async function send(route: Route, path: string, body: unknown): Promise<Answer> {+ await app.get(RateLimitService).reset();+ try {+ const pending = request(baseUrl)[route.method](path).agent(keepAlive).set('Cookie', cookie).timeout(10_000);+ const res =+ route.method === 'get' || route.method === 'delete' ? await pending : await pending.send(body as object);+ return { status: res.status, body: JSON.stringify(res.body) };+ } catch (err) {+ return { status: 599, body: `no answer: ${String(err)}` };+ }+ }++ it('ROUTEID-001: no route answers a non-canonical id differently from "abc", and none with a 5xx', async () => {+ // A body the route's DTO accepts, so the Zod pipe's 400 does not answer before the handler reads the path.+ const bodySchemas = routeBodySchemas(app);+ const routes = registeredRoutes();+ const staleExemptions = Object.keys(NOT_ROW_IDS).filter(+ (key) => !routes.some((route) => pathParams(route).some((param) => exempts(key, route, param))),+ );+ const cases = routes.flatMap((route) =>+ pathParams(route)+ .filter((param) => ID_PARAM.test(param) && !exempt(route, param))+ .map((param) => {+ const schema = bodySchemas.get(`${route.method} ${route.path}`);+ return {+ route,+ label: `${route.method.toUpperCase()} ${route.path} :${param}`,+ spellings: malformedIdSpellings(idFor(route.path, param)),+ body: schema ? sampleBody(schema) : undefined,+ fill: (value: string) =>+ route.path.replace(/:([A-Za-z_]+)/g, (_m, name: string) =>+ name === param ? encodeURIComponent(value) : String(idFor(route.path, name)),+ ),+ };+ }),+ );++ // One request at a time: they share one server and one seeded world.+ const failures = (+ await oneByOne(cases, async ({ route, label, spellings, body, fill }) => {+ const unknown = await send(route, fill('abc'), body ?? {});+ const found =+ unknown.status >= 500 ? [`${label}='abc' answered ${unknown.status} ${unknown.body.slice(0, 160)}`] : [];+ const answers = await oneByOne(spellings, (spelling) => send(route, fill(spelling), body ?? {}));+ answers.forEach((got, i) => {+ if (got.status >= 500 || got.status !== unknown.status || got.body !== unknown.body) {+ found.push(+ `${label}='${spellings[i]}' answered ${got.status} ${got.body.slice(0, 160)}, 'abc' ${unknown.status} ${unknown.body.slice(0, 80)}`,+ );+ }+ });+ // The same spellings of an id no row has: none may reach SQL, as a string or as the number it spells.+ await oneByOne(['abc', ...malformedIdSpellings(UNUSED_ID)], async (spelling) => {+ const sent = await recordSql(() => send(route, fill(spelling), body ?? {}));+ const sql = carriedTo(sent, spelling, UNUSED_ID);+ if (sql) found.push(`${label}='${spelling}' reached SQL: ${sql.slice(0, 160)}`);+ });+ return found;+ })+ ).flat();+ const bodied = cases.filter((c) => c.body !== undefined).length;+ const checked = cases.length;++ expect(bodied, 'no route got a body its DTO accepts; routeBodySchemas lost the handlers').toBeGreaterThan(100);+ expect(checked, 'the walk found no route with an id param; the route listing broke').toBeGreaterThan(300);+ expect(staleExemptions, 'a NOT_ROW_IDS entry names a param no route has any more; remove it').toEqual([]);+ expect(+ failures,+ [+ 'A route reads a path id some other way than toRowId, so a spelling no client sends reaches SQL (as sent, or as',+ 'the number Number() or parseInt() makes of it) or answers unlike an unknown id.',+ "Declare the param with @Param('<name>', rowId('<the route's not-found error>'[, status])) from",+ 'src/nest/common/row-id.pipe.ts (ParseRowIdPipe in place of ParseIntPipe), or read it with toRowId/withRowId',+ '(src/db/row-id.ts) in the guard, service or repository that looks the row up, answering what an id that',+ 'names no row gets. If the param is not an integer row id, add it to NOT_ROW_IDS above with the reason.',+ ].join(' '),+ ).toEqual([]);+ }, 600_000);++ it('ROUTEID-002: no request schema lets a malformed row id through to the handler', () => {+ // The same schema object is one entry, under its @trek/shared name where it has one.+ const named = new Map<z.ZodType, string>();+ for (const [name, value] of Object.entries(contracts)) {+ if (/(Request|Body|Query)Schema$/.test(name) && isZodSchema(value)) named.set(value, name);+ }+ for (const dto of routeRequestDtos(app)) if (!named.has(dto.schema)) named.set(dto.schema, dto.name);++ const seen = new Set<string>();+ const failures: string[] = [];+ for (const [schema, name] of named) {+ for (const field of rowIdFields(schema)) {+ const key = `${name} ${field.path}`;+ seen.add(key).add(field.name);+ if (NOT_ROW_ID_FIELDS[field.name] || NOT_ROW_ID_FIELDS[key] || READ_BY_HANDLER[key]) continue;+ const accepted = malformedAccepted(field.schema);+ if (accepted.length) failures.push(`${key} accepts ${accepted.map((v) => JSON.stringify(v)).join(', ')}`);+ }+ }+ const stale = [...Object.keys(NOT_ROW_ID_FIELDS), ...Object.keys(READ_BY_HANDLER)].filter((key) => !seen.has(key));++ expect(named.size, 'no request schema found; the contracts import or the DTO walk broke').toBeGreaterThan(300);+ expect(stale, 'an entry names a field no request schema holds any more; remove it').toEqual([]);+ expect(+ failures,+ [+ 'A request field named like a row id accepts a spelling toRowId refuses, so the handler receives it and SQL',+ 'answers it three ways. In the @trek/shared schema, declare the field rowIdInputSchema (a number or its digits,',+ 'kept as sent), rowIdStringSchema (digits only, a multipart field), idParamSchema (read as a number), or',+ 'z.number().int() for a list of numbers (src/common/primitives.schema.ts), and read it with toRowId on the',+ 'server. If the field does not hold an integer row id, add it to NOT_ROW_ID_FIELDS above with the reason.',+ ].join(' '),+ ).toEqual([]);+ });+});diff --git a/server/tests/helpers/route-bodies.ts b/server/tests/helpers/route-bodies.tsnew file mode 100644index 000000000..922088610--- /dev/null+++ b/server/tests/helpers/route-bodies.ts@@ -0,0 +1,187 @@+/**+ * A request body each registered route's Zod DTO accepts, for suites that walk+ * every route (route-row-ids.e2e.test.ts): a body the Zod pipe refuses answers+ * 400 before the handler runs, and would hide what the handler does with the+ * path. Built from the DTO's JSON schema with the smallest values that pass;+ * a schema the sample does not satisfy (a refinement, a pattern) gets no body.+ * The same walk lists every route's body and query DTO (`routeRequestDtos`).+ */+import { RequestMethod } from '@nestjs/common';+import type { INestApplication } from '@nestjs/common';+import { METHOD_METADATA, PATH_METADATA, ROUTE_ARGS_METADATA } from '@nestjs/common/constants';+import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum';+import { ModulesContainer } from '@nestjs/core';++import { z } from 'zod';++type JsonSchema = Record<string, unknown>;++interface ZodDtoClass {+ schema?: z.ZodType;+}++const MAX_DEPTH = 8;++function firstType(schema: JsonSchema): string | undefined {+ const type = schema.type;+ if (Array.isArray(type)) return (type as string[]).find((t) => t !== 'null') ?? 'null';+ return typeof type === 'string' ? type : undefined;+}++function sampleString(schema: JsonSchema): string {+ switch (schema.format) {+ case 'date':+ return '2026-03-01';+ case 'date-time':+ return '2026-03-01T10:00:00Z';+ case 'time':+ return '10:00:00';+ case 'email':+ return '[email protected]';+ case 'uri':+ case 'url':+ return 'https://example.test/';+ case 'uuid':+ return '00000000-0000-4000-8000-000000000000';+ default: {+ const min = typeof schema.minLength === 'number' ? schema.minLength : 1;+ return 'x'.repeat(Math.max(1, min));+ }+ }+}++function sampleNumber(schema: JsonSchema): number {+ const min = typeof schema.minimum === 'number' ? schema.minimum : undefined;+ const exclusive = typeof schema.exclusiveMinimum === 'number' ? schema.exclusiveMinimum : undefined;+ const floor = min ?? (exclusive !== undefined ? exclusive + 1 : 1);+ return Math.max(1, floor);+}++/** The smallest value `schema` (a JSON schema from z.toJSONSchema) describes, as far as a walk can tell. */+function sampleJsonSchema(schema: JsonSchema | boolean, root: JsonSchema | boolean = schema, depth = 0): unknown {+ // A boolean schema (`items: false`) allows anything or nothing; neither needs a value.+ if (typeof schema !== 'object' || typeof root !== 'object' || depth > MAX_DEPTH) return null;+ const ref = schema.$ref;+ if (typeof ref === 'string' && ref.startsWith('#/$defs/')) {+ const defs = (root.$defs ?? {}) as Record<string, JsonSchema>;+ return sampleJsonSchema(defs[ref.slice('#/$defs/'.length)] ?? {}, root, depth + 1);+ }+ if ('const' in schema) return schema.const;+ if (Array.isArray(schema.enum)) return (schema.enum as unknown[])[0];+ const alternatives = (schema.anyOf ?? schema.oneOf) as JsonSchema[] | undefined;+ if (Array.isArray(alternatives) && alternatives.length > 0) {+ const chosen = alternatives.find((alt) => alt.type !== 'null') ?? alternatives[0];+ return sampleJsonSchema(chosen, root, depth + 1);+ }+ if (Array.isArray(schema.allOf) && schema.allOf.length > 0) {+ return sampleJsonSchema(schema.allOf[0] as JsonSchema, root, depth + 1);+ }+ switch (firstType(schema)) {+ case 'object': {+ const properties = (schema.properties ?? {}) as Record<string, JsonSchema>;+ const required = (schema.required ?? []) as string[];+ return Object.fromEntries(required.map((key) => [key, sampleJsonSchema(properties[key] ?? {}, root, depth + 1)]));+ }+ case 'array': {+ const count = typeof schema.minItems === 'number' ? schema.minItems : 0;+ const items = (schema.items ?? {}) as JsonSchema;+ return Array.from({ length: count }, () => sampleJsonSchema(items, root, depth + 1));+ }+ case 'string':+ return sampleString(schema);+ case 'integer':+ case 'number':+ return sampleNumber(schema);+ case 'boolean':+ return false;+ case 'null':+ return null;+ default:+ return 'x';+ }+}++/** A body `schema` accepts, or undefined when the sample does not pass it. */+export function sampleBody(schema: z.ZodType): unknown {+ let json: JsonSchema;+ try {+ json = z.toJSONSchema(schema, { io: 'input', unrepresentable: 'any' }) as JsonSchema;+ } catch {+ return undefined;+ }+ const sample = sampleJsonSchema(json);+ return schema.safeParse(sample).success ? sample : undefined;+}++function joinPath(...parts: string[]): string {+ return (+ '/' ++ parts+ .map((p) => p.replace(/^\/+|\/+$/g, ''))+ .filter(Boolean)+ .join('/')+ );+}++/** One Zod DTO a route handler declares, for its body or its query. */+interface RouteDto {+ /** `method /path`, Express's spelling of the route. */+ route: string;+ /** The DTO class's name. */+ name: string;+ schema: z.ZodType;+}++/** Every handler argument of type `paramtype` (whole body or whole query) that is a Zod DTO, per route. */+function routeDtos(app: INestApplication, paramtype: RouteParamtypes): RouteDto[] {+ const container = app.get(ModulesContainer, { strict: false });+ const dtos: RouteDto[] = [];+ for (const module of container.values()) {+ for (const wrapper of module.controllers.values()) {+ const ctor = wrapper.metatype as (new (...args: never[]) => object) | undefined;+ if (!ctor?.prototype) continue;+ const controllerPaths = [Reflect.getMetadata(PATH_METADATA, ctor) as string | string[] | undefined].flat();+ for (const name of Object.getOwnPropertyNames(ctor.prototype)) {+ const handler = (ctor.prototype as Record<string, unknown>)[name];+ if (name === 'constructor' || typeof handler !== 'function') continue;+ const method = Reflect.getMetadata(METHOD_METADATA, handler) as RequestMethod | undefined;+ if (method === undefined) continue;+ const args = (Reflect.getMetadata(ROUTE_ARGS_METADATA, ctor, name) ?? {}) as Record<+ string,+ { index: number; data?: unknown }+ >;+ const arg = Object.entries(args).find(+ ([key, a]) => key.split(':')[0] === String(paramtype) && a.data === undefined,+ )?.[1];+ if (!arg) continue;+ const types = (Reflect.getMetadata('design:paramtypes', ctor.prototype, name) ?? []) as unknown[];+ const dto = types[arg.index] as (ZodDtoClass & { name?: string }) | undefined;+ if (!dto?.schema) continue;+ const handlerPaths = [Reflect.getMetadata(PATH_METADATA, handler) as string | string[] | undefined].flat();+ for (const controllerPath of controllerPaths) {+ for (const handlerPath of handlerPaths) {+ dtos.push({+ route: `${RequestMethod[method].toLowerCase()} ${joinPath(controllerPath ?? '', handlerPath ?? '')}`,+ name: dto.name ?? 'anonymous DTO',+ schema: dto.schema,+ });+ }+ }+ }+ }+ }+ return dtos;+}++/**+ * `METHOD /path` (Express's spelling of the route) to the Zod schema of the+ * handler's `@Body()` DTO, for every handler that declares one.+ */+export function routeBodySchemas(app: INestApplication): Map<string, z.ZodType> {+ return new Map(routeDtos(app, RouteParamtypes.BODY).map((dto) => [dto.route, dto.schema]));+}++/** The `@Body()` and `@Query()` DTOs of every route, for suites that check what the Zod pipe lets through. */+export function routeRequestDtos(app: INestApplication): RouteDto[] {+ return [...routeDtos(app, RouteParamtypes.BODY), ...routeDtos(app, RouteParamtypes.QUERY)];+}diff --git a/server/tests/helpers/row-id-fields.ts b/server/tests/helpers/row-id-fields.tsnew file mode 100644index 000000000..4507bd845--- /dev/null+++ b/server/tests/helpers/row-id-fields.ts@@ -0,0 +1,124 @@+/**+ * The fields of a request schema that hold a row id, and the spellings of an id+ * each one lets through. For ROUTEID-002 (route-row-ids.e2e.test.ts): a body+ * or query id a schema accepts as '0x1', '1e0' or 1.5 reaches the handler, and+ * from there SQL, where SQLite matches nothing, Postgres 14 and 15 refuse the+ * cast and 16 and later read hex and underscores as a number.+ */+import type { z } from 'zod';++/** A field is taken for a row id when it is named like one: `id`, `ids`, `*_id`, `*_ids`, `*Id`, `*Ids`. */+export const ROW_ID_NAME = /^(id|ids|[a-z][a-z0-9_]*_ids?|[a-z][A-Za-z0-9]*Ids?)$/;++/** Fields that hold a row id under a name the pattern above does not catch. */+export const ROW_ID_NAMED_OTHERWISE: ReadonlySet<string> = new Set(['reply_to', 'replyTo']);++/**+ * Spellings of row 1 that toRowId refuses: Number() and parseInt() read most+ * of them as 1, a Postgres 16+ cast reads hex and underscores, Postgres 14+ * and 15 refuse them, and 1.5 and 1e20 are no bigint on any Postgres.+ */+const MALFORMED_ROW_IDS: readonly unknown[] = [+ '0x1',+ '1e0',+ '1.0',+ '+1',+ ' 1',+ '1 ',+ '1_0',+ 'abc',+ '99999999999999999999',+ 1.5,+ 1e20,+];++/**+ * The spellings of `id` toRowId refuses that Number(), parseInt() or a+ * Postgres 16+ cast read as `id`, plus a number past the bigint range, for an+ * id that travels as text (a path, a URI).+ */+export function malformedIdSpellings(id: number): string[] {+ return [`0x${id.toString(16)}`, `${id}e0`, `${id}.0`, `+${id}`, `0_${id}`, ` ${id}`, '99999999999999999999'];+}++interface RowIdField {+ /** `$.a.b[].c`, from the schema's root. */+ path: string;+ name: string;+ schema: z.ZodType;+}++interface ZodDefLike {+ type: string;+ shape?: Record<string, z.ZodType>;+ getter?: () => z.ZodType;+ [key: string]: unknown;+}++function defOf(schema: unknown): ZodDefLike | undefined {+ return (schema as { _zod?: { def?: ZodDefLike } } | undefined)?._zod?.def;+}++function isZod(value: unknown): value is z.ZodType {+ return defOf(value) !== undefined;+}++/** Every field of `root` named like a row id, at every depth, once per path. */+export function rowIdFields(root: z.ZodType): RowIdField[] {+ const found = new Map<string, RowIdField>();+ const ancestors = new Set<unknown>();+ const visit = (schema: unknown, path: string): void => {+ const def = defOf(schema);+ if (!def || ancestors.has(schema)) return;+ ancestors.add(schema);+ try {+ walk(def, path);+ } finally {+ ancestors.delete(schema);+ }+ };+ const walk = (def: ZodDefLike, path: string): void => {+ if (def.type === 'object') {+ for (const [name, value] of Object.entries(def.shape ?? {})) {+ const at = `${path}.${name}`;+ if ((ROW_ID_NAME.test(name) || ROW_ID_NAMED_OTHERWISE.has(name)) && !found.has(at)) {+ found.set(at, { path: at, name, schema: value });+ }+ visit(value, at);+ }+ return;+ }+ if (def.type === 'lazy' && def.getter) {+ visit(def.getter(), path);+ return;+ }+ if (def.type === 'array') {+ visit(def.element, `${path}[]`);+ return;+ }+ for (const key of ['innerType', 'in', 'out', 'valueType', 'left', 'right', 'rest']) {+ if (isZod(def[key])) visit(def[key], path);+ }+ for (const key of ['options', 'items']) {+ const list = def[key];+ if (Array.isArray(list)) list.forEach((option) => visit(option, path));+ }+ };+ visit(root, '$');+ return [...found.values()];+}++const WRAPPERS = new Set(['optional', 'nullable', 'default', 'prefault', 'readonly', 'catch', 'nonoptional']);++/** True when the field, under its optional/nullable/default wrappers, is a list. */+function holdsList(schema: z.ZodType): boolean {+ let def = defOf(schema);+ while (def && WRAPPERS.has(def.type)) def = defOf(def.innerType);+ return def?.type === 'array';+}++/** The spellings in MALFORMED_ROW_IDS a row id field accepts; a list field is tried with one element. */+export function malformedAccepted(field: z.ZodType): unknown[] {+ const list = holdsList(field);+ return MALFORMED_ROW_IDS.filter((value) => field.safeParse(list ? [value] : value).success);+}diff --git a/server/tests/helpers/sql-capture.ts b/server/tests/helpers/sql-capture.tsnew file mode 100644index 000000000..8e947019b--- /dev/null+++ b/server/tests/helpers/sql-capture.ts@@ -0,0 +1,116 @@+/**+ * Records what the database drivers are handed while a recording runs: each+ * statement's SQL text and its bound values, on SQLite (better-sqlite3) and on+ * Postgres (pg), below MikroORM and Kysely, so every statement the app sends+ * is seen whichever API built it.+ *+ * For suites that must prove a value never reached SQL (ROUTEID, in+ * route-row-ids.e2e.test.ts): a route that parsed '0x3ade68b1' with Number()+ * binds 987654321 even when no row has that id, which no answer would show.+ */+import Database from 'better-sqlite3';+import { createRequire } from 'node:module';+import path from 'node:path';++interface SentStatement {+ sql: string;+ values: unknown[];+}++type Method = (this: unknown, ...args: unknown[]) => unknown;+type Patchable = Record<string, Method>;++let recording: SentStatement[] | null = null;+const restores: Array<() => void> = [];++function flatten(args: unknown[]): unknown[] {+ return args.flatMap((arg) => {+ if (Array.isArray(arg)) return arg as unknown[];+ if (arg !== null && typeof arg === 'object') return Object.values(arg);+ return [arg];+ });+}++function patch(target: Patchable, name: string, record: (self: unknown, args: unknown[]) => void): void {+ const original = target[name];+ if (typeof original !== 'function') return;+ target[name] = function (this: unknown, ...args: unknown[]) {+ if (recording) record(this, args);+ return original.apply(this, args);+ };+ restores.push(() => {+ target[name] = original;+ });+}++/** better-sqlite3's Statement prototype: every statement of every handle runs through its methods. */+function patchSqlite(): void {+ const scratch = new Database(':memory:');+ const prepare = (Database.prototype as unknown as Patchable)['prepare'];+ const statementProto = Object.getPrototypeOf(prepare.call(scratch, 'SELECT 1')) as Patchable;+ scratch.close();+ for (const method of ['run', 'get', 'all', 'iterate']) {+ patch(statementProto, method, (self, args) => {+ recording?.push({ sql: String((self as { source?: unknown }).source ?? ''), values: flatten(args) });+ });+ }+}++/** pg's Client.prototype.query, which the pool's clients run every statement through. */+function patchPostgres(): void {+ // pg arrives with @mikro-orm/postgresql rather than as a dependency of the server's own.+ const fromServer = createRequire(path.join(process.cwd(), 'package.json'));+ const fromDriver = createRequire(fromServer.resolve('@mikro-orm/postgresql'));+ const pg = fromDriver('pg') as { Client: { prototype: Patchable } };+ patch(pg.Client.prototype, 'query', (_self, args) => {+ const [config, values] = args;+ if (typeof config === 'string') {+ recording?.push({ sql: config, values: Array.isArray(values) ? values : [] });+ } else if (config !== null && typeof config === 'object') {+ const query = config as { text?: unknown; values?: unknown };+ recording?.push({+ sql: String(query.text ?? ''),+ values: Array.isArray(query.values) ? query.values : Array.isArray(values) ? values : [],+ });+ }+ });+}++/** Wraps both drivers once per test file; `uninstallSqlCapture` puts them back. */+export function installSqlCapture(): void {+ if (restores.length > 0) return;+ patchSqlite();+ patchPostgres();+}++export function uninstallSqlCapture(): void {+ for (const restore of restores.splice(0).reverse()) restore();+ recording = null;+}++/** The statements the drivers were handed while `run` ran. */+export async function recordSql(run: () => Promise<unknown>): Promise<SentStatement[]> {+ const sent: SentStatement[] = [];+ recording = sent;+ try {+ await run();+ } finally {+ recording = null;+ }+ return sent;+}++/**+ * The first statement that carried `spelling` to the database, as a bound value+ * or quoted in its text, or the number `id` that Number() or parseInt() makes+ * of it; undefined when none did.+ */+export function carriedTo(sent: SentStatement[], spelling: string, id: number): string | undefined {+ const bare = new RegExp(`(^|[^0-9])${id}([^0-9]|$)`);+ return sent.find(+ ({ sql, values }) =>+ values.some((v) => v === spelling || v === id || v === String(id)) ||+ sql.includes(`'${spelling}'`) ||+ bare.test(sql),+ )?.sql;+}diff --git a/server/tests/unit/nest-rpc/rpc-row-id-params.test.ts b/server/tests/unit/nest-rpc/rpc-row-id-params.test.tsnew file mode 100644index 000000000..4f4ea79de--- /dev/null+++ b/server/tests/unit/nest-rpc/rpc-row-id-params.test.ts@@ -0,0 +1,71 @@+/**+ * RPCROWID: every plugin RPC handler reads an id param through rowIdParam.+ *+ * A plugin sends its params as JSON, so an id can arrive as '0x5', '5e0', 1.5+ * or 1e20. num() and Number() read the first two as 5 and pass the others on,+ * which SQLite matches against nothing while Postgres refuses them or reads+ * the string its own way. The params have no schema a walk could vary, so+ * this reads the handlers' source: every `params.<name>` named like a row id+ * must sit directly inside rowIdParam(), optionalRowIdParam() or toRowId(), or+ * inside str() for an id that is a string (a plugin's slug, a migration id).+ * A handler's `input` is parsed with a @trek/shared request schema, which+ * ROUTEID-002 holds to the same rule.+ */+import { ROW_ID_NAME, ROW_ID_NAMED_OTHERWISE } from '../../helpers/row-id-fields';++import fs from 'node:fs';+import path from 'node:path';+import { describe, expect, it } from 'vitest';++const SRC = path.resolve(__dirname, '../../../src');+/** A `params.<id>` read directly inside one of these, line breaks allowed. */+const WRAPPED = /\b(rowIdParam|optionalRowIdParam|toRowId|str)\(\s*$/;+const PARAM_READ = /\bparams\.(\w+)/g;++function rpcFiles(dir: string): string[] {+ return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {+ const full = path.join(dir, entry.name);+ if (entry.isDirectory()) return rpcFiles(full);+ return entry.name.endsWith('.ts') && fs.readFileSync(full, 'utf8').includes('@PluginMethod(') ? [full] : [];+ });+}++/** The `params.<id>` reads of `source` that no reader wraps, as `line: text`. */+function unparsedIdReads(source: string): string[] {+ const lines = source.split('\n');+ const found: string[] = [];+ for (const match of source.matchAll(PARAM_READ)) {+ if (!ROW_ID_NAME.test(match[1]) && !ROW_ID_NAMED_OTHERWISE.has(match[1])) continue;+ if (WRAPPED.test(source.slice(Math.max(0, match.index - 200), match.index))) continue;+ const line = source.slice(0, match.index).split('\n').length;+ found.push(`${line}: ${lines[line - 1].trim()}`);+ }+ return found;+}++describe('RPCROWID: plugin RPC handlers read id params through rowIdParam', () => {+ it('RPCROWID-001: finds an id param read any other way, and passes the readers', () => {+ expect(unparsedIdReads('const id = num(params.tripId, "tripId");')).toHaveLength(1);+ expect(unparsedIdReads('const id = Number(params.entryId);')).toHaveLength(1);+ expect(unparsedIdReads('await this.svc.list(params.tripId);')).toHaveLength(1);+ expect(unparsedIdReads('const id = rowIdParam(params.tripId, "tripId");')).toEqual([]);+ expect(unparsedIdReads('const id = rowIdParam(\n params.tripId,\n "tripId",\n );')).toEqual([]);+ expect(unparsedIdReads('const id = optionalRowIdParam(params.dayId, "dayId");')).toEqual([]);+ expect(unparsedIdReads('return ctx.data.migrate(str(params.id, "id"));')).toEqual([]);+ expect(unparsedIdReads('const text = params.text;')).toEqual([]);+ });++ it('RPCROWID-002: no handler reads an id param past rowIdParam', () => {+ const files = rpcFiles(SRC);+ const failures = files.flatMap((file) =>+ unparsedIdReads(fs.readFileSync(file, 'utf8')).map((read) => `${path.relative(SRC, file)}:${read}`),+ );+ expect(files.length, 'no file declares a @PluginMethod; the walk broke').toBeGreaterThan(20);+ expect(+ failures,+ 'Read a plugin RPC id param with rowIdParam(params.<name>, "<name>") from src/nest-rpc/rpc-params.ts ' ++ '(optionalRowIdParam when it may be left out), or with str() when the id is a string, never with num(), ' ++ 'Number() or as sent.',+ ).toEqual([]);+ });+});