Skip to content

Commit 0d671d2

Browse files
authored
fix(documentai-toolbox): enable autoescape in export_hocr_str (#18140)
export_hocr_str builds its Jinja2 Environment without autoescape, so the title argument is written into the `<title>` element unescaped. A title such as `</title><script>alert(1)</script>` breaks out of the element and lands as raw script markup, producing invalid XML in an hOCR document that is meant to be valid XHTML rendered in a browser. The word and line text already pass through the `escape` filter, so enabling autoescape closes the remaining gap inside the helper while leaving the output byte-identical for normal input (existing golden-file tests are unchanged). - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-cloud-python/issues) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary)
1 parent 3ba7d02 commit 0d671d2

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/document.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ def export_hocr_str(self, title: str) -> str:
943943
A string hOCR version of the Document
944944
"""
945945
environment = Environment(
946-
loader=PackageLoader("google.cloud.documentai_toolbox", "templates")
946+
loader=PackageLoader("google.cloud.documentai_toolbox", "templates"),
947+
autoescape=True,
947948
)
948949
template = environment.get_template("hocr_document_template.xml.j2")
949950
content = template.render(pages=self.pages, title=title)

‎packages/google-cloud-documentai-toolbox/tests/unit/test_document.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,23 @@ def test_export_hocr_str_with_escape_characters():
949949
assert actual_hocr == expected
950950

951951

952+
def test_export_hocr_str_escapes_title():
953+
wrapped_document = document.Document.from_document_path(
954+
document_path="tests/unit/resources/0/toolbox_invoice_test-0.json"
955+
)
956+
957+
actual_hocr = wrapped_document.export_hocr_str(
958+
title="</title><script>alert(1)</script>"
959+
)
960+
961+
assert "<script>alert(1)</script>" not in actual_hocr
962+
963+
element = ElementTree.fromstring(actual_hocr)
964+
assert element is not None
965+
title_element = element.find(".//{http://www.w3.org/1999/xhtml}title")
966+
assert title_element.text == "</title><script>alert(1)</script>"
967+
968+
952969
def test_document_to_merged_documentai_document(get_bytes_multiple_files_mock):
953970
wrapped_document = document.Document.from_gcs(
954971
gcs_bucket_name="test-directory", gcs_prefix="documentai/output/123456789/1/"

0 commit comments

Comments
 (0)
⌂ 换网址