|
| 1 | +import { |
| 2 | + parseTSQLSelect, |
| 3 | + SyntaxError as TSQLSyntaxError, |
| 4 | + type Field, |
| 5 | + type JoinExpr, |
| 6 | + type SelectQuery, |
| 7 | + type SelectSetQuery, |
| 8 | +} from "@internal/tsql"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Extract every known table a TRQL query reads — the FROM table, every JOIN in |
| 12 | + * the chain, and any subqueries — for per-table JWT-scope authorization. |
| 13 | + * |
| 14 | + * `allowedTableNames` is the set of recognised table names (matched |
| 15 | + * case-insensitively); anything not in it is ignored. Injected so this stays |
| 16 | + * dependency-free (the caller derives it from the query schemas). |
| 17 | + * |
| 18 | + * Returns `null` when the query can't be parsed; callers MUST treat `null` as |
| 19 | + * deny-by-default. |
| 20 | + */ |
| 21 | +export function detectQueryTables(query: string, allowedTableNames: Set<string>): string[] | null { |
| 22 | + let ast: SelectQuery | SelectSetQuery; |
| 23 | + try { |
| 24 | + ast = parseTSQLSelect(query); |
| 25 | + } catch (err) { |
| 26 | + if (err instanceof TSQLSyntaxError) return null; |
| 27 | + throw err; |
| 28 | + } |
| 29 | + |
| 30 | + const allowed = new Map(Array.from(allowedTableNames, (n) => [n.toLowerCase(), n])); |
| 31 | + const seen = new Set<string>(); |
| 32 | + const scanned = new WeakSet<object>(); |
| 33 | + |
| 34 | + function visitSelect(q: SelectQuery): void { |
| 35 | + // CTE bodies: `WITH r AS (SELECT ... FROM <table>) ...` — the table is |
| 36 | + // read by the CTE even when the outer query only references the CTE alias. |
| 37 | + if (q.ctes) { |
| 38 | + for (const cte of Object.values(q.ctes)) { |
| 39 | + scanForSubqueries(cte.expr); |
| 40 | + } |
| 41 | + } |
| 42 | + // FROM / JOIN chain (tables + FROM-position subqueries). |
| 43 | + if (q.select_from) visitJoin(q.select_from); |
| 44 | + // Subqueries anywhere else (WHERE, SELECT list, GROUP BY, ORDER BY, etc.) |
| 45 | + // can each embed a SELECT that reads a real table, e.g. |
| 46 | + // `WHERE id IN (SELECT … FROM runs)`. |
| 47 | + scanForSubqueries(q.select); |
| 48 | + scanForSubqueries(q.where); |
| 49 | + scanForSubqueries(q.prewhere); |
| 50 | + scanForSubqueries(q.having); |
| 51 | + scanForSubqueries(q.group_by); |
| 52 | + scanForSubqueries(q.array_join_list); |
| 53 | + scanForSubqueries(q.order_by); |
| 54 | + scanForSubqueries(q.limit); |
| 55 | + scanForSubqueries(q.offset); |
| 56 | + scanForSubqueries(q.limit_by); |
| 57 | + scanForSubqueries(q.window_exprs); |
| 58 | + } |
| 59 | + // Shape-agnostic walk of an expression subtree: descends every nested |
| 60 | + // object/array and hands any embedded SELECT to the query visitors, so a new |
| 61 | + // node shape can't silently reintroduce a detection gap. The WeakSet guards |
| 62 | + // against back-reference cycles the AST might carry. |
| 63 | + function scanForSubqueries(node: unknown): void { |
| 64 | + if (node === null || typeof node !== "object") return; |
| 65 | + if (scanned.has(node)) return; |
| 66 | + scanned.add(node); |
| 67 | + if (Array.isArray(node)) { |
| 68 | + for (const item of node) scanForSubqueries(item); |
| 69 | + return; |
| 70 | + } |
| 71 | + const expressionType = (node as { expression_type?: string }).expression_type; |
| 72 | + if (expressionType === "select_query") { |
| 73 | + visitSelect(node as SelectQuery); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (expressionType === "select_set_query") { |
| 77 | + visitSelectSet(node as SelectSetQuery); |
| 78 | + return; |
| 79 | + } |
| 80 | + for (const value of Object.values(node)) scanForSubqueries(value); |
| 81 | + } |
| 82 | + function visitSelectSet(q: SelectSetQuery): void { |
| 83 | + visitAny(q.initial_select_query); |
| 84 | + for (const node of q.subsequent_select_queries ?? []) { |
| 85 | + visitAny(node.select_query); |
| 86 | + } |
| 87 | + } |
| 88 | + function visitAny(q: SelectQuery | SelectSetQuery): void { |
| 89 | + if (q.expression_type === "select_query") visitSelect(q); |
| 90 | + else visitSelectSet(q); |
| 91 | + } |
| 92 | + function visitJoin(node: JoinExpr): void { |
| 93 | + const tableExpr = node.table; |
| 94 | + if (tableExpr) { |
| 95 | + if ((tableExpr as Field).expression_type === "field") { |
| 96 | + const name = (tableExpr as Field).chain[0]; |
| 97 | + const canonicalName = |
| 98 | + typeof name === "string" ? allowed.get(name.toLowerCase()) : undefined; |
| 99 | + if (canonicalName) seen.add(canonicalName); |
| 100 | + } else if ((tableExpr as SelectQuery).expression_type === "select_query") { |
| 101 | + visitSelect(tableExpr as SelectQuery); |
| 102 | + } else if ((tableExpr as SelectSetQuery).expression_type === "select_set_query") { |
| 103 | + visitSelectSet(tableExpr as SelectSetQuery); |
| 104 | + } |
| 105 | + } |
| 106 | + if (node.next_join) visitJoin(node.next_join); |
| 107 | + } |
| 108 | + |
| 109 | + if (ast.expression_type === "select_set_query") visitSelectSet(ast); |
| 110 | + else visitSelect(ast); |
| 111 | + |
| 112 | + return Array.from(seen); |
| 113 | +} |
0 commit comments