Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
Merge upstream next into next
myhloli8b3bf6c635b5
diff --cc docs/next/middle-json/rendering.mdindex 1ba45132,3336b836..2160fd78--- a/docs/next/middle-json/rendering.md+++ b/docs/next/middle-json/rendering.md@@@ -1,8 -1,8 +1,8 @@@# Rendering Contract- 状态: 统一入口 / Markdown / LaTeX / Content List V1/V2 / Structured Content / DOCX / EPUB 3.3 / PDF / HTML v1+ 状态: Implemented读者: render 开发者、backend 开发者、SDK 开发者-范围: 严格 MiddleJson 到 Markdown、Content List V1/V2、Structured Content、DOCX、EPUB、PDF 和 HTML 的统一消费契约+范围: 严格 MiddleJson 到 Markdown、LaTeX、Content List V1/V2、Structured Content、DOCX、EPUB、PDF 和 HTML 的统一消费契约## 统一入口diff --cc mineru/utils/image_payload.pyindex 7302ef3a,6ad3c836..a0b14013--- a/mineru/utils/image_payload.py+++ b/mineru/utils/image_payload.py@@@ -229,53 -228,10 +228,8 @@@ def extract_mineru_generated_svg_fallbareturn fallback, width, height- def image_path_from_key(path_key: str, image_format: str = "JPEG") -> str:- """复用旧裁图路径哈希规则,根据逻辑路径生成稳定图片文件名。"""- ext = normalize_image_extension(image_format)- return f"{path_key}.{ext}"--- class ImagePayloadCache:- """保存运行时图片载荷,public middle_json 只通过 image_path 引用图片。"""-- def __init__(self, images: dict[str, bytes] | None = None) -> None:- self._images: dict[str, bytes] = {}- if images:- self.update(images)-- def register_bytes(- self,- img_bytes: bytes,- image_format: str = "JPEG",- *,- path_key: str | None = None,- image_path: str | None = None,- ) -> str:- """登记图片字节,优先使用显式路径,其次使用逻辑 path_key 生成稳定路径。"""- if image_path:- img_path = image_path- elif path_key:- img_path = image_path_from_key(path_key, image_format)- else:- ext = normalize_image_extension(image_format)- payload_key = base64.b64encode(img_bytes).decode("ascii")- img_path = f"{hashlib.sha256(payload_key.encode('utf-8')).hexdigest()}.{ext}"- img_path = validate_image_sidecar_path(img_path)- self._images[img_path] = img_bytes- return img_path-- def update(self, images: dict[str, bytes]) -> None:- """合并外部 sidecar 图片字节,供 API client 绑定远端输出。"""- for image_path, image_bytes in images.items():- self.register_bytes(image_bytes, image_path=image_path)-- def images(self) -> dict[str, bytes]:- """返回 image_path 到图片字节的副本,避免调用方改写内部缓存。"""- return dict(self._images)--def validate_image_sidecar_path(image_path: str) -> str:"""校验图片 sidecar 路径只能是安全的相对子路径,并返回规范化 POSIX 路径。"""- posix_path = Path(image_path)- windows_path = PureWindowsPath(image_path)if (not image_pathor image_path == "."diff --cc tests/unittest/test_parser_api_contract.pyindex ed9ddc98,588c7575..c1c9f3fa--- a/tests/unittest/test_parser_api_contract.py+++ b/tests/unittest/test_parser_api_contract.py@@@ -2094,58 -2089,6 +2089,49 @@@ def test_api_server_zip_includes_model_assert "\n " in model_output_text- def test_parse_result_rejects_unsafe_image_cache_path() -> None:- """验证 ParseResult 绑定外部图片缓存时立即拒绝不安全路径。"""- with pytest.raises(ValueError, match="Unsafe image sidecar path"):- ParseResult(- middle_json=_full_middle_json(PageInfo(page_idx=0)),- _image_cache={"../escape.png": b"bad-image"},- )--+def test_api_server_zip_rejects_unsafe_image_sidecar_path(+ monkeypatch: pytest.MonkeyPatch,+ tmp_path: Path,+) -> None:+ class _UnsafeResult:+ """模拟非标准解析结果,验证 ZIP writer 的下游路径防护仍生效。"""++ def save(self, writer: Any) -> None:+ """尝试写出不安全 sidecar 路径。"""+ writer.write("../escape.png", b"bad-image")++ async def fake_parse_async(*args: object, **kwargs: object) -> Any:+ return _UnsafeResult()++ monkeypatch.setattr("mineru.parser.api_server.parse_async", fake_parse_async)+ file_store = FileStore(tmp_path / "api-files")+ source = tmp_path / "demo.pdf"+ source.write_bytes(b"%PDF-1.7\n")+ request = CreateJobRequest.model_validate(+ {+ "files": [{"source": {"type": "local", "path": str(source)}}],+ "tier": "standard",+ "output_formats": ["zip"],+ }+ )+ rec = api_server.JobStore().create(request, file_store)++ asyncio.run(+ api_server._run_job(+ rec,+ request,+ file_store,+ ocr_mode="auto",+ image_analysis=True,+ allow_local_source=True,+ )+ )++ assert rec.files[0].status == "failed"+ assert rec.files[0].error is not None+ assert "Unsafe image sidecar path" in rec.files[0].error.message++def test_api_server_zip_skips_model_output_when_parse_result_has_none(monkeypatch: pytest.MonkeyPatch,tmp_path: Path,