Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.
Python aiai-agentscrawl4ai-cloudcrawlerdata-extractionllmmarkdownmcpopen-sourceplaywrightpythonragscraperweb-crawlerweb-crawlingweb-scraping
fix(pdf): escape paragraph text in cleaned_html to prevent XSS
ntohidiceaf6a3edec0
diff --git a/crawl4ai/processors/pdf/utils.py b/crawl4ai/processors/pdf/utils.pyindex 3dc0e739..e7cc6328 100644--- a/crawl4ai/processors/pdf/utils.py+++ b/crawl4ai/processors/pdf/utils.py@@ -97,10 +97,14 @@ def clean_pdf_text_to_html(page_number, text):para = ' '.join(current_paragraph)para = re.sub(r'\s+', ' ', para).strip()if para:- # escaped_para = html.escape(para)- escaped_para = para- # escaped_para = re.sub(r'\.\n', '.\n\n', escaped_para)- # Split escaped_para by <|break|> to avoid HTML escaping+ # Paragraph text comes verbatim from the PDF and is attacker-+ # controlled. Escape it so markup like <img onerror=...> cannot+ # survive into cleaned_html and execute when the result is+ # rendered (DOM XSS). Every other sink in this function already+ # escapes; this one was the gap.+ escaped_para = html.escape(para)+ # Whitespace was collapsed above, so this split yields a single+ # element -- kept only to preserve the original structure.escaped_para = escaped_para.split('.\n\n')# Wrap each part in <p> tagescaped_para = [f'<p>{part}</p>' for part in escaped_para]diff --git a/tests/unit/test_pdf_html_escaping.py b/tests/unit/test_pdf_html_escaping.pynew file mode 100644index 00000000..3fba16a7--- /dev/null+++ b/tests/unit/test_pdf_html_escaping.py@@ -0,0 +1,76 @@+"""Regression test for GHSA-7g3g-vhm6-79f3.++PDF paragraph text is attacker-controlled and flows verbatim into cleaned_html+(clean_pdf_text_to_html -> pdf_page.html -> cleaned_html -> crawl JSON). It must+be HTML-escaped so injected markup cannot execute when the result is rendered.++The escaping was disabled at the paragraph sink; every other sink in the+function already escaped. These tests pin the paragraph sink specifically and+assert the advisory's acceptance criterion: < > " ' survive only as entities.+"""++import pytest++from crawl4ai.processors.pdf.utils import clean_pdf_text_to_html+++# A leading short line becomes an <h2> title (its own escaped path); the blank+# line then starts a fresh paragraph, which is the sink under test.+TITLE = "Quarterly Financial Report\n\n"+++def _paragraph_html(body: str) -> str:+ return clean_pdf_text_to_html(2, TITLE + body)+++def test_event_handler_markup_does_not_survive_as_live_html():+ payload = "<img src=x onerror=alert(document.cookie)>"+ body = f"Attackers embed {payload} inside pdf paragraph body text that an operator later views in the playground here."++ html = _paragraph_html(body)++ assert "<img" not in html, "raw <img tag reached cleaned_html (XSS)"+ assert "<img" in html, "payload should appear escaped"+ # The event handler must not survive in a live tag context.+ assert "onerror=alert" not in html or "<img" in html+++def test_svg_onload_with_quotes_is_escaped():+ payload = "\"><svg onload='steal()'>"+ body = f"A crafted string {payload} placed in the paragraph flow of the document body for testing purposes here now."++ html = _paragraph_html(body)++ assert "<svg" not in html+ assert "<svg" in html+++ "char, entity",+ [("<", "<"), (">", ">"), ('"', """), ("'", "'")],+)+def test_dangerous_chars_appear_only_as_entities(char, entity):+ body = (+ f"Paragraph body text containing a raw {char} character that must be "+ f"escaped before it reaches cleaned html output for safety reasons here."+ )++ html = _paragraph_html(body)+ paragraph = html.split('<div class="paragraph">', 1)[1]++ assert entity in paragraph, f"{char!r} should be emitted as {entity}"+ # The only literal angle brackets/quotes allowed in the paragraph region are+ # the ones this code emits itself (<p>, </p>, <div>, <hr/>). The injected+ # char must not appear raw inside the text.+ text = paragraph.replace("<p>", "").replace("</p>", "")+ text = text.replace('<div class="paragraph">', "").replace("</div><hr/>", "")+ assert char not in text, f"raw {char!r} survived into paragraph text"+++def test_benign_paragraph_text_is_unchanged():+ body = "An ordinary paragraph of report text with no special characters at all in it whatsoever today."++ html = _paragraph_html(body)++ assert "ordinary paragraph of report text" in html+ assert "<" not in html and "&" not in html