|
| 1 | +"""Security tests for HTML splitters to prevent XXE attacks.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from langchain_text_splitters.html import HTMLSectionSplitter |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.requires("lxml", "bs4") |
| 9 | +class TestHTMLSectionSplitterSecurity: |
| 10 | + """Security tests for HTMLSectionSplitter to ensure XXE prevention.""" |
| 11 | + |
| 12 | + def test_xxe_entity_attack_blocked(self) -> None: |
| 13 | + """Test that external entity attacks are blocked.""" |
| 14 | + # Create HTML content to process |
| 15 | + html_content = """<html><body><p>Test content</p></body></html>""" |
| 16 | + |
| 17 | + # Since xslt_path parameter is removed, this attack vector is eliminated |
| 18 | + # The splitter should use only the default XSLT |
| 19 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 20 | + |
| 21 | + # Process the HTML - should not contain any external entity content |
| 22 | + result = splitter.split_text(html_content) |
| 23 | + |
| 24 | + # Verify that no external entity content is present |
| 25 | + all_content = " ".join([doc.page_content for doc in result]) |
| 26 | + assert "root:" not in all_content # /etc/passwd content |
| 27 | + assert "XXE Attack Result" not in all_content |
| 28 | + |
| 29 | + def test_xxe_document_function_blocked(self) -> None: |
| 30 | + """Test that XSLT document() function attacks are blocked.""" |
| 31 | + # Even if someone modifies the default XSLT internally, |
| 32 | + # the secure parser configuration should block document() attacks |
| 33 | + |
| 34 | + html_content = ( |
| 35 | + """<html><body><h1>Test Header</h1><p>Test content</p></body></html>""" |
| 36 | + ) |
| 37 | + |
| 38 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 39 | + |
| 40 | + # Process the HTML safely |
| 41 | + result = splitter.split_text(html_content) |
| 42 | + |
| 43 | + # Should process normally without any security issues |
| 44 | + assert len(result) > 0 |
| 45 | + assert any("Test content" in doc.page_content for doc in result) |
| 46 | + |
| 47 | + def test_secure_parser_configuration(self) -> None: |
| 48 | + """Test that parsers are configured with security settings.""" |
| 49 | + # This test verifies our security hardening is in place |
| 50 | + html_content = """<html><body><h1>Test</h1></body></html>""" |
| 51 | + |
| 52 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 53 | + |
| 54 | + # The convert_possible_tags_to_header method should use secure parsers |
| 55 | + result = splitter.convert_possible_tags_to_header(html_content) |
| 56 | + |
| 57 | + # Result should be valid transformed HTML |
| 58 | + assert result is not None |
| 59 | + assert isinstance(result, str) |
| 60 | + |
| 61 | + def test_no_network_access(self) -> None: |
| 62 | + """Test that network access is blocked in parsers.""" |
| 63 | + # Create HTML that might trigger network access |
| 64 | + html_with_external_ref = """<?xml version="1.0"?> |
| 65 | +<!DOCTYPE html [ |
| 66 | + <!ENTITY external SYSTEM "http://attacker.com/xxe"> |
| 67 | +]> |
| 68 | +<html> |
| 69 | + <body> |
| 70 | + <h1>Test</h1> |
| 71 | + <p>&external;</p> |
| 72 | + </body> |
| 73 | +</html>""" |
| 74 | + |
| 75 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 76 | + |
| 77 | + # Process the HTML - should not make network requests |
| 78 | + result = splitter.split_text(html_with_external_ref) |
| 79 | + |
| 80 | + # Verify no external content is included |
| 81 | + all_content = " ".join([doc.page_content for doc in result]) |
| 82 | + assert "attacker.com" not in all_content |
| 83 | + |
| 84 | + def test_dtd_processing_disabled(self) -> None: |
| 85 | + """Test that DTD processing is disabled.""" |
| 86 | + # HTML with DTD that attempts to define entities |
| 87 | + html_with_dtd = """<!DOCTYPE html [ |
| 88 | + <!ELEMENT html (body)> |
| 89 | + <!ELEMENT body (h1, p)> |
| 90 | + <!ELEMENT h1 (#PCDATA)> |
| 91 | + <!ELEMENT p (#PCDATA)> |
| 92 | + <!ENTITY test "This is a test entity"> |
| 93 | +]> |
| 94 | +<html> |
| 95 | + <body> |
| 96 | + <h1>Header</h1> |
| 97 | + <p>&test;</p> |
| 98 | + </body> |
| 99 | +</html>""" |
| 100 | + |
| 101 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 102 | + |
| 103 | + # Process the HTML - entities should not be resolved |
| 104 | + result = splitter.split_text(html_with_dtd) |
| 105 | + |
| 106 | + # The entity should not be expanded |
| 107 | + all_content = " ".join([doc.page_content for doc in result]) |
| 108 | + assert "This is a test entity" not in all_content |
| 109 | + |
| 110 | + def test_safe_default_xslt_usage(self) -> None: |
| 111 | + """Test that the default XSLT file is used safely.""" |
| 112 | + # Test with HTML that has font-size styling (what the default XSLT handles) |
| 113 | + html_with_font_size = """<html> |
| 114 | +<body> |
| 115 | + <span style="font-size: 24px;">Large Header</span> |
| 116 | + <p>Content under large text</p> |
| 117 | + <span style="font-size: 18px;">Small Header</span> |
| 118 | + <p>Content under small text</p> |
| 119 | +</body> |
| 120 | +</html>""" |
| 121 | + |
| 122 | + splitter = HTMLSectionSplitter(headers_to_split_on=[("h1", "Header 1")]) |
| 123 | + |
| 124 | + # Process the HTML using the default XSLT |
| 125 | + result = splitter.split_text(html_with_font_size) |
| 126 | + |
| 127 | + # Should successfully process the content |
| 128 | + assert len(result) > 0 |
| 129 | + # Large font text should be converted to header |
| 130 | + assert any("Large Header" in str(doc.metadata.values()) for doc in result) |
0 commit comments