@zhin.js/core 1.6.0 → 1.7.0
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/lib/built/login-assist.d.ts +317 -14
- package/lib/built/login-assist.js +1139 -64
- package/lib/built/login-captcha.d.ts +62 -0
- package/lib/built/login-captcha.js +107 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +24 -1
- package/lib/plugin-runtime/im/endpoint-runtime.js +53 -0
- package/lib/plugin-runtime/im/host/database-config.js +38 -0
- package/lib/plugin-runtime/im/host/database-console-schema.d.ts +79 -0
- package/lib/plugin-runtime/im/host/database-console-schema.js +477 -0
- package/lib/plugin-runtime/im/host/database-console-sql.d.ts +66 -0
- package/lib/plugin-runtime/im/host/database-console-sql.js +459 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +48 -17
- package/lib/plugin-runtime/im/host/database-host-installer.js +770 -46
- package/lib/plugin-runtime/im/im-runtime.js +1 -0
- package/package.json +13 -13
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Statement analysis for the Console SQL editor (`db:query` / `db:execute`).
|
|
3
|
+
*
|
|
4
|
+
* This is a guard rail, not the enforcement. The Host runs every Console statement through a
|
|
5
|
+
* single-statement protocol (PostgreSQL extended protocol, MySQL prepared statements, SQLite
|
|
6
|
+
* `prepare()`), so text this lexer splits differently from the server can still never carry a
|
|
7
|
+
* second statement; read-only statements also run in a read-only transaction (SQLite
|
|
8
|
+
* `query_only`, PostgreSQL `READ ONLY`, MySQL `START TRANSACTION READ ONLY`).
|
|
9
|
+
*/
|
|
10
|
+
import { isGatewayOwnedTable, referencesGatewayOwnedTable } from '@zhin.js/database';
|
|
11
|
+
export class ConsoleSqlRefusal extends Error {
|
|
12
|
+
code;
|
|
13
|
+
constructor(code, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.name = 'ConsoleSqlRefusal';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const WORD = /^[A-Za-z_\u0080-\uffff][A-Za-z0-9_$\u0080-\uffff]*/;
|
|
20
|
+
/** PostgreSQL dollar-quote tag: `$$` or `$name$` (a letter or `_` first, then letters / digits / `_`). */
|
|
21
|
+
const DOLLAR_TAG = /^\$(?:[A-Za-z_\u0080-\uffff][A-Za-z0-9_\u0080-\uffff]*)?\$/;
|
|
22
|
+
/**
|
|
23
|
+
* Splits SQL into statements on top-level `;`, honouring per dialect: strings (`E'…'` backslash
|
|
24
|
+
* escapes on PostgreSQL, backslashes in MySQL strings), quoted names, `--` / `#` / nested
|
|
25
|
+
* PostgreSQL block comments and `$tag$` bodies. Refuses what it cannot analyse safely: PostgreSQL
|
|
26
|
+
* `U&` escaped names and strings, MySQL executable comments (`/*! … *\/`) and optimizer hints.
|
|
27
|
+
*/
|
|
28
|
+
export function splitConsoleSql(sql, dialect) {
|
|
29
|
+
return lexConsoleSql(sql, dialect);
|
|
30
|
+
}
|
|
31
|
+
function lexConsoleSql(sql, dialect, spans) {
|
|
32
|
+
const statements = [];
|
|
33
|
+
let start = 0;
|
|
34
|
+
let code = '';
|
|
35
|
+
let tokens = [];
|
|
36
|
+
let index = 0;
|
|
37
|
+
const flush = (end) => {
|
|
38
|
+
const text = sql.slice(start, end).trim();
|
|
39
|
+
if (text) {
|
|
40
|
+
const identifiers = tokens.filter(token => token.type === 'word' || token.type === 'quoted').map(token => token.value.toLowerCase());
|
|
41
|
+
statements.push(Object.freeze({
|
|
42
|
+
text,
|
|
43
|
+
code: code.toLowerCase().replace(/\s+/g, ' ').trim(),
|
|
44
|
+
identifiers: Object.freeze(identifiers),
|
|
45
|
+
tokens: Object.freeze(tokens),
|
|
46
|
+
functions: Object.freeze(calledFunctions(tokens)),
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
code = '';
|
|
50
|
+
tokens = [];
|
|
51
|
+
};
|
|
52
|
+
const push = (type, value) => tokens.push(Object.freeze({ type, value }));
|
|
53
|
+
while (index < sql.length) {
|
|
54
|
+
const char = sql[index];
|
|
55
|
+
const next = sql[index + 1];
|
|
56
|
+
// MySQL: `--` starts a comment only before whitespace or a control character (`--x` is `- -x`).
|
|
57
|
+
if (char === '-' && next === '-' && (dialect !== 'mysql' || index + 2 >= sql.length || /[\s\u0000-\u001f]/.test(sql[index + 2]))
|
|
58
|
+
|| (dialect === 'mysql' && char === '#')) {
|
|
59
|
+
const end = sql.indexOf('\n', index);
|
|
60
|
+
spans?.push({ kind: 'comment', start: index, end: end === -1 ? sql.length : end });
|
|
61
|
+
index = end === -1 ? sql.length : end;
|
|
62
|
+
code += ' ';
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (char === '/' && next === '*') {
|
|
66
|
+
if (dialect === 'mysql' && (sql[index + 2] === '!' || sql[index + 2] === '+')) {
|
|
67
|
+
throw new ConsoleSqlRefusal('refused_statement', 'MySQL executable comments (/*! … */) and optimizer hints (/*+ … */) are not allowed');
|
|
68
|
+
}
|
|
69
|
+
const end = skipBlockComment(sql, index, dialect === 'pg');
|
|
70
|
+
spans?.push({ kind: 'comment', start: index, end });
|
|
71
|
+
index = end;
|
|
72
|
+
code += ' ';
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (dialect === 'pg' && (char === 'u' || char === 'U') && next === '&' && /['"]/.test(sql[index + 2] ?? '') && !isWordChar(sql[index - 1])) {
|
|
76
|
+
throw new ConsoleSqlRefusal('refused_statement', 'U& escaped names and strings are not allowed from the Console');
|
|
77
|
+
}
|
|
78
|
+
if (char === '\'' || (char === '"' && dialect === 'mysql')) {
|
|
79
|
+
// PostgreSQL E'…' (the `E` was just read as a word) resolves backslash escapes.
|
|
80
|
+
const previous = tokens.at(-1);
|
|
81
|
+
const escapeString = dialect === 'mysql'
|
|
82
|
+
|| (dialect === 'pg' && previous?.type === 'word' && previous.value === 'e' && /[eE]/.test(sql[index - 1] ?? ''));
|
|
83
|
+
if (escapeString && dialect === 'pg') {
|
|
84
|
+
tokens.pop();
|
|
85
|
+
code = code.slice(0, -1);
|
|
86
|
+
}
|
|
87
|
+
const end = skipQuoted(sql, index, char, escapeString);
|
|
88
|
+
spans?.push({ kind: 'string', start: index, end, placeholder: `${char}…${char}` });
|
|
89
|
+
push('string', unquote(sql.slice(index + 1, end - 1), char, escapeString));
|
|
90
|
+
code += ' \'\' ';
|
|
91
|
+
index = end;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (char === '"' || char === '`' || (char === '[' && dialect === 'sqlite')) {
|
|
95
|
+
const close = char === '[' ? ']' : char;
|
|
96
|
+
const end = skipQuoted(sql, index, close, false, char === '[');
|
|
97
|
+
push('quoted', unquote(sql.slice(index + 1, end - 1), close, false));
|
|
98
|
+
code += ' "q" ';
|
|
99
|
+
index = end;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (char === '$' && dialect === 'pg') {
|
|
103
|
+
const tag = DOLLAR_TAG.exec(sql.slice(index, index + 256));
|
|
104
|
+
if (tag) {
|
|
105
|
+
const end = sql.indexOf(tag[0], index + tag[0].length);
|
|
106
|
+
if (end === -1)
|
|
107
|
+
throw new ConsoleSqlRefusal('refused_statement', 'Unterminated dollar-quoted string');
|
|
108
|
+
push('string', sql.slice(index + tag[0].length, end));
|
|
109
|
+
spans?.push({ kind: 'string', start: index, end: end + tag[0].length, placeholder: `${tag[0]}…${tag[0]}` });
|
|
110
|
+
index = end + tag[0].length;
|
|
111
|
+
code += ' \'\' ';
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (char === ';') {
|
|
116
|
+
flush(index);
|
|
117
|
+
index += 1;
|
|
118
|
+
start = index;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const word = WORD.exec(sql.slice(index, index + 256));
|
|
122
|
+
if (word) {
|
|
123
|
+
push('word', word[0].toLowerCase());
|
|
124
|
+
code += word[0];
|
|
125
|
+
index += word[0].length;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!/\s/.test(char))
|
|
129
|
+
push('punct', char);
|
|
130
|
+
code += char;
|
|
131
|
+
index += 1;
|
|
132
|
+
}
|
|
133
|
+
flush(sql.length);
|
|
134
|
+
return statements;
|
|
135
|
+
}
|
|
136
|
+
function isWordChar(char) {
|
|
137
|
+
return char !== undefined && /[A-Za-z0-9_$\u0080-\uffff]/.test(char);
|
|
138
|
+
}
|
|
139
|
+
/** End of a block comment starting at `from`; PostgreSQL nests them (`/* /* *\/ *\/`). */
|
|
140
|
+
function skipBlockComment(sql, from, nested) {
|
|
141
|
+
let depth = 0;
|
|
142
|
+
let index = from;
|
|
143
|
+
while (index < sql.length) {
|
|
144
|
+
if (sql[index] === '/' && sql[index + 1] === '*') {
|
|
145
|
+
if (depth === 0 || nested)
|
|
146
|
+
depth += 1;
|
|
147
|
+
index += 2;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (sql[index] === '*' && sql[index + 1] === '/') {
|
|
151
|
+
depth -= 1;
|
|
152
|
+
index += 2;
|
|
153
|
+
if (depth === 0)
|
|
154
|
+
return index;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
index += 1;
|
|
158
|
+
}
|
|
159
|
+
throw new ConsoleSqlRefusal('refused_statement', 'Unterminated comment');
|
|
160
|
+
}
|
|
161
|
+
function skipQuoted(sql, from, quote, backslash, bracket = false) {
|
|
162
|
+
let index = from + 1;
|
|
163
|
+
while (index < sql.length) {
|
|
164
|
+
const char = sql[index];
|
|
165
|
+
if (backslash && char === '\\') {
|
|
166
|
+
index += 2;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (char === quote) {
|
|
170
|
+
if (!bracket && sql[index + 1] === quote) {
|
|
171
|
+
index += 2;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
return index + 1;
|
|
175
|
+
}
|
|
176
|
+
index += 1;
|
|
177
|
+
}
|
|
178
|
+
throw new ConsoleSqlRefusal('refused_statement', 'Unterminated quoted text');
|
|
179
|
+
}
|
|
180
|
+
/** Body of a quoted token: doubled closing quotes collapsed, backslash escapes reduced to the escaped character. */
|
|
181
|
+
function unquote(body, close, backslash) {
|
|
182
|
+
const collapsed = close === ']' ? body : body.replaceAll(close + close, close);
|
|
183
|
+
return backslash ? collapsed.replace(/\\(.)/gs, '$1') : collapsed;
|
|
184
|
+
}
|
|
185
|
+
/** Last name part of every `name (` call. */
|
|
186
|
+
function calledFunctions(tokens) {
|
|
187
|
+
const names = [];
|
|
188
|
+
for (let index = 0; index + 1 < tokens.length; index += 1) {
|
|
189
|
+
const token = tokens[index];
|
|
190
|
+
const following = tokens[index + 1];
|
|
191
|
+
if ((token.type === 'word' || token.type === 'quoted') && following.type === 'punct' && following.value === '(') {
|
|
192
|
+
names.push(token.value.toLowerCase());
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return names;
|
|
196
|
+
}
|
|
197
|
+
/** SQLite PRAGMAs that only report, with or without a table / index argument. */
|
|
198
|
+
const READ_PRAGMAS = new Set([
|
|
199
|
+
'table_info', 'table_xinfo', 'table_list', 'index_list', 'index_info', 'index_xinfo', 'foreign_key_list',
|
|
200
|
+
'foreign_key_check', 'integrity_check', 'quick_check', 'database_list', 'collation_list', 'compile_options',
|
|
201
|
+
'function_list', 'module_list', 'pragma_list', 'user_version', 'schema_version', 'application_id', 'page_count',
|
|
202
|
+
'page_size', 'freelist_count', 'encoding', 'journal_mode', 'foreign_keys', 'data_version',
|
|
203
|
+
]);
|
|
204
|
+
/**
|
|
205
|
+
* Server functions the Console refuses (read and write SQL, owners included), matched against the
|
|
206
|
+
* called name with quotes and schema removed (`"pg_catalog"."set_config"(…)` is `set_config`):
|
|
207
|
+
* effects a read-only transaction does not stop (backend signals, files, large objects, advisory
|
|
208
|
+
* locks, settings — `set_config` would lift `statement_timeout`), and dynamic SQL that runs a
|
|
209
|
+
* query built at run time (`query_to_xml('select … from con' || 'sole_users', …)`), which no text
|
|
210
|
+
* check of the statement could see.
|
|
211
|
+
*/
|
|
212
|
+
const REFUSED_FUNCTIONS = new Set([
|
|
213
|
+
'set_config', 'pg_terminate_backend', 'pg_cancel_backend', 'pg_reload_conf', 'pg_rotate_logfile', 'pg_promote',
|
|
214
|
+
'pg_switch_wal', 'pg_stat_file', 'pg_create_restore_point', 'pg_import_system_collations', 'pg_notify',
|
|
215
|
+
'pg_start_backup', 'pg_stop_backup', 'pg_backup_start', 'pg_backup_stop', 'loread', 'lowrite',
|
|
216
|
+
'query_to_xml', 'query_to_xmlschema', 'query_to_xml_and_xmlschema', 'cursor_to_xml', 'cursor_to_xmlschema',
|
|
217
|
+
'ts_stat', 'ts_rewrite', 'xpath', 'xpath_exists',
|
|
218
|
+
// MySQL / SQLite
|
|
219
|
+
'load_file', 'sys_exec', 'sys_eval', 'load_extension', 'readfile', 'writefile', 'edit', 'fts3_tokenizer',
|
|
220
|
+
]);
|
|
221
|
+
const REFUSED_FUNCTION_PATTERN = /^(?:dblink\w*|lo_\w+|pg_read_\w+|pg_ls_\w+|pg_file_\w+|pg_(?:try_)?advisory\w*|pg_logical_\w+|pg_\w*replication\w*|pg_\w*_slot\w*|\w*_to_xml\w*|query_to_\w+|xpath\w*)$/;
|
|
222
|
+
export function isRefusedConsoleFunction(name) {
|
|
223
|
+
const bare = name.toLowerCase();
|
|
224
|
+
return REFUSED_FUNCTIONS.has(bare) || REFUSED_FUNCTION_PATTERN.test(bare);
|
|
225
|
+
}
|
|
226
|
+
/** First refused function a statement calls, if any. */
|
|
227
|
+
export function refusedFunctionIn(statement) {
|
|
228
|
+
return statement.functions.find(isRefusedConsoleFunction);
|
|
229
|
+
}
|
|
230
|
+
/** Statements that control the transaction or run SQL text built at run time (both paths refuse them). */
|
|
231
|
+
const TRANSACTION_CONTROL = new Set([
|
|
232
|
+
'begin', 'commit', 'rollback', 'savepoint', 'release', 'start', 'end', 'abort', 'set', 'reset', 'xa',
|
|
233
|
+
'prepare', 'execute', 'deallocate', 'lock', 'unlock',
|
|
234
|
+
]);
|
|
235
|
+
/** SQLite table-valued PRAGMA functions only take literals (`pragma_table_info('t')`), never expressions. */
|
|
236
|
+
function nonLiteralPragmaCall(statement) {
|
|
237
|
+
const { tokens } = statement;
|
|
238
|
+
for (let index = 0; index + 1 < tokens.length; index += 1) {
|
|
239
|
+
const token = tokens[index];
|
|
240
|
+
if ((token.type !== 'word' && token.type !== 'quoted') || !token.value.toLowerCase().startsWith('pragma_'))
|
|
241
|
+
continue;
|
|
242
|
+
if (tokens[index + 1]?.value !== '(' || tokens[index + 1]?.type !== 'punct')
|
|
243
|
+
continue;
|
|
244
|
+
for (let cursor = index + 2; cursor < tokens.length; cursor += 1) {
|
|
245
|
+
const argument = tokens[cursor];
|
|
246
|
+
if (argument.type === 'punct' && argument.value === ')')
|
|
247
|
+
break;
|
|
248
|
+
if (argument.type === 'string')
|
|
249
|
+
continue;
|
|
250
|
+
if (argument.type === 'punct' && /^[,0-9.\-]$/.test(argument.value))
|
|
251
|
+
continue;
|
|
252
|
+
return token.value;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const WRITE_WORDS = /\b(?:insert|update|delete|merge|replace|upsert|create|drop|alter|truncate|grant|revoke|vacuum|reindex|attach|detach|copy|call|do|lock|set|reset|load)\b/;
|
|
258
|
+
/**
|
|
259
|
+
* The read form of one statement, or null when it may write. Callers still run reads in a
|
|
260
|
+
* read-only transaction.
|
|
261
|
+
*/
|
|
262
|
+
export function readStatementKind(statement, dialect) {
|
|
263
|
+
const code = statement.code;
|
|
264
|
+
const first = /^\(*\s*([a-z_]+)/.exec(code)?.[1];
|
|
265
|
+
switch (first) {
|
|
266
|
+
case 'select':
|
|
267
|
+
case 'values':
|
|
268
|
+
return /\binto\b|\bfor (?:update|share|no key update|key share)\b/.test(code) ? null : first;
|
|
269
|
+
case 'with':
|
|
270
|
+
return WRITE_WORDS.test(code) || /\binto\b|\bfor (?:update|share)\b/.test(code) ? null : 'with';
|
|
271
|
+
case 'explain': {
|
|
272
|
+
if (WRITE_WORDS.test(code.replace(/^explain\b/, '')) || /\binto\b/.test(code))
|
|
273
|
+
return null;
|
|
274
|
+
return 'explain';
|
|
275
|
+
}
|
|
276
|
+
case 'show':
|
|
277
|
+
return dialect === 'sqlite' || /\bshow (?:grants|create user)\b/.test(code) ? null : 'show';
|
|
278
|
+
case 'describe':
|
|
279
|
+
case 'desc':
|
|
280
|
+
return dialect === 'mysql' ? 'describe' : null;
|
|
281
|
+
case 'pragma': {
|
|
282
|
+
if (dialect !== 'sqlite' || code.includes('='))
|
|
283
|
+
return null;
|
|
284
|
+
const match = /^pragma (?:"q" ?\. ?|[a-z_]+ ?\. ?)?([a-z_]+) ?(\(.*\))? ?$/.exec(code);
|
|
285
|
+
if (!match || !READ_PRAGMAS.has(match[1]))
|
|
286
|
+
return null;
|
|
287
|
+
// An argument sets most pragmas (`PRAGMA query_only(0)`); only table / index pragmas read with one.
|
|
288
|
+
if (match[2] && !/^(?:table_info|table_xinfo|table_list|index_list|index_info|index_xinfo|foreign_key_list|foreign_key_check|integrity_check|quick_check)$/.test(match[1]))
|
|
289
|
+
return null;
|
|
290
|
+
return 'pragma';
|
|
291
|
+
}
|
|
292
|
+
default:
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Tables the Console never changes: the gateway's `console_*` (never even read), migration
|
|
298
|
+
* bookkeeping and engine catalogs. Reads of the latter are allowed.
|
|
299
|
+
*/
|
|
300
|
+
const PROTECTED_TABLE = /^(?:__zhin_persistent_state|_migrations|agent_schedule_migration|sqlite_\w+|pg_\w+|information_schema|mysql|performance_schema|sys)$/;
|
|
301
|
+
export function isProtectedConsoleTable(name) {
|
|
302
|
+
const bare = String(name).trim().toLowerCase();
|
|
303
|
+
return isGatewayOwnedTable(bare) || PROTECTED_TABLE.test(bare);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* First protected table a statement mentions, if any: identifiers are compared unquoted, with
|
|
307
|
+
* doubled quotes resolved and lower-cased (schema parts are separate tokens, so `main."console_x"`
|
|
308
|
+
* is `console_x`), string literals by content, and the raw text with escape sequences decoded.
|
|
309
|
+
*/
|
|
310
|
+
export function protectedTableIn(statement, options) {
|
|
311
|
+
for (const identifier of statement.identifiers) {
|
|
312
|
+
if (isGatewayOwnedTable(identifier))
|
|
313
|
+
return identifier;
|
|
314
|
+
if (!options.forRead && isProtectedConsoleTable(identifier))
|
|
315
|
+
return identifier;
|
|
316
|
+
}
|
|
317
|
+
// `console_x` can also hide in a string literal (`SELECT * FROM pragma_table_info('console_users')`).
|
|
318
|
+
for (const token of statement.tokens) {
|
|
319
|
+
if (token.type !== 'string')
|
|
320
|
+
continue;
|
|
321
|
+
const literal = /(?<![A-Za-z0-9_$])console_[A-Za-z0-9_$]*/gi;
|
|
322
|
+
for (const match of token.value.matchAll(literal))
|
|
323
|
+
if (isGatewayOwnedTable(match[0]))
|
|
324
|
+
return match[0].toLowerCase();
|
|
325
|
+
}
|
|
326
|
+
if (referencesGatewayOwnedTable(statement.text))
|
|
327
|
+
return 'console_*';
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
/** `CREATE` / `ALTER` forms that define code running SQL later (functions, triggers, rules, events) or change roles and server settings. */
|
|
331
|
+
const REFUSED_DEFINITION = /^(?:create (?:or replace )?(?:definer ?= ?(?:"q"|[^\s@]+)(?: ?@ ?(?:"q"|\S+))? )?(?:(?:temp|temporary|trusted|procedural|constraint) )*(?:function|procedure|trigger|rule|extension|language|server|event|role|user|foreign|aggregate|operator|cast|transform|publication|subscription|access method|text search)\b|alter (?:system|role|user|database|function|procedure|extension|server|default privileges|event|language|foreign|publication|subscription)\b|import foreign\b|security label\b)/;
|
|
332
|
+
/**
|
|
333
|
+
* Bulk or cascading destruction that name-based table protection cannot see: `DROP OWNED` /
|
|
334
|
+
* `REASSIGN OWNED`, databases and schemas, any `DROP … CASCADE` / `TRUNCATE … CASCADE`, ownership
|
|
335
|
+
* changes, tablespaces, users and roles, and MySQL server administration.
|
|
336
|
+
*/
|
|
337
|
+
const REFUSED_BULK = /^(?:drop owned\b|reassign owned\b|(?:create|alter|drop) (?:database|schema|tablespace)\b|alter large object\b|drop (?:user|role)\b|rename user\b|alter instance\b|restart\b|clone\b|purge\b|binlog\b|change (?:replication|master)\b|(?:create|alter|drop) resource group\b|(?:create|alter|drop) logfile group\b|(?:create|drop) spatial reference\b|(?:drop|truncate)\b.*\bcascade\b|alter\b.*\bowner to\b|analyze\b.*\bupdate histogram\b)/;
|
|
338
|
+
/** Statements the Console refuses even for an owner (transaction control, engine settings, dynamic SQL). */
|
|
339
|
+
export function refusedWriteStatement(statement) {
|
|
340
|
+
const first = /^\(*\s*([a-z_]+)/.exec(statement.code)?.[1] ?? '';
|
|
341
|
+
if (TRANSACTION_CONTROL.has(first) || ['attach', 'detach', 'vacuum', 'load', 'grant', 'revoke', 'copy', 'do', 'call', 'handler', 'flush', 'kill', 'shutdown', 'install', 'uninstall', 'listen', 'unlisten', 'notify', 'discard', 'checkpoint'].includes(first)) {
|
|
342
|
+
return first.toUpperCase();
|
|
343
|
+
}
|
|
344
|
+
if (first === 'pragma' && statement.code.includes('='))
|
|
345
|
+
return 'PRAGMA … =';
|
|
346
|
+
if (first === 'pragma' && /\b(?:writable_schema|query_only|journal_mode|locking_mode|key|rekey|trusted_schema)\b/.test(statement.code))
|
|
347
|
+
return 'PRAGMA';
|
|
348
|
+
if (/\binto (?:outfile|dumpfile)\b/.test(statement.code))
|
|
349
|
+
return 'INTO OUTFILE';
|
|
350
|
+
const definition = REFUSED_DEFINITION.exec(statement.code.replace(/^\(+\s*/, ''));
|
|
351
|
+
if (definition)
|
|
352
|
+
return definition[0].toUpperCase();
|
|
353
|
+
const bulk = REFUSED_BULK.exec(statement.code);
|
|
354
|
+
if (bulk)
|
|
355
|
+
return bulk[0].length > 40 ? `${bulk[0].slice(0, 40).toUpperCase()}…` : bulk[0].toUpperCase();
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
/** Checks both paths share: refused functions, non-literal PRAGMA functions, protected tables. */
|
|
359
|
+
/**
|
|
360
|
+
* Relations the Console never reads or writes: statistics samples (`pg_stats` histograms and most
|
|
361
|
+
* common values, `sqlite_stat*`, MySQL histograms) hold values of every table, the gateway's
|
|
362
|
+
* included; role password hashes, raw large objects and server access rules are credentials.
|
|
363
|
+
*/
|
|
364
|
+
const REFUSED_RELATIONS = new Set([
|
|
365
|
+
'pg_stats', 'pg_stats_ext', 'pg_stats_ext_exprs', 'pg_statistic', 'pg_statistic_ext', 'pg_statistic_ext_data',
|
|
366
|
+
'sqlite_stat1', 'sqlite_stat2', 'sqlite_stat3', 'sqlite_stat4', 'sqlite_dbpage', 'column_statistics',
|
|
367
|
+
'pg_authid', 'pg_shadow', 'pg_user_mapping', 'pg_user_mappings', 'pg_largeobject', 'pg_hba_file_rules', 'pg_ident_file_mappings',
|
|
368
|
+
]);
|
|
369
|
+
/** First refused relation a statement names (normalised identifiers), or the MySQL `mysql.` schema. */
|
|
370
|
+
export function refusedRelationIn(statement, dialect) {
|
|
371
|
+
const named = statement.identifiers.find(identifier => REFUSED_RELATIONS.has(identifier));
|
|
372
|
+
if (named)
|
|
373
|
+
return named;
|
|
374
|
+
if (dialect !== 'mysql')
|
|
375
|
+
return undefined;
|
|
376
|
+
const { tokens } = statement;
|
|
377
|
+
for (let index = 0; index + 1 < tokens.length; index += 1) {
|
|
378
|
+
const token = tokens[index];
|
|
379
|
+
if ((token.type === 'word' || token.type === 'quoted') && token.value.toLowerCase() === 'mysql' && tokens[index + 1].type === 'punct' && tokens[index + 1].value === '.')
|
|
380
|
+
return 'mysql.*';
|
|
381
|
+
}
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
function assertConsoleStatement(statement, forRead, dialect) {
|
|
385
|
+
const relation = refusedRelationIn(statement, dialect);
|
|
386
|
+
if (relation)
|
|
387
|
+
throw new ConsoleSqlRefusal('refused_statement', `${relation} is not accessible from the Console (it exposes other tables' values or credentials)`);
|
|
388
|
+
const refusedFunction = refusedFunctionIn(statement);
|
|
389
|
+
if (refusedFunction)
|
|
390
|
+
throw new ConsoleSqlRefusal('refused_statement', `${refusedFunction}() is not allowed from the Console`);
|
|
391
|
+
const pragma = nonLiteralPragmaCall(statement);
|
|
392
|
+
if (pragma)
|
|
393
|
+
throw new ConsoleSqlRefusal('refused_statement', `${pragma}() only takes literal arguments from the Console`);
|
|
394
|
+
const table = protectedTableIn(statement, { forRead });
|
|
395
|
+
if (table) {
|
|
396
|
+
throw new ConsoleSqlRefusal('protected_table', forRead || isGatewayOwnedTable(table) || table === 'console_*'
|
|
397
|
+
? `Table '${table}' is owned by the management gateway and is not accessible from the instance`
|
|
398
|
+
: `Table '${table}' is protected and cannot be changed from the Console`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/** Validates one read-only statement for `db:query`; throws {@link ConsoleSqlRefusal}. */
|
|
402
|
+
export function readOnlyConsoleStatement(sql, dialect) {
|
|
403
|
+
const statements = splitConsoleSql(sql, dialect);
|
|
404
|
+
if (!statements.length)
|
|
405
|
+
throw new ConsoleSqlRefusal('empty', 'SQL is empty');
|
|
406
|
+
if (statements.length > 1)
|
|
407
|
+
throw new ConsoleSqlRefusal('multi_statement', 'Only one statement can run as a read-only query');
|
|
408
|
+
const statement = statements[0];
|
|
409
|
+
const first = /^\(*\s*([a-z_]+)/.exec(statement.code)?.[1] ?? '';
|
|
410
|
+
if (TRANSACTION_CONTROL.has(first))
|
|
411
|
+
throw new ConsoleSqlRefusal('refused_statement', `${first.toUpperCase()} is not allowed from the Console SQL editor`);
|
|
412
|
+
const kind = readStatementKind(statement, dialect);
|
|
413
|
+
if (!kind)
|
|
414
|
+
throw new ConsoleSqlRefusal('read_only', 'Only read statements (SELECT / WITH / EXPLAIN / SHOW / read PRAGMA) run as a read-only query; use a write run for others');
|
|
415
|
+
assertConsoleStatement(statement, true, dialect);
|
|
416
|
+
return Object.freeze({ ...statement, kind });
|
|
417
|
+
}
|
|
418
|
+
/** Validates the statements of `db:execute`; throws {@link ConsoleSqlRefusal}. */
|
|
419
|
+
export function writeConsoleStatements(sql, dialect, maxStatements = 50) {
|
|
420
|
+
const statements = splitConsoleSql(sql, dialect);
|
|
421
|
+
if (!statements.length)
|
|
422
|
+
throw new ConsoleSqlRefusal('empty', 'SQL is empty');
|
|
423
|
+
if (statements.length > maxStatements)
|
|
424
|
+
throw new ConsoleSqlRefusal('multi_statement', `At most ${maxStatements} statements per run`);
|
|
425
|
+
for (const statement of statements) {
|
|
426
|
+
const refused = refusedWriteStatement(statement);
|
|
427
|
+
if (refused)
|
|
428
|
+
throw new ConsoleSqlRefusal('refused_statement', `${refused} is not allowed from the Console SQL editor`);
|
|
429
|
+
assertConsoleStatement(statement, false, dialect);
|
|
430
|
+
}
|
|
431
|
+
return statements;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* SQL text for the audit log with literal values replaced by `'…'` (strings, `E'…'`, `$tag$…$tag$`,
|
|
435
|
+
* MySQL double-quoted strings) and comments removed: statements may carry passwords or tokens that a
|
|
436
|
+
* viewer of the audit log must not read. It uses the same lexer as the Console, so quotes inside
|
|
437
|
+
* comments, quoted names or backticks never shift what counts as a literal. Keywords, identifiers
|
|
438
|
+
* and numbers stay; text the lexer cannot read is not recorded at all.
|
|
439
|
+
*/
|
|
440
|
+
export function redactConsoleSql(sql, dialect) {
|
|
441
|
+
const spans = [];
|
|
442
|
+
try {
|
|
443
|
+
lexConsoleSql(sql, dialect, spans);
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
return `[SQL not recorded: ${sql.length} characters the Console could not read]`;
|
|
447
|
+
}
|
|
448
|
+
let out = '';
|
|
449
|
+
let cursor = 0;
|
|
450
|
+
for (const span of spans.sort((left, right) => left.start - right.start)) {
|
|
451
|
+
if (span.start < cursor)
|
|
452
|
+
continue;
|
|
453
|
+
out += sql.slice(cursor, span.start);
|
|
454
|
+
out += span.kind === 'string' ? span.placeholder : ' ';
|
|
455
|
+
cursor = span.end;
|
|
456
|
+
}
|
|
457
|
+
out += sql.slice(cursor);
|
|
458
|
+
return out.trim();
|
|
459
|
+
}
|
|
@@ -1,29 +1,60 @@
|
|
|
1
|
-
import { type DatabaseHost, type
|
|
1
|
+
import { type DatabaseHost, type ConfigDocumentPort, type RootResourceInstaller, type RuntimeConfigDocument } from '@zhin.js/plugin-runtime';
|
|
2
2
|
import { type DatabaseHostConfig } from './database-config.js';
|
|
3
|
+
import { type ConsoleSqlDialect } from './database-console-sql.js';
|
|
3
4
|
export type { DatabaseHostConfig } from './database-config.js';
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
readonly
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
5
|
+
/** Test and composition knobs of {@link createDatabaseHost} (not part of the config file). */
|
|
6
|
+
export interface DatabaseHostInternalOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Tests only: `false` turns the Console SQL text checks off (statement classification, refused
|
|
9
|
+
* statements, the `console_*` name guard), so a test can show that the storage boundary alone
|
|
10
|
+
* keeps the gateway's data out of reach (ADR 0059 §5). Default true.
|
|
11
|
+
*/
|
|
12
|
+
readonly textGuard?: boolean;
|
|
13
|
+
/** Minimum time between two probes of the restricted Console account after a failure (default 5 min). */
|
|
14
|
+
readonly consoleProbeIntervalMs?: number;
|
|
15
|
+
/** How long a successful probe is trusted before raw SQL probes again (default 60 s). */
|
|
16
|
+
readonly consoleProbeTtlMs?: number;
|
|
17
|
+
readonly now?: () => number;
|
|
18
|
+
/**
|
|
19
|
+
* Tests: opens the restricted account's connection from the options the host would pass to
|
|
20
|
+
* `Registry.create` (default: `Registry.create`).
|
|
21
|
+
*/
|
|
22
|
+
readonly openConsoleConnection?: (dialect: 'pg' | 'mysql', options: Readonly<Record<string, unknown>>) => unknown;
|
|
17
23
|
}
|
|
18
|
-
|
|
24
|
+
/** How long a failed restricted-account probe is kept before the next try. */
|
|
25
|
+
export declare const CONSOLE_CREDENTIAL_RETRY_MS: number;
|
|
26
|
+
/** How long a successful probe is trusted (raw SQL probes again after this, and after any error). */
|
|
27
|
+
export declare const CONSOLE_CREDENTIAL_TTL_MS = 60000;
|
|
28
|
+
/** Minimum time between error-triggered re-probes. */
|
|
29
|
+
export declare const CONSOLE_CREDENTIAL_STALE_MIN_MS = 30000;
|
|
30
|
+
/** Why raw SQL is off on PostgreSQL / MySQL without a restricted account. */
|
|
31
|
+
export declare const CONSOLE_ROLE_MISSING_REASON = "Raw SQL on PostgreSQL / MySQL runs only on a restricted database account: run `zhin db console-role` (or `zhin doctor --fix`; `--print-sql` for a DBA) and restart";
|
|
32
|
+
export declare function createDatabaseHost(config: DatabaseHostConfig, internal?: DatabaseHostInternalOptions): DatabaseHost & {
|
|
19
33
|
readonly db: {
|
|
20
34
|
define: (name: string, definition: unknown) => void;
|
|
21
35
|
start(): Promise<void>;
|
|
22
36
|
stop(): Promise<void>;
|
|
23
37
|
models: Map<string, unknown>;
|
|
24
38
|
};
|
|
25
|
-
/** See {@link GatewayTableHost}; never pass it to plugins, Agent or Console database RPC. */
|
|
26
|
-
gatewayTableHost(): GatewayTableHost;
|
|
27
39
|
};
|
|
40
|
+
/** Row cap and statement timeout of the Console SQL editor. */
|
|
41
|
+
export declare const CONSOLE_QUERY_MAX_ROWS = 1000;
|
|
42
|
+
export declare const CONSOLE_QUERY_TIMEOUT_MS = 10000;
|
|
43
|
+
/** Thrown for a malformed `where` / `orderBy` of `db:select`. */
|
|
44
|
+
export declare class ConsoleSelectRefusal extends Error {
|
|
45
|
+
readonly code = "invalid";
|
|
46
|
+
constructor(message: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Why the Console must not run raw SQL on this connection, or undefined. The Console lexer reads
|
|
50
|
+
* strings the way the server does by default; a server that treats backslashes (MySQL
|
|
51
|
+
* `NO_BACKSLASH_ESCAPES`, PostgreSQL `standard_conforming_strings = off`) or double quotes (MySQL
|
|
52
|
+
* `ANSI_QUOTES`) differently would split and classify statements differently. SQLite needs a file
|
|
53
|
+
* database: only a separate connection can be stopped at the time limit.
|
|
54
|
+
*/
|
|
55
|
+
export declare function rawSqlCompatibilityProblem(dialect: ConsoleSqlDialect, probe: {
|
|
56
|
+
query(sql: string): Promise<unknown>;
|
|
57
|
+
isolatedReads(): boolean;
|
|
58
|
+
}): Promise<string | undefined>;
|
|
28
59
|
export declare function resolveDatabaseConfig(projectRoot: string, config: RuntimeConfigDocument | ConfigDocumentPort): Promise<DatabaseHostConfig>;
|
|
29
60
|
export declare function installDatabaseHost(host: DatabaseHost): RootResourceInstaller;
|