Skip to content

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

feat: add safety checks for image sidecar paths and enhance sidecar writing functionality

myhloli1f31705258af
diff --git a/mineru/kit/common.py b/mineru/kit/common.py
index 9346812c..f2134146 100644
--- a/mineru/kit/common.py
+++ b/mineru/kit/common.py
@@ -5,7 +5,7 @@ from __future__ import annotations
import json
import os
import zipfile
-from pathlib import Path
+from pathlib import Path, PureWindowsPath
from typing import Literal
from ..data.data_reader_writer.filebase import FileBasedDataWriter
@@ -143,10 +143,11 @@ def save_parse_result(result: ParseResult, dest: Path, format: KitFormat) -> Non
dest.parent.mkdir(parents=True, exist_ok=True)
if format == "markdown":
_write_utf8_text(dest, result.markdown())
+ _write_image_sidecars(dest.parent, result.images())
return
if format == "middle_json":
_write_utf8_text(dest, result.to_export_json())
- _write_image_sidecars(FileBasedDataWriter(str(dest.parent)), result.images())
+ _write_image_sidecars(dest.parent, result.images())
return
if format == "zip":
tmp_dir = dest.parent / f".{dest.stem}"
@@ -171,9 +172,38 @@ def _write_utf8_text(path: Path, content: str) -> None:
path.write_bytes(content.encode("utf-8", errors="replace"))
-def _write_image_sidecars(writer: FileBasedDataWriter, images: dict[str, bytes]) -> None:
+def _resolve_safe_sidecar_path(output_dir: Path, image_path: str) -> str:
+ """校验图片 sidecar 路径必须落在输出目录内,并返回安全的相对路径。"""
+ posix_path = Path(image_path)
+ windows_path = PureWindowsPath(image_path)
+ if (
+ not image_path
+ or posix_path.is_absolute()
+ or windows_path.is_absolute()
+ or windows_path.drive
+ or windows_path.root
+ or ".." in posix_path.parts
+ or ".." in windows_path.parts
+ ):
+ raise ValueError(f"Unsafe image sidecar path: {image_path}")
+
+ output_root = output_dir.resolve()
+ target_path = (output_root / posix_path).resolve()
+ try:
+ target_path.relative_to(output_root)
+ except ValueError as exc:
+ raise ValueError(f"Unsafe image sidecar path: {image_path}") from exc
+ return target_path.relative_to(output_root).as_posix()
+
+
+def _write_image_sidecars(output_dir: Path, images: dict[str, bytes]) -> None:
"""将 public middle_json 引用的图片 sidecar 写到输出目录,避免 image_path 悬空。"""
- for image_path, image_bytes in images.items():
+ writer = FileBasedDataWriter(str(output_dir))
+ safe_images = [
+ (_resolve_safe_sidecar_path(output_dir, image_path), image_bytes)
+ for image_path, image_bytes in images.items()
+ ]
+ for image_path, image_bytes in safe_images:
writer.write(image_path, image_bytes)
diff --git a/tests/unittest/test_kit_commands.py b/tests/unittest/test_kit_commands.py
index dbb6df44..4e1be39a 100644
--- a/tests/unittest/test_kit_commands.py
+++ b/tests/unittest/test_kit_commands.py
@@ -317,12 +317,19 @@ def test_parse_single_file_markdown(monkeypatch: Any, tmp_path: Path) -> None:
class _Result:
def markdown(self) -> str:
+ """返回用于单文件 markdown 输出的测试内容。"""
return "# demo\n"
def to_json(self) -> str:
+ """保留旧 fake 接口,避免无关测试关注 JSON 输出细节。"""
return '{"pages":[]}'
+ def images(self) -> dict[str, bytes]:
+ """当前 markdown 无图片 sidecar 时返回空图片集合。"""
+ return {}
+
def save(self, writer: Any) -> None:
+ """模拟 zip 输出所需的完整保存接口。"""
writer.write_string("markdown.md", self.markdown())
writer.write_string("middle_json.json", self.to_json())
@@ -344,9 +351,11 @@ def test_parse_single_file_middle_json_writes_image_sidecars(
class _Result:
def to_export_json(self) -> str:
+ """返回带图片引用的 public middle_json。"""
return '{"pages":[{"para_blocks":[{"lines":[{"spans":[{"image_path":"figure.png"}]}]}]}]}'
def images(self) -> dict[str, bytes]:
+ """返回需要随 public middle_json 一起落盘的图片 sidecar。"""
return {"figure.png": b"figure-bytes"}
monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
@@ -363,6 +372,153 @@ def test_parse_single_file_middle_json_writes_image_sidecars(
assert (tmp_path / "figure.png").read_bytes() == b"figure-bytes"
+def test_parse_single_file_middle_json_rejects_parent_sidecar_paths(
+ monkeypatch: Any,
+ tmp_path: Path,
+) -> None:
+ """拒绝 API 或解析结果提供的父目录逃逸 sidecar 路径。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.json"
+ escaped = tmp_path / "escape.png"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Result:
+ def to_export_json(self) -> str:
+ """返回引用逃逸路径的 public middle_json。"""
+ return '{"pages":[]}'
+
+ def images(self) -> dict[str, bytes]:
+ """模拟远端返回包含 .. 的图片 sidecar 路径。"""
+ return {"../escape.png": b"escape-bytes"}
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(
+ app,
+ ["parse", str(source), "-o", str(output), "--format", "middle_json"],
+ )
+
+ assert result.exit_code == 1
+ assert "Unsafe image sidecar path" in result.output
+ assert not escaped.exists()
+
+
+def test_parse_single_file_middle_json_rejects_absolute_sidecar_paths(
+ monkeypatch: Any,
+ tmp_path: Path,
+) -> None:
+ """拒绝 API 或解析结果提供的绝对 sidecar 路径。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.json"
+ absolute = tmp_path / "absolute.png"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Result:
+ def to_export_json(self) -> str:
+ """返回普通 public middle_json 内容,重点验证 sidecar 路径。"""
+ return '{"pages":[]}'
+
+ def images(self) -> dict[str, bytes]:
+ """模拟远端返回绝对图片 sidecar 路径。"""
+ return {str(absolute): b"absolute-bytes"}
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(
+ app,
+ ["parse", str(source), "-o", str(output), "--format", "middle_json"],
+ )
+
+ assert result.exit_code == 1
+ assert "Unsafe image sidecar path" in result.output
+ assert not absolute.exists()
+
+
+def test_parse_single_file_middle_json_rejects_windows_rooted_sidecar_paths(
+ monkeypatch: Any,
+ tmp_path: Path,
+) -> None:
+ """拒绝 Windows rooted 形式的 sidecar 路径,避免跨平台逃逸。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.json"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Result:
+ def to_export_json(self) -> str:
+ """返回普通 public middle_json 内容,重点验证 Windows 路径。"""
+ return '{"pages":[]}'
+
+ def images(self) -> dict[str, bytes]:
+ """模拟远端返回 Windows rooted 图片 sidecar 路径。"""
+ return {"\\escape.png": b"escape-bytes"}
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(
+ app,
+ ["parse", str(source), "-o", str(output), "--format", "middle_json"],
+ )
+
+ assert result.exit_code == 1
+ assert "Unsafe image sidecar path" in result.output
+ assert not (tmp_path / "\\escape.png").exists()
+
+
+def test_parse_single_file_markdown_writes_image_sidecars(
+ monkeypatch: Any,
+ tmp_path: Path,
+) -> None:
+ """markdown 单文件输出也要写出渲染结果引用的图片 sidecar。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.md"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Result:
+ def markdown(self) -> str:
+ """返回带相对图片引用的 markdown 内容。"""
+ return "![](figure.png)\n"
+
+ def images(self) -> dict[str, bytes]:
+ """返回 markdown 引用的图片 sidecar。"""
+ return {"figure.png": b"figure-bytes"}
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(app, ["parse", str(source), "-o", str(output)])
+
+ assert result.exit_code == 0
+ assert output.read_text(encoding="utf-8") == "![](figure.png)\n"
+ assert (tmp_path / "figure.png").read_bytes() == b"figure-bytes"
+
+
+def test_parse_single_file_markdown_rejects_unsafe_sidecar_paths(
+ monkeypatch: Any,
+ tmp_path: Path,
+) -> None:
+ """markdown 补写 sidecar 时同样不能允许路径逃逸输出目录。"""
+ source = tmp_path / "demo.pdf"
+ output = tmp_path / "out.md"
+ escaped = tmp_path / "escape.png"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Result:
+ def markdown(self) -> str:
+ """返回引用逃逸路径的 markdown 内容。"""
+ return "![](../escape.png)\n"
+
+ def images(self) -> dict[str, bytes]:
+ """模拟远端或解析结果返回逃逸图片路径。"""
+ return {"../escape.png": b"escape-bytes"}
+
+ monkeypatch.setattr(parse, "local_parse", lambda *args, **kwargs: _Result())
+
+ result = runner.invoke(app, ["parse", str(source), "-o", str(output)])
+
+ assert result.exit_code == 1
+ assert "Unsafe image sidecar path" in result.output
+ assert not escaped.exists()
+
+
def test_parse_output_replaces_surrogate_chars(monkeypatch: Any, tmp_path: Path) -> None:
source = tmp_path / "demo.pdf"
output = tmp_path / "out.md"
@@ -370,12 +526,19 @@ def test_parse_output_replaces_surrogate_chars(monkeypatch: Any, tmp_path: Path)
class _Result:
def markdown(self) -> str:
+ """返回包含孤立 surrogate 的 markdown 内容。"""
return "before \ud83d after\n"
def to_json(self) -> str:
+ """保留旧 fake 接口,避免无关测试关注 JSON 输出细节。"""
return '{"pages":[]}'
+ def images(self) -> dict[str, bytes]:
+ """当前 markdown 无图片 sidecar 时返回空图片集合。"""
+ return {}
+
def save(self, writer: Any) -> None:
+ """模拟 zip 输出所需的完整保存接口。"""
writer.write_string("markdown.md", self.markdown())
writer.write_string("middle_json.json", self.to_json())