Skip to content

Move and delete attachment files only inside the uploads directory - #1238

Merged
vytisbulkevicius merged 11 commits into
developmentfrom
fix/rename-path-checks
Nov 12, 2026
Merged

vytisbulkevicius merged 11 commits into
developmentfrom
fix/rename-path-checks

Conversation

@vytisbulkevicius

@vytisbulkevicius vytisbulkevicius commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Rename, replace and offload build file paths from attachment metadata, then move, delete or upload those files. A path with ../ in that metadata reached files outside the uploads directory. Each of these paths is now checked against the uploads directory first.

What changed

  • Path check (new OptimoleWP\Media\UploadsPath::contains()): resolves the folder that holds the file with realpath(), so .. and symlinks are followed, and accepts the path only inside the uploads base directory. A folder that does not exist is refused. On Windows, realpath() returns backslashes, so the separator of the running system is normalized before the compare.

  • Rename (Optml_Attachment_Rename): refuses a main file outside uploads with the usual "Error renaming file." message, before anything moves. A scaled or size file outside uploads is skipped. Size links are renamed only for the size files that moved, so links to a skipped size keep working.

  • Replace (Optml_Attachment_Replace): refuses an original file outside uploads before it moves the uploaded file over it. Old size files and the old scaled file are deleted only inside uploads.

  • Offload (Optml_Media_Offload): treats a main file outside uploads like a missing file, so it is not uploaded or deleted. Generated size files and the scaled file are deleted only inside uploads.

Note

WordPress core checks its own attachment deletes the same way, with wp_delete_file_from_directory().

Out of scope: a symlink at the file itself, and a folder swapped for a symlink between the check and the use. Both need write access to the server files.

Known limits: an uploads subfolder that is a symlink to storage outside uploads is refused. An attachment stored under another uploads root, when a plugin changes upload_dir per request, is refused too. Rename, replace and offload then fail for that file.

QA

  1. Upload an image to WP Admin → Media → Library. Add it to a post with an Image block, set Resolution to Thumbnail, and publish. Create a file next to the uploads directory, and point one image size at it. Use WP-CLI, with the attachment ID:

    echo keep > wp-content/optml-qa-outside.txt
    wp eval '$m = wp_get_attachment_metadata( <ID> ); $m["sizes"]["thumbnail"]["file"] = "../../../optml-qa-outside.txt"; wp_update_attachment_metadata( <ID>, $m );'

    The three ../ assume the default year and month folders, such as uploads/2026/11.

  2. Open the image in WP Admin → Media → Library, click Replace or Rename, enter a new name under Rename attached file, and click Rename.

    Expect: the rename succeeds, and wp-content/optml-qa-outside.txt is still there with its content. A post that shows the thumbnail still displays it.

  3. On the same image, choose a new file under Replace file, and click Replace file.

    Expect: the replace succeeds, and wp-content/optml-qa-outside.txt is still there.

  4. Rename another image normally.

    Expect: the rename works as before, with all sizes renamed.

Review responses

  • Divi page resource write (inc/compatibilities/divi_builder.php, untested area on d6849fa): not changed. $page_resource->path is the path of Divi's own static CSS file, which Divi builds. It does not come from attachment metadata.

Issue: Codeinwp/optimole-service#1947

Rename, replace and offload build file paths from attachment metadata. Those
paths are now checked against the uploads directory before a file is moved,
deleted or offloaded.
@pirate-bot

pirate-bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Plugin build for 4f55b0b is ready 🛎️!

Replace now refuses a main file path outside uploads before it moves the
uploaded file. The uploads check also compares normalized paths, so Windows
paths match.
Offload checks the scaled file path before it deletes it, because the extension
is replaced in the whole path. Only the separator of the running system is
normalized, so a backslash in a folder name does not pass the uploads check.

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The uploads checks guard every changed move and delete, but a skipped size still gets its content URLs rewritten to a missing file.

Validation details
  • Files reviewed: 8/8 changed files. History questions not settled: issues #1136, #1108, #1201, #1119, #1139, #727 and #429.
  • Source trace: every changed move, delete, overwrite and upload runs after UploadsPath::contains().
  • No other place in inc/ moves or deletes a metadata path without a check.
  • The runner reported that the eight added tests pass at HEAD and fail on pr-base. These runs have no run record, so they are unverified.
Untested areas
  • Earlier issues #1136 (rollback suffixes), #1108 (transfer at 0%), #1201 (rename below WordPress 6.2), #1119 (missing local file), #1139 (lookup cache memory), #727 (offload below WordPress 6.0) and #429 (Avada fatal error) were not settled.
  • The Divi page resource write was not traced. Its path producer is not in the checkout.
  • The QA steps were not run on a real screen.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • inc/compatibilities/divi_builder.php:28,53-60 — unverified: put_contents( $page_resource-path, … ) has no containment check. Whether Divi can derive that path from attachment metadata requires the installed Divi version and ET_Core_PageReso…

🤖 Automated review · run code-review-agent_6af4e122d095d1.03926728.


🤖 Review agent — review posted ✅ on d6849fa8 · changes requested · 4 findings · 25 min

Run code-review-agent_6af4e122d095d1.03926728 · trail

Comment thread inc/v2/Media/UploadsPath.php Resolved
Comment thread inc/media_rename/attachment_replace.php Resolved
Comment thread inc/media_rename/attachment_rename.php Resolved

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

Three earlier findings are fixed. The skipped-size content link finding is still open and needs a fix.

Validation details
  • Files reviewed: 5/5 files changed since the prior review. The commit changes only tests.
  • The dot-dot test passes at HEAD: OK (1 test, 2 assertions) (R4).
  • The replace and rename outside-uploads tests pass at HEAD: OK (4 tests, 8 assertions) (R5).
  • The media outside-uploads tests pass at HEAD: OK (3 tests, 6 assertions) (R6).
  • The scaled replace test fails with the pr-base replace code: Failed asserting that file "/var/www/html/wp-content/optml-replace-photo-scaled.jpg/replace-photo-scaled.jpg" exists. (R8). It passes at HEAD: OK (1 test, 1 assertion) (R9).
  • A new variant pass found no other unchecked metadata path in inc/.
Untested areas
  • The Divi page resource path was traced in a public Divi copy. Its version is not confirmed.
  • The security pass was reused from run code-review-agent_6af4e122d095d1.03926728 and not rerun. The commit changes no plugin code.
  • The QA steps were not run on a real screen.

🤖 Automated review · run code-review-agent_6af527e4533418.52393007.


🤖 Review agent — review posted ✅ on f23a9e22 · changes requested · 0 findings · 18 min

Run code-review-agent_6af527e4533418.52393007 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The skipped-size link finding still fails when tampered metadata also changes a size's width or height.

Validation details
  • Files reviewed: 3/3 files changed since the prior review. History questions not settled in this run: rename below WordPress 6.2 (#1201), rollback filename suffixes (#1136), transfer stuck at 0% (#1108), offloaded download file (#1119), lookup cache memory (#1139), offload below WordPress 6.0 (#727), Avada fatal error (#429).
  • The new rename tests pass at HEAD: mkdir -p /workspace/checks/C2; cd /repo && set -o pipefail; timeout 600 vendor/bin/phpunit --do-not-cache-result --filte… printed OK (2 tests, 5 assertions) (run R2).
  • The kept-links test fails with the f23a9e2 rename code: B=/workspace/checks/C2/base-tree; rm -rf $B; mkdir -p $B && cd /repo && git archive HEAD | tar -x -C $B && for f in inc/… printed Failures: 1. (run R4)
  • A variant pass found no other rewrite of a skipped file's links.
Untested areas
  • The seven history questions above reuse earlier answers. This run did not settle them again.
  • The security pass was reused from run code-review-agent_6af4e122d095d1.03926728 and not rerun. The new commits change no trust-boundary site.
  • Replace links to newly generated sizes were not checked against the core metadata generator.
  • The QA steps were not run on a real screen.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • inc/media_rename/attachment_replace.php:101-114,224-238,252-264 — unverified: Replacement rewrites skipped-deletion references using freshly generated size metadata or the successfully overwritten original, rather than rename’s proposed size filename. Decidi…

🤖 Automated review · run code-review-agent_6af56c686efae4.70757492.


🤖 Review agent — review posted ✅ on 36e4da77 · changes requested · 0 findings · 14 min

Run code-review-agent_6af56c686efae4.70757492 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The skipped-size link finding still fails when two size entries share one file with different dimensions.

Validation details
  • Files reviewed: 4/4 files changed since the prior review. Rows not settled: rename below WordPress 6.2, rollback suffixes, transfer stuck at 0%, offloaded download file, lookup cache memory, offload below WordPress 6.0, Avada fatal error.
  • The rename tests pass at HEAD: cd /repo; set -o pipefail; timeout 300 vendor/bin/phpunit --do-not-cache-result --testdox --filter 'Test_Attachment_Rena… printed OK (36 tests, 270 assertions) (run R2)
  • The changed-width case fails with the 36e4da7 rename code: set -o pipefail; D=/workspace/checks/C6/base-36e4da7 && rm -rf $D && mkdir -p $D && git -C /repo archive 6f70beb0dd76cac… (run R9, exit 0)
  • The twin-size case fails at HEAD: cd /workspace/checks/C6 && python3 - <<'EOF' import re p='/workspace/checks/C6/C6_Twin_Test.php' s=open(p).read() old=""… printed Failed asserting that file (run R11)
  • Scaled, main, edited and original URLs are still renamed in an isolated run: cp /repo/inc/media_rename/attachment_db_renamer.php /repo/inc/media_rename/attachment_rename.php /repo/inc/media_rename/… (run R3, exit 0)
Untested areas
  • The seven history rows above reuse earlier answers. This run did not settle them.
  • The security pass was not rerun. The new commits change no trust-boundary site.
  • The variant pass was reused from run code-review-agent_6af56c686efae4.70757492.
  • The QA steps were not run on a real screen.

🤖 Automated review · run code-review-agent_6af58f2f3d2222.48182591.


🤖 Review agent — review posted ✅ on 6f70beb0 · changes requested · 0 findings · 20 min

Run code-review-agent_6af58f2f3d2222.48182591 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Approved

The PR passes: the skipped-size link finding is fixed, and the new commit adds no new problem.

Validation details
  • Files reviewed: 3/3 files changed since the prior review. Rows not settled: rename below WordPress 6.2, rollback suffixes, transfer stuck at 0%, offloaded download file, lookup cache memory, offload below WordPress 6.0, Avada fatal error.
  • The rename tests pass at HEAD: cd /repo && set -o pipefail; timeout 300 vendor/bin/phpunit --do-not-cache-result --testdox --filter 'Test_Attachment_Re… printed OK (8 tests, 194 assertions) (run R3)
  • The new test fails with the 6f70beb rename code: set -o pipefail; D=/workspace/checks/C1/alt-6f70beb && rm -rf $D && mkdir -p $D && git -C /repo archive 4f55b0b49a54f21d… printed Failures: 1 (run R4)
Untested areas
  • The seven history rows above reuse earlier answers. This run did not settle them.
  • A failed move of an existing scaled file still rewrites its links. Base code does the same. No move failure was run.
  • The security pass was not rerun. The new commit changes no trust-boundary site.
  • The QA steps were not run on a real screen.

🤖 Automated review · run code-review-agent_6af5984d0e30a7.37287564.


🤖 Review agent — review posted ✅ on 4f55b0b4 · approved · 0 findings · 23 min

Run code-review-agent_6af5984d0e30a7.37287564 · trail

@vytisbulkevicius
vytisbulkevicius merged commit a2514a5 into development Nov 12, 2026
15 checks passed
@vytisbulkevicius
vytisbulkevicius deleted the fix/rename-path-checks branch November 12, 2026 15:03
@pirate-bot

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 4.2.20 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@pirate-bot pirate-bot added the released Indicate that an issue has been resolved and released in a particular version of the product. label Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Indicate that an issue has been resolved and released in a particular version of the product.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants