Utils: Add make_temp_file() and make_temp_dir() helper functions - #6374
Utils: Add make_temp_file() and make_temp_dir() helper functions#6374swissspidy wants to merge 12 commits into
make_temp_file() and make_temp_dir() helper functions#6374Conversation
…ch_editor_for_input()
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR adds secure temporary file and directory helpers with bounded retries. Extraction, editor input, and Phar update flows use these helpers instead of manually constructed paths. Tests cover validation, permissions, cleanup, symlink handling, formatting, and extracted-path normalization. ChangesTemporary resource security
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
php/utils.php (1)
599-613: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider reusing
make_temp_file()here for consistency.
launch_editor_for_input()still implements its own exclusive-creation retry loop instead of calling the newmake_temp_file()helper this PR introduces. The rest of the codebase now centralizes secure temporary-file creation inmake_temp_file(). Duplicating the same 'xb' exclusive-open retry pattern here increases maintenance cost and risks the two implementations drifting apart over time.Consider passing a prefix derived from
$tmpfileand a suffix of'.' . $exttomake_temp_file(), then writing$inputto the returned path with an explicit write-and-check step.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@php/utils.php` around lines 599 - 613, Update launch_editor_for_input() to use the existing make_temp_file() helper instead of its local exclusive-creation retry loop. Pass a prefix derived from $tmpfile and the '.'.$ext suffix, then write $input to the returned path with an explicit write-result check while preserving the existing cleanup/error behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@php/utils.php`:
- Around line 609-612: Update the file-writing block around $fp to validate the
return value of fwrite() and call WP_CLI::error() when the write fails or is
incomplete, before closing the file or opening the editor. Preserve the existing
successful-write flow and cleanup behavior.
---
Nitpick comments:
In `@php/utils.php`:
- Around line 599-613: Update launch_editor_for_input() to use the existing
make_temp_file() helper instead of its local exclusive-creation retry loop. Pass
a prefix derived from $tmpfile and the '.'.$ext suffix, then write $input to the
returned path with an explicit write-result check while preserving the existing
cleanup/error behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 2ac0db3b-1510-4469-a1a5-b4fc72855e2c
📒 Files selected for processing (4)
php/WP_CLI/Extractor.phpphp/commands/src/CLI_Command.phpphp/utils.phptests/UtilsTest.php
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This comment was marked as resolved.
This comment was marked as resolved.
`fopen( ..., 'xb' )` applies the process umask, which typically leaves the file at 0644 and therefore readable by other local users. Both call sites can hold sensitive input - the editor buffer in `launch_editor_for_input()` and whatever callers write to a `make_temp_file()` path - so create them under a temporary 0177 umask, matching the 0700 used for temporary directories. Narrowing the umask around `fopen()` rather than chmod'ing afterwards also avoids the window in which the file exists with wider permissions.
`ReflectionMethod::invoke()` returns mixed, so narrow it before passing it to `assertStringNotContainsString()`. `assertInstanceOf()` inside a typed `catch` block is always true. Assert the exit code carried by the exception instead, which is what the calls were meant to establish.
9ef297c to
51fa905
Compare
Summary by CodeRabbit
Security
Bug Fixes
Tests