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(exports): prevent markdown sanitizer backtracking (#97525)

Vasco de Krijgera0c13c784209
diff --git a/posthog/helpers/markdown_safety.py b/posthog/helpers/markdown_safety.py
index e8425a9f9b41..06086b1dcfe5 100644
--- a/posthog/helpers/markdown_safety.py
+++ b/posthog/helpers/markdown_safety.py
@@ -12,8 +12,14 @@ from posthog.api.utils import hostname_in_allowed_url_list
_ALLOWED_LINK_URLS = ["https://posthog.com", "https://*.posthog.com"]
# Match all CommonMark title forms (double/single-quoted, parenthesized) and allow whitespace before
# `)` (trailing `\s*`) — otherwise `[x](url "t")`, `[x](url 't')`, or `[x](url\n)` slip past this rule
-# and the bare-URL rule (which skips `](`-prefixed URLs), reaching Slack un-defanged.
-_MARKDOWN_LINK_RE = re.compile(r"\[([^\]]*)\]\(((?:[^()\s]+|\([^)]*\))+)(?:\s+(?:\"[^\"]*\"|'[^']*'|\([^)]*\)))?\s*\)")
+# and the bare-URL rule (which skips `](`-prefixed URLs), reaching Slack un-defanged. Match ordinary
+# destination characters as one atomic run. This removes the ambiguous nested repetition that makes
+# malformed links backtrack, and it keeps no repeat state per destination character. Keep `[` out of
+# the label as well, so a run of `[` cannot make the engine rescan the same suffix from every start.
+# That drops the nested labels CommonMark rejects anyway, and `_ORPHAN_DEST_RE` still defangs them.
+_MARKDOWN_LINK_RE = re.compile(
+ r"\[([^\[\]]*)\]\(((?:(?>[^()\s]+)|\([^)]*\))+)(?:\s+(?:\"[^\"]*\"|'[^']*'|\([^)]*\)))?\s*\)"
+)
_MARKDOWN_IMAGE_RE = re.compile(r"!\[([^\]]*)\]\([^)]*\)")
# A malformed link the rule above can't span (e.g. `[x](url\nmore)`) leaves its URL after `](`, which
# the bare-URL rule skips — defang it here as a safety net.
diff --git a/posthog/helpers/test_markdown_safety.py b/posthog/helpers/test_markdown_safety.py
index 32320147867f..2e37649eb30a 100644
--- a/posthog/helpers/test_markdown_safety.py
+++ b/posthog/helpers/test_markdown_safety.py
@@ -1,3 +1,4 @@
+import pytest
from posthog.test.base import BaseTest
from parameterized import parameterized
@@ -19,8 +20,13 @@ class TestStripExternalLinks(BaseTest):
("space_before_paren", "[x](https://evil.example.com )"),
# Not a CommonMark link, but the destination URL would otherwise survive bare after `](`.
("newline_mid_dest", "[x](https://evil.example.com\nmore)"),
+ ("unquoted_title", "[x](https://evil.example.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa invalid title)"),
+ # A `[` run used to cost the label class quadratic time, because the engine rescanned the
+ # same suffix from every start. 32,000 of them took over three seconds.
+ ("leading_bracket_run", "[" * 32000 + "](https://evil.example.com invalid title)"),
]
)
+ @pytest.mark.timeout(1, func_only=True)
def test_non_posthog_urls_defanged(self, _label: str, markdown: str) -> None:
out = strip_external_links_markdown(markdown)
# The host must never survive as a live link — only inside an inert code span at most.