: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(experiments): harden html validation for web experiments (#127222)
Andersef0e2f3deb21
diff --git a/products/experiments/backend/presentation/web_experiments.py b/products/experiments/backend/presentation/web_experiments.pyindex 3aa1d65a9b44..3417099f5148 100644--- a/products/experiments/backend/presentation/web_experiments.py+++ b/products/experiments/backend/presentation/web_experiments.py@@ -1,10 +1,13 @@import re+import html+import stringfrom typing import Anyfrom django.http import HttpResponse, JsonResponsefrom django.utils.text import slugifyfrom django.views.decorators.csrf import csrf_exempt+import nh3from drf_spectacular.utils import extend_schemafrom nanoid import generatefrom rest_framework import serializers, status, viewsets@@ -24,30 +27,113 @@ from products.experiments.backend.models.web_experiment import WebExperimentfrom products.feature_flags.backend.facade.api import create_flag, update_flagfrom products.feature_flags.backend.facade.filters import replace_variant_distribution-# XSS vector patterns, paired with the error message shown when one matches:-# script tags (opening and closing), event handlers (onclick, onerror, etc.),-# javascript: protocol, data:text/html, iframe/object/embed tags+_EVENT_HANDLER_MESSAGE = "contains disallowed event handlers (onclick, onerror, etc.)"+_JAVASCRIPT_URL_MESSAGE = "contains disallowed javascript: protocol"+_VBSCRIPT_URL_MESSAGE = "contains disallowed vbscript: protocol"+_DATA_HTML_URL_MESSAGE = "contains disallowed data:text/html"+_XSS_PATTERNS: list[tuple[re.Pattern[str], str]] = [(re.compile(r"<script[^>]*>", re.IGNORECASE), "contains disallowed <script> tags"),(re.compile(r"</script>", re.IGNORECASE), "contains disallowed </script> tags"),- (re.compile(r"\son\w+\s*=", re.IGNORECASE), "contains disallowed event handlers (onclick, onerror, etc.)"),- (re.compile(r"javascript\s*:", re.IGNORECASE), "contains disallowed javascript: protocol"),- (re.compile(r"data\s*:\s*text/html", re.IGNORECASE), "contains disallowed data:text/html"),+ (re.compile(r"[\s/\"']on\w+\s*=", re.IGNORECASE), _EVENT_HANDLER_MESSAGE),+ (re.compile(r"javascript\s*:", re.IGNORECASE), _JAVASCRIPT_URL_MESSAGE),+ (re.compile(r"data\s*:\s*text/html", re.IGNORECASE), _DATA_HTML_URL_MESSAGE),(re.compile(r"<iframe[^>]*>", re.IGNORECASE), "contains disallowed <iframe> tags"),(re.compile(r"<(object|embed)[^>]*>", re.IGNORECASE), "contains disallowed <object> or <embed> tags"),]+# Browsers ignore whitespace and control characters in a URL scheme; ";" and "," split SVG "values" lists.+_URL_CONTROL_CHARS = re.compile(r"[\x00-\x20\s]+")+_URL_SCHEMES: list[tuple[str, str]] = [+ ("javascript:", _JAVASCRIPT_URL_MESSAGE),+ ("vbscript:", _VBSCRIPT_URL_MESSAGE),+ ("data:text/html", _DATA_HTML_URL_MESSAGE),+]-def validate_no_xss(content: str, field_name: str) -> None:+_START_TAG_NAME = re.compile(r"<([a-zA-Z][^\t\n\f\r />]*)")+# The HTML5 parse cost is quadratic in nesting depth and in attributes per tag, so these caps bound it per request.+_MAX_START_TAGS_PER_REQUEST = 5000+_MAX_ATTRIBUTE_STARTS_PER_REQUEST = 10000+# Every attribute name starts after whitespace, "/" or a quote, so this also counts text words but never misses an attribute.+_ATTRIBUTE_START = re.compile(r"(?<=[\t\n\f\r /\"'])[^\t\n\f\r />]")+# The HTML5 tokenizer lowercases only ASCII letters in a tag name and replaces NUL with U+FFFD.+_ASCII_LOWER = str.maketrans(string.ascii_uppercase + "\x00", string.ascii_lowercase + "\ufffd")+# nh3 drops elements missing from its allow list with their attributes, and html5ever camel-cases these SVG names.+_SVG_TAG_NAMES = {+ name.lower(): name+ for name in (+ "altGlyph altGlyphDef altGlyphItem animateColor animateMotion animateTransform clipPath feBlend "+ "feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap "+ "feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge "+ "feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence "+ "foreignObject glyphRef linearGradient radialGradient textPath"+ ).split()+}+_URL_SCHEMES_TO_INSPECT = set(nh3.ALLOWED_URL_SCHEMES) | {"javascript", "vbscript", "data"}+++def _find_unsafe_url(value: str, anywhere: bool = False) -> str | None:+ url = _URL_CONTROL_CHARS.sub("", value).lower()+ for scheme, message in _URL_SCHEMES:+ if scheme in url if anywhere else re.search(rf"(?:^|[;,]){re.escape(scheme)}", url):+ return message+ return None+++def _find_unsafe_attribute(content: str) -> str | None:"""- Validates that content doesn't contain XSS vectors.- Raises ValidationError if dangerous content is found.+ Parses the content as an HTML5 fragment, as innerHTML does in the browser, and checks every attribute.- This validation-only approach preserves the original formatting while preventing XSS attacks.+ A parser that is not HTML5-compliant can end raw-text elements such as <style> in a different+ place than the browser does, and then it does not see the attributes that the browser creates."""+ tag_names: set[str] = set()+ for raw_name in _START_TAG_NAME.findall(content):+ name = raw_name.translate(_ASCII_LOWER)+ tag_names.add(_SVG_TAG_NAMES.get(name, name))++ problems: list[str] = []++ def inspect_attribute(_tag: str, attribute: str, value: str) -> str:+ if attribute.lower().startswith("on"):+ problems.append(_EVENT_HANDLER_MESSAGE)+ problem = _find_unsafe_url(value)+ if problem:+ problems.append(problem)+ return value++ nh3.clean(+ content,+ tags=tag_names,+ clean_content_tags=set(),+ attributes={},+ generic_attribute_prefixes={""},+ attribute_filter=inspect_attribute,+ url_schemes=_URL_SCHEMES_TO_INSPECT,+ link_rel=None,+ )+ return problems[0] if problems else None+++def find_unsafe_html(content: str) -> str | None:+ problem = _find_unsafe_attribute(content)+ if problem:+ return problemfor pattern, message in _XSS_PATTERNS:if pattern.search(content):- raise ValidationError(f"{field_name} {message}")+ return message+ return _find_unsafe_url(html.unescape(content), anywhere=True)+++def validate_no_xss(content: str, field_name: str) -> None:+ """+ Raises ValidationError if the content contains markup that can run script.++ The check rejects the content and does not change it, so valid content keeps its original formatting.+ """+ problem = find_unsafe_html(content)+ if problem:+ raise ValidationError(f"{field_name} {problem}")class WebExperimentsAPISerializer(serializers.ModelSerializer):@@ -150,6 +236,8 @@ class WebExperimentsAPISerializer(serializers.ModelSerializer):raise ValidationError("Experiment variants should be a dictionary of keys -> transforms")if "control" not in variants:raise ValidationError("Experiment should contain a control variant")+ start_tag_count = 0+ attribute_start_count = 0for name, variant in variants.items():if variant.get("rollout_percentage") is None:raise ValidationError(f"Experiment variant '{name}' does not have any rollout percentage")@@ -162,10 +250,20 @@ class WebExperimentsAPISerializer(serializers.ModelSerializer):)# Validate text and html fields to prevent XSS attacks- if "text" in transform and isinstance(transform["text"], str):- validate_no_xss(transform["text"], f"Transform text in variant '{name}'")- if "html" in transform and isinstance(transform["html"], str):- validate_no_xss(transform["html"], f"Transform html in variant '{name}'")+ for field in ("text", "html"):+ content = transform.get(field)+ if not isinstance(content, str):+ continue+ start_tag_count += len(_START_TAG_NAME.findall(content))+ attribute_start_count += len(_ATTRIBUTE_START.findall(content))+ if (+ start_tag_count > _MAX_START_TAGS_PER_REQUEST+ or attribute_start_count > _MAX_ATTRIBUTE_STARTS_PER_REQUEST+ ):+ raise ValidationError(+ "Experiment transforms contain too much HTML to check. Split the change into fewer or smaller transforms."+ )+ validate_no_xss(content, f"Transform {field} in variant '{name}'")return attrsdiff --git a/products/experiments/backend/test/test_web_experiments.py b/products/experiments/backend/test/test_web_experiments.pyindex 8e6a1b6ed99f..ea718099e953 100644--- a/products/experiments/backend/test/test_web_experiments.py+++ b/products/experiments/backend/test/test_web_experiments.py@@ -524,12 +524,91 @@ class TestWebExperimentValidationNoDB(SimpleTestCase):],["iframe in html", {"selector": "#t", "html": '<iframe src="https://evil.com"></iframe>'}, "iframe"],["object tag in html", {"selector": "#t", "html": "<object data=x></object>"}, "object"],- ["data:text/html in html", {"selector": "#t", "html": "data:text/html,<b>x</b>"}, "data:text/html"],+ [+ "data:text/html in html",+ {"selector": "#t", "html": '<a href="data:text/html,<b>x</b>">x</a>'},+ "data:text/html",+ ],+ [+ "event handler in upper case after slash separator",+ {"selector": "#t", "html": "<SVG/ONLOAD=alert(1)>"},+ "event handler",+ ],+ [+ "event handler without space",+ {"selector": "#t", "html": '<img src="x"onerror=alert(1)>'},+ "event handler",+ ],+ [+ "javascript protocol with entities, mixed case and whitespace",+ {"selector": "#t", "html": '<a href=" jaVa	ScRiPt
:alert(1)">x</a>'},+ "javascript:",+ ],+ [+ "javascript protocol in svg animation values",+ {"selector": "#t", "html": '<svg><animate attributeName="href" values="x;javascript:alert(1)"/></svg>'},+ "javascript:",+ ],+ ["vbscript protocol in html", {"selector": "#t", "html": '<a href="vbscript:x">x</a>'}, "vbscript:"],+ [+ "markup hidden in a style comment",+ {"selector": "#t", "html": "<style><!--</style><img src=x onerror=alert(1)>--></style>"},+ "event handler",+ ],+ [+ "markup hidden in an attribute value",+ {"selector": "#t", "html": '<style><a title="</style><img src=x onerror=alert(1)>"></a></style>'},+ "event handler",+ ],+ [+ "markup hidden in an end tag attribute",+ {"selector": "#t", "html": '<style></a title="</style><img src=x onerror=alert(1)>">'},+ "event handler",+ ],+ [+ "unterminated quoted start tag",+ {"selector": "#t", "html": '<img src=x onerror=alert(1) title="x'},+ "event handler",+ ],+ [+ "markup inside svg style",+ {"selector": "#t", "html": "<svg><style><img src=x onerror=alert(1)></style></svg>"},+ "event handler",+ ],])def test_rejects_xss_in_transform(self, _name: str, transform: dict, expected_substring: str) -> None:self._assert_invalid(_variants_with_test_transform(transform), expected_substring)+ @parameterized.expand(+ [+ ["link and image", '<a href="https://example.com/?a=1&b=2">Link</a><img src="/logo.png" alt="Logo">'],+ ["relative and mailto links", '<a href="/pricing">Pricing</a> <a href="mailto:[email protected]">Mail</a>'],+ ["inline style and data attributes", '<p style="color: red" data-testid="hero">Buy now</p>'],+ ["scheme words in a non-url attribute", '<span title="javascript is fun" class="online">x</span>'],+ ["encoded markup in an attribute", '<span title="<b>bold</b>">x</span>'],+ ]+ )+ def test_accepts_safe_html_in_transform(self, _name: str, html: str) -> None:+ serializer = WebExperimentsAPISerializer(+ data={"name": "x", "variants": _variants_with_test_transform({"selector": "#t", "html": html})}+ )+ assert serializer.is_valid(), serializer.errors++ @parameterized.expand(+ [+ ["start tags", "<b>x</b>" * 2600],+ ["attributes in one tag", "<div " + " ".join(f"a{i}=x" for i in range(5001)) + "></div>"],+ ]+ )+ def test_rejects_html_over_request_parse_budget(self, _name: str, html: str) -> None:+ variants = {+ "control": {"rollout_percentage": 50},+ "test": {"rollout_percentage": 50, "transforms": [{"selector": "#a", "html": html}]},+ "test2": {"rollout_percentage": 50, "transforms": [{"selector": "#b", "html": html}]},+ }+ self._assert_invalid(variants, "too much html")+def test_rejects_missing_control_variant(self) -> None:self._assert_invalid({"test": {"rollout_percentage": 50}}, "control variant")