mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
test(api): hold every request row id to toRowId on paths, bodies, queries, MCP URIs and plugin RPC
ROUTEID walks every route the app registers. - ROUTEID-001 sends 'abc' and the non-canonical spellings of a seeded row's id to each path param named like an id, and requires no 5xx and the answer 'abc' gets. It then sends the spellings of an id no row has and records what the database drivers are handed (tests/helpers/sql-capture.ts, below MikroORM and Kysely). It fails when a spelling, or the number Number() makes of it, reaches SQL, so a route over a table the suite seeds nothing in is held too. Stale NOT_ROW_IDS entries fail, and the walk runs on one listening keep-alive server. - ROUTEID-002 walks every @trek/shared request, body and query schema and every route's body and query DTO, and fails on a field named like a row id that accepts such a spelling. String ids are listed with their reason; open fields name the handler test that holds them. Three more suites cover the other entry points: - MCPROWID does the same for every MCP resource template. - RPCROWID refuses a plugin RPC id param read past rowIdParam. - BODYID, JRNID and QUERYID cover the open accommodation update body, the collab reply, the journey fields a handler parses itself and the notifications limit and offset. All suites that boot the app run in the SQLite suite and in the Postgres lane on 14 and 18. server/CLAUDE.md states the rule and the four gates.
This commit is contained in:
@@ -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]),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user