Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
security: enforce multifile download path boundary (#9258)
Bill01e327227fcd
diff --git a/invokeai/app/services/download/download_default.py b/invokeai/app/services/download/download_default.pyindex 13e86d1828..56197dcfb3 100644--- a/invokeai/app/services/download/download_default.py+++ b/invokeai/app/services/download/download_default.py@@ -180,7 +180,8 @@ class DownloadQueueService(DownloadQueueServiceBase):on_cancelled: Optional[DownloadEventHandler] = None,on_error: Optional[DownloadExceptionHandler] = None,) -> MultiFileDownloadJob:- mfdj = MultiFileDownloadJob(dest=dest, id=self._next_id())+ dest_root = dest.resolve()+ mfdj = MultiFileDownloadJob(dest=dest_root, id=self._next_id())mfdj.set_callbacks(on_start=on_start,on_progress=on_progress,@@ -191,8 +192,9 @@ class DownloadQueueService(DownloadQueueServiceBase):for part in parts:url = part.url- path = dest / part.path- assert path.is_relative_to(dest), "only relative download paths accepted"+ path = (dest_root / part.path).resolve()+ if not path.is_relative_to(dest_root):+ raise ValueError("only relative download paths accepted")job = DownloadJob(source=url,dest=path,diff --git a/tests/app/services/download/test_download_queue.py b/tests/app/services/download/test_download_queue.pyindex edf3c115ac..bfd2e00fdb 100644--- a/tests/app/services/download/test_download_queue.py+++ b/tests/app/services/download/test_download_queue.py@@ -310,12 +310,38 @@ def test_multifile_onefile(tmp_path: Path, mm2_session: Session) -> None:queue.stop()+def test_multifile_download_with_relative_dest(tmp_path: Path, mm2_session: Session, monkeypatch: Any) -> None:+ started_paths: list[Path | None] = []+ monkeypatch.chdir(tmp_path)+ queue = DownloadQueueService(+ requests_session=mm2_session,+ )+ queue.start()+ job = queue.multifile_download(+ parts=[+ RemoteModelFile(+ url=AnyHttpUrl("http://www.civitai.com/models/12345"),+ path=Path("nested/mock12345.safetensors"),+ )+ ],+ dest=Path("relative-downloads"),+ on_start=lambda job: started_paths.append(job.download_path),+ )+ queue.join()++ assert job.status == DownloadJobStatus("completed"), "expected job status to be completed"+ assert job.download_path == tmp_path / "relative-downloads/nested"+ assert started_paths == [tmp_path / "relative-downloads/nested"]+ assert Path(tmp_path, "relative-downloads/nested/mock12345.safetensors").exists()+ queue.stop()++def test_multifile_no_rel_paths(tmp_path: Path, mm2_session: Session) -> None:queue = DownloadQueueService(requests_session=mm2_session,)- with pytest.raises(AssertionError) as error:+ with pytest.raises(ValueError) as error:queue.multifile_download(parts=[RemoteModelFile(url=AnyHttpUrl("http://www.civitai.com/models/12345"), path=Path("/etc/passwd"))],dest=tmp_path,@@ -323,6 +349,24 @@ def test_multifile_no_rel_paths(tmp_path: Path, mm2_session: Session) -> None:assert str(error.value) == "only relative download paths accepted"+def test_multifile_no_parent_traversal_paths(tmp_path: Path, mm2_session: Session) -> None:+ queue = DownloadQueueService(+ requests_session=mm2_session,+ )++ with pytest.raises(ValueError) as error:+ queue.multifile_download(+ parts=[+ RemoteModelFile(+ url=AnyHttpUrl("http://www.civitai.com/models/12345"),+ path=Path("../outside.safetensors"),+ )+ ],+ dest=tmp_path,+ )+ assert str(error.value) == "only relative download paths accepted"++@contextmanagerdef clear_config() -> Generator[None, None, None]:try: