mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
fix(db): refuse raw SQL everywhere under src/, not only in the repositories
The ESLint walls against raw(), the sql tag and SQL text handed to execute/run/all/get applied to src/db/repositories and src/db/seeders only. A service, job, guard or MCP tool could send its own SQLite-only SQL through em.getConnection().execute(...), which the Postgres probe never runs, since it only calls the repositories. The walls now cover all of src/. The dialect layer, the engines and the migrations stay the homes of raw SQL, and the four boot-time lifecycle files that hold SQL text today are named and pinned to their count.
This commit is contained in:
@@ -192,9 +192,80 @@ void safeFetch(url);`,
|
||||
}
|
||||
});
|
||||
|
||||
it('does not reach outside src/db', async () => {
|
||||
const RAW_MESSAGE_PREFIX = 'Dialect SQL goes through';
|
||||
const rawHits = (options: unknown[], code: string) =>
|
||||
messages(
|
||||
options,
|
||||
`${SQL}\ndeclare function raw(s: string): unknown;\ndeclare const sql: (s: TemplateStringsArray) => unknown;\n${code}`,
|
||||
).filter((m) => m.startsWith(SQL_MESSAGE_PREFIX) || m.startsWith(RAW_MESSAGE_PREFIX));
|
||||
|
||||
// A service with SQLite-only SQL of its own: no repository, so the Postgres
|
||||
// probe never sends it, and only a lane suite that happens to reach it would.
|
||||
const SERVICE_SQL =
|
||||
"void connection.execute('SELECT trip_id, group_concat(name, \\', \\') AS names, ifnull(SUM(cost), 0) AS total FROM places WHERE trip_id = ? GROUP BY trip_id', [1]);";
|
||||
|
||||
it.each([
|
||||
'src/nest/trips/trip-stats.service.ts',
|
||||
'src/nest/demo/demo-reset.job.ts',
|
||||
'src/nest/auth/auth.guard.ts',
|
||||
'src/nest/trips/trips.mcp.ts',
|
||||
'src/utils/probe.ts',
|
||||
'src/db/probe.ts',
|
||||
'src/app-config/probe.ts',
|
||||
'src/nest/plugins/host/plugin-audit.ts',
|
||||
])('refuses raw SQL in %s, outside the repositories', async (file) => {
|
||||
const options = await selectorsFor(file);
|
||||
expect(rawHits(options, SERVICE_SQL), file).toHaveLength(1);
|
||||
expect(rawHits(options, "void raw('now()');"), file).toHaveLength(1);
|
||||
expect(rawHits(options, 'void sql`select 1`;'), file).toHaveLength(1);
|
||||
expect(rawHits(options, "void qb.execute('run'); void repo.get('key');"), file).toEqual([]);
|
||||
});
|
||||
|
||||
// Where SQL may be spelled outside the repositories and seeders: the homes
|
||||
// (each engine's own SQL, the migrations) and the boot-time lifecycle files,
|
||||
// with the SQL strings each holds today. A count may only go down.
|
||||
const SQL_HOMES = ['src/db/dialect/', 'src/db/engine/', 'src/db/migrations/'];
|
||||
const LIFECYCLE_SQL_STRINGS: Record<string, number> = {
|
||||
'src/db/email-case-index.ts': 3,
|
||||
'src/db/legacy-baseline.ts': 3,
|
||||
'src/db/migration-utils.ts': 6,
|
||||
'src/db/pre-migrate-snapshot.ts': 3,
|
||||
};
|
||||
|
||||
it('leaves out exactly the homes of raw SQL and the pinned lifecycle files, and keeps the other guards there', async () => {
|
||||
const exempt: string[] = [];
|
||||
const outside = filesUnder('src').filter(
|
||||
(file) => !file.startsWith('src/db/repositories/') && !file.startsWith('src/db/seeders/'),
|
||||
);
|
||||
for (const file of outside) {
|
||||
// A file ESLint ignores altogether (generated, gitignored) is linted by nothing.
|
||||
if (await eslint.isPathIgnored(path.join(serverRoot, file))) continue;
|
||||
const options = (await selectorsFor(file)) as { message?: string }[];
|
||||
if (!options.some((o) => o.message?.startsWith(SQL_MESSAGE_PREFIX))) exempt.push(file);
|
||||
}
|
||||
const unexpected = exempt.filter(
|
||||
(file) => !SQL_HOMES.some((home) => file.startsWith(home)) && !(file in LIFECYCLE_SQL_STRINGS),
|
||||
);
|
||||
expect(unexpected).toEqual([]);
|
||||
for (const file of Object.keys(LIFECYCLE_SQL_STRINGS)) {
|
||||
expect(exempt, file).toContain(file);
|
||||
const options = await selectorsFor(file);
|
||||
expect(messages(options, `${PRELUDE}\nvoid safeFetch(url);`), file).toContain(TIMEOUT);
|
||||
}
|
||||
});
|
||||
|
||||
it('holds each lifecycle file to the SQL strings it has today', async () => {
|
||||
const options = await selectorsFor('src/nest/memories/probe.service.ts');
|
||||
expect(hits(options, "void connection.execute('SELECT 1');")).toEqual([]);
|
||||
const counts: Record<string, number> = {};
|
||||
for (const file of Object.keys(LIFECYCLE_SQL_STRINGS)) {
|
||||
const source = fs.readFileSync(path.join(serverRoot, file), 'utf8');
|
||||
counts[file] = messages(options, source).filter(
|
||||
(m) => m.startsWith(SQL_MESSAGE_PREFIX) || m.startsWith(RAW_MESSAGE_PREFIX),
|
||||
).length;
|
||||
}
|
||||
expect(counts, 'new SQL text in a lifecycle file fails; after removing some, lower its pin').toEqual(
|
||||
LIFECYCLE_SQL_STRINGS,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user