Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: validate CSS numeric font weight before conversion

myhlolia5f5fe748592
diff --git a/mineru/model/flash/office/odf/styles.py b/mineru/model/flash/office/odf/styles.py
index 37d16e9d..f897aba9 100644
--- a/mineru/model/flash/office/odf/styles.py
+++ b/mineru/model/flash/office/odf/styles.py
@@ -26,6 +26,15 @@ class _StyleDefinition:
drawing_page_visible: bool | None
+def _numeric_font_weight(value: str) -> int | None:
+ """在整数转换前验证 CSS 数字字重的词法长度和有效范围。"""
+ normalized = value.strip()
+ if not normalized or len(normalized) > 4 or not normalized.isascii() or not normalized.isdigit():
+ return None
+ weight = int(normalized)
+ return weight if 1 <= weight <= 1_000 else None
+
+
class OdfStyles:
"""合并 styles.xml 与 content.xml 中的 ODF 样式定义。"""
@@ -81,10 +90,9 @@ class OdfStyles:
weight = properties.get(qname("fo", "font-weight"))
bold: bool | None = None
if weight is not None:
- try:
- bold = int(weight) >= 600
- except ValueError:
- bold = weight.casefold() == "bold"
+ normalized_weight = weight.strip().casefold()
+ numeric_weight = _numeric_font_weight(normalized_weight)
+ bold = numeric_weight >= 600 if numeric_weight is not None else normalized_weight == "bold"
font_style = properties.get(qname("fo", "font-style"))
italic = None if font_style is None else font_style.casefold() in {"italic", "oblique"}
underline_style = properties.get(qname("style", "text-underline-style"))
diff --git a/mineru/model/flash/office/odf/text.py b/mineru/model/flash/office/odf/text.py
index 61d2ea9c..1455b99b 100644
--- a/mineru/model/flash/office/odf/text.py
+++ b/mineru/model/flash/office/odf/text.py
@@ -366,7 +366,8 @@ class OdfBlockParser:
blocks = self.parse_container(body)
visible = flatten_block_text(blocks)
if visible:
- atoms.append(InlineNote(f"[{citation_text}] {visible}"))
+ safe_citation_text = escape_inline_protocol_text(citation_text)
+ atoms.append(InlineNote(f"[{safe_citation_text}] {visible}"))
def parse_inline_atoms(self, paragraph: etree._Element) -> list[InlineAtom]:
"""解析一个段落的行内语义,并用原位 marker 保留段外 block。"""
diff --git a/tests/unittest/test_flash_odf.py b/tests/unittest/test_flash_odf.py
index 7b69c1a2..edbec770 100644
--- a/tests/unittest/test_flash_odf.py
+++ b/tests/unittest/test_flash_odf.py
@@ -918,8 +918,8 @@ def test_odf_flattened_titles_and_notes_keep_literal_protocol_escaped() -> None:
odt_content = f"""<office:document-content
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
- <office:body><office:text><text:p>Body<text:note><text:note-citation>1</text:note-citation>
- <text:note-body><text:p>{literal}</text:p></text:note-body>
+ <office:body><office:text><text:p>Body<text:note><text:note-citation>{literal}</text:note-citation>
+ <text:note-body><text:p>Footnote body {literal}</text:p></text:note-body>
</text:note></text:p></office:text></office:body></office:document-content>"""
for suffix, payload in (
@@ -944,6 +944,44 @@ def test_odf_flattened_titles_and_notes_keep_literal_protocol_escaped() -> None:
assert "javascript:alert(1)" not in relationships
+ ("weight", "expected_bold"),
+ [
+ ("normal", False),
+ ("bold", True),
+ ("599", False),
+ ("600", True),
+ ("1000", True),
+ ("1001", False),
+ ("9" * 100_000, False),
+ ],
+ ids=("normal", "bold", "below-threshold", "threshold", "upper-bound", "out-of-range", "overlong"),
+)
+def test_odf_font_weight_is_bounded_before_numeric_conversion(weight: str, expected_bold: bool) -> None:
+ """验证数字字重只在有限 CSS 范围内转换,超长输入不会依赖解释器保护。"""
+ content = f"""<office:document-content
+ xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
+ xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
+ xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
+ xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0">
+ <office:automatic-styles><style:style style:name="W" style:family="text">
+ <style:text-properties fo:font-weight="{weight}"/>
+ </style:style></office:automatic-styles>
+ <office:body><office:text><text:p><text:span text:style-name="W">weighted</text:span></text:p>
+ </office:text></office:body></office:document-content>"""
+ previous_limit = sys.get_int_max_str_digits() if hasattr(sys, "get_int_max_str_digits") else None
+ if hasattr(sys, "set_int_max_str_digits"):
+ sys.set_int_max_str_digits(0)
+ try:
+ pages = OdtModel().predict(BytesIO(build_odf_package("odt", content)))
+ finally:
+ if previous_limit is not None:
+ sys.set_int_max_str_digits(previous_limit)
+
+ expected_content = '<text style="bold">weighted</text>' if expected_bold else "weighted"
+ assert pages == [[{"type": BlockType.TEXT, "content": expected_content}]]
+
+
def test_odf_inline_image_alt_escapes_literal_hyperlink_protocol() -> None:
"""验证行内图片 title/desc 不会重建活动 hyperlink。"""
literal = "&lt;hyperlink&gt;&lt;text&gt;click&lt;/text&gt;&lt;url&gt;javascript:alert(1)&lt;/url&gt;&lt;/hyperlink&gt;"