Skip to content

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

fix(filters): escape LIKE wildcards in Postgres table filters (#19117)

Raphael Fakhrib28dc5334f31
diff --git a/packages/shared/src/server/filterToPrisma.test.ts b/packages/shared/src/server/filterToPrisma.test.ts
index 5491d28112..fcafc124de 100644
--- a/packages/shared/src/server/filterToPrisma.test.ts
+++ b/packages/shared/src/server/filterToPrisma.test.ts
@@ -43,3 +43,60 @@ describe("tableColumnsToSqlFilter metadata presence operators", () => {
expect(sql.values).toContain("env");
});
});
+
+describe("tableColumnsToSqlFilter LIKE wildcards", () => {
+ const nameCol: ColumnDefinition = {
+ name: "Name",
+ id: "name",
+ type: "string",
+ internal: 'p."name"',
+ };
+
+ const buildString = (
+ operator:
+ | "="
+ | "contains"
+ | "does not contain"
+ | "starts with"
+ | "ends with",
+ value: string,
+ ) =>
+ tableColumnsToSqlFilter(
+ [{ type: "string", column: "name", operator, value }] as FilterState,
+ [nameCol],
+ "prompts",
+ );
+
+ it.each([
+ "contains",
+ "does not contain",
+ "starts with",
+ "ends with",
+ ] as const)("escapes LIKE wildcards for `%s`", (operator) => {
+ const sql = buildString(operator, "team_a/100%\\x");
+ expect(sql.sql).toContain("ILIKE");
+ expect(sql.values).toEqual(["team\\_a/100\\%\\\\x"]);
+ });
+
+ it("keeps the value unescaped for `=`", () => {
+ const sql = buildString("=", "team_a/100%");
+ expect(sql.values).toEqual(["team_a/100%"]);
+ });
+
+ it("escapes the value of a stringObject filter", () => {
+ const sql = tableColumnsToSqlFilter(
+ [
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "env",
+ operator: "contains",
+ value: "prod_eu",
+ },
+ ] as FilterState,
+ [metadataCol],
+ "dataset_items",
+ );
+ expect(sql.values).toEqual(["env", "prod\\_eu"]);
+ });
+});
diff --git a/packages/shared/src/server/filterToPrisma.ts b/packages/shared/src/server/filterToPrisma.ts
index 83bb383719..e9e97dd519 100644
--- a/packages/shared/src/server/filterToPrisma.ts
+++ b/packages/shared/src/server/filterToPrisma.ts
@@ -5,6 +5,7 @@ import { filterOperators, timeFilter } from "../interfaces/filters";
import { z } from "zod";
import { logger } from "./index";
import { InvalidRequestError } from "../errors";
+import { escapeSqlLikePattern } from "./utils/sqlLike";
const operatorReplacements = {
"any of": "IN",
@@ -15,6 +16,13 @@ const operatorReplacements = {
"ends with": "ILIKE",
};
+const likeOperators = [
+ "contains",
+ "does not contain",
+ "starts with",
+ "ends with",
+];
+
const arrayOperatorReplacements = {
"any of": "&&",
"all of": "@>",
@@ -97,7 +105,10 @@ export function tableColumnsToSqlFilter(
break;
case "string":
case "stringObject":
- valuePrisma = Prisma.sql`${filter.value}`;
+ // ILIKE treats `_`, `%` and `\` in the value as pattern characters
+ valuePrisma = likeOperators.includes(filter.operator)
+ ? Prisma.sql`${escapeSqlLikePattern(filter.value)}`
+ : Prisma.sql`${filter.value}`;
break;
case "stringOptions":
valuePrisma = Prisma.sql`(${Prisma.join(