: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): read link variables the email editor saved with encoded spaces (#126246)
Daniel Marchukf87d26cf648e
diff --git a/frontend/src/lib/utils/liquid.test.ts b/frontend/src/lib/utils/liquid.test.tsindex 002d024a6eea..ba1f26a9bd88 100644--- a/frontend/src/lib/utils/liquid.test.ts+++ b/frontend/src/lib/utils/liquid.test.ts@@ -8,6 +8,8 @@ describe('LiquidRenderer', () => {it.each([['on one line', '{% if person.properties.first_name != "" %}'],['across lines', '{% if person.properties.first_name\n != "" %}'],+ // The email editor saves a link's spaces as %20, inside the tag too.+ ['in a link', '{%%20if%20person.properties.first_name%20!=%20""%20%}'],])('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')@@ -22,6 +24,13 @@ describe('LiquidRenderer', () => {expect(LiquidRenderer.resolves(expression, context)).toBe(expected)})+ it.each([+ ['{{%20person.properties.company%20|%20default:%20"a%20b"%20}}', 'a%20b'],+ ['{{%20"a\\"b%20c"%20|%20size%20}}', '7'],+ ])('keeps %%20 inside a quoted string, where it is part of the value: %s', (template, expected) => {+ expect(LiquidRenderer.render(template, context)).toBe(expected)+ })+it('formats money with the currency filter, like the worker does at send time', () => {expect(LiquidRenderer.render('{{ amount | currency: "USD" }}', { amount: 1234.5 })).toBe('$1,234.50')})diff --git a/frontend/src/lib/utils/liquid.ts b/frontend/src/lib/utils/liquid.tsindex 6a025620ee08..1d07e85f2c79 100644--- a/frontend/src/lib/utils/liquid.ts+++ b/frontend/src/lib/utils/liquid.ts@@ -149,6 +149,38 @@ export class LiquidRenderer {.replace(/&/g, '&') // NOTE: This should always be last}+ // The email editor saves every space in a link URL as %20, including the spaces inside a tag, so+ // `{{ name | url_encode }}` arrives as `{{%20name%20|%20url_encode%20}}`. A quoted string keeps its %20,+ // because there it is part of the value and a link needs it encoded.+ private static decodeEncodedSpaces(tag: string): string {+ if (!tag.includes('%20')) {+ return tag+ }+ let out = ''+ let quote = ''+ let escaped = false+ for (let i = 0; i < tag.length; i++) {+ const char = tag[i]+ if (quote) {+ if (escaped) {+ escaped = false+ } else if (char === '\\') {+ escaped = true+ } else if (char === quote) {+ quote = ''+ }+ } else if (char === '"' || char === "'") {+ quote = char+ } else if (tag.startsWith('%20', i)) {+ out += ' '+ i += 2+ continue+ }+ out += char+ }+ return out+ }+// TRICKY: Unlayer replaces all liquid's elements like > for example with >// 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@@ -173,7 +205,9 @@ export class LiquidRenderer {cursor = open + 1continue}- out += template.slice(cursor, open) + this.decodeEntities(template.slice(open, close + 2))+ out +=+ template.slice(cursor, open) ++ this.decodeEncodedSpaces(this.decodeEntities(template.slice(open, close + 2)))cursor = close + 2}diff --git a/nodejs/src/cdp/utils/liquid.test.ts b/nodejs/src/cdp/utils/liquid.test.tsindex ddaa138ce0e5..989bd6dc93c4 100644--- a/nodejs/src/cdp/utils/liquid.test.ts+++ b/nodejs/src/cdp/utils/liquid.test.ts@@ -168,6 +168,15 @@ describe('LiquidRenderer', () => {expect(result).toMatchInlineSnapshot(`"hello!"`)})+ it.each([+ ['a link variable', '<a href="/?n={{%20person.name%20|%20url_encode%20}}">', '<a href="/?n=test_person">'],+ ['a quoted string', '{{%20person.missing%20|%20default:%20"a%20b"%20}}', 'a%20b'],+ ['a tag', '{%%20if%201%20<%202%20%}yes{%%20endif%20%}', 'yes'],+ ['a quoted string with an escaped quote', '{{%20"a\\"b%20c"%20|%20size%20}}', '7'],+ ])('reads spaces the email editor saved as %%20 in %s', (_, template, expected) => {+ expect(LiquidRenderer.renderWithHogFunctionGlobals(template, globals)).toBe(expected)+ })+it('preserves $ in template', () => {const template = '{{ "$100" }}'const result = LiquidRenderer.renderWithHogFunctionGlobals(template, globals)diff --git a/nodejs/src/cdp/utils/liquid.ts b/nodejs/src/cdp/utils/liquid.tsindex 602c32ee7456..6794e5689fb0 100644--- a/nodejs/src/cdp/utils/liquid.ts+++ b/nodejs/src/cdp/utils/liquid.ts@@ -53,6 +53,38 @@ const decodeEntities = (tag: string): string => {.replace(/&/g, '&') // NOTE: This should always be last}+// The email editor saves every space in a link URL as %20, including the spaces inside a tag, so+// `{{ name | url_encode }}` arrives as `{{%20name%20|%20url_encode%20}}`. A quoted string keeps its %20,+// because there it is part of the value and a link needs it encoded.+const decodeEncodedSpaces = (tag: string): string => {+ if (!tag.includes('%20')) {+ return tag+ }+ let out = ''+ let quote = ''+ let escaped = false+ for (let i = 0; i < tag.length; i++) {+ const char = tag[i]+ if (quote) {+ if (escaped) {+ escaped = false+ } else if (char === '\\') {+ escaped = true+ } else if (char === quote) {+ quote = ''+ }+ } else if (char === '"' || char === "'") {+ quote = char+ } else if (tag.startsWith('%20', i)) {+ out += ' '+ i += 2+ continue+ }+ out += char+ }+ return out+}+// TRICKY: Unlayer replaces all liquid's elements like > for example with >// We need to decode these but _only_ for the liquid elements i.e. content within {{ }} or {% %}// This is a single forward pass: a regex with a lazy `.*?` rescans to the end of the line for every@@ -77,7 +109,7 @@ const decodeLiquidTags = (template: string): string => {cursor = open + 1continue}- out += template.slice(cursor, open) + decodeEntities(template.slice(open, close + 2))+ out += template.slice(cursor, open) + decodeEncodedSpaces(decodeEntities(template.slice(open, close + 2)))cursor = close + 2}diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.tsindex 7b54e32e6f78..9e31861866b9 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts@@ -866,21 +866,18 @@ describe('broadcastWizardLogic', () => {})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 }) => {+ { field: 'link in the body', email: { html: '<a href="https://example.com/?n={{%20person.name%20}">Hi</a>' } },+ { field: 'subject', email: { subject: 'Hi {{ person.name }' } },+ ])('blocks the content step on Liquid that cannot parse in the $field', ({ email }) => {+ // The email editor saves a link's spaces as %20, inside the tag too.const valid = {...DEFAULT_BROADCAST_EMAIL,from: { integrationId: 1 },subject: 'Spring sale',- html: '<p>Hi {{ person.properties.name }}</p>',+ html: '<a href="https://example.com/?n={{%20person.properties.name%20|%20url_encode%20}}">Hi</a>',}logic.actions.setEmail({ ...valid, ...email })- expect(logic.values.stepValidationErrors.content).toEqual([expect.stringMatching(error)])+ expect(logic.values.stepValidationErrors.content).toEqual([expect.stringMatching(/^Liquid template error: /)])logic.actions.setEmail(valid)expect(logic.values.stepValidationErrors.content).toEqual([])diff --git a/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts b/products/workflows/frontend/Workflows/hogflows/steps/templatingError.tsindex e4e2910fabf6..ed59da003b48 100644--- a/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts+++ b/products/workflows/frontend/Workflows/hogflows/steps/templatingError.ts@@ -1,16 +1,10 @@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}`}}