mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
cc0fe6ce2b
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.
72 lines
3.6 KiB
TypeScript
72 lines
3.6 KiB
TypeScript
/**
|
|
* 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([]);
|
|
});
|
|
});
|