Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(query-ast): reject table-function sources in the tenancy pass (#18813)

Niklas Semmlerf7c29b67dca5
diff --git a/packages/shared/src/server/query-ast/README.md b/packages/shared/src/server/query-ast/README.md
index bed030cc7c..aab23b5051 100644
--- a/packages/shared/src/server/query-ast/README.md
+++ b/packages/shared/src/server/query-ast/README.md
@@ -208,7 +208,9 @@ is where tenancy is enforced:
their ClickHouse type from the compared column's registry entry when one is
in scope (`scores.value > 1` → `{p:Float64}`).
5. Raw-SQL table sources (``selectFrom(sql`...`)``) and raw fragments embedding
- a `SELECT`/`FROM`/`JOIN` in SELECT/WHERE throw `UnscopedRelationError`.
+ a `SELECT`/`FROM`/`JOIN` in SELECT/WHERE throw `UnscopedRelationError`, as
+ does any FROM/JOIN source that is neither a table reference nor a subquery
+ (table functions such as `remote()`, `clusterAllReplicas()`, `merge()`).
Kysely's own keyword fragments (`asc`/`desc`) are not relations.
So query bodies here never filter `project_id` by hand — it is redundant, and
diff --git a/packages/shared/src/server/query-ast/binds.test.ts b/packages/shared/src/server/query-ast/binds.test.ts
index 593dcf78bf..b552184eed 100644
--- a/packages/shared/src/server/query-ast/binds.test.ts
+++ b/packages/shared/src/server/query-ast/binds.test.ts
@@ -340,16 +340,6 @@ describe("typed params from the column registry", () => {
expect(sql).toMatch(/is_deleted = \{p\d+:Int64\}/);
});
- it("uses the name-only type for a table-function source", () => {
- const { sql } = compile(
- db
- .selectFrom((eb) => eb.fn("numbers", [eb.lit(10)]).as("n"))
- .selectAll()
- .where(db.dynamic.ref("n.is_deleted"), "=", 0),
- );
- expect(sql).toMatch(/n\.is_deleted = \{p\d+:UInt8\}/);
- });
-
it("uses the name-only type for a qualifier declared nowhere", () => {
const { sql } = compile(
db
diff --git a/packages/shared/src/server/query-ast/nodes.test.ts b/packages/shared/src/server/query-ast/nodes.test.ts
index 7c38839e39..0c6a364b3f 100644
--- a/packages/shared/src/server/query-ast/nodes.test.ts
+++ b/packages/shared/src/server/query-ast/nodes.test.ts
@@ -107,6 +107,45 @@ describe("tenancy injection", () => {
);
});
+ it.each(["remote", "clusterAllReplicas", "merge"])(
+ "rejects the %s table function as a FROM source",
+ (fn) => {
+ const qb = getClickhouseKysely()
+ .selectFrom((eb) =>
+ eb
+ .fn<{
+ environment: string;
+ }>(fn, [eb.val("default"), eb.val("traces")])
+ .as("t"),
+ )
+ .select("t.environment");
+
+ expect(() => compileClickhouseQuery(qb, ctx)).toThrow(
+ UnscopedRelationError,
+ );
+ },
+ );
+
+ it.each(["remote", "clusterAllReplicas", "merge"])(
+ "rejects the %s table function as a JOIN source",
+ (fn) => {
+ const qb = getClickhouseKysely()
+ .selectFrom("events_core as e")
+ .innerJoin(
+ (eb) =>
+ eb
+ .fn<{ id: string }>(fn, [eb.val("default"), eb.val("traces")])
+ .as("t"),
+ (join) => join.onRef("e.trace_id", "=", "t.id"),
+ )
+ .select("t.id");
+
+ expect(() => compileClickhouseQuery(qb, ctx)).toThrow(
+ UnscopedRelationError,
+ );
+ },
+ );
+
it("rejects raw SQL fragments in SELECT and WHERE, not only FROM/JOIN", () => {
const selectRaw = getClickhouseKysely()
.selectFrom("events_core")
diff --git a/packages/shared/src/server/query-ast/schema.ts b/packages/shared/src/server/query-ast/schema.ts
index 33978a2ea8..b802ef76bd 100644
--- a/packages/shared/src/server/query-ast/schema.ts
+++ b/packages/shared/src/server/query-ast/schema.ts
@@ -356,7 +356,7 @@ export const TABLE_BIND_TYPES: Record<
/**
* Bind type by bare column name, for operands whose relation is opaque to the
- * compiler (e.g. a table-function source). Only names with the same bind type in
+ * compiler (e.g. a qualifier no relation in scope declares). Only names with the same bind type in
* every table are kept, since `start_time` is `DateTime64(6)` on events but
* `DateTime64(3)` on observations.
*/
diff --git a/packages/shared/src/server/query-ast/tenancy.ts b/packages/shared/src/server/query-ast/tenancy.ts
index 273a8aead7..10cd8834e7 100644
--- a/packages/shared/src/server/query-ast/tenancy.ts
+++ b/packages/shared/src/server/query-ast/tenancy.ts
@@ -61,9 +61,11 @@ type Relation =
* tenanted physical relation that does not already have that predicate, then
* stamps the tree so {@link ClickHouseQueryCompiler} will compile it.
*
- * Any `RawNode` whose SQL fragments introduce a relation (`SELECT` / `FROM` /
- * `JOIN`) is rejected — not only FROM/JOIN table sources. Kysely's own
- * keyword fragments (`asc` / `desc`) are not relations and are allowed.
+ * FROM/JOIN sources must be table references or subqueries; anything else,
+ * such as a table function, is rejected. Any `RawNode` whose SQL fragments
+ * introduce a relation (`SELECT` / `FROM` / `JOIN`) is rejected — not only
+ * FROM/JOIN table sources. Kysely's own keyword fragments (`asc` / `desc`) are
+ * not relations and are allowed.
*/
export class TenancyInjectionPlugin implements KyselyPlugin {
constructor(private readonly ctx: ExecutionContext) {
@@ -188,6 +190,11 @@ function assertRelationAllowed(relation: Relation): void {
"Raw SQL table sources are rejected: they can introduce an unscoped relation that the tenancy pass cannot prove. Use a traced table reference instead.",
);
}
+ if (relation.kind === "other") {
+ throw new UnscopedRelationError(
+ "Only table references and subqueries are accepted as FROM/JOIN sources: a table function such as remote() or merge() reads a relation the tenancy pass cannot scope.",
+ );
+ }
}
function describeRelation(node: OperationNode): Relation {