From d0eb5ec6d6b00a600b8eef02bde69fee6adfac7e Mon Sep 17 00:00:00 2001 From: whning Date: Sat, 31 Oct 2026 20:23:52 +0800 Subject: [PATCH] Reject duplicate keys in manifest JSON --- artifactManifest/artifact_manifest.py | 11 ++++++++++- artifactManifest/tests/test_artifact_manifest.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/artifactManifest/artifact_manifest.py b/artifactManifest/artifact_manifest.py index c529c33..60e84c3 100644 --- a/artifactManifest/artifact_manifest.py +++ b/artifactManifest/artifact_manifest.py @@ -210,9 +210,18 @@ def _validate_relative_path(value: object) -> str: return value +def _unique_json_object(pairs: list[tuple[str, object]]) -> dict[str, object]: + result: dict[str, object] = {} + for key, value in pairs: + if key in result: + raise ManifestError(f"duplicate JSON key: {key!r}") + result[key] = value + return result + + def _load_expected(manifest_path: Path) -> dict[str, ManifestFile]: try: - payload = json.loads(manifest_path.read_text(encoding="utf-8")) + payload = json.loads(manifest_path.read_text(encoding="utf-8"), object_pairs_hook=_unique_json_object) except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc: raise ManifestError(f"cannot read manifest {manifest_path}: {exc}") from exc if not isinstance(payload, dict): diff --git a/artifactManifest/tests/test_artifact_manifest.py b/artifactManifest/tests/test_artifact_manifest.py index f9cfc91..82b4296 100644 --- a/artifactManifest/tests/test_artifact_manifest.py +++ b/artifactManifest/tests/test_artifact_manifest.py @@ -221,3 +221,16 @@ def test_schema_version_requires_integer(tmp_path: Path, version: object) -> Non manifest.write_text(json.dumps({"schema_version": version, "root": ".", "files": []}), encoding="utf-8") with pytest.raises(artifact_manifest.ManifestError, match="unsupported schema_version"): artifact_manifest.verify_manifest(root, manifest) + + +@pytest.mark.parametrize("content", [ + '{"schema_version": 1, "schema_version": 1, "root": ".", "files": []}', + '{"schema_version": 1, "root": ".", "files": [{"path":"x", "path":"y"}]}', +]) +def test_manifest_rejects_duplicate_json_keys(tmp_path: Path, content: str) -> None: + root = tmp_path / "artifact" + root.mkdir() + manifest = tmp_path / "manifest.json" + manifest.write_text(content, encoding="utf-8") + with pytest.raises(artifact_manifest.ManifestError, match="duplicate JSON key"): + artifact_manifest.verify_manifest(root, manifest)