|
| 1 | +import { ConnectionTypesEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js'; |
| 2 | +import sqlParser from 'node-sql-parser'; |
| 3 | +import { connectionTypeToParserDialect } from '../../../table-schema/utils/assert-dialect-supported.js'; |
| 4 | + |
| 5 | +const { Parser } = sqlParser; |
| 6 | + |
| 7 | +// Connection types that node-sql-parser cannot analyze. Their queries are not SQL, so we never |
| 8 | +// attempt to parse them and instead let the caller fall back to the all-tables permission check. |
| 9 | +const NON_SQL_CONNECTION_TYPES: ReadonlySet<ConnectionTypesEnum> = new Set([ |
| 10 | + ConnectionTypesEnum.mongodb, |
| 11 | + ConnectionTypesEnum.agent_mongodb, |
| 12 | + ConnectionTypesEnum.elasticsearch, |
| 13 | + ConnectionTypesEnum.redis, |
| 14 | + ConnectionTypesEnum.agent_redis, |
| 15 | + ConnectionTypesEnum.dynamodb, |
| 16 | +]); |
| 17 | + |
| 18 | +export type CollectQueryTablesResult = |
| 19 | + | { kind: 'tables'; tables: Array<string> } |
| 20 | + | { kind: 'indeterminate'; reason: string }; |
| 21 | + |
| 22 | +/** |
| 23 | + * Extracts the real tables a read-only query references, so the caller can verify the user has |
| 24 | + * read permission on each of them. |
| 25 | + * |
| 26 | + * Returns `{ kind: 'tables' }` only when a confident, non-empty list of concrete tables could be |
| 27 | + * resolved. In every other case — non-SQL connections, parse failures, or statements that resolve |
| 28 | + * to no concrete table (e.g. `SELECT 1`, `SHOW`, `DESCRIBE`) — it returns `{ kind: 'indeterminate' }` |
| 29 | + * and the caller must fall back to a stricter check rather than assume the query is harmless. |
| 30 | + */ |
| 31 | +export function collectQueryTables(query: string, connectionType: ConnectionTypesEnum): CollectQueryTablesResult { |
| 32 | + if (NON_SQL_CONNECTION_TYPES.has(connectionType)) { |
| 33 | + return { kind: 'indeterminate', reason: `non-SQL connection type "${connectionType}"` }; |
| 34 | + } |
| 35 | + |
| 36 | + const dialect = connectionTypeToParserDialect(connectionType); |
| 37 | + const parser = new Parser(); |
| 38 | + |
| 39 | + let rawTableList: Array<string>; |
| 40 | + let cteNames: Set<string>; |
| 41 | + try { |
| 42 | + rawTableList = parser.tableList(query, { database: dialect }); |
| 43 | + cteNames = collectCteNames(parser, query, dialect); |
| 44 | + } catch (error) { |
| 45 | + return { kind: 'indeterminate', reason: `parse error: ${(error as Error).message}` }; |
| 46 | + } |
| 47 | + |
| 48 | + const tables = new Set<string>(); |
| 49 | + for (const entry of rawTableList) { |
| 50 | + // node-sql-parser returns entries formatted as "{type}::{db}::{table}". We ignore the schema |
| 51 | + // ("db") segment since permissions are keyed on bare table names. |
| 52 | + const tableName = entry.split('::').pop(); |
| 53 | + if (!tableName || tableName === 'null') { |
| 54 | + continue; |
| 55 | + } |
| 56 | + // Common Table Expressions are aliases for inline subqueries, not real tables; the subqueries' |
| 57 | + // underlying tables are already present in the list, so the CTE names must be dropped. |
| 58 | + if (cteNames.has(tableName.toLowerCase())) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + tables.add(tableName); |
| 62 | + } |
| 63 | + |
| 64 | + if (tables.size === 0) { |
| 65 | + return { kind: 'indeterminate', reason: 'query resolved to no concrete table' }; |
| 66 | + } |
| 67 | + |
| 68 | + return { kind: 'tables', tables: Array.from(tables) }; |
| 69 | +} |
| 70 | + |
| 71 | +function collectCteNames(parser: InstanceType<typeof Parser>, query: string, dialect: string): Set<string> { |
| 72 | + const names = new Set<string>(); |
| 73 | + const ast = parser.astify(query, { database: dialect }); |
| 74 | + const statements = Array.isArray(ast) ? ast : [ast]; |
| 75 | + for (const statement of statements) { |
| 76 | + const withClause = (statement as { with?: unknown })?.with; |
| 77 | + if (!Array.isArray(withClause)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + for (const cte of withClause) { |
| 81 | + const name = extractCteName((cte as { name?: unknown })?.name); |
| 82 | + if (name) { |
| 83 | + names.add(name.toLowerCase()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return names; |
| 88 | +} |
| 89 | + |
| 90 | +function extractCteName(nameNode: unknown): string | null { |
| 91 | + if (typeof nameNode === 'string') { |
| 92 | + return nameNode; |
| 93 | + } |
| 94 | + if (nameNode && typeof nameNode === 'object') { |
| 95 | + const value = (nameNode as { value?: unknown }).value; |
| 96 | + if (typeof value === 'string') { |
| 97 | + return value; |
| 98 | + } |
| 99 | + } |
| 100 | + return null; |
| 101 | +} |
0 commit comments