from collections import namedtuple
from CTFd.utils.security.sanitize import sanitize_html
Case = namedtuple("Case", ["input", "expected"])
def test_sanitize_html_empty():
"""Test sanitize_html with empty input"""
assert sanitize_html("") == ""
def test_sanitize_html_basic_tags():
"""Test that basic HTML tags are preserved"""
cases = [
Case("
Hello World
", "Hello World
"),
Case("Content
", "Content
"),
Case("Text", "Text"),
Case("Bold", "Bold"),
Case("Italic", "Italic"),
Case("Header
", "Header
"),
Case("Header
", "Header
"),
Case("Header
", "Header
"),
Case(
"",
"",
),
Case(
"- Item 1
- Item 2
",
"- Item 1
- Item 2
",
),
]
for case in cases:
assert sanitize_html(case.input) == case.expected
def test_sanitize_html_links():
"""Test that links are sanitized with proper rel attributes"""
cases = [
Case(
'Link',
'Link',
),
Case(
'Link',
'Link',
),
Case(
'Link',
'Link',
),
Case(
'Link',
'Link',
),
Case(
'Email',
'Email',
),
Case(
'Phone',
'Phone',
),
Case(
'Evil',
'Evil',
),
Case(
'Anchor',
'Anchor',
),
Case(
'Query',
'Query',
),
Case(
'Query',
'Query',
),
]
for case in cases:
result = sanitize_html(case.input)
assert (
result == case.expected
), f"Input: {case.input}, Expected: {case.expected}, Got: {result}"
def test_sanitize_html_images():
"""Test that images are preserved with allowed attributes"""
cases = [
Case(
'
',
'
',
),
Case(
'
',
'
',
),
Case(
'
',
'
',
),
Case(
'
',
'
',
),
]
for case in cases:
result = sanitize_html(case.input)
assert (
result == case.expected
), f"Input: {case.input}, Expected: {case.expected}, Got: {result}"
def test_sanitize_html_dangerous_content():
"""Test that dangerous content is removed or sanitized"""
cases = [
Case('', ""),
Case('', ""),
Case('', ""),
Case('