firstly 0.10.6 → 0.10.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # firstly
2
2
 
3
+ ## 0.10.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [#393](https://github.com/jycouet/firstly/pull/393) [`37feeb8`](https://github.com/jycouet/firstly/commit/37feeb820a2a7867a449d14181e3be9f72df3ec5) Thanks [@jycouet](https://github.com/jycouet)! - Dependencies are `^` ranges instead of exact pins, so apps share one copy of each with their own deps. `tailwindcss` is no longer a dependency (firstly never imports it). `vite-plugin-kit-routes` 1.1.1 and `vite-plugin-stripper` 0.10.6 publish ranges too, so `@kitql/*` and `esrap` dedupe as well.
8
+
9
+ ## 0.10.7
10
+
11
+ ### Patch Changes
12
+
13
+ - [#388](https://github.com/jycouet/firstly/pull/388) [`7eca18a`](https://github.com/jycouet/firstly/commit/7eca18af4a55e2481dc81d38ba9e4f7f0d0f01a0) Thanks [@jycouet](https://github.com/jycouet)! - sqlAdmin: reads now run as a read-only Postgres role upserted at boot (`readPool`, default `'auto'`, `false` to opt out, or your own pool); `tokens.pool` is removed. SQL and results now travel encoded (`ffsql1:`) from `<SqlAdmin />` and `ff-sql`, so WAF rules stop blocking them; plain SQL still works.
14
+
3
15
  ## 0.10.6
4
16
 
5
17
  ### Patch Changes
package/bin/args.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  /**
2
2
  * @param {string[]} argv
3
- * @returns {{ help: boolean, json: boolean, origin?: string, apiPath?: string, sql: string, error?: string }}
3
+ * @returns {{ help: boolean, json: boolean, raw: boolean, origin?: string, apiPath?: string, sql: string, error?: string }}
4
4
  */
5
5
  export function parseArgs(argv: string[]): {
6
6
  help: boolean;
7
7
  json: boolean;
8
+ raw: boolean;
8
9
  origin?: string;
9
10
  apiPath?: string;
10
11
  sql: string;
package/bin/args.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // the cli. Plain JS like its caller: bin/ ships unbuilt.
3
3
 
4
4
  /** Flags that take no value. */
5
- const BOOLEAN = ['json', 'help']
5
+ const BOOLEAN = ['json', 'help', 'raw']
6
6
  /** Flags that need one, as `--x=y` or `--x y`. */
7
7
  const VALUED = ['origin', 'api-path']
8
8
 
@@ -13,11 +13,11 @@ const FLAG = /^--[a-z][\w-]*(=|$)/
13
13
 
14
14
  /**
15
15
  * @param {string[]} argv
16
- * @returns {{ help: boolean, json: boolean, origin?: string, apiPath?: string, sql: string, error?: string }}
16
+ * @returns {{ help: boolean, json: boolean, raw: boolean, origin?: string, apiPath?: string, sql: string, error?: string }}
17
17
  */
18
18
  export function parseArgs(argv) {
19
19
  /** @type {any} */
20
- const out = { help: false, json: false, sql: '' }
20
+ const out = { help: false, json: false, raw: false, sql: '' }
21
21
  const positional = []
22
22
  const seen = new Set()
23
23
 
package/bin/ff-sql.js CHANGED
@@ -15,6 +15,7 @@ const HELP = `ff-sql - run SQL through a firstly sql token
15
15
  SQL
16
16
 
17
17
  --json raw JSON instead of a table
18
+ --raw send plain SQL, not ffsql1: (servers older than the encoding)
18
19
  -- end of flags, everything after is SQL
19
20
  --origin=URL the app to query (or FF_SQL_ORIGIN)
20
21
  --api-path=P remult api root (default /api)
@@ -27,6 +28,13 @@ camelCase and need double quotes: "createdAt", not created_at. A heredoc avoids
27
28
  fighting the shell over single quotes, and keeps the token out of the line.`
28
29
 
29
30
  const TRAILING_SLASH = /\/$/
31
+ // Same scheme as `sqlWire.ts`: keeps WAF rules from matching SQL in transit.
32
+ const WIRE = 'ffsql1:'
33
+ const encode = (s) => WIRE + Buffer.from(s, 'utf8').toString('base64')
34
+ const decode = (s) =>
35
+ typeof s === 'string' && s.startsWith(WIRE)
36
+ ? Buffer.from(s.slice(WIRE.length), 'base64').toString('utf8')
37
+ : s
30
38
 
31
39
  const cli = parseArgs(process.argv.slice(2))
32
40
 
@@ -65,7 +73,7 @@ try {
65
73
  res = await fetch(`${origin.replace(TRAILING_SLASH, '')}${apiPath}/ff/sqlAdmin/exec`, {
66
74
  method: 'POST',
67
75
  headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
68
- body: JSON.stringify({ args: [cmd] }),
76
+ body: JSON.stringify({ args: [cli.raw ? cmd : encode(cmd)] }),
69
77
  })
70
78
  } catch (err) {
71
79
  console.error(`${origin} unreachable: ${err.message}`)
@@ -75,11 +83,13 @@ try {
75
83
  const body = await res.json().catch(() => null)
76
84
  if (!res.ok) {
77
85
  // 403 here is almost always a dead token: expired, revoked, or minted elsewhere.
78
- console.error(`${res.status} ${body?.message ?? res.statusText}`)
86
+ console.error(`${res.status} ${decode(body?.message) ?? res.statusText}`)
79
87
  process.exit(1)
80
88
  }
81
89
 
82
- const { rows, rowCount, took } = body.data ?? body
90
+ let data = body.data ?? body
91
+ if (typeof data === 'string') data = JSON.parse(decode(data))
92
+ const { rows, rowCount, took } = data
83
93
  // Table and rows on stdout, the summary on stderr, so `| jq` and `> file` stay clean.
84
94
  const out = new Console(process.stdout)
85
95
  if (cli.json) out.log(JSON.stringify(rows, null, 2))
@@ -47,17 +47,19 @@ export type SqlTokensOptions = {
47
47
  apiPath?: string;
48
48
  /** @default 30 */
49
49
  callLogRetentionDays?: number;
50
- /** Override the pool used by `read` tokens. Defaults to the Postgres pool behind the data provider. */
51
- pool?: SqlTokenPool;
52
50
  };
53
51
  export declare const SERVER_ONLY = "sqlAdmin: server-only";
54
52
  export declare const SQL_ADMINS: ("FF_Role.Admin" | "SqlAdmin.Admin")[];
53
+ export declare function getDb(): SqlDatabase;
54
+ /** The pg pool remult wraps, if any. `_getSourceSql` is internal but has been stable since remult 1. */
55
+ export declare function poolFrom(db: SqlDatabase): SqlTokenPool | undefined;
55
56
  export declare class SqlAdminController {
56
57
  /** Optional override set by the `sqlAdmin()` module's `initApi`. Falls back to `SqlDatabase.getDb()`. */
57
58
  static dp?: SqlDatabase;
58
59
  /** Set by the `sqlAdmin()` module's `initApi`. */
59
60
  static options: {
60
61
  tokens?: SqlTokensOptions;
62
+ readPool?: SqlTokenPool;
61
63
  };
62
64
  /**
63
65
  * @param cmd SQL to run.
@@ -67,8 +69,11 @@ export declare class SqlAdminController {
67
69
  * want to mutate - the UI gates this behind an explicit checkbox.
68
70
  *
69
71
  * With a sql token the token decides, not the caller.
72
+ *
73
+ * A `cmd` sent through `encodeSqlWire` gets its result (or error message)
74
+ * back encoded the same way, as a string; plain SQL gets plain JSON.
70
75
  */
71
- static exec(cmd: string, capabilities?: SqlCapability[]): Promise<SqlResult>;
76
+ static exec(cmd: string, capabilities?: SqlCapability[]): Promise<SqlResult | string>;
72
77
  private static run;
73
78
  private static execAsToken;
74
79
  /**
@@ -5,21 +5,25 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { BackendMethod, remult, repo, SqlDatabase } from 'remult';
8
+ import { Log } from '@kitql/helpers';
8
9
  import { FF_Role } from '../core/common';
9
10
  import { Roles_SqlAdmin } from './Roles_SqlAdmin';
10
11
  import { SQL_CAPABILITIES, SQL_TOKEN_TTLS, SqlToken, SqlTokenCall, } from './sqlTokenEntities';
12
+ import { decodeSqlWire, encodeSqlWire, isSqlWire } from './sqlWire';
11
13
  // Thrown when a server-only body runs on the client (its `import.meta.env.SSR` block is gone there).
12
14
  export const SERVER_ONLY = 'sqlAdmin: server-only';
13
15
  export const SQL_ADMINS = [Roles_SqlAdmin.SqlAdmin_Admin, FF_Role.FF_Role_Admin];
14
16
  const CALL_LOG_CMD_MAX = 4000;
17
+ // Not `log` from '../index': that barrel pulls the svelte components into the server.
18
+ const log = new Log('sqlAdmin');
15
19
  /** Retention is a housekeeping sweep, not something to pay for on every query. */
16
20
  const CALL_LOG_SWEEP_EVERY_MS = 3_600_000;
17
21
  let lastCallLogSweep = 0;
18
- function getDb() {
22
+ export function getDb() {
19
23
  return SqlAdminController.dp ?? SqlDatabase.getDb();
20
24
  }
21
25
  /** The pg pool remult wraps, if any. `_getSourceSql` is internal but has been stable since remult 1. */
22
- function poolFrom(db) {
26
+ export function poolFrom(db) {
23
27
  const pool = db._getSourceSql?.()?.pool;
24
28
  return typeof pool?.connect === 'function' ? pool : undefined;
25
29
  }
@@ -36,12 +40,35 @@ export class SqlAdminController {
36
40
  * want to mutate - the UI gates this behind an explicit checkbox.
37
41
  *
38
42
  * With a sql token the token decides, not the caller.
43
+ *
44
+ * A `cmd` sent through `encodeSqlWire` gets its result (or error message)
45
+ * back encoded the same way, as a string; plain SQL gets plain JSON.
39
46
  */
40
47
  static async exec(cmd, capabilities = ['read']) {
41
- const token = remult.context.sqlToken;
42
- if (token)
43
- return SqlAdminController.execAsToken(token, cmd);
44
- return SqlAdminController.run(cmd, capabilities);
48
+ const wire = isSqlWire(cmd);
49
+ if (wire)
50
+ cmd = decodeSqlWire(cmd);
51
+ try {
52
+ const token = remult.context.sqlToken;
53
+ const res = token
54
+ ? await SqlAdminController.execAsToken(token, cmd)
55
+ : await SqlAdminController.run(cmd, capabilities);
56
+ return wire ? encodeSqlWire(JSON.stringify(res)) : res;
57
+ }
58
+ catch (err) {
59
+ if (!wire)
60
+ throw err;
61
+ const msg = err instanceof Error ? err.message : String(err);
62
+ // remult logs the message it sends, so the clear one goes to the server log here.
63
+ log.error(msg);
64
+ // No stack: it repeats the message in clear. Status flags kept, or a 403 turns into a 400.
65
+ const { httpStatusCode, isForbiddenError } = (err ?? {});
66
+ throw Object.assign(new Error(encodeSqlWire(msg)), {
67
+ stack: undefined,
68
+ httpStatusCode,
69
+ isForbiddenError,
70
+ });
71
+ }
45
72
  }
46
73
  static async run(cmd, capabilities) {
47
74
  if (import.meta.env.SSR) {
@@ -54,7 +81,8 @@ export class SqlAdminController {
54
81
  return { rows, rowCount: rows.length, took: performance.now() - start };
55
82
  }
56
83
  const { readOnlySql } = await import('./server/readOnlySql');
57
- return await readOnlySql(db, SqlAdminController.options.tokens?.pool ?? poolFrom(db), cmd);
84
+ const o = SqlAdminController.options;
85
+ return await readOnlySql(db, o.readPool ?? poolFrom(db), cmd);
58
86
  }
59
87
  catch (err) {
60
88
  const { enrichSqlError } = await import('./server/sqlError');
@@ -3,6 +3,7 @@ export { Roles_SqlAdmin } from './Roles_SqlAdmin';
3
3
  export { SqlAdminController, type SqlResult, type SqlTokensOptions } from './SqlAdminController';
4
4
  export { SqlDriftController, type SqlDriftFlag } from './SqlDriftController';
5
5
  export { SQL_CAPABILITIES, SQL_TOKEN_TTLS, SqlToken, SqlTokenCall, type SqlCapability, type SqlTokenTtl, } from './sqlTokenEntities';
6
+ export { decodeSqlWire, encodeSqlWire, isSqlWire, SQL_WIRE_PREFIX } from './sqlWire';
6
7
  export { default as SqlAdmin } from './ui/SqlAdmin.svelte';
7
8
  export { default as SqlDrift } from './ui/SqlDrift.svelte';
8
9
  export { default as SqlTokens } from './ui/SqlTokens.svelte';
@@ -3,6 +3,7 @@ export { Roles_SqlAdmin } from './Roles_SqlAdmin';
3
3
  export { SqlAdminController } from './SqlAdminController';
4
4
  export { SqlDriftController } from './SqlDriftController';
5
5
  export { SQL_CAPABILITIES, SQL_TOKEN_TTLS, SqlToken, SqlTokenCall, } from './sqlTokenEntities';
6
+ export { decodeSqlWire, encodeSqlWire, isSqlWire, SQL_WIRE_PREFIX } from './sqlWire';
6
7
  export { default as SqlAdmin } from './ui/SqlAdmin.svelte';
7
8
  export { default as SqlDrift } from './ui/SqlDrift.svelte';
8
9
  export { default as SqlTokens } from './ui/SqlTokens.svelte';
@@ -1,6 +1,6 @@
1
1
  import { type ClassType, type SqlDatabase } from 'remult';
2
2
  import { Module } from 'remult/server';
3
- import { type SqlTokensOptions } from '../SqlAdminController';
3
+ import { type SqlTokenPool, type SqlTokensOptions } from '../SqlAdminController';
4
4
  import { type SqlDriftOptions } from '../SqlDriftController';
5
5
  export type { SqlTokensOptions, SqlTokenPool } from '../SqlAdminController';
6
6
  export type { SqlDriftFlag, SqlDriftOptions } from '../SqlDriftController';
@@ -33,6 +33,16 @@ export type SqlAdminOptions = {
33
33
  * @default true
34
34
  */
35
35
  sqlAdmin?: boolean;
36
+ /**
37
+ * Run `read` queries (console and tokens) as a read-only role instead of the
38
+ * app role. `'auto'` upserts `ff_readonly_<db>` at boot through the app's own
39
+ * pool (needs CREATEROLE), or pass a pool you connected yourself. `false`, or a
40
+ * database other than Postgres, leaves reads to the READ ONLY transaction alone.
41
+ *
42
+ * @default 'auto'
43
+ * @example readPool: new pg.Pool({ connectionString: env.DATABASE_URL_READONLY, max: 2 })
44
+ */
45
+ readPool?: 'auto' | false | SqlTokenPool;
36
46
  /**
37
47
  * Bearer tokens that can run SQL from outside a browser session (a script,
38
48
  * an AI on a dev machine). Nothing is registered unless set - no entities,
@@ -2,7 +2,7 @@ import { remult, repo } from 'remult';
2
2
  import { Module } from 'remult/server';
3
3
  import { yellow } from '@kitql/helpers';
4
4
  import { log } from '..';
5
- import { SqlAdminController } from '../SqlAdminController';
5
+ import { getDb, poolFrom, SqlAdminController, } from '../SqlAdminController';
6
6
  import { SqlDriftController } from '../SqlDriftController';
7
7
  import { isSqlTokenLive, SqlToken, sqlTokenEntities } from '../sqlTokenEntities';
8
8
  import { hashToken } from './token';
@@ -30,6 +30,27 @@ function readPathname(req) {
30
30
  return '';
31
31
  }
32
32
  }
33
+ async function resolveReadPool(opt = 'auto') {
34
+ if (opt !== 'auto')
35
+ return opt || undefined;
36
+ const app = poolFrom(getDb());
37
+ // Not Postgres (or a custom `dp`): nothing to create, the transaction guard applies.
38
+ if (!app)
39
+ return undefined;
40
+ try {
41
+ const { ensureReadOnlyPool } = await import('./readOnlyRole');
42
+ const ro = await ensureReadOnlyPool(app);
43
+ if (ro.updated)
44
+ log.info(`reads run as ${yellow(ro.role)} (role created or updated).`);
45
+ return ro.pool;
46
+ }
47
+ catch (err) {
48
+ // Boot must survive: the READ ONLY transaction still guards reads.
49
+ const msg = err instanceof Error ? err.message : String(err);
50
+ log.error(`readPool: 'auto' failed (${msg}) - reads use the app role. ${yellow('readPool: false')} silences this.`);
51
+ return undefined;
52
+ }
53
+ }
33
54
  /**
34
55
  * Drop-in SQL admin endpoint + companion `<SqlAdmin />` component (`firstly/sqlAdmin`).
35
56
  *
@@ -76,7 +97,10 @@ export const sqlAdmin = (opts) => {
76
97
  SqlAdminController.dp = await opts.dp();
77
98
  SqlDriftController.dp = SqlAdminController.dp;
78
99
  }
79
- SqlAdminController.options = { tokens };
100
+ SqlAdminController.options = {
101
+ tokens,
102
+ readPool: enabled ? await resolveReadPool(opts?.readPool) : undefined,
103
+ };
80
104
  SqlDriftController.options = driftOn ? drift : undefined;
81
105
  if (enabled)
82
106
  log.info(`AI Hint: visit ${yellow(path)} to query raw SQL.`);
@@ -0,0 +1,15 @@
1
+ import type { SqlTokenPool } from '../SqlAdminController';
2
+ export declare const readOnlyRoleName: (db: string) => string;
3
+ /**
4
+ * Returns a pool connected as a login role that can only SELECT in this
5
+ * database. Per-database grants rather than `pg_read_all_data`: that one
6
+ * reaches every database of the cluster. A boot where the role is already
7
+ * right costs one login and one catalog query; anything off (new schema,
8
+ * rotated app password, missing grant) re-runs the full setup. Needs
9
+ * CREATEROLE on the app role.
10
+ */
11
+ export declare function ensureReadOnlyPool(app: SqlTokenPool): Promise<{
12
+ pool: SqlTokenPool;
13
+ role: string;
14
+ updated: boolean;
15
+ }>;
@@ -0,0 +1,123 @@
1
+ import { createHmac, randomBytes } from 'node:crypto';
2
+ const NON_IDENT = /[^a-z0-9_]/g;
3
+ const MAX_IDENT = 63;
4
+ const DUPLICATE_OBJECT = '42710';
5
+ const STATEMENT_TIMEOUT = '30s';
6
+ /** Schemas the app can grant on: it owns them, or is a member of their owner. */
7
+ const GRANTABLE_SCHEMA = `n.nspname !~ '^pg_' and n.nspname <> 'information_schema' and pg_has_role(n.nspowner, 'USAGE')`;
8
+ /** One round trip: is the role still exactly what setup would leave behind? */
9
+ const STATE_SQL = `
10
+ select
11
+ r.rolsuper or r.rolcreaterole or r.rolcreatedb or r.rolreplication or r.rolbypassrls as extra,
12
+ coalesce(r.rolconfig @> array['default_transaction_read_only=on', 'statement_timeout=${STATEMENT_TIMEOUT}'], false)
13
+ and not exists (
14
+ select 1 from pg_namespace n
15
+ where ${GRANTABLE_SCHEMA}
16
+ and (not has_schema_privilege(r.oid, n.oid, 'USAGE')
17
+ or not exists (
18
+ select 1 from pg_default_acl d, aclexplode(d.defaclacl) a
19
+ where d.defaclnamespace = n.oid and d.defaclobjtype = 'r'
20
+ and d.defaclrole = (select oid from pg_roles where rolname = current_user)
21
+ and a.grantee = r.oid and a.privilege_type = 'SELECT')))
22
+ and not exists (
23
+ select 1 from pg_class c join pg_namespace n on n.oid = c.relnamespace
24
+ where ${GRANTABLE_SCHEMA} and c.relkind in ('r', 'v', 'm', 'p', 'f')
25
+ and pg_has_role(c.relowner, 'USAGE')
26
+ and not has_table_privilege(r.oid, c.oid, 'SELECT')) as ready
27
+ from pg_roles r where r.rolname = $1`;
28
+ const quote = (s) => `"${s.replaceAll('"', '""')}"`;
29
+ export const readOnlyRoleName = (db) => `ff_readonly_${db.toLowerCase().replace(NON_IDENT, '_')}`.slice(0, MAX_IDENT);
30
+ /** The app's own password: every instance derives the same role password, nothing to store. */
31
+ function appSecret(options) {
32
+ if (options.connectionString) {
33
+ const url = new URL(options.connectionString);
34
+ const pw = url.searchParams.get('password') || decodeURIComponent(url.password);
35
+ if (pw)
36
+ return pw;
37
+ }
38
+ if (typeof options.password === 'string' && options.password)
39
+ return options.password;
40
+ return process.env.PGPASSWORD || undefined;
41
+ }
42
+ function withCredentials(options, user, password) {
43
+ const { connectionString, ...rest } = options;
44
+ // pg lets the connection string win over `user`/`password`, so rewrite the URL itself.
45
+ // Query params, not userinfo: a hostless URL (unix socket) silently drops userinfo, and pg reads params first.
46
+ if (connectionString) {
47
+ const url = new URL(connectionString);
48
+ url.username = '';
49
+ url.password = '';
50
+ url.searchParams.set('user', user);
51
+ url.searchParams.set('password', password);
52
+ return { ...rest, connectionString: url.toString(), max: 2 };
53
+ }
54
+ return { ...rest, user, password, max: 2 };
55
+ }
56
+ /**
57
+ * Returns a pool connected as a login role that can only SELECT in this
58
+ * database. Per-database grants rather than `pg_read_all_data`: that one
59
+ * reaches every database of the cluster. A boot where the role is already
60
+ * right costs one login and one catalog query; anything off (new schema,
61
+ * rotated app password, missing grant) re-runs the full setup. Needs
62
+ * CREATEROLE on the app role.
63
+ */
64
+ export async function ensureReadOnlyPool(app) {
65
+ const pool = app;
66
+ if (typeof pool.query !== 'function' || !pool.options)
67
+ throw new Error('not a pg pool');
68
+ const { rows } = await pool.query('select current_database() as db');
69
+ const db = rows[0].db;
70
+ const role = readOnlyRoleName(db);
71
+ const secret = appSecret(pool.options);
72
+ const password = secret
73
+ ? createHmac('sha256', secret).update(role).digest('hex')
74
+ : randomBytes(32).toString('hex');
75
+ const Pool = pool.constructor;
76
+ const ro = new Pool(withCredentials(pool.options, role, password));
77
+ const state = await readState(pool, ro, role);
78
+ // A role with the same name made by someone else must not become our reader.
79
+ if (state?.extra)
80
+ throw new Error(`role ${role} has more than read rights, refusing it`);
81
+ if (state?.ready)
82
+ return { pool: ro, role, updated: false };
83
+ await setup(pool, db, role, password);
84
+ // Handing back a pool that cannot log in would break every read instead of falling back.
85
+ if (!(await connectsAs(ro, role))) {
86
+ await ro.end?.().catch(() => { });
87
+ throw new Error(`role ${role} was set up but cannot log in (pg_hba, pooler user naming?)`);
88
+ }
89
+ return { pool: ro, role, updated: true };
90
+ }
91
+ async function readState(app, ro, role) {
92
+ const row = (await app.query(STATE_SQL, [role])).rows[0];
93
+ if (!row)
94
+ return undefined;
95
+ // Logging in proves the derived password still matches (the app's may have rotated).
96
+ const loggedIn = !row.extra && (await connectsAs(ro, role));
97
+ return { extra: row.extra, ready: loggedIn && row.ready === true };
98
+ }
99
+ /** As `role`, not merely connected: a dropped credential would log in as the app. */
100
+ const connectsAs = (ro, role) => ro.query('select current_user as u').then((r) => r.rows[0]?.u === role, () => false);
101
+ async function setup(pool, db, role, password) {
102
+ const r = quote(role);
103
+ try {
104
+ await pool.query(`CREATE ROLE ${r} LOGIN`);
105
+ }
106
+ catch (err) {
107
+ if (err?.code !== DUPLICATE_OBJECT)
108
+ throw err;
109
+ }
110
+ // Only a superuser may set NOSUPERUSER & co, hence the `extra` check instead.
111
+ await pool.query(`ALTER ROLE ${r} WITH LOGIN PASSWORD '${password}'`);
112
+ await pool.query(`ALTER ROLE ${r} SET default_transaction_read_only = on`);
113
+ await pool.query(`ALTER ROLE ${r} SET statement_timeout = '${STATEMENT_TIMEOUT}'`);
114
+ await pool.query(`GRANT CONNECT ON DATABASE ${quote(db)} TO ${r}`);
115
+ const schemas = await pool.query(`select n.nspname from pg_namespace n where ${GRANTABLE_SCHEMA}`);
116
+ for (const { nspname } of schemas.rows) {
117
+ const s = quote(nspname);
118
+ await pool.query(`GRANT USAGE ON SCHEMA ${s} TO ${r}`);
119
+ await pool.query(`GRANT SELECT ON ALL TABLES IN SCHEMA ${s} TO ${r}`);
120
+ // Tables the app creates later (remult ensureSchema, migrations).
121
+ await pool.query(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${s} GRANT SELECT ON TABLES TO ${r}`);
122
+ }
123
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Reversible wire encoding for SQL and its results. Not a secret (TLS + roles
3
+ * are the security): it only keeps WAF rule sets from pattern-matching
4
+ * `SELECT ... FROM` or Postgres error text in requests and responses.
5
+ * The version suffix leaves room for another scheme if a WAF learns to decode this one.
6
+ */
7
+ export declare const SQL_WIRE_PREFIX = "ffsql1:";
8
+ export declare const isSqlWire: (s: unknown) => s is string;
9
+ export declare function encodeSqlWire(s: string): string;
10
+ /** Anything without the prefix passes through: plain SQL keeps working. */
11
+ export declare function decodeSqlWire(s: string): string;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Reversible wire encoding for SQL and its results. Not a secret (TLS + roles
3
+ * are the security): it only keeps WAF rule sets from pattern-matching
4
+ * `SELECT ... FROM` or Postgres error text in requests and responses.
5
+ * The version suffix leaves room for another scheme if a WAF learns to decode this one.
6
+ */
7
+ export const SQL_WIRE_PREFIX = 'ffsql1:';
8
+ /** Spreading a huge array into fromCharCode overflows the stack. */
9
+ const CHUNK = 0x8000;
10
+ export const isSqlWire = (s) => typeof s === 'string' && s.startsWith(SQL_WIRE_PREFIX);
11
+ export function encodeSqlWire(s) {
12
+ const bytes = new TextEncoder().encode(s);
13
+ let bin = '';
14
+ for (let i = 0; i < bytes.length; i += CHUNK) {
15
+ bin += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
16
+ }
17
+ return SQL_WIRE_PREFIX + btoa(bin);
18
+ }
19
+ /** Anything without the prefix passes through: plain SQL keeps working. */
20
+ export function decodeSqlWire(s) {
21
+ if (!isSqlWire(s))
22
+ return s;
23
+ const bin = atob(s.slice(SQL_WIRE_PREFIX.length));
24
+ const bytes = new Uint8Array(bin.length);
25
+ for (let i = 0; i < bin.length; i++)
26
+ bytes[i] = bin.charCodeAt(i);
27
+ return new TextDecoder().decode(bytes);
28
+ }
@@ -11,14 +11,15 @@
11
11
  * can read them with `list_console_messages`.
12
12
  */
13
13
  import { log } from '../index'
14
- import { SqlAdminController } from '../SqlAdminController'
14
+ import { SqlAdminController, type SqlResult } from '../SqlAdminController'
15
+ import { decodeSqlWire, encodeSqlWire } from '../sqlWire'
15
16
 
16
17
  const defaultQuery = `SELECT *
17
18
  FROM "public"."users"
18
19
  LIMIT 10`
19
20
 
20
21
  let sqlInput = $state(defaultQuery)
21
- let result: { rows: any[]; rowCount: number; took: number } | undefined = $state()
22
+ let result: SqlResult | undefined = $state()
22
23
  let error = $state('')
23
24
  let isLoading = $state(false)
24
25
  let allowWrites = $state(false)
@@ -64,10 +65,19 @@ ORDER BY tablename, indexname`,
64
65
  try {
65
66
  error = ''
66
67
  isLoading = true
67
- result = await SqlAdminController.exec(sqlInput, allowWrites ? ['read', 'write'] : ['read'])
68
- log.info('for AI:', JSON.stringify(result.rows))
69
- log.info('for humans:', result)
70
- } catch (e) {
68
+ // Encoded both ways: WAF rules block SQL text in requests and Postgres errors in responses.
69
+ const res = await SqlAdminController.exec(
70
+ encodeSqlWire(sqlInput),
71
+ allowWrites ? ['read', 'write'] : ['read'],
72
+ )
73
+ const r: SqlResult = typeof res === 'string' ? JSON.parse(decodeSqlWire(res)) : res
74
+ result = r
75
+ log.info('for AI:', JSON.stringify(r.rows))
76
+ log.info('for humans:', r)
77
+ } catch (e: any) {
78
+ if (typeof e?.message === 'string') e.message = decodeSqlWire(e.message)
79
+ // A stale table under a new error reads as this query's result.
80
+ result = undefined
71
81
  error = JSON.stringify(e, null, 2)
72
82
  } finally {
73
83
  isLoading = false
@@ -171,13 +181,6 @@ ORDER BY tablename, indexname`,
171
181
  Allow writes
172
182
  </label>
173
183
 
174
- {#if error}
175
- <pre
176
- class="flex-1 overflow-auto border border-destructive bg-destructive/10 p-3 text-sm text-destructive">{error.replaceAll(
177
- '\\n',
178
- '\n',
179
- )}</pre>
180
- {/if}
181
184
  {#if result}
182
185
  <div
183
186
  class="flex flex-1 items-center justify-between gap-3 border border-border bg-muted p-3 text-sm text-muted-foreground"
@@ -187,6 +190,13 @@ ORDER BY tablename, indexname`,
187
190
  </div>
188
191
  {/if}
189
192
  </div>
193
+ {#if error}
194
+ <pre
195
+ class="overflow-auto border border-destructive bg-destructive/10 p-3 text-sm text-destructive">{error.replaceAll(
196
+ '\\n',
197
+ '\n',
198
+ )}</pre>
199
+ {/if}
190
200
  </form>
191
201
  {#if result}
192
202
  {#if result.rows && result.rows.length > 0}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "firstly",
3
- "version": "0.10.6",
3
+ "version": "0.10.8",
4
4
  "type": "module",
5
5
  "description": "Firstly, an opinionated Remult setup!",
6
6
  "bin": {
@@ -34,19 +34,18 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@kitql/helpers": "0.8.15",
38
- "@layerstack/utils": "1.0.0",
39
- "@mdi/js": "7.4.47",
40
- "@types/nodemailer": "8.0.0",
41
- "clsx": "2.1.1",
42
- "cron": "4.4.0",
43
- "esm-env": "1.2.2",
44
- "nodemailer": "10.0.10",
45
- "svelte-sonner": "1.2.1",
46
- "tailwind-merge": "3.7.0",
47
- "tailwindcss": "4.3.0",
48
- "vite-plugin-kit-routes": "1.0.6",
49
- "vite-plugin-stripper": "0.10.4"
37
+ "@kitql/helpers": "^0.8.15",
38
+ "@layerstack/utils": "^1.0.0",
39
+ "@mdi/js": "^7.4.47",
40
+ "@types/nodemailer": "^8.0.0",
41
+ "clsx": "^2.1.1",
42
+ "cron": "^4.4.0",
43
+ "esm-env": "^1.2.2",
44
+ "nodemailer": "^10.0.10",
45
+ "svelte-sonner": "^1.2.1",
46
+ "tailwind-merge": "^3.7.0",
47
+ "vite-plugin-kit-routes": "^1.1.1",
48
+ "vite-plugin-stripper": "^0.10.6"
50
49
  },
51
50
  "sideEffects": false,
52
51
  "exports": {