Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/245.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lists with a page size or offset, such as a meeting's action receipts, now run on TiDB Cloud, which rejects a prepared `LIMIT ?`.
2 changes: 1 addition & 1 deletion docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ npm run start -w server # keep it running

## Cloudflare

The deployed instance runs in the 42nights Cloudflare account from [deploy/cloudflare](../deploy/cloudflare): Worker `sanctum`, Container applications for `SanctumApi` and `SanctumJobs` (same image, one instance each), MySQL-compatible TiDB Cloud Starter (free tier, AWS us-east-1, database `sanctum`) over verified TLS; set `SANCTUM_TEST_MYSQL_CA_CERT` to run the tests against it. TiDB has no `FULLTEXT`, so context search matches words with regular expressions there and migrations skip the `FULLTEXT` and `STORED` steps (`hasFullText` in `server/src/db.ts`), recordings in the private R2 bucket `sanctum-recordings`.
The deployed instance runs in the 42nights Cloudflare account from [deploy/cloudflare](../deploy/cloudflare): Worker `sanctum`, Container applications for `SanctumApi` and `SanctumJobs` (same image, one instance each), MySQL-compatible TiDB Cloud Starter (free tier, AWS us-east-1, database `sanctum`) over verified TLS; set `SANCTUM_TEST_MYSQL_CA_CERT` to run the tests against it, and raise `SANCTUM_TEST_TIMEOUT_MS` (default 30000) because each statement crosses the internet; it also rejects a prepared `LIMIT ?`, so `translated` in `server/src/db.ts` writes safe integer bounds into the text there. TiDB has no `FULLTEXT`, so context search matches words with regular expressions there and migrations skip the `FULLTEXT` and `STORED` steps (`hasFullText` in `server/src/db.ts`), recordings in the private R2 bucket `sanctum-recordings`.
It serves two Workers custom domains declared in `wrangler.jsonc`: `https://app.sanctum.42nights.dev` (the app, `/api/v1`, `/mcp`, `/.well-known/*`, `/auth/*`, `/invite/*`) and the apex `https://sanctum.42nights.dev`; `workers_dev` is off.
On deploy, Cloudflare creates the DNS record and certificate for each custom domain in the `42nights.dev` zone; no DNS record is added by hand.

Expand Down
36 changes: 29 additions & 7 deletions server/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,35 @@ const forDialect = (text: string) =>
fullTextAvailable ? text : text.replace(/\)\s*AS\s+(\w+)\s+ON DUPLICATE KEY UPDATE([\s\S]*)$/i, (_all, alias: string, update: string) =>
`) ON DUPLICATE KEY UPDATE${update.replace(new RegExp(`\\b${alias}\\.(\`?\\w+\`?)`, 'g'), 'VALUES($1)')}`);

const translated = (c: SqlConnection.Connection): SqlConnection.Connection => ({
execute: (sql, params, transform) => c.execute(forDialect(sql), params, transform),
executeRaw: (sql, params) => c.executeRaw(forDialect(sql), params),
executeValues: (sql, params) => c.executeValues(forDialect(sql), params),
executeUnprepared: (sql, params, transform) => c.executeUnprepared(forDialect(sql), params, transform),
executeStream: (sql, params, transform) => c.executeStream(forDialect(sql), params, transform),
});
/**
* TiDB rejects a prepared `LIMIT ?` / `OFFSET ?` ("Incorrect arguments to LIMIT"); a safe integer bound there is
* written into the text instead (it cannot inject) and leaves the parameter list.
*/
const inlineBounds = (text: string, params: ReadonlyArray<unknown>): readonly [string, ReadonlyArray<unknown>] => {
if (fullTextAvailable) return [text, params];
const rest: Array<unknown> = [];
let index = 0;
const inlined = text.replace(/\b(LIMIT|OFFSET)\s+\?|\?/gi, (placeholder, keyword?: string) => {
const value = params[index++];
if (keyword === undefined || !Number.isSafeInteger(value) || (value as number) < 0) {
rest.push(value);
return placeholder;
}
return `${keyword} ${value}`;
});
return [inlined, rest];
};

const translated = (c: SqlConnection.Connection): SqlConnection.Connection => {
const prepare = <A>(run: (sql: string, params: ReadonlyArray<unknown>) => A) => (sql: string, params: ReadonlyArray<unknown>) => run(...inlineBounds(forDialect(sql), params));
return {
execute: (sql, params, transform) => prepare((text, rest) => c.execute(text, rest, transform))(sql, params),
executeRaw: prepare((text, rest) => c.executeRaw(text, rest)),
executeValues: prepare((text, rest) => c.executeValues(text, rest)),
executeUnprepared: (sql, params, transform) => prepare((text, rest) => c.executeUnprepared(text, rest, transform))(sql, params),
executeStream: (sql, params, transform) => prepare((text, rest) => c.executeStream(text, rest, transform))(sql, params),
};
};

/**
* The pool cannot drop a waiter: mysql2 keeps the queued callback of a cancelled request, and
Expand Down
4 changes: 3 additions & 1 deletion server/tests/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ vi.mock('../src/integrations.ts', async importOriginal => {

beforeEach(() => provider.reset());

/** 300 ms on a local server; ~12 statements run before the submission is recorded, so each round trip adds to the budget (33 ms median to TiDB Cloud). */
const CEILING_MS = 3_000;
const SEND = 'gmail-send-email';

const request = (overrides: Partial<Parameters<typeof requestAction>[1]> = {}) => ({
Expand Down Expand Up @@ -253,7 +255,7 @@ describe('action gateway', () => {
provider.mode = 'hold';
const queued = yield* requestAction(agent, request());
const worker = yield* Effect.fork(
runWorker({ 'action.execute': executeAction }, { leaseMs: 3_000, pollMs: 50, concurrency: 1, ceilingMs: 300 }).pipe(Effect.provide(actionServices)),
runWorker({ 'action.execute': executeAction }, { leaseMs: 3_000, pollMs: 50, concurrency: 1, ceilingMs: CEILING_MS }).pipe(Effect.provide(actionServices)),
);
const poll = <A>(check: Effect.Effect<A, unknown, SqlClient.SqlClient>, done: (value: A) => boolean) =>
check.pipe(Effect.filterOrFail(done, () => 'not yet'), Effect.retry(Schedule.spaced('50 millis')), Effect.timeout('10 seconds'));
Expand Down
3 changes: 2 additions & 1 deletion server/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export default defineProject({
name: 'server',
include: ['tests/**/*.test.ts'],
globalSetup: ['tests/support/mysql-server.ts'],
testTimeout: 30_000,
// A database across the internet (TiDB Cloud, ~33 ms per statement) needs 50-110 s to migrate one test database.
testTimeout: Number(process.env.SANCTUM_TEST_TIMEOUT_MS ?? 30_000),
hookTimeout: 120_000,
},
});
Loading