Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(workflows): block broadcast launch on Liquid that can't parse (#125621)

Daniel Marchuk736da9a72a97
diff --git a/frontend/src/lib/utils/liquid.test.ts b/frontend/src/lib/utils/liquid.test.ts
index e75279edcadd..002d024a6eea 100644
--- a/frontend/src/lib/utils/liquid.test.ts
+++ b/frontend/src/lib/utils/liquid.test.ts
@@ -3,11 +3,13 @@ import { LiquidRenderer } from './liquid'
describe('LiquidRenderer', () => {
const context = { person: { properties: { first_name: 'Ada', company: null } } }
- it('renders a template the editor has escaped', () => {
- // Unlayer stores `>` as `>`, so a renderer that skips the decode step compares the
- // literal entity and silently takes the wrong branch.
- const template =
- '{% if person.properties.first_name != "" %}Hi {{ person.properties.first_name }}{% endif %}'
+ // Unlayer stores `>` as `>`, so a renderer that skips the decode step compares the
+ // literal entity and silently takes the wrong branch.
+ it.each([
+ ['on one line', '{% if person.properties.first_name != "" %}'],
+ ['across lines', '{% if person.properties.first_name\n != "" %}'],
+ ])('renders a tag the editor has escaped %s', (_, openTag) => {
+ const template = `${openTag}Hi {{ person.properties.first_name }}{% endif %}`
expect(LiquidRenderer.render(template, context)).toBe('Hi Ada')
})
diff --git a/frontend/src/lib/utils/liquid.ts b/frontend/src/lib/utils/liquid.ts
index af2c9143ef4d..6a025620ee08 100644
--- a/frontend/src/lib/utils/liquid.ts
+++ b/frontend/src/lib/utils/liquid.ts
@@ -1,7 +1,7 @@
// Lazy load liquid library
import { Context, Emitter, Liquid, Output, Tag, TagToken, Template, TopLevelToken } from 'liquidjs'
-const LIQUID_REGEX = /\{\{(.*?)\}\}|{%(.*?)%}/g
+const LIQUID_TAG_CLOSERS: Record<string, string> = { '{': '}}', '%': '%}' }
// Copy of the currency filter in nodejs/src/cdp/utils/liquid.ts, so previews format money the way sends do.
const REGION_CURRENCIES: Record<string, string> = {
@@ -151,8 +151,33 @@ export class LiquidRenderer {
// TRICKY: Unlayer replaces all liquid's elements like > for example with &gt;
// We need to decode these but _only_ for the liquid elements i.e. content within {{ }} or {% %}
+ // Copy of decodeLiquidTags in nodejs/src/cdp/utils/liquid.ts: a single forward pass, so a tag can span
+ // lines without a regex rescanning the rest of the template for every unclosed opener.
private static decode(template: string): string {
- return template.replace(LIQUID_REGEX, (match) => this.decodeEntities(match))
+ let out = ''
+ let cursor = 0
+ const exhausted = new Set<string>()
+
+ while (cursor < template.length) {
+ const open = template.indexOf('{', cursor)
+ if (open === -1) {
+ break
+ }
+ const closer = LIQUID_TAG_CLOSERS[template[open + 1]]
+ const close = closer && !exhausted.has(closer) ? template.indexOf(closer, open + 2) : -1
+ if (close === -1) {
+ if (closer) {
+ exhausted.add(closer)
+ }
+ out += template.slice(cursor, open + 1)
+ cursor = open + 1
+ continue
+ }
+ out += template.slice(cursor, open) + this.decodeEntities(template.slice(open, close + 2))
+ cursor = close + 2
+ }
+
+ return out + template.slice(cursor)
}
public static parse(template: string): Template[] {
diff --git a/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx b/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx
index 2b7d743727e2..0a714108861d 100644
--- a/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx
+++ b/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx
@@ -54,7 +54,7 @@ export type EmailEditorMode = 'full' | 'preview'
// Maps a templater field key onto its validation message slot. Only the sender, recipient, and
// subject rows have their own message; body content is reported separately near the editor.
function fieldErrorFor(fieldKey: string, fieldErrors?: EmailFieldErrors): string | undefined {
- if (fieldKey === 'from' || fieldKey === 'to' || fieldKey === 'subject') {
+ if (fieldKey === 'from' || fieldKey === 'to' || fieldKey === 'subject' || fieldKey === 'preheader') {
return fieldErrors?.[fieldKey]
}
return undefined
diff --git a/frontend/src/scenes/hog-functions/email-templater/types.ts b/frontend/src/scenes/hog-functions/email-templater/types.ts
index 53d8803bac72..1561d4ce6d25 100644
--- a/frontend/src/scenes/hog-functions/email-templater/types.ts
+++ b/frontend/src/scenes/hog-functions/email-templater/types.ts
@@ -35,5 +35,6 @@ export type EmailFieldErrors = {
from?: string
to?: string
subject?: string
+ preheader?: string
body?: string
}
diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
index 08c552c097ee..7b54e32e6f78 100644
--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
@@ -17,7 +17,13 @@ import type { BlastRadiusApi, HogFlowApi } from 'products/workflows/frontend/gen
import { draftMessage } from '../MessageAudience/messageDrafts'
import { urlForNewBroadcastWithAudience } from './broadcastAudiencePrefill'
import { broadcastTestSendLogic } from './broadcastTestSendLogic'
-import { DEFAULT_BROADCAST_EMAIL, DELETED_SENDER_ERROR, broadcastWizardLogic } from './broadcastWizardLogic'
+import {
+ DEFAULT_BROADCAST_EMAIL,
+ DEFAULT_BROADCAST_PUSH,
+ DELETED_SENDER_ERROR,
+ broadcastWizardLogic,
+ getBroadcastTemplatingErrors,
+} from './broadcastWizardLogic'
const LOCAL_AUDIENCE: AnyPropertyFilter[] = [
{ key: 'plan', value: ['pro'], operator: PropertyOperator.Exact, type: PropertyFilterType.Person },
@@ -859,6 +865,63 @@ describe('broadcastWizardLogic', () => {
expect(logic.values.stepValidationErrors.review).toEqual(expected)
})
+ it.each([
+ {
+ field: 'link in the body',
+ email: { html: '<a href="https://example.com/?n={{%20person.name%20}}">Hi</a>' },
+ error: /^A link has a variable with spaces inside/,
+ },
+ { field: 'subject', email: { subject: 'Hi {{ person.name }' }, error: /^Liquid template error: / },
+ ])('blocks the content step on Liquid that cannot parse in the $field', ({ email, error }) => {
+ const valid = {
+ ...DEFAULT_BROADCAST_EMAIL,
+ from: { integrationId: 1 },
+ subject: 'Spring sale',
+ html: '<p>Hi {{ person.properties.name }}</p>',
+ }
+ logic.actions.setEmail({ ...valid, ...email })
+ expect(logic.values.stepValidationErrors.content).toEqual([expect.stringMatching(error)])
+
+ logic.actions.setEmail(valid)
+ expect(logic.values.stepValidationErrors.content).toEqual([])
+ })
+
+ it('puts a preheader error on the preheader, and parses a tag that spans lines', () => {
+ const email = {
+ ...DEFAULT_BROADCAST_EMAIL,
+ subject: 'Spring sale',
+ preheader: 'Hi {{ person.name }',
+ html: '<p>{{\n person.properties.name | default: &quot;friend&quot;\n}}</p>',
+ }
+
+ expect(getBroadcastTemplatingErrors('email', email, DEFAULT_BROADCAST_PUSH, null)).toEqual({
+ subject: undefined,
+ preheader: expect.stringMatching(/^Liquid template error: /),
+ body: undefined,
+ to: undefined,
+ })
+ })
+
+ it('leaves push text saved as Hog unparsed', () => {
+ const broadcast = {
+ actions: [
+ {
+ type: 'function_push',
+ config: { inputs: { title: { value: '', templating: 'hog' }, body: { value: '' } } },
+ },
+ ],
+ } as unknown as HogFlowApi
+ const push = { ...DEFAULT_BROADCAST_PUSH, title: 'Hi {person.name}', body: 'Sale {{ ends' }
+
+ expect(getBroadcastTemplatingErrors('push', DEFAULT_BROADCAST_EMAIL, push, broadcast)).toEqual({
+ title: undefined,
+ message: undefined,
+ })
+ expect(getBroadcastTemplatingErrors('push', DEFAULT_BROADCAST_EMAIL, push, null).message).toMatch(
+ /^Liquid template error: /
+ )
+ })
+
it('counts the audience missing the To field property on the review step only, and never for an unfiltered audience', async () => {
const missingByProperty: Record<string, number> = { email: 98, $email: 0 }
const checkedFilters: { key: string; operator?: string }[][] = []
diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
index 1525a454c74c..7e58fb792e5b 100644
--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
@@ -88,6 +88,7 @@ import {
keepOnlyRealtimeCohort,
} from '../Workflows/hogflows/steps/realtimeCohortStatus'
import { recipientEmailProperty } from '../Workflows/hogflows/steps/recipientEmail'
+import { getTemplatingError } from '../Workflows/hogflows/steps/templatingError'
import { ResourceSaveQueue } from '../Workflows/resourceSaveQueue'
import {
type AudienceCohortLaunchError,
@@ -249,10 +250,39 @@ function readPush(broadcast: HogFlowApi): BroadcastPushValue | null {
}
// A saved input without `templating` renders as Hog in the worker, so only a new input defaults to Liquid.
-function pushTemplating(existing: { templating?: string } | undefined): string {
+function inputTemplating(existing: { templating?: string } | undefined): string {
return existing ? (existing.templating ?? 'hog') : 'liquid'
}
+export type BroadcastTemplatingErrors = Partial<
+ Record<'subject' | 'preheader' | 'body' | 'to' | 'title' | 'message', string>
+>
+
+// The worker parses the template for each recipient, so one that can't parse fails every send.
+export function getBroadcastTemplatingErrors(
+ channel: BroadcastChannel,
+ email: BroadcastEmailValue,
+ push: BroadcastPushValue,
+ broadcast: HogFlowApi | null
+): BroadcastTemplatingErrors {
+ if (channel === 'push') {
+ const inputs = findPushInputs(broadcast)
+ return {
+ title: getTemplatingError(push.title, inputTemplating(inputs?.title)),
+ message: getTemplatingError(push.body, inputTemplating(inputs?.body)),
+ }
+ }
+ const templating = inputTemplating(
+ broadcast ? findAction(broadcast, 'function_email')?.config?.inputs?.email : undefined
+ )
+ return {
+ subject: getTemplatingError(email.subject, templating),
+ preheader: getTemplatingError(email.preheader ?? '', templating),
+ body: getTemplatingError(email.html, templating) ?? getTemplatingError(email.text, templating),
+ to: getTemplatingError(email.to?.email ?? '', templating),
+ }
+}
+
// Inputs the wizard does not show, such as a custom recipient or extra data keys set in the workflow
// editor, keep their saved values.
export function pushInputs(push: BroadcastPushValue, existing: Record<string, any> = {}): Record<string, any> {
@@ -261,8 +291,8 @@ export function pushInputs(push: BroadcastPushValue, existing: Record<string, an
// A batch run's person is the recipient, as in the template's default.
distinctId: existing.distinctId ?? { value: '{event.distinct_id}' },
channels: { value: push.channels },
- title: { value: push.title, templating: pushTemplating(existing.title) },
- body: { value: push.body, templating: pushTemplating(existing.body) },
+ title: { value: push.title, templating: inputTemplating(existing.title) },
+ body: { value: push.body, templating: inputTemplating(existing.body) },
data: existing.data ?? { value: {} },
}
}
@@ -419,6 +449,7 @@ export interface broadcastWizardLogicValues {
summaryStatus: BroadcastStatus
summaryTab: BroadcastSummaryTab
tags: string[]
+ templatingErrors: BroadcastTemplatingErrors
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -785,6 +816,12 @@ export interface broadcastWizardLogicMeta {
blastRadius: BlastRadiusApi | null
) => MissingRecipientEmailBannerProps | null
selectedSender: (email: BroadcastEmailValue, integrations: IntegrationType[] | null) => IntegrationType | null
+ templatingErrors: (
+ channel: BroadcastChannel,
+ email: BroadcastEmailValue,
+ push: BroadcastPushValue,
+ broadcast: HogFlowApi | null
+ ) => BroadcastTemplatingErrors
stepValidationErrors: (
goalEnabled: boolean,
conversion: HogFlowConversionApi,
@@ -799,7 +836,8 @@ export interface broadcastWizardLogicMeta {
audienceCohortLaunchErrors: AudienceCohortLaunchError[],
blastRadius: BlastRadiusApi | null,
channel: BroadcastChannel,
- push: BroadcastPushValue
+ push: BroadcastPushValue,
+ templatingErrors: Partial<Record<'body' | 'message' | 'preheader' | 'subject' | 'title' | 'to', string>>
) => Record<BroadcastWizardStep, string[]>
currentStepHasErrors: (
stepValidationErrors: Record<BroadcastWizardStep, string[]>,
@@ -1516,6 +1554,15 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
(integration) => integration.kind === 'email' && integration.id === email.from?.integrationId
) ?? null,
],
+ templatingErrors: [
+ (s) => [s.channel, s.email, s.push, s.broadcast],
+ (
+ channel: BroadcastChannel,
+ email: BroadcastEmailValue,
+ push: BroadcastPushValue,
+ broadcast: HogFlowApi | null
+ ): BroadcastTemplatingErrors => getBroadcastTemplatingErrors(channel, email, push, broadcast),
+ ],
stepValidationErrors: [
(s) => [
s.goalEnabled,
@@ -1532,6 +1579,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
s.blastRadius,
s.channel,
s.push,
+ s.templatingErrors,
],
(
goalEnabled: boolean,
@@ -1547,7 +1595,8 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
audienceCohortLaunchErrors: AudienceCohortLaunchError[],
blastRadius: BlastRadiusApi | null,
channel: BroadcastChannel,
- push: BroadcastPushValue
+ push: BroadcastPushValue,
+ templatingErrors: BroadcastTemplatingErrors
): Record<BroadcastWizardStep, string[]> => {
const errors: Record<BroadcastWizardStep, string[]> = {
recipients: [],
@@ -1595,6 +1644,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
errors.content.push('Add email content')
}
}
+ errors.content.push(...Object.values(templatingErrors).filter((error): error is string => !!error))
if (scheduleMode === 'later') {
if (!sendAt) {
diff --git a/products/workflows/frontend/Broadcasts/steps/BroadcastContentStep.tsx b/products/workflows/frontend/Broadcasts/steps/BroadcastContentStep.tsx
index 788ea6637795..dbec2b503f7e 100644
--- a/products/workflows/frontend/Broadcasts/steps/BroadcastContentStep.tsx
+++ b/products/workflows/frontend/Broadcasts/steps/BroadcastContentStep.tsx
@@ -26,8 +26,17 @@ import { BroadcastChannelSwitch } from './BroadcastChannelSwitch'
import { BroadcastPushComposer } from './BroadcastPushComposer'
export function BroadcastContentStep(): JSX.Element {
- const { broadcast, email, name, stepValidationErrors, selectedSender, emailSettings, aiEmailDraftStatus, channel } =
- useValues(broadcastWizardLogic)
+ const {
+ broadcast,
+ email,
+ name,
+ stepValidationErrors,
+ templatingErrors,
+ selectedSender,
+ emailSettings,
+ aiEmailDraftStatus,
+ channel,
+ } = useValues(broadcastWizardLogic)
const { setEmail, setEmailSettings } = useActions(broadcastWizardLogic)
const { integrations, integrationsLoading } = useValues(integrationsLogic)
const { loadIntegrations } = useActions(integrationsLogic)
@@ -68,11 +77,15 @@ export function BroadcastContentStep(): JSX.Element {
}
}
- const errors = stepValidationErrors.content
+ // A parser message can quote the template, so it is kept out of the matching by wording below.
+ const templatingMessages: (string | undefined)[] = Object.values(templatingErrors)
+ const errors = stepValidationErrors.content.filter((error) => !templatingMessages.includes(error))
const fieldErrors: EmailFieldErrors = {
from: errors.find((error) => error.includes('sender')),
- subject: errors.find((error) => error.includes('subject')),
- body: errors.find((error) => error.includes('content')),
+ to: templatingErrors.to,
+ subject: errors.find((error) => error.includes('subject')) ?? templatingErrors.subject,
+ preheader: templatingErrors.preheader,
+ body: errors.find((error) => error.includes('content')) ?? templatingErrors.body,
}
// A push broadcast keeps its composer even with the flag off, so it can still be read and fixed.
diff --git a/products/workflows/frontend/Broadcasts/steps/BroadcastPushComposer.tsx b/products/workflows/frontend/Broadcasts/steps/BroadcastPushComposer.tsx
index 94e5489bdb44..806ce31ca2da 100644
--- a/products/workflows/frontend/Broadcasts/steps/BroadcastPushComposer.tsx
+++ b/products/workflows/frontend/Broadcasts/steps/BroadcastPushComposer.tsx
@@ -18,11 +18,13 @@ const CHANNELS_SCHEMA: CyclotronJobInputSchemaType = {
}
export function BroadcastPushComposer(): JSX.Element {
- const { push, stepValidationErrors } = useValues(broadcastWizardLogic)
+ const { push, stepValidationErrors, templatingErrors } = useValues(broadcastWizardLogic)
const { setPush } = useActions(broadcastWizardLogic)
- const errors = stepValidationErrors.content
+ const templatingMessages: (string | undefined)[] = Object.values(templatingErrors)
+ const errors = stepValidationErrors.content.filter((error) => !templatingMessages.includes(error))
const channelsError = errors.find((error) => error.includes('channel'))
- const titleError = errors.find((error) => error.includes('title'))
+ const titleError = errors.find((error) => error.includes('title')) ?? templatingErrors.title
+ const messageError = templatingErrors.message
return (
<div className="grid grid-cols-1 gap-4 @3xl:grid-cols-2">
@@ -65,6 +67,7 @@ export function BroadcastPushComposer(): JSX.Element {
minRows={3}
data-attr="broadcast-push-body"
/>
+ {messageError ? <span className="text-xs text-danger">{messageError}</span> : null}
</div>
</div>
<BroadcastPushPreview />
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts b/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts
new file mode 100644
index 000000000000..e4e2910fabf6
--- /dev/null
+++ b/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts
@@ -0,0 +1,17 @@
+import { LiquidRenderer } from 'lib/utils/liquid'
+
+// The email editor saves a link's spaces as %20, including the spaces inside a {{ }} tag.
+const ENCODED_SPACE_IN_TAG = /\{[{%][^}]*%20/
+
+export function getTemplatingError(value: string, templating?: string): string | undefined {
+ if (templating === 'liquid' && typeof value === 'string') {
+ try {
+ LiquidRenderer.parse(value)
+ } catch (e: any) {
+ if (ENCODED_SPACE_IN_TAG.test(value)) {
+ return 'A link has a variable with spaces inside {{ }}, which the email editor saves as %20. Remove the spaces, for example {{person.properties.name}}.'
+ }
+ return `Liquid template error: ${e.message}`
+ }
+ }
+}
diff --git a/products/workflows/frontend/Workflows/workflowLogic.ts b/products/workflows/frontend/Workflows/workflowLogic.ts
index 6eeb3f1c01f9..a46508e1a74f 100644
--- a/products/workflows/frontend/Workflows/workflowLogic.ts
+++ b/products/workflows/frontend/Workflows/workflowLogic.ts
@@ -16,7 +16,6 @@ import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { type FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { publicWebhooksHostOrigin } from 'lib/utils/apiHost'
-import { LiquidRenderer } from 'lib/utils/liquid'
import { objectsEqual } from 'lib/utils/objects'
import { sanitizeInputs } from 'scenes/hog-functions/configuration/hogFunctionConfigurationLogic'
import type { EmailFieldErrors } from 'scenes/hog-functions/email-templater/types'
@@ -65,6 +64,7 @@ import {
stateToRRule,
} from './hogflows/steps/components/rrule-helpers'
import type { ScheduleState } from './hogflows/steps/components/rrule-helpers'
+import { getTemplatingError } from './hogflows/steps/templatingError'
import {
HogFlowActionSchema,
SCHEDULED_TRIGGER_TYPES,
@@ -182,16 +182,6 @@ async function withDefaultEmailSender(workflow: HogFlow): Promise<HogFlow> {
}
}
-function getTemplatingError(value: string, templating?: 'liquid' | 'hog'): string | undefined {
- if (templating === 'liquid' && typeof value === 'string') {
- try {
- LiquidRenderer.parse(value)
- } catch (e: any) {
- return `Liquid template error: ${e.message}`
- }
- }
-}
-
export function sanitizeWorkflow(
workflow: HogFlow,
hogFunctionTemplatesById: Record<string, HogFunctionTemplateType>