Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/commands/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hasTinybirdMembershipDelimiter,
isCanonicalFilterOperator,
isCanonicalFilterValue,
isEmptyMembershipArray,
isValuelessFilterOperator,
} from '../lib/filters'
import { parseJsonObject } from '../lib/json'
Expand Down Expand Up @@ -87,6 +88,9 @@ function validateAnalyticsFilter(
if (!isCanonicalFilterOperator(record.op)) {
throw new Error(`${path} requires a canonical "op"`)
}
if (isEmptyMembershipArray(record.value)) {
throw new Error(`${path}: membership arrays cannot be empty`)
}
Comment on lines +91 to +93

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate membership arrays inside analytics params

For funnel or flow calls that put filters in the documented --params structures (such as steps[].filters or global_filters), this check is never reached because buildAnalyticsParams JSON-stringifies object parameters without traversing them. For example, a step containing { "field": "sku", "op": "in", "value": [] } is still sent to the API and receives the server-side rejection this change is intended to catch locally; validate the filter-bearing pipe parameters as well.

Useful? React with 👍 / 👎.

if (
!isValuelessFilterOperator(record.op) &&
(record.value === undefined ||
Expand Down
4 changes: 4 additions & 0 deletions src/commands/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Cli, z } from 'incur'
import { createClient, requireApiKey } from '../lib/client'
import {
isCanonicalFilterOperator,
isEmptyMembershipArray,
isValuelessFilterOperator,
} from '../lib/filters'
import {
Expand Down Expand Up @@ -194,6 +195,9 @@ export function parseSearchFilters(raw: string): unknown[] {
'--filters: each entry must use a canonical "op" (eq, neq, gt, lt, gte, lte, in, nin, startsWith, endsWith, contains, notEmpty, or isEmpty)',
)
}
if (isEmptyMembershipArray(record.value)) {
throw new Error('--filters: membership arrays cannot be empty')
}
if (
!isValuelessFilterOperator(record.op) &&
(record.value === undefined || record.value === null)
Expand Down
4 changes: 4 additions & 0 deletions src/commands/segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hasTinybirdMembershipDelimiter,
isCanonicalFilterValue,
isCanonicalFilterOperator,
isEmptyMembershipArray,
isValuelessFilterOperator,
} from '../lib/filters'
import { parseJsonArray } from '../lib/json'
Expand Down Expand Up @@ -70,6 +71,9 @@ export function buildCreateSegmentBody(options: CreateSegmentOptions) {
if (!isCanonicalFilterOperator(record.op)) {
throw new Error('--filters: each entry requires a canonical "op"')
}
if (isEmptyMembershipArray(record.value)) {
throw new Error('--filters: membership arrays cannot be empty')
}
if (
!isValuelessFilterOperator(record.op) &&
(record.value === undefined ||
Expand Down
5 changes: 5 additions & 0 deletions src/lib/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ export function isCanonicalFilterValue(value: unknown): boolean {
typeof value === 'number' ||
typeof value === 'boolean' ||
(Array.isArray(value) &&
value.length > 0 &&
value.every(
(item) => typeof item === 'string' || typeof item === 'number',
))
)
}

export function isEmptyMembershipArray(value: unknown): boolean {
return Array.isArray(value) && value.length === 0
}

export function hasTinybirdMembershipDelimiter(value: unknown): boolean {
return (
Array.isArray(value) &&
Expand Down
22 changes: 22 additions & 0 deletions test/commands/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ describe('commands/analytics', function () {
).to.throw(/cannot contain "\|"/);
});

it('rejects empty membership arrays, including nested filters', function () {
expect(() =>
buildAnalyticsParams({
filters: JSON.stringify([
{ field: 'browser', op: 'in', value: [] },
]),
}),
).to.throw(/membership arrays cannot be empty/);
expect(() =>
buildAnalyticsParams({
filters: JSON.stringify([
{
field: 'event',
op: 'eq',
value: 'purchase',
filters: [{ field: 'sku', op: 'nin', value: [] }],
},
]),
}),
).to.throw(/membership arrays cannot be empty/);
});

it('merges primitive params through unchanged', function () {
const params = buildAnalyticsParams({
params: '{"limit":10,"group_by":"device"}',
Expand Down
8 changes: 8 additions & 0 deletions test/commands/bodyBuilders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ describe('commands / body builders', function () {
parseSearchFilters('[{"field":"users.browser","op":"notEmpty"}]'),
).to.not.throw();
});

it('rejects empty membership arrays', function () {
expect(() =>
parseSearchFilters(
'[{"field":"users.browser","op":"in","value":[]}]',
),
).to.throw(/membership arrays cannot be empty/);
});
});

// ── Input validation guards ──
Expand Down
9 changes: 9 additions & 0 deletions test/commands/segments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@ describe('commands/segments', function () {
expect(() => createSegmentRun({ title: 'x', filters: '5' })).to.throw(/filters/);
expect(() => createSegmentRun({ title: 'x', filters: '"foo"' })).to.throw(/filters/);
});

it('rejects empty membership arrays', function () {
expect(() =>
createSegmentRun({
title: 'x',
filters: '[{"field":"browser","op":"in","value":[]}]',
}),
).to.throw(/membership arrays cannot be empty/);
});
});
});