|
| 1 | +import type { EventCriteria } from '@openpanel/validation'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { buildEventCriteriaQuery } from './cohort.service'; |
| 4 | + |
| 5 | +const PROJECT_ID = 'test-cohort-timeframe'; |
| 6 | + |
| 7 | +function criteria(timeframe: EventCriteria['timeframe']): EventCriteria { |
| 8 | + return { |
| 9 | + name: 'screen_view', |
| 10 | + filters: [], |
| 11 | + timeframe, |
| 12 | + } as EventCriteria; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Remove every quoted string literal from the SQL, leaving the skeleton the |
| 17 | + * ClickHouse parser would act on. Anything a caller smuggled in stays inside a |
| 18 | + * literal if it was escaped properly, so it disappears here; if it broke out, |
| 19 | + * it shows up in the skeleton. |
| 20 | + */ |
| 21 | +function skeleton(sql: string): string { |
| 22 | + return sql.replace(/'(?:\\.|[^'\\])*'/g, "''"); |
| 23 | +} |
| 24 | + |
| 25 | +// Values that break out of a naive `toDate('${value}')` interpolation. |
| 26 | +const HOSTILE = [ |
| 27 | + "2024-01-01') OR 1=1 --", |
| 28 | + "2024-01-01'", |
| 29 | + "2024-01-01') UNION ALL SELECT id FROM profiles --", |
| 30 | +]; |
| 31 | + |
| 32 | +describe('buildEventCriteriaQuery timeframe escaping', () => { |
| 33 | + it.each(HOSTILE)('escapes a hostile start (%s)', (start) => { |
| 34 | + const sql = buildEventCriteriaQuery( |
| 35 | + PROJECT_ID, |
| 36 | + criteria({ type: 'absolute', start }) |
| 37 | + ); |
| 38 | + |
| 39 | + // The value survives as exactly one quoted literal inside toDate(...). |
| 40 | + expect(sql).toContain( |
| 41 | + `event_date >= toDate('${start.replace(/'/g, "\\'")}')` |
| 42 | + ); |
| 43 | + // The parser sees the same shape as a well-formed date: no extra clause, |
| 44 | + // no early close of the toDate() call, no trailing comment. |
| 45 | + expect(skeleton(sql)).toContain("event_date >= toDate('')"); |
| 46 | + expect(skeleton(sql)).not.toMatch(/OR|UNION|--/); |
| 47 | + }); |
| 48 | + |
| 49 | + it.each(HOSTILE)('escapes a hostile end (%s)', (end) => { |
| 50 | + const sql = buildEventCriteriaQuery( |
| 51 | + PROJECT_ID, |
| 52 | + criteria({ type: 'absolute', start: '2024-01-01', end }) |
| 53 | + ); |
| 54 | + |
| 55 | + expect(sql).toContain( |
| 56 | + `event_date BETWEEN toDate('2024-01-01') AND toDate('${end.replace(/'/g, "\\'")}')` |
| 57 | + ); |
| 58 | + expect(skeleton(sql)).toContain( |
| 59 | + "event_date BETWEEN toDate('') AND toDate('')" |
| 60 | + ); |
| 61 | + expect(skeleton(sql)).not.toMatch(/OR|UNION|--/); |
| 62 | + }); |
| 63 | + |
| 64 | + it('leaves well-formed dates readable', () => { |
| 65 | + expect( |
| 66 | + buildEventCriteriaQuery( |
| 67 | + PROJECT_ID, |
| 68 | + criteria({ type: 'absolute', start: '2024-01-01' }) |
| 69 | + ) |
| 70 | + ).toContain("event_date >= toDate('2024-01-01')"); |
| 71 | + |
| 72 | + expect( |
| 73 | + buildEventCriteriaQuery( |
| 74 | + PROJECT_ID, |
| 75 | + criteria({ type: 'absolute', start: '2024-01-01', end: '2024-02-01' }) |
| 76 | + ) |
| 77 | + ).toContain( |
| 78 | + "event_date BETWEEN toDate('2024-01-01') AND toDate('2024-02-01')" |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('still builds relative timeframes', () => { |
| 83 | + expect( |
| 84 | + buildEventCriteriaQuery( |
| 85 | + PROJECT_ID, |
| 86 | + criteria({ type: 'relative', value: '30d' }) |
| 87 | + ) |
| 88 | + ).toContain('event_date >= toDate(now() - INTERVAL 30 DAY)'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments