Files
TREK/server/tests/unit/eslint-rules/restricted-imports-xml.test.ts
T
Maurice c970245e0d fix(plugins): hold plugin data on Postgres to its own schema and to one contract on both engines
The plugin SQL guard now refuses what still reached outside a plugin's
schema or depended on the server:
- reads of the system columns (xmin, xmax, cmin, cmax, ctid, tableoid),
  which show transaction ids and storage layout and do not exist on
  SQLite; a value of the plugin's own may still be named xmin with AS
- the xid and xid8 types wherever the word stands for the type, since
  age() of an xid is the cluster's transaction counter
- the text search functions that coerce a text argument to regconfig, a
  catalog lookup by name, which leave the allowlist
- collations and access methods outside the set every server has, which
  could tell whether a locale or an extension exists

Every plugin transaction pins, with SET LOCAL, the settings that decide
how values print and parse (TimeZone UTC, DateStyle, the C locales, float,
bytea and xml output, the text search configuration), so a plugin's output
neither depends on nor reveals the server's configuration.

Both stores now keep one contract. Its texts and caps live once, in
plugin-data-contract.ts, and both stores throw only those: NUL in SQL,
an argument or a migration id is refused on SQLite as on Postgres,
transaction control is refused in every call on both engines (tx() is
how a batch becomes atomic), a closed store refuses every call, and the
Postgres store answers the cases better-sqlite3 names with its words.
The SQLite store moves beside the Postgres one as SqlitePluginDataStore
in db/engine/sqlite, takes its file from the backend so src/db never
imports src/nest, and no longer needs the driver exemption. A contract
test runs every violation on both stores and fails when they answer
differently. The Postgres lane holds the allowlist, the pinned settings
and the SQL plugins commonly write against a real database.
2026-11-18 13:20:07 +01:00

86 lines
3.5 KiB
TypeScript

import { ESLint, Linter } from 'eslint';
import path from 'path';
import tseslint from 'typescript-eslint';
import { describe, expect, it } from 'vitest';
// fast-xml-parser is allowed in place-import's codecs and the WebDAV client
// only, through no-restricted-imports in eslint.config.mjs. That rule's options
// are replaced, not merged, by every flat-config block that restates it, so a
// later block can silently drop the restriction (or the driver and services
// walls beside it). This asks ESLint for the options it really applies to a
// path and runs them over an import of each restricted module.
const serverRoot = path.resolve(__dirname, '../../..');
const eslint = new ESLint({ cwd: serverRoot });
async function restrictedImportsFor(file: string): Promise<unknown[]> {
const config = (await eslint.calculateConfigForFile(path.join(serverRoot, file))) as {
rules?: Record<string, [string, ...unknown[]]>;
};
return config.rules?.['no-restricted-imports']?.slice(1) ?? [];
}
async function flags(file: string, source: string): Promise<boolean> {
const options = await restrictedImportsFor(file);
const linter = new Linter({ configType: 'flat' });
const result = linter.verify(
source,
[
{
files: ['**/*.ts'],
languageOptions: { parser: tseslint.parser },
rules: { 'no-restricted-imports': ['error', ...(options as object[])] },
},
],
'probe.ts',
);
return result.some((m) => m.ruleId === 'no-restricted-imports');
}
const XML = "import { XMLParser } from 'fast-xml-parser';\n";
const DRIVER = "import Database from 'better-sqlite3';\n";
const SERVICES = "import { x } from '../services/x';\n";
describe('fast-xml-parser stays in place-import and the WebDAV client', () => {
it.each([
'src/nest/places/places.service.ts',
'src/nest/collections/collections.service.ts',
'src/nest/tours/tours.service.ts',
'src/nest/maps/maps.service.ts',
'src/db/repositories/Places.repository.ts',
// The driver's own homes, which the driver wall leaves out, the plugin
// data directories, which it holds again, and the RPC kit, which restates the rule.
'src/db/database.ts',
'src/db/connection.ts',
'src/db/orm-driver.ts',
'src/db/durability.ts',
'src/db/reseat-booked-nights.ts',
'src/nest/plugins/host/plugin-data.service.ts',
'src/nest-rpc/rpc-kit/registry.ts',
])('refuses it in %s', async (file) => {
expect(await flags(file, XML)).toBe(true);
});
it.each([
'src/nest/place-import/gpx.codec.ts',
'src/nest/place-import/kml.codec.ts',
'src/nest/doc-sync/providers/webdav.client.ts',
])('allows it in %s, and keeps the driver and services walls there', async (file) => {
expect(await flags(file, XML)).toBe(false);
expect(await flags(file, DRIVER)).toBe(true);
expect(await flags(file, SERVICES)).toBe(true);
});
it('keeps the services wall in the driver-exempt files and the driver wall in the RPC kit', async () => {
expect(await flags('src/db/connection.ts', SERVICES)).toBe(true);
expect(await flags('src/db/connection.ts', DRIVER)).toBe(false);
expect(await flags('src/nest/plugins/host/plugin-data.service.ts', DRIVER)).toBe(true);
expect(await flags('src/nest-rpc/rpc-kit/registry.ts', DRIVER)).toBe(true);
});
it('keeps the driver and services walls where the parser is refused', async () => {
expect(await flags('src/nest/places/places.service.ts', DRIVER)).toBe(true);
expect(await flags('src/nest/places/places.service.ts', SERVICES)).toBe(true);
});
});