Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

scan_packages: refuse VCS, URL and local-path specs before pip download (#12357)

Daniel Han01e99610d7eb
diff --git a/scripts/scan_packages.py b/scripts/scan_packages.py
index acab54ad3c0..03bd4e9869c 100644
--- a/scripts/scan_packages.py
+++ b/scripts/scan_packages.py
@@ -1737,6 +1737,44 @@ _PIP_DOWNLOAD_PIN_FLAGS = [
_RE_PKG_NAME_SANITIZE = re.compile(r"[^A-Za-z0-9._-]")
+# `--only-binary :all:` only filters index candidates: pip still builds a VCS, URL or local-path requirement for metadata before anything is scanned.
+_RE_INDEX_SPEC = re.compile(
+ r"^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?\s*(\[[A-Za-z0-9._,\s-]*\])?"
+ r"[\s()<>=!~*+,.A-Za-z0-9_-]*$"
+)
+_LOCAL_ARCHIVE_SUFFIXES = (
+ ".whl",
+ ".zip",
+ ".tar",
+ ".tar.gz",
+ ".tgz",
+ ".tar.bz2",
+ ".tbz",
+ ".tar.xz",
+ ".txz",
+ ".tlz",
+ ".tar.lz",
+ ".tar.lzma",
+)
+
+
+def _split_index_specs(specs: list[str], download_errors: list[str]) -> list[str]:
+ """Keep specs pip resolves from the index; record the rest as scan errors."""
+ kept = []
+ for spec in specs:
+ requirement = spec.split(";", 1)[0].strip()
+ # pip drops trailing extras before its local-archive check.
+ path_like = re.sub(r"\[[^\]]*\]$", "", requirement).rstrip().lower()
+ if _RE_INDEX_SPEC.match(requirement) and not path_like.endswith(_LOCAL_ARCHIVE_SUFFIXES):
+ kept.append(spec)
+ continue
+ download_errors.append(
+ f"refusing to download {spec}: VCS, URL and local-path requirements "
+ "run build code before they can be scanned; inspect it manually"
+ )
+ return kept
+
+
# sdist fallback. `--only-binary :all:` never builds an sdist, but a wheel-less project then cannot be fetched at all and one such package fails the whole --with-deps resolve. So on resolve failure we drop to per-spec and fetch any sdist-only package's raw tarball from the PyPI JSON API for scan_archive() to read statically: no pip, no build, same no-exec guarantee. Transport failures are still exit 2; only "no wheel" is downgraded.
# How many levels of indirect-dep recovery to chase. Bounded with dedup so recovery always terminates.
@@ -2064,6 +2102,9 @@ def _resolve_per_spec_with_deps(
if key in seen:
continue
seen.add(key)
+ if not _split_index_specs([dep], download_errors):
+ print(f" [WARN] skipping non-index indirect dep {dep}", file = sys.stderr)
+ continue
dep_ver = _spec_pin_version(dep)
cmd = [
sys.executable,
@@ -2135,6 +2176,9 @@ def download_packages(
results: list[tuple[str, str]] = []
download_errors: list[str] = []
env = _pip_download_env()
+ specs = _split_index_specs(specs, download_errors)
+ if not specs:
+ return results, download_errors
if with_deps:
os.makedirs(dest, exist_ok = True)
diff --git a/tests/security/test_scan_packages.py b/tests/security/test_scan_packages.py
index 2b008d4beaa..7a5e6658fcd 100644
--- a/tests/security/test_scan_packages.py
+++ b/tests/security/test_scan_packages.py
@@ -222,6 +222,41 @@ def test_scan_packages_pip_download_failure_propagates(tmp_path):
assert "SCAN INCOMPLETE" in combined or "pip download failed" in combined
+ "spec",
+ [
+ "./evilpkg",
+ "git+https://example.invalid/evil.git",
+ "evil @ https://example.invalid/evil-1.0.tar.gz",
+ "evil-1.0.tar.gz",
+ "evil.zip[x]",
+ "evil.zip [x]",
+ "evil.tar.gz[a] ; python_version>'3'",
+ ],
+)
+def test_direct_source_specs_never_reach_pip(tmp_path, monkeypatch, spec):
+ calls = []
+ monkeypatch.setattr(sp.subprocess, "run", lambda *a, **k: calls.append(a))
+ for with_deps in (False, True):
+ results, errors = sp.download_packages([spec], str(tmp_path), with_deps = with_deps)
+ assert results == []
+ assert errors and "refusing to download" in errors[0]
+ assert calls == []
+
+
+def test_index_specs_still_reach_pip():
+ errors = []
+ specs = [
+ "torch>=2.4.0",
+ "foo[bar,baz]>=1,<2",
+ "six>=1.16 ; python_version>'3'",
+ "zope.interface!=5.*",
+ "backports.lzma",
+ ]
+ assert sp._split_index_specs(specs, errors) == specs
+ assert errors == []
+
+
def test_archive_corruption_produces_critical_finding(tmp_path):
"""SF1: a corrupted wheel (once silently skipped) must yield a CRITICAL `archive_corrupted`."""
bad = tmp_path / "broken-0.0.1-py3-none-any.whl"