: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(surveys): don't let multi-line question text break the Results tab query (#76099)
Phill1cf37589cccc
diff --git a/frontend/src/scenes/surveys/SurveySQLHelper.tsx b/frontend/src/scenes/surveys/SurveySQLHelper.tsxindex e70aded06482..476b72f8dd81 100644--- a/frontend/src/scenes/surveys/SurveySQLHelper.tsx+++ b/frontend/src/scenes/surveys/SurveySQLHelper.tsx@@ -14,7 +14,8 @@ import { Survey, SurveyEventName, SurveyEventProperties, SurveyQuestion } from 'import { buildPartialResponsesFilter, createAnswerFilterHogQLExpression } from './utils'function escapeSqlIdentifier(value: string): string {- return value.replace(/"/g, '""')+ // Collapse newlines first (a multi-line alias is undesirable and fragile), then escape quotes.+ return value.replace(/\s*[\r\n]+\s*/g, ' ').replace(/"/g, '""')}interface SurveySQLHelperProps {diff --git a/frontend/src/scenes/surveys/surveyLogic.test.ts b/frontend/src/scenes/surveys/surveyLogic.test.tsindex 23ba1c920d8f..d1975272a0c7 100644--- a/frontend/src/scenes/surveys/surveyLogic.test.ts+++ b/frontend/src/scenes/surveys/surveyLogic.test.ts@@ -1313,6 +1313,33 @@ describe('survey filters', () => {})})+ it('collapses newlines in question text so the generated HogQL select stays single-line', async () => {+ // Regression for the "Unexpected character U+00E9" crash on the Survey Results tab: a question+ // whose text spans multiple lines used to leak past the `--` comment appended per response+ // column, turning the trailing (often accented) text into invalid HogQL.+ const surveyWithMultilineQuestion: Survey = {+ ...MULTIPLE_CHOICE_SURVEY,+ id: 'multiline-survey',+ questions: [+ {+ type: SurveyQuestionType.Open,+ question: 'Queremos compensar tu experiencia.\nDéjanos tu correo y te contactaremos para ayudarte.',+ description: '',+ },+ ],+ }++ await expectLogic(logic, () => {+ logic.actions.loadSurveySuccess(surveyWithMultilineQuestion)+ }).toDispatchActions(['loadSurveySuccess'])++ const select = (logic.values.dataTableQuery as unknown as { source: { select: string[] } }).source.select+ // The question text is still used as the column comment...+ expect(select.some((col) => col.includes('Déjanos'))).toBe(true)+ // ...but every generated column expression is single-line, so the `--` comment can't leak.+ select.forEach((col) => expect(col).not.toMatch(/[\r\n]/))+ })+it('updates query filters when property filters change', async () => {// Set initial filtersconst initialFilters: AnyPropertyFilter[] = [diff --git a/frontend/src/scenes/surveys/utils.test.ts b/frontend/src/scenes/surveys/utils.test.tsindex b71732dc0ad7..6412d8586638 100644--- a/frontend/src/scenes/surveys/utils.test.ts+++ b/frontend/src/scenes/surveys/utils.test.ts@@ -26,6 +26,7 @@ import {calculateNpsBreakdown,createAnswerFilterHogQLExpression,doesSurveyRepeatOnEveryEvent,+ getExpressionCommentForQuestion,getSurveyNotificationFilters,getRecurringSurveyScheduleInfo,getResolvedSurveyDateRange,@@ -310,6 +311,40 @@ describe('survey utils', () => {})})+ describe('getExpressionCommentForQuestion', () => {+ const makeQuestion = (question: string): SurveyQuestion =>+ ({ id: 'q-1', type: SurveyQuestionType.Open, question }) as SurveyQuestion++ it('returns single-line question text unchanged', () => {+ expect(getExpressionCommentForQuestion(makeQuestion('¿Cómo calificarías tu experiencia?'), 0)).toBe(+ '¿Cómo calificarías tu experiencia?'+ )+ })++ it('collapses newlines so multi-line question text stays on a single line', () => {+ // Regression: a newline followed by a non-ASCII char used to leak past the `--` HogQL+ // comment and crash the Survey Results query with "Unexpected character U+00E9".+ const result = getExpressionCommentForQuestion(+ makeQuestion('Queremos compensar tu experiencia.\nDéjanos tu correo y te contactaremos para ayudarte.'),+ 4+ )+ expect(result).not.toMatch(/[\r\n]/)+ expect(result).toBe(+ 'Queremos compensar tu experiencia. Déjanos tu correo y te contactaremos para ayudarte.'+ )+ })++ it('collapses CRLF and surrounding whitespace', () => {+ expect(getExpressionCommentForQuestion(makeQuestion('line one \r\n line two'), 0)).toBe(+ 'line one line two'+ )+ })++ it('falls back to a positional label when the question is empty or whitespace', () => {+ expect(getExpressionCommentForQuestion(makeQuestion(' '), 2)).toBe('Question 3')+ })+ })+describe('sanitizeSurveyAppearance', () => {it('returns null for null input', () => {expect(sanitizeSurveyAppearance(null)).toBeNull()diff --git a/frontend/src/scenes/surveys/utils.ts b/frontend/src/scenes/surveys/utils.tsindex cc31cf10e0d0..a74cb566478e 100644--- a/frontend/src/scenes/surveys/utils.ts+++ b/frontend/src/scenes/surveys/utils.ts@@ -992,8 +992,12 @@ export function getExpressionCommentForQuestion(q: BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion,questionIndex: number): string {- if (q.question.trim().length > 0) {- return q.question+ const question = q.question.trim()+ if (question.length > 0) {+ // This is appended after `--` in the generated HogQL, and HogQL `--` comments are+ // single-line. Collapse any newlines so multi-line question text can't leak past the+ // comment and break the query (e.g. a stray non-ASCII char -> "Unexpected character").+ return question.replace(/\s*[\r\n]+\s*/g, ' ')}return `Question ${questionIndex + 1}`}