The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Cap metadata filters at 5 and preserve special characters in values.
Kiran K10bbda45121f
diff --git a/apps/web/lib/api/commissions/get-commissions.ts b/apps/web/lib/api/commissions/get-commissions.tsindex 9289771e16..0e820e404f 100644--- a/apps/web/lib/api/commissions/get-commissions.ts+++ b/apps/web/lib/api/commissions/get-commissions.ts@@ -199,14 +199,7 @@ export async function getCommissions(filters: CommissionsFilters) {...metadataWhere,},include: {- customer: true,- partner: true,- programEnrollment: true,- payout: {- select: {- paidAt: true,- },- },+ ...commissionIncludes,},...paginationQuery,});diff --git a/apps/web/lib/api/commissions/metadata-filters.ts b/apps/web/lib/api/commissions/metadata-filters.tsindex 0493ecafe9..f42b5d234a 100644--- a/apps/web/lib/api/commissions/metadata-filters.ts+++ b/apps/web/lib/api/commissions/metadata-filters.ts@@ -14,6 +14,8 @@ export type ParsedCommissionMetadataQuery = {filters: CommissionMetadataFilter[];};+const MAX_METADATA_FILTER_CONDITIONS = 5;+function mapOperator(operator: string): CommissionMetadataFilterOp {switch (operator) {case ":":@@ -44,10 +46,7 @@ function parseCondition(condition: string): CommissionMetadataFilter {}const [, key, operator, rawValue] = match;- const value = rawValue- .trim()- .replace(/^['"`]|['"`]$/g, "")- .replace(/[;\\]|--|\*\/|\/\*/g, "");+ const value = rawValue.trim().replace(/^['"`]|['"`]$/g, "");if (!value) {throw new DubApiError({@@ -100,6 +99,13 @@ export function parseCommissionMetadataQuery(});}+ if (filters.length > MAX_METADATA_FILTER_CONDITIONS) {+ throw new DubApiError({+ code: "unprocessable_entity",+ message: `Metadata query supports at most ${MAX_METADATA_FILTER_CONDITIONS} conditions.`,+ });+ }+return {logic,filters,diff --git a/apps/web/tests/commissions/metadata-filters.test.ts b/apps/web/tests/commissions/metadata-filters.test.tsindex e34d7feb6c..03a23fae2d 100644--- a/apps/web/tests/commissions/metadata-filters.test.ts+++ b/apps/web/tests/commissions/metadata-filters.test.ts@@ -124,6 +124,51 @@ describe("parseCommissionMetadataQuery", () => {DubApiError,);});++ it("preserves filter values containing --, ;, \\, /*, and */", () => {+ expect(parseCommissionMetadataQuery("metadata['sku']='AB--12'")).toEqual({+ logic: "AND",+ filters: [{ key: "sku", op: "equals", value: "AB--12" }],+ });+ expect(+ parseCommissionMetadataQuery("metadata['name']='Smith; Jane'"),+ ).toEqual({+ logic: "AND",+ filters: [{ key: "name", op: "equals", value: "Smith; Jane" }],+ });+ expect(parseCommissionMetadataQuery("metadata['path']='a\\b'")).toEqual({+ logic: "AND",+ filters: [{ key: "path", op: "equals", value: "a\\b" }],+ });+ expect(+ parseCommissionMetadataQuery("metadata['note']='/* comment */'"),+ ).toEqual({+ logic: "AND",+ filters: [{ key: "note", op: "equals", value: "/* comment */" }],+ });+ });++ it("allows up to 5 conditions and rejects more", () => {+ const five = [+ "metadata['a']='1'",+ "metadata['b']='2'",+ "metadata['c']='3'",+ "metadata['d']='4'",+ "metadata['e']='5'",+ ].join(" AND ");+ expect(parseCommissionMetadataQuery(five)?.filters).toHaveLength(5);++ const six = `${five} AND metadata['f']='6'`;+ expect(() => parseCommissionMetadataQuery(six)).toThrow(DubApiError);+ try {+ parseCommissionMetadataQuery(six);+ } catch (error) {+ expect((error as DubApiError).code).toBe("unprocessable_entity");+ expect((error as DubApiError).message).toBe(+ "Metadata query supports at most 5 conditions.",+ );+ }+ });});describe("buildCommissionMetadataWhere", () => {