Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix: address CodeRabbit review - root filter, missing trigger/metadata in pipes, query parser guards, type consolidation

Pedro Ladeira8a0b78fe54e2
diff --git a/apps/web/lib/analytics/query-parser.ts b/apps/web/lib/analytics/query-parser.ts
index 7780e6fffa..7403c9fe15 100644
--- a/apps/web/lib/analytics/query-parser.ts
+++ b/apps/web/lib/analytics/query-parser.ts
@@ -92,9 +92,8 @@ function parseCondition(condition: string): InternalFilter | null {
.replace(/['"]\]/g, ""); // Remove trailing '] or "]
// Security: Validate metadata key contains only safe characters
- // Only allow alphanumeric, underscore, and dot (for nested keys)
- if (!/^[a-zA-Z0-9_.]+$/.test(extractedKey)) return null;
-
+ // Only allow alphanumeric and underscore (no dots — nested keys are not supported)
+ if (!/^[a-zA-Z0-9_]+$/.test(extractedKey)) return null;
operand = `metadata.${extractedKey}`;
} else {
@@ -107,6 +106,8 @@ function parseCondition(condition: string): InternalFilter | null {
.replace(/^['"`]|['"`]$/g, "")
.replace(/[;\\]|--|\*\/|\/\*/g, "");
+ if (!sanitizedValue) return null;
+
return {
operand,
operator: mapOperator(operator),
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index b190a0296f..0fa6bd1348 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -490,8 +490,12 @@ export const analyticsFilterTB = z.object({
root: z
.union([z.string(), z.boolean(), z.array(z.union([z.string(), z.boolean()]))])
.transform((v) => {
- if (Array.isArray(v)) return v.map(val => typeof val === 'boolean' ? val : val === 'true');
- return typeof v === 'boolean' ? [v] : [v === 'true'];
+ const normalize = (val: string | boolean): boolean => {
+ if (typeof val === 'boolean') return val;
+ return val === 'true' || val === '1' || val === 'yes';
+ };
+ if (Array.isArray(v)) return v.map(normalize);
+ return [normalize(v)];
})
.optional()
.describe("Filter for root domain links."),
diff --git a/package.json b/package.json
index 78b89077fd..be78aebc28 100644
--- a/package.json
+++ b/package.json
@@ -32,5 +32,5 @@
"resolutions": {
"chrono-node": "2.7.5"
},
- "packageManager": "[email protected]"
+ "packageManager": "[email protected]"
}
diff --git a/packages/tinybird/pipes/v3_count.pipe b/packages/tinybird/pipes/v3_count.pipe
index 999525f04e..88b06b2dbd 100644
--- a/packages/tinybird/pipes/v3_count.pipe
+++ b/packages/tinybird/pipes/v3_count.pipe
@@ -66,9 +66,9 @@ SQL >
{% end %}
{% if defined(root) %}
{% if defined(rootOperator) and String(rootOperator) == 'NOT IN' %}
- AND NOT (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND NOT (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% else %}
- AND (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% end %}
{% end %}
diff --git a/packages/tinybird/pipes/v3_events.pipe b/packages/tinybird/pipes/v3_events.pipe
index bb5d4ab246..36fc609bd8 100644
--- a/packages/tinybird/pipes/v3_events.pipe
+++ b/packages/tinybird/pipes/v3_events.pipe
@@ -66,9 +66,9 @@ SQL >
{% end %}
{% if defined(root) %}
{% if defined(rootOperator) and String(rootOperator) == 'NOT IN' %}
- AND NOT (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND NOT (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% else %}
- AND (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% end %}
{% end %}
@@ -346,6 +346,10 @@ SQL >
{% if operator == 'IN' %} AND os IN {{ Array(values, 'String') }}
{% elif operator == 'NOT IN' %} AND os NOT IN {{ Array(values, 'String') }}
{% end %}
+ {% elif field == 'trigger' %}
+ {% if operator == 'IN' %} AND trigger IN {{ Array(values, 'String') }}
+ {% elif operator == 'NOT IN' %} AND trigger NOT IN {{ Array(values, 'String') }}
+ {% end %}
{% elif field == 'referer' %}
{% if operator == 'IN' %} AND referer IN {{ Array(values, 'String') }}
{% elif operator == 'NOT IN' %} AND referer NOT IN {{ Array(values, 'String') }}
diff --git a/packages/tinybird/pipes/v3_group_by.pipe b/packages/tinybird/pipes/v3_group_by.pipe
index b0f4e5cb4c..dae18e1f73 100644
--- a/packages/tinybird/pipes/v3_group_by.pipe
+++ b/packages/tinybird/pipes/v3_group_by.pipe
@@ -66,9 +66,9 @@ SQL >
{% end %}
{% if defined(root) %}
{% if defined(rootOperator) and String(rootOperator) == 'NOT IN' %}
- AND NOT (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND NOT (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% else %}
- AND (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% end %}
{% end %}
diff --git a/packages/tinybird/pipes/v3_group_by_link_metadata.pipe b/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
index d916f8be09..b0a02fdaf9 100644
--- a/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
+++ b/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
@@ -90,9 +90,9 @@ SQL >
{% end %}
{% if defined(root) %}
{% if defined(rootOperator) and String(rootOperator) == 'NOT IN' %}
- AND NOT (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND NOT (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% else %}
- AND (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% end %}
{% end %}
diff --git a/packages/tinybird/pipes/v3_timeseries.pipe b/packages/tinybird/pipes/v3_timeseries.pipe
index 77980f9557..3557e784dc 100644
--- a/packages/tinybird/pipes/v3_timeseries.pipe
+++ b/packages/tinybird/pipes/v3_timeseries.pipe
@@ -139,9 +139,9 @@ SQL >
{% end %}
{% if defined(root) %}
{% if defined(rootOperator) and String(rootOperator) == 'NOT IN' %}
- AND NOT (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND NOT (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% else %}
- AND (0{% for val in root %}{% if val %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
+ AND (0{% for val in root %}{% if val == 'true' %} OR key = '_root'{% else %} OR key != '_root'{% end %}{% end %})
{% end %}
{% end %}
@@ -463,6 +463,23 @@ SQL >
{% elif field == 'saleType' %}
{# saleType is handled in the sales subquery, not here #}
{% end %}
+ {% elif item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+ {% if operator == 'equals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND toFloat64OrNull(JSONExtractString(metadata, {{ metadataKey }})) > toFloat64OrNull({{ value }})
+ {% elif operator == 'lessThan' %}
+ AND toFloat64OrNull(JSONExtractString(metadata, {{ metadataKey }})) < toFloat64OrNull({{ value }})
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND toFloat64OrNull(JSONExtractString(metadata, {{ metadataKey }})) >= toFloat64OrNull({{ value }})
+ {% elif operator == 'lessThanOrEqual' %}
+ AND toFloat64OrNull(JSONExtractString(metadata, {{ metadataKey }})) <= toFloat64OrNull({{ value }})
+ {% end %}
{% end %}
{% end %}
{% end %}
@@ -693,6 +710,23 @@ SQL >
{% elif field == 'saleType' %}
{# saleType is handled after this subquery #}
{% end %}
+ {% elif item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+ {% if operator == 'equals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND toFloat64OrNull(JSONExtractString(se.metadata, {{ metadataKey }})) > toFloat64OrNull({{ value }})
+ {% elif operator == 'lessThan' %}
+ AND toFloat64OrNull(JSONExtractString(se.metadata, {{ metadataKey }})) < toFloat64OrNull({{ value }})
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND toFloat64OrNull(JSONExtractString(se.metadata, {{ metadataKey }})) >= toFloat64OrNull({{ value }})
+ {% elif operator == 'lessThanOrEqual' %}
+ AND toFloat64OrNull(JSONExtractString(se.metadata, {{ metadataKey }})) <= toFloat64OrNull({{ value }})
+ {% end %}
{% end %}
{% end %}
{% end %}
diff --git a/packages/ui/src/filter/types.ts b/packages/ui/src/filter/types.ts
index 1fd4349fd9..d9dd751574 100644
--- a/packages/ui/src/filter/types.ts
+++ b/packages/ui/src/filter/types.ts
@@ -1,3 +1,4 @@
+import { type FilterOperator } from "@dub/utils";
import { LucideIcon } from "lucide-react";
import { ComponentType, ReactNode, SVGProps } from "react";
@@ -6,7 +7,7 @@ type FilterIcon =
| ReactNode
| ComponentType<SVGProps<SVGSVGElement>>;
-export type FilterOperator = "IS" | "IS_NOT" | "IS_ONE_OF" | "IS_NOT_ONE_OF";
+export type { FilterOperator };
export type Filter = {
key: string;