Tweak svg data cleaning: (#4941)

- Decode data if passed as bytes
This commit is contained in:
Oliver 2023-06-01 16:47:47 +10:00 committed by GitHub
parent cb0f0e34d9
commit 18d9ecd0f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,7 +43,7 @@ ALLOWED_ATTRIBUTES_SVG = [
]
def sanitize_svg(file_data: str, strip: bool = True, elements: str = ALLOWED_ELEMENTS_SVG, attributes: str = ALLOWED_ATTRIBUTES_SVG) -> str:
def sanitize_svg(file_data, strip: bool = True, elements: str = ALLOWED_ELEMENTS_SVG, attributes: str = ALLOWED_ATTRIBUTES_SVG) -> str:
"""Sanatize a SVG file.
Args:
@ -56,6 +56,10 @@ def sanitize_svg(file_data: str, strip: bool = True, elements: str = ALLOWED_ELE
str: Sanitzied SVG file.
"""
# Handle byte-encoded data
if type(file_data) == bytes:
file_data = file_data.decode('utf-8')
cleaned = clean(
file_data,
tags=elements,
@ -64,4 +68,5 @@ def sanitize_svg(file_data: str, strip: bool = True, elements: str = ALLOWED_ELE
strip_comments=strip,
css_sanitizer=CSSSanitizer()
)
return cleaned