Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix: eliminate TOCTOU races in catalog_fetch() for file:// and bare path URLs (#3910)

Quratulain-bilal36205c28f713
diff --git a/src/specify_cli/bundles/adapters.py b/src/specify_cli/bundles/adapters.py
index 69b0f903a..5178529cb 100644
--- a/src/specify_cli/bundles/adapters.py
+++ b/src/specify_cli/bundles/adapters.py
@@ -22,7 +22,7 @@ from urllib.request import url2pathname
from .._assets import _locate_core_pack, _repo_root
from .._download_security import MAX_JSON_CATALOG_BYTES, read_response_limited
from . import BundlerError
-from .yamlio import load_json, loads_json
+from .yamlio import loads_json
from .catalogs import CatalogSource
from .manifest import ComponentRef
@@ -183,15 +183,21 @@ def make_catalog_fetcher(*, allow_network: bool = True):
if scheme == "file":
path = _file_url_to_path(parsed)
- if not path.exists():
- raise BundlerError(f"Catalog file not found: {path}")
- return load_json(path)
+ try:
+ return loads_json(path.read_text(encoding="utf-8"), origin=str(path))
+ except FileNotFoundError:
+ raise BundlerError(f"Catalog file not found: {path}") from None
+ except (OSError, UnicodeError) as exc:
+ raise BundlerError(f"Could not read {path}: {exc}") from exc
if scheme == "" or _is_windows_drive_path(url):
path = Path(url)
- if not path.exists():
- raise BundlerError(f"Catalog file not found: {path}")
- return load_json(path)
+ try:
+ return loads_json(path.read_text(encoding="utf-8"), origin=str(path))
+ except FileNotFoundError:
+ raise BundlerError(f"Catalog file not found: {path}") from None
+ except (OSError, UnicodeError) as exc:
+ raise BundlerError(f"Could not read {path}: {exc}") from exc
if scheme in ("http", "https"):
if not allow_network:
diff --git a/tests/specify_cli/bundles/test_adapters.py b/tests/specify_cli/bundles/test_adapters.py
index 6ce9a3232..0f9577074 100644
--- a/tests/specify_cli/bundles/test_adapters.py
+++ b/tests/specify_cli/bundles/test_adapters.py
@@ -7,7 +7,9 @@ import ssl
import urllib.error
import urllib.request
import warnings
+from pathlib import Path
from typing import Self
+from unittest.mock import MagicMock, patch
import pytest
@@ -515,3 +517,24 @@ def test_validate_remote_url_rejects_malformed_url_cleanly(url):
caller. Bundler sibling of #3369."""
with pytest.raises(BundlerError):
adapters._validate_remote_url("team", url)
+
+
[email protected]("use_file_url", [False, True], ids=["path", "file-url"])
+def test_local_catalog_toctou_race(tmp_path, use_file_url):
+ """A missing file at read time must retain the fetcher's BundlerError contract.
+
+ The mocked Path raises FileNotFoundError from read_text(), simulating a
+ deletion immediately before the catalog is opened while verifying that no
+ existence pre-check is needed."""
+ catalog_path = tmp_path / "catalog.json"
+ url = catalog_path.as_uri() if use_file_url else str(catalog_path)
+
+ mock_path = MagicMock(spec=Path)
+ mock_path.read_text.side_effect = FileNotFoundError(str(catalog_path))
+
+ fetcher = adapters.make_catalog_fetcher(allow_network=False)
+
+ with patch.object(adapters.Path, "__new__", return_value=mock_path):
+ with pytest.raises(BundlerError, match="Catalog file not found"):
+ fetcher(_source(url))
+ mock_path.exists.assert_not_called()