: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): stop html-escaping liquid in email subjects and plain text (#120171)
Harley Alexander0933d18c6fc4
diff --git a/nodejs/src/cdp/services/hog-inputs.service.test.ts b/nodejs/src/cdp/services/hog-inputs.service.test.tsindex 7a098e63e308..60f583711cca 100644--- a/nodejs/src/cdp/services/hog-inputs.service.test.ts+++ b/nodejs/src/cdp/services/hog-inputs.service.test.ts@@ -382,6 +382,37 @@ describe('Hog Inputs', () => {)})+ it('escapes liquid output only in the email fields that are written into html', async () => {+ const value = '{{ event.properties.feature }}'+ globals.event.properties.feature = `Logs & "Traces" <beta>`+ hogFunction.inputs = {+ email: {+ templating: 'liquid',+ value: {+ to: { email: '{{person.properties.email}}', name: value },+ from: { email: '[email protected]', name: value },+ subject: `Welcome to ${value}`,+ text: `Welcome to ${value}`,+ preheader: value,+ html: `<p>${value}</p>`,+ },+ },+ }+ hogFunction.inputs_schema = [{ key: 'email', type: 'native_email', required: true, templating: true }]++ const inputs = await hogInputsService.buildInputs(hogFunction, globals)+ const raw = `Logs & "Traces" <beta>`+ const escaped = 'Logs & "Traces" <beta>'+ expect(inputs.email).toMatchObject({+ to: { name: raw },+ from: { name: raw },+ subject: `Welcome to ${raw}`,+ text: `Welcome to ${raw}`,+ preheader: escaped,+ html: `<p>${escaped}</p>`,+ })+ })+it('drops the editor design from a liquid email instead of rendering it', async () => {const tag = '{% if event.event == "test" %}Hi{% endif %}'hogFunction.inputs = {diff --git a/nodejs/src/cdp/services/hog-inputs.service.ts b/nodejs/src/cdp/services/hog-inputs.service.tsindex 06f81b7620e1..b8a0faa0107d 100644--- a/nodejs/src/cdp/services/hog-inputs.service.ts+++ b/nodejs/src/cdp/services/hog-inputs.service.ts@@ -17,6 +17,20 @@ export const EXTEND_OBJECT_KEY = '$$_extend_object'const EMAIL_INPUT_TYPES = ['native_email', 'email']+// Email fields that become headers or the plain-text part, where an HTML entity shows up literally.+// `html` and `preheader` are not here: both are written into the HTML body, so their output stays escaped.+const EMAIL_PLAIN_TEXT_FIELDS = [+ 'subject',+ 'text',+ 'replyTo',+ 'cc',+ 'bcc',+ 'to.email',+ 'to.name',+ 'from.email',+ 'from.name',+]+const ACCESS_TOKEN_PLACEHOLDER_PURPOSE = 'integration-access-token-placeholder'const accessTokenPlaceholderBase = (integrationId: number): string => `${ACCESS_TOKEN_PLACEHOLDER}${integrationId}`@@ -73,7 +87,10 @@ export class HogInputsService {const { design: _design, ...rest } = valuevalue = rest}- return formatLiquidInput(value, newGlobals, key, liquidBudget)+ const plainTextKeys = EMAIL_INPUT_TYPES.includes(schemaTypes[key])+ ? new Set(EMAIL_PLAIN_TEXT_FIELDS.map((field) => `${key}.${field}`))+ : undefined+ return formatLiquidInput(value, newGlobals, key, liquidBudget, plainTextKeys)}if (templating === 'hog' && input?.bytecode) {try {@@ -367,7 +384,8 @@ export const formatLiquidInput = (value: unknown,globals: HogFunctionInvocationGlobalsWithInputs,key?: string,- budget: LiquidRenderBudget = new LiquidRenderBudget()+ budget: LiquidRenderBudget = new LiquidRenderBudget(),+ plainTextKeys?: ReadonlySet<string>): any => {if (value === null || value === undefined) {return value@@ -375,7 +393,9 @@ export const formatLiquidInput = (if (typeof value === 'string') {try {- return LiquidRenderer.renderWithHogFunctionGlobals(value, globals, budget)+ return LiquidRenderer.renderWithHogFunctionGlobals(value, globals, budget, {+ escapeHtml: !(key && plainTextKeys?.has(key)),+ })} catch (error) {// The renderer's message names the template line the owner has to fix, so it stays as is.const message = error instanceof Error ? error.message : String(error)@@ -384,14 +404,14 @@ export const formatLiquidInput = (}if (Array.isArray(value)) {- return value.map((item) => formatLiquidInput(item, globals, key, budget))+ return value.map((item) => formatLiquidInput(item, globals, key, budget, plainTextKeys))}if (typeof value === 'object' && value !== null) {return Object.fromEntries(Object.entries(value).map(([key2, value]) => [key2,- formatLiquidInput(value, globals, key ? `${key}.${key2}` : key2, budget),+ formatLiquidInput(value, globals, key ? `${key}.${key2}` : key2, budget, plainTextKeys),]))}diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.tsindex cf75681706d2..b1bd974778f8 100644--- a/nodejs/src/cdp/services/messaging/email.service.test.ts+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts@@ -21,7 +21,13 @@ import { TeamWorkflowsConfigService } from '../managers/team-workflows-config.seimport { RateLimiterService } from '../rate-limiter/rate-limiter.service'import { selectEmailSenderIntegrationId } from './email-sender-selection'import { EmailSuppressionService, emailSuppressionConfigFromEnv } from './email-suppression.service'-import { EmailService, parseAddressList, sanitizeEmailSubject, teamEmailCapBuckets } from './email.service'+import {+ EmailService,+ formatMailbox,+ parseAddressList,+ sanitizeEmailSubject,+ teamEmailCapBuckets,+} from './email.service'import { MailDevAPI } from './helpers/maildev'import { EmailTrackingCodeSigner } from './helpers/tracking-code'import { MessageAssetsService } from './message-assets.service'@@ -57,6 +63,24 @@ describe('sanitizeEmailSubject', () => {})})+describe('formatMailbox', () => {+ it.each([+ ['no name', undefined, '[email protected]'],+ ['an empty name', ' ', '[email protected]'],+ ['a plain name', 'Dry Run', '"Dry Run" <[email protected]>'],+ ['an apostrophe and ampersand', "O'Brien & Co", `"O'Brien & Co" <[email protected]>`],+ ['a double quote', 'The "Real" Dry', '"The \\"Real\\" Dry" <[email protected]>'],+ ['a backslash', 'Dry\\Run', '"Dry\\\\Run" <[email protected]>'],+ [+ 'a line break that would start a header',+ 'Dry\r\nBcc: [email protected]',+ '"DryBcc: [email protected]" <[email protected]>',+ ],+ ])('formats %s', (_name, name, expected) => {+ expect(formatMailbox(name, '[email protected]')).toEqual(expected)+ })+})+describe('parseAddressList', () => {it.each([['clean input', '[email protected], [email protected]', ['[email protected]', '[email protected]']],diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.tsindex c739a9693862..204a87f93836 100644--- a/nodejs/src/cdp/services/messaging/email.service.ts+++ b/nodejs/src/cdp/services/messaging/email.service.ts@@ -324,6 +324,14 @@ function sanitizeFromName(name: string): string {return name.replace(/[\x00-\x1F\x7F"<>\\]/g, '').trim()}+// A display name can come from Liquid output, so it may hold quotes, backslashes or line breaks.+// Escape it as an RFC 5322 quoted string and drop control characters, which also stops a templated+// name from injecting a header.+export function formatMailbox(name: string | undefined, email: string): string {+ const cleaned = (name ?? '').replace(/[\x00-\x1F\x7F]/g, '').trim()+ return cleaned ? `"${cleaned.replace(/[\\"]/g, '\\$&')}" <${email}>` : email+}+export function parseAddressList(value?: string): string[] | undefined {if (!value || !value.trim()) {return undefined@@ -1033,8 +1041,8 @@ export class EmailService {): Promise<void> {// This can timeout but there is no native timeout so we do our own oneconst mailOptions: SendMailOptions = {- from: from.name ? `"${from.name}" <${from.email}>` : from.email,- to: params.to.name ? `"${params.to.name}" <${params.to.email}>` : params.to.email,+ from: formatMailbox(from.name, from.email),+ to: formatMailbox(params.to.name, params.to.email),subject: sanitizeEmailSubject(params.subject),...(params.text ? { text: params.text } : {}),headers: { [AUTO_SUBMITTED_HEADER.Name!]: AUTO_SUBMITTED_HEADER.Value! },@@ -1108,9 +1116,9 @@ export class EmailService {: {}const sendEmailParams: SendEmailCommandInput = {- FromEmailAddress: from.name ? `"${from.name}" <${from.email}>` : from.email,+ FromEmailAddress: formatMailbox(from.name, from.email),Destination: {- ToAddresses: [params.to.name ? `"${params.to.name}" <${params.to.email}>` : params.to.email],+ ToAddresses: [formatMailbox(params.to.name, params.to.email)],},Content: {Simple: {diff --git a/nodejs/src/cdp/utils/liquid.ts b/nodejs/src/cdp/utils/liquid.tsindex abd677b2d658..7eea32080a10 100644--- a/nodejs/src/cdp/utils/liquid.ts+++ b/nodejs/src/cdp/utils/liquid.ts@@ -84,33 +84,46 @@ const decodeLiquidTags = (template: string): string => {return out + template.slice(cursor)}+const createLiquid = (outputEscape: 'escape' | undefined): Liquid => {+ const liquid = new Liquid({+ outputEscape,+ // Render partials from an in-memory map only: this disables LiquidJS's filesystem-backed+ // partial loading, so user-controlled templates can't read local files via include/render/layout.+ templates: {},+ parseLimit: LIQUID_PARSE_LIMIT_CHARS,+ renderLimit: LIQUID_RENDER_LIMIT_MS,+ memoryLimit: LIQUID_MEMORY_LIMIT_UNITS,+ })+ for (const name of LIQUID_UNBOUNDED_FILTERS) {+ liquid.registerFilter(name, () => {+ throw new Error(`liquid filter ${name} is not supported`)+ })+ }+ return liquid+}++export type LiquidRenderOptions = {+ // Only for output that is never parsed as HTML, such as an email subject or its plain-text part.+ escapeHtml?: boolean+}+export class LiquidRenderer {private static _liquid: Liquid | null = null+ private static _plainTextLiquid: Liquid | null = nullprivate static get liquid(): Liquid {- if (!this._liquid) {- this._liquid = new Liquid({- outputEscape: 'escape',- // Render partials from an in-memory map only: this disables LiquidJS's filesystem-backed- // partial loading, so user-controlled templates can't read local files via include/render/layout.- templates: {},- parseLimit: LIQUID_PARSE_LIMIT_CHARS,- renderLimit: LIQUID_RENDER_LIMIT_MS,- memoryLimit: LIQUID_MEMORY_LIMIT_UNITS,- })- for (const name of LIQUID_UNBOUNDED_FILTERS) {- this._liquid.registerFilter(name, () => {- throw new Error(`liquid filter ${name} is not supported`)- })- }- }- return this._liquid+ return (this._liquid ??= createLiquid('escape'))+ }++ private static get plainTextLiquid(): Liquid {+ return (this._plainTextLiquid ??= createLiquid(undefined))}static renderWithHogFunctionGlobals(template: string,globals: HogFunctionInvocationGlobalsWithInputs,- budget: LiquidRenderBudget = new LiquidRenderBudget()+ budget: LiquidRenderBudget = new LiquidRenderBudget(),+ { escapeHtml = true }: LiquidRenderOptions = {}): string {// Checked before decoding so the decoder never sees a string LiquidJS would reject anyway.if (template.length > LIQUID_PARSE_LIMIT_CHARS) {@@ -130,7 +143,8 @@ export class LiquidRenderer {const start = performance.now()try {- const result: string = this.liquid.parseAndRenderSync(decodeLiquidTags(template), context, {+ const liquid = escapeHtml ? this.liquid : this.plainTextLiquid+ const result: string = liquid.parseAndRenderSync(decodeLiquidTags(template), context, {renderLimit: budget.remainingMs(),})// String length is O(1) on a V8 rope, so this rejects a huge result before anything flattens it.