Skip to content

🛡️ Sentinel: [HIGH] Fix predictable temporary file vulnerability in cache#632

Merged
abhimehro merged 2 commits intomainfrom
sentinel-fix-temp-file-cache-7722500068853735371
Mar 12, 2026
Merged

🛡️ Sentinel: [HIGH] Fix predictable temporary file vulnerability in cache#632
abhimehro merged 2 commits intomainfrom
sentinel-fix-temp-file-cache-7722500068853735371

Conversation

@abhimehro
Copy link
Owner

🚨 Severity: HIGH
💡 Vulnerability: cache.py used a predictable temporary file name (blocklists.tmp) with logic that unlinked any existing file before opening it with O_EXCL. This logic was susceptible to a Time-Of-Check-To-Time-Of-Use (TOCTOU) race condition and symlink attacks.
🎯 Impact: A malicious actor with local system access could use a symlink attack to overwrite arbitrary files the application user has write access to, or potentially manipulate the loaded cache data by swapping files mid-write.
🔧 Fix: Replaced predictable file logic with Python's secure tempfile.mkstemp using O_CREAT | O_EXCL and 0o600 permissions. Wraps writing and renaming inside a robust try...finally block for secure execution and artifact cleanup in case of exceptions.
Verification: Run uv run pytest and uv run ruff check . tests successfully. Tested and reviewed memory to ensure artifacts are properly removed and operations run safely.


PR created automatically by Jules for task 7722500068853735371 started by @abhimehro

…ache

Replaced `temp_file = cache_file.with_suffix(".tmp")` and unlinking logic with `tempfile.mkstemp` inside `save_disk_cache`.
This securely generates an unpredictable temporary file name within the targeted directory, using `O_CREAT | O_EXCL` and `0o600` permissions. This closes a Time-Of-Check-To-Time-Of-Use (TOCTOU) vulnerability window that could allow attackers to manipulate symlinks to overwrite arbitrary files or inject malicious payload into the cache. Included robust cleanup via `try...finally` block.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@trunk-io
Copy link

trunk-io bot commented Mar 11, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@github-actions
Copy link

👋 Development Partner is reviewing this PR. Will provide feedback shortly.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical security vulnerability in the caching mechanism by overhauling how temporary files are created and managed. The changes prevent potential Time-Of-Check-To-Time-Of-Use (TOCTOU) race conditions and symlink attacks, thereby safeguarding against arbitrary file overwrites or cache data manipulation by malicious actors. The update ensures that temporary files are generated securely and cleaned up robustly, significantly enhancing the application's security posture.

Highlights

  • Security Vulnerability Fix: Addressed a high-severity predictable temporary file vulnerability in cache.py that could lead to TOCTOU race conditions and symlink attacks.
  • Secure Temporary File Handling: Replaced insecure manual temporary file creation with Python's tempfile.mkstemp for unique, securely permissioned temporary files.
  • Enhanced Robustness and Cleanup: Implemented a try...finally block to ensure atomic renaming and reliable cleanup of temporary files, even in case of exceptions.
Changelog
  • .jules/sentinel.md
    • Added a new entry documenting the critical predictable temporary file vulnerability in the cache.
  • cache.py
    • Imported the tempfile module for secure temporary file operations.
    • Replaced the manual os.open call for temporary file creation with tempfile.mkstemp.
    • Introduced a try...finally block to ensure the temporary file is always cleaned up, even if the rename operation fails.
Activity
  • PR created automatically by Jules for task 7722500068853735371, initiated by @abhimehro.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a critical TOCTOU (Time-Of-Check-To-Time-Of-Use) vulnerability in the disk caching mechanism. The previous implementation used a predictable temporary filename and a non-atomic unlink-then-create pattern, which created a race condition exploitable via symlink attacks. The fix correctly replaces this with tempfile.mkstemp to generate a secure, unique temporary file, and wraps the file operations in a try...finally block for robust cleanup. The change is well-implemented and follows security best practices. I have one suggestion to improve the logging in the cleanup logic to make it even more robust.

Comment on lines +231 to +232
except OSError:
pass

Choose a reason for hiding this comment

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

medium

The except OSError: pass silently ignores failures to clean up the temporary file. While the main logic is now secure, failing to remove temporary files can lead to disk space exhaustion over time if errors are frequent. It's better to log this failure as a warning so that operators are aware of potential issues and can investigate if they occur repeatedly.

Suggested change
except OSError:
pass
except OSError as e:
log.warning(f"Failed to remove temporary file {temp_path}: {_sanitize_fn(e)}")

# Security: use tempfile.mkstemp to securely create a unique temporary file
# with O_CREAT | O_EXCL and 0o600 permissions, preventing predictable
# temporary file vulnerabilities and TOCTOU races.
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Variable name "fd" doesn't conform to snake_case naming style Warning

Variable name "fd" doesn't conform to snake_case naming style
# Security: use tempfile.mkstemp to securely create a unique temporary file
# with O_CREAT | O_EXCL and 0o600 permissions, preventing predictable
# temporary file vulnerabilities and TOCTOU races.
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Line too long (106/100) Warning

Line too long (106/100)
json.dump(_disk_cache, f, indent=2)

# POSIX guarantees rename is atomic.
temp_path.replace(cache_file)

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Variable name "f" doesn't conform to snake_case naming style Warning

Variable name "f" doesn't conform to snake_case naming style
# Security: use tempfile.mkstemp to securely create a unique temporary file
# with O_CREAT | O_EXCL and 0o600 permissions, preventing predictable
# temporary file vulnerabilities and TOCTOU races.
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))

Check warning

Code scanning / Pylint (reported by Codacy)

Line too long (106/100) Warning

Line too long (106/100)
# Security: use tempfile.mkstemp to securely create a unique temporary file
# with O_CREAT | O_EXCL and 0o600 permissions, preventing predictable
# temporary file vulnerabilities and TOCTOU races.
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))

Check warning

Code scanning / Pylint (reported by Codacy)

Variable name "fd" doesn't conform to snake_case naming style Warning

Variable name "fd" doesn't conform to snake_case naming style
json.dump(_disk_cache, f, indent=2)

# POSIX guarantees rename is atomic.
temp_path.replace(cache_file)

Check warning

Code scanning / Pylint (reported by Codacy)

Variable name "f" doesn't conform to snake_case naming style Warning

Variable name "f" doesn't conform to snake_case naming style
**Vulnerability:** The `_api_client` and `_gh` HTTP clients in `main.py` relied on a broad scalar timeout (`timeout=30`), opening the possibility for attackers to use Slowloris-style DoS attacks or slow network scenarios to exhaust connection pools or processing limits.
**Learning:** Default or generic long timeouts are not enough; specifying an explicit connection timeout enforces that sockets resolve or fail promptly, separating wait time for establishing a connection from reading data.
**Prevention:** Use explicit timeouts via `httpx.Timeout(default=read_timeout, connect=connect_timeout)` (e.g., `httpx.Timeout(default=10.0, connect=5.0)`) instead of simple scalar delays, forcing connections to be completed quickly and mitigating resource exhaustion vulnerabilities.
## 2024-03-11 - [CRITICAL] Predictable Temporary File Vulnerability in Cache

Check notice

Code scanning / Remark-lint (reported by Codacy)

Warn when shortcut reference links are used. Note

[no-shortcut-reference-link] Use the trailing [] on reference links
**Vulnerability:** The `_api_client` and `_gh` HTTP clients in `main.py` relied on a broad scalar timeout (`timeout=30`), opening the possibility for attackers to use Slowloris-style DoS attacks or slow network scenarios to exhaust connection pools or processing limits.
**Learning:** Default or generic long timeouts are not enough; specifying an explicit connection timeout enforces that sockets resolve or fail promptly, separating wait time for establishing a connection from reading data.
**Prevention:** Use explicit timeouts via `httpx.Timeout(default=read_timeout, connect=connect_timeout)` (e.g., `httpx.Timeout(default=10.0, connect=5.0)`) instead of simple scalar delays, forcing connections to be completed quickly and mitigating resource exhaustion vulnerabilities.
## 2024-03-11 - [CRITICAL] Predictable Temporary File Vulnerability in Cache

Check notice

Code scanning / Remark-lint (reported by Codacy)

Warn when references to undefined definitions are found. Note

[no-undefined-references] Found reference to undefined definition
Keep both sentinel journal entries (SSRF refactor from #630 and
temp file vulnerability from #632) in chronological order.
Copilot AI review requested due to automatic review settings March 12, 2026 00:10
@github-actions
Copy link

👋 Development Partner is reviewing this PR. Will provide feedback shortly.

Copy link
Owner Author

@abhimehro abhimehro left a comment

Choose a reason for hiding this comment

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

Review Summary

Verdict: Approve for merge

This PR correctly fixes a TOCTOU/symlink attack vulnerability in save_disk_cache() by replacing predictable temp file names with tempfile.mkstemp. The fix is sound:

  1. Security: tempfile.mkstemp generates unpredictable filenames with O_CREAT | O_EXCL and 0o600 permissions — eliminates the symlink attack vector.
  2. Cleanup: The try/finally block correctly handles both success (where replace() moves the temp file, so exists() returns False) and failure (where the temp file is cleaned up).
  3. Atomicity: The atomic rename/replace pattern is preserved.

Minor note

The except OSError: pass in cleanup was flagged by Gemini code review as a candidate for logging. This is valid but non-blocking — silent cleanup failure in a finally block is a common pattern and won't affect correctness.

Conflict resolution

Resolved a trivial conflict in .jules/sentinel.md caused by both #630 and #632 appending entries to the same file. Both entries are now present in chronological order.

All CI checks passing. Squash-merging.


🤖 Reviewed by automated backlog cleanup agent

@abhimehro abhimehro merged commit 4afad64 into main Mar 12, 2026
16 checks passed
@abhimehro abhimehro deleted the sentinel-fix-temp-file-cache-7722500068853735371 branch March 12, 2026 00:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Addresses a local security vulnerability in cache.save_disk_cache() by replacing a predictable temp filename + unlink/retry pattern with a securely generated unique temp file, reducing TOCTOU and symlink-attack risk when writing the on-disk cache.

Changes:

  • Switch temp-file creation in save_disk_cache() to tempfile.mkstemp(...) and wrap write+rename in try...finally cleanup.
  • Document the vulnerability and prevention guidance in the Sentinel security log.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
cache.py Uses tempfile.mkstemp for unique temp cache writes and ensures cleanup after atomic replace.
.jules/sentinel.md Adds an entry documenting the predictable temp-file vulnerability and mitigation guidance.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +80 to +81
## 2024-03-11 - [CRITICAL] Predictable Temporary File Vulnerability in Cache
**Vulnerability:** `save_disk_cache` used a predictable file name (`cache_file.with_suffix(".tmp")`) when creating temporary cache files. Despite using `O_EXCL`, the code would unlink existing files and retry if a collision occurred. This opened up a Time-Of-Check-To-Time-Of-Use (TOCTOU) vulnerability window where a malicious user could exploit a symlink attack to overwrite arbitrary system files or inject malicious cache data.
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The new Sentinel entry labels this issue as [CRITICAL], but the PR title/description classify it as [HIGH]. Please align the severity wording between the PR metadata and the Sentinel log to avoid confusion in future triage/reporting.

Copilot uses AI. Check for mistakes.
Comment on lines +217 to +221
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))
temp_path = Path(temp_file_path_str)

# POSIX guarantees rename is atomic.
temp_file.replace(cache_file)
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

If os.fdopen(fd, ...) raises before the context manager takes ownership, the raw file descriptor fd will leak and the finally cleanup may fail to unlink the temp file on Windows (open files can’t be deleted). Consider ensuring fd is closed in the finally block when it hasn’t been successfully wrapped (e.g., set fd = None after os.fdopen succeeds, and os.close(fd) in finally when still set), and perform cleanup after closing.

Copilot uses AI. Check for mistakes.
Comment on lines +214 to +218
# Security: use tempfile.mkstemp to securely create a unique temporary file
# with O_CREAT | O_EXCL and 0o600 permissions, preventing predictable
# temporary file vulnerabilities and TOCTOU races.
fd, temp_file_path_str = tempfile.mkstemp(prefix="blocklists.", suffix=".tmp", dir=str(cache_dir))
temp_path = Path(temp_file_path_str)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The existing disk-cache tests appear to assert the absence of a specific temp filename (e.g., blocklists.tmp). With mkstemp(prefix="blocklists.", suffix=".tmp"), that assertion becomes a no-op (the file is never created under that fixed name), so the atomic-write/temp-cleanup behavior may no longer be meaningfully tested. Please update/add a test that checks the cache dir for any leftover blocklists.*.tmp files after save_disk_cache() and (where supported) validates that predictable temp names/symlink pre-creation cannot influence the write.

Copilot generated this review using guidance from repository custom instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants