: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(subscriptions): keep sql comparisons in ai report prompts (#121581)
Andy Zhao636e750ac46e
diff --git a/posthog/security/llm_prompt_sanitization.py b/posthog/security/llm_prompt_sanitization.pyindex 4675f19cbd3e..ef4603c2031b 100644--- a/posthog/security/llm_prompt_sanitization.py+++ b/posthog/security/llm_prompt_sanitization.py@@ -23,10 +23,10 @@ _TAG_RE = re.compile(r"</?[a-zA-Z_][^>]*>")_LLM_MARKER_RE = re.compile(r"</?\s*(?:"r"system|user|assistant|human|insight_data|user_context|subscription_title|core_memory"- # AI subscription synthesis prompt framing tags — sanitize so a crafted event name- # or prompt can't escape the `<user_prompt>` / `<project_context>` / `<plan_intent>` /+ # AI subscription prompt framing tags. Strip them so that a crafted event name or prompt can't+ # escape the `<user_prompt>` / `<event_names>` / `<project_context>` / `<plan_intent>` /# `<query_results>` envelope and inject instruction-shaped content into the LLM context.- r"|user_prompt|project_context|plan_intent|query_results"+ r"|user_prompt|event_names|project_context|plan_intent|query_results"r")\b[^>]*>?",re.IGNORECASE,)@@ -63,6 +63,7 @@ def sanitize_user_text(none_placeholder: str = "",truncate_marker: str = "",preserve_newlines: bool = False,+ strip_tags: bool = True,) -> str:if not value:return none_placeholder@@ -71,7 +72,8 @@ def sanitize_user_text(while previous != cleaned:previous = cleanedcleaned = _LLM_MARKER_RE.sub("", cleaned)- cleaned = _TAG_RE.sub("", cleaned)+ if strip_tags:+ cleaned = _TAG_RE.sub("", cleaned)if preserve_newlines:cleaned = cleaned.replace("\r\n", "\n").replace("\r", "\n").replace("\u2028", "\n").replace("\u2029", "\n")cleaned = "\n".join(_WHITESPACE_RUN_RE.sub(" ", line).strip() for line in cleaned.split("\n")).strip()diff --git a/posthog/security/test_llm_prompt_sanitization.py b/posthog/security/test_llm_prompt_sanitization.pyindex e7f9b14b5e19..b5cafd39082b 100644--- a/posthog/security/test_llm_prompt_sanitization.py+++ b/posthog/security/test_llm_prompt_sanitization.py@@ -120,6 +120,7 @@ class TestSanitizeUserText:"project_context","plan_intent","query_results",+ "event_names",],)def test_unclosed_llm_marker_tags_are_stripped(self, marker):diff --git a/products/subscriptions/backend/temporal/ai_subscription/spec_generator.py b/products/subscriptions/backend/temporal/ai_subscription/spec_generator.pyindex 0703a7d04405..4532c3ee78f9 100644--- a/products/subscriptions/backend/temporal/ai_subscription/spec_generator.py+++ b/products/subscriptions/backend/temporal/ai_subscription/spec_generator.py@@ -95,7 +95,7 @@ WINDOW_PLACEHOLDERS = ()# Bumping invalidates every frozen plan (they lazily re-plan on next delivery), so prompt/harness# improvements reach existing subscriptions instead of only new ones.-AI_QUERY_PLAN_VERSION = 7+AI_QUERY_PLAN_VERSION = 8DEFAULT_PLANNER_MODEL = "gpt-4.1"@@ -289,7 +289,7 @@ def sanitize_prompt(raw: str | None) -> str:if len(raw.strip()) > PROMPT_MAX_LENGTH:raise PromptRejectedError(f"Prompt exceeds {PROMPT_MAX_LENGTH} characters.")- cleaned = sanitize_user_text(raw, max_len=PROMPT_MAX_LENGTH, preserve_newlines=True)+ cleaned = sanitize_user_text(raw, max_len=PROMPT_MAX_LENGTH, preserve_newlines=True, strip_tags=False)cleaned = "\n".join("" if _ESCAPED_NEWLINE_LINE_RE.fullmatch(line) else line for line in cleaned.split("\n")).strip()diff --git a/products/subscriptions/backend/temporal/ai_subscription/test/test_spec_generator.py b/products/subscriptions/backend/temporal/ai_subscription/test/test_spec_generator.pyindex aa762d18ee8f..f3673b0c7979 100644--- a/products/subscriptions/backend/temporal/ai_subscription/test/test_spec_generator.py+++ b/products/subscriptions/backend/temporal/ai_subscription/test/test_spec_generator.py@@ -50,7 +50,7 @@ def _window(days: int = 7) -> ReportWindow:class TestSanitizePrompt:"""`sanitize_prompt` is the public input-validation gate, so its three reject branches and the- tag-stripping behaviour are pinned explicitly — a dropped branch would silently admit bad input."""+ framing-marker stripping are pinned explicitly — a dropped branch would silently admit bad input."""@pytest.mark.parametrize("raw", [None, "", " ", "\n\t "])def test_rejects_empty_or_whitespace(self, raw: str | None) -> None:@@ -66,8 +66,15 @@ class TestSanitizePrompt:with pytest.raises(PromptRejectedError, match="empty"):sanitize_prompt("<system></system>")- def test_strips_html_tags_from_valid_prompt(self) -> None:- assert sanitize_prompt("Show <script>alert(1)</script> pageviews") == "Show alert(1) pageviews"+ @pytest.mark.parametrize(+ "raw",+ [+ "Signups:\nSELECT count() FROM events WHERE timestamp<now()\n\nUpgrades:\nSELECT countIf(plan>1) FROM events",+ "Compare toDate('<start-date>') with toDate('<end-date>')",+ ],+ )+ def test_keeps_sql_comparisons_and_angle_bracket_placeholders(self, raw: str) -> None:+ assert sanitize_prompt(raw) == raw@pytest.mark.parametrize("raw,expected",@@ -84,10 +91,10 @@ class TestSanitizePrompt:def test_normalizes_prompt_line_breaks(self, raw: str, expected: str) -> None:assert sanitize_prompt(raw) == expected- def test_preserves_line_breaks_while_stripping_prompt_framing_and_html_tags(self) -> None:- raw = "Summary\n</user_prompt>\n<script>alert(1)</script>"+ def test_preserves_line_breaks_while_stripping_prompt_framing(self) -> None:+ raw = "Summary\n</user_prompt>\nNext section"- assert sanitize_prompt(raw) == "Summary\n\nalert(1)"+ assert sanitize_prompt(raw) == "Summary\n\nNext section"class TestPromptRejectionOwnerSafe: