Skip to content

fix(security): avoid torch import-hijack in PyTorch ZIP CVE checks#728

Merged
mldangelo-oai merged 2 commits intomainfrom
codex/fix-pytorch-cve-checks-import-issue
Mar 20, 2026
Merged

fix(security): avoid torch import-hijack in PyTorch ZIP CVE checks#728
mldangelo-oai merged 2 commits intomainfrom
codex/fix-pytorch-cve-checks-import-issue

Conversation

@mldangelo-oai
Copy link
Contributor

Motivation

  • The PyTorch ZIP CVE gating previously imported torch during scans which allowed import-hijack execution if an attacker placed a malicious torch.py on sys.path or in the working directory.
  • The change removes that scanner-triggered import to preserve the static-analysis threat model and avoid executing untrusted code during scanning.

Description

  • Updated PyTorchZipScanner._get_installed_pytorch_version to avoid import torch and instead read torch.__version__ only from sys.modules when torch is already imported.
  • Adjusted CVE version-selection behavior to continue using the helper but without causing a module import side-effect.
  • Added a regression test test_get_installed_pytorch_version_does_not_import_torch in tests/scanners/test_pytorch_zip_scanner.py that verifies the helper does not trigger a torch import (monkeypatches __import__).
  • Files changed: modelaudit/scanners/pytorch_zip_scanner.py and tests/scanners/test_pytorch_zip_scanner.py.

Testing

  • Ran targeted scanner tests with uv run pytest tests/scanners/test_pytorch_zip_scanner.py and all related tests passed (44 passed).
  • Ran formatting and linting with uv run ruff format modelaudit/ tests/ and uv run ruff check --fix modelaudit/ tests/ which succeeded with no changes required.
  • Ran type checks with uv run mypy modelaudit/ which reported no issues.
  • Ran the full non-integration test suite with uv run pytest -n auto -m "not slow and not integration" --maxfail=1, which mostly passed but aborted due to an unrelated environment-sensitive test failure (tests/utils/helpers/test_secure_hasher.py::TestErrorHandling::test_hash_permission_denied), and is not tied to this change.

Codex Task

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 20, 2026

Warning

Rate limit exceeded

@mldangelo-oai has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 0 minutes and 40 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3f0b622b-2358-45e1-860a-822be34e8e80

📥 Commits

Reviewing files that changed from the base of the PR and between d19d6fd and db49133.

📒 Files selected for processing (2)
  • modelaudit/scanners/pytorch_zip_scanner.py
  • tests/scanners/test_pytorch_zip_scanner.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-pytorch-cve-checks-import-issue

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7636cc9a1f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +995 to +997
torch_module = sys.modules.get("torch")
if torch_module is None:
return None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Detect installed torch version without requiring prior import

Return value now depends on torch already being present in sys.modules. In normal scanner runs, torch is often installed but not imported, so this returns None. That causes _select_pytorch_version_for_check() to ignore vulnerable local runtimes (or skip version-gated CVE checks when metadata is missing), creating false negatives in security detection.

Useful? React with 👍 / 👎.

Remove any preloaded torch module from sys.modules in the import-guard test so
it verifies the scanner does not import torch, independent of CI environment
state.

Co-authored-by: Codex <noreply@openai.com>
@mldangelo-oai mldangelo-oai merged commit badd611 into main Mar 20, 2026
25 checks passed
@mldangelo-oai mldangelo-oai deleted the codex/fix-pytorch-cve-checks-import-issue branch March 20, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant