Skip to content

Only delete, export and store media files inside the uploads folder - #2242

Merged
subodhr258 merged 2 commits into
mainfrom
fix/media-file-paths
Oct 26, 2026
Merged

subodhr258 merged 2 commits into
mainfrom
fix/media-file-paths

Conversation

@subodhr258

Copy link
Copy Markdown
Collaborator

What changes

GoDAM now only deletes, exports and stores media files inside the WordPress uploads folder.

  • Deleting a media item (admin/godam-transcoder-functions.php): the transcoded files and thumbnails listed in the item's meta are deleted only when they resolve (realpath()) to a file inside the uploads folder. Anything else is skipped. New helper rtgodam_get_uploads_file_path(); sites whose uploads live on a stream wrapper get a normalized-path check instead.
  • Protected meta: rtgodam_media_thumbnails and rtgodam_media_transcoded_files are now protected (is_protected_meta), so XML-RPC custom fields and the Custom Fields box no longer write them. GoDAM's own update_post_meta() calls are unaffected.
  • create-media-entry (inc/classes/rest-api/class-media-library.php):
    • _wp_attached_file keeps only the bare file name from filename (no directories, no . or ..). Virtual media has no local file; every reader only uses the name or its extension. The name itself is kept as sent, so the Media Library shows the same file name as before.
    • url, mpd_url and hls_url must be public http or https addresses (wp_http_validate_url(), plus the 169.254.0.0/16 range, which core only refuses from 7.x). Anything else returns 400 invalid_url and nothing is created.
  • Folder ZIP export (inc/classes/media-library/class-media-folder-create-zip.php): adds a file only if it is inside the uploads folder, so existing entries with any other stored path are skipped.

Testing

Throwaway WordPress (Playground, WordPress 7.1.3, PHP 8.3) with the 2.5.0 release plus these files; Author checks with a real Author login over HTTP; harmless marker files only.

Check Result
XML-RPC wp.editPost custom fields for the two GoDAM keys on the Author's own image not written; a plain custom field in the same call is still saved
Custom Fields box (add-meta) for the two keys refused; a plain key is still saved
Deleting an item whose meta lists paths outside uploads those files are left alone; the item's own upload, transcoded file and thumbnail inside uploads are deleted
Ordinary upload deleted over REST 200, file gone (unchanged)
create-media-entry with a filename containing directories only the file name is stored
create-media-entry with normal CDN https URLs stored unchanged
url / mpd_url / hls_url set to loopback, private, link-local or file:// addresses 400 invalid_url, nothing created
Folder ZIP with normal uploads plus items whose stored path is outside uploads only the normal uploads are in the ZIP
Folder ZIP of normal uploads only unchanged
  • PHPUnit: 50 new tests in tests/php/MediaFilePathsTest.php and tests/php/CreateMediaEntryUrlTest.php; full suite OK (663 tests).
  • phpcs clean on every changed file; bin/hook-check balance and coverage checks pass.

Worth knowing

  • Values already stored in the database are not migrated; delete and ZIP skip them.
  • A development site whose GoDAM API points at a private or .local host would now get invalid_url for virtual media. It can allow its host with core's http_request_host_is_external filter.
  • wp_http_validate_url() only allows ports 80, 443 and 8080 and does a DNS lookup per URL. GoDAM's CDN URLs use 443.

QA:

  1. Upload and transcode a video, then delete it: its transcoded files and thumbnails in uploads are removed.
  2. Add a GoDAM Central (virtual) video from the Media Library: it plays, shows its original file name, and exports in a folder ZIP.
  3. Export a folder of normal uploads as a ZIP: all files are there.

Issue: rtCamp/godam-plugin-wp#70

@subodhr258 subodhr258 self-assigned this Oct 22, 2026
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

🔍 WordPress Plugin Check Report

⚠️ Status: Passed with warnings

📊 Report

🎯 Total Issues ❌ Errors ⚠️ Warnings
13 0 13

⚠️ Warnings (13)

📁 readme.txt (2 warnings)
📍 Line 🔖 Check 💬 Message
0 mismatched_plugin_name Plugin name "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" is different from the name declared in plugin header "GoDAM".
0 trademarked_term The plugin name includes a restricted term. Your chosen plugin name - "GoDAM - Organize WordPress Media Library & File Manager with Unlimited Folders for Images, Videos & more" - contains the restricted term "wordpress" which cannot be used at all in your plugin name.
📁 assets/build/css/main.css (1 warning)
📍 Line 🔖 Check 💬 Message
0 EnqueuedStylesScope This style is being loaded in all contexts.
📁 assets/src/libs/analytics.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/10/26/hello-world/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
📁 assets/build/js/main.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/10/26/hello-world/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.

🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

- Deleting a media item removes the transcoded files and thumbnails listed
  in its meta only when they are files inside the uploads folder. Any other
  path in the meta is skipped.
- rtgodam_media_thumbnails and rtgodam_media_transcoded_files are protected
  meta, so XML-RPC custom fields and the custom fields box no longer write
  them. GoDAM keeps saving them with update_post_meta().
- create-media-entry stores a bare file name as the media item's file, with
  no directories, and only saves the url, mpd_url and hls_url values that
  are public http(s) addresses.
- The folder ZIP export adds only files that are inside the uploads folder.
- Add PHPUnit tests for each of the above.
@subodhr258
subodhr258 changed the base branch from develop to main October 23, 2026 06:21
@subodhr258
subodhr258 force-pushed the fix/media-file-paths branch from 63d4659 to 0caa100 Compare October 23, 2026 06:21
@subodhr258

Copy link
Copy Markdown
Collaborator Author

Tested together with the other five 2.5.1 PRs on a hosted test site connected to production GoDAM Central. Full table and all recordings: https://github.com/rtCamp/godam-plugin-wp/issues/68 (recordings are in the rtCamp Test media library; sign in to watch).

Tested commit 63d465907; the current head 0caa10051 has the same code.

  • Every test video transcoded and was deleted without errors.
  • Deleting a transcoded video leaves the originally uploaded file in uploads: T8. 2.5.0 does the same: B8. After transcoding, the attachment points at the -original copy downloaded from Central, and nothing keeps track of the uploaded file. This is existing behaviour, not something this PR changes.
  • Not covered: the folder ZIP export and adding a video from the GoDAM tab.

@elifvish elifvish 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.

Low, consistency: the new public-URL check covers url, mpd_url and hls_url, but icon, thumbnail_url and preview_pdf_url from the same request are still saved as meta with only esc_url_raw(). Nothing fetches them server-side today, so it is not a request-forgery path, just the same rule half-applied. Adding them to the loop is enough (see inline).

Worth knowing: wp_http_validate_url() returns false when the host does not resolve, so a DNS hiccup on the WordPress host shows up as "Invalid media URL" in the GoDAM tab.

Verification

  • Head tested: 0caa100513. PHPUnit on this PR's head: 663/663 OK.
  • All seven 2.5.1 PRs merged onto main (no conflicts): PHPUnit 915/915, Jest 690/690, build:prod OK.
  • Playwright API checks against that build on a local WordPress 7.1.3 site: 7/7 for this PR.
  • Not covered: Folder ZIP export end to end (needs Action Scheduler run); covered by code reading and unit tests; Offloaded (stream-wrapper) uploads: no such setup locally.

Screenshots and a recording of the local run are available on request.

@KMchaudhary

Copy link
Copy Markdown
Collaborator

Review: approvable ✅

Reviewed at high effort with a focus on the path-traversal surface (delete / ZIP export / store). The fix is solid and thoroughly tested.

  • rtgodam_get_uploads_file_path() resolves with realpath() before confining, so .. and symlinks-pointing-outside are rejected; the trailingslashit($basedir) prefix check closes the /uploads-private lookalike bypass; null-byte / non-string / directory inputs fail closed. The stream-wrapper branch compensates for the absence of realpath() by refusing .. segments.
  • The ZIP path's new resolver still enforces file existence via is_file() (no behavior lost vs. the old file_exists), and the delete caller flattens the nested transcoded-files meta before the new is_string gate, so no legitimate deletions are dropped.
  • Confirmed rtgodam_get_uploads_file_path() is available in cron/Action-Scheduler context (godam.php requires admin/godam-transcoder-functions.php unconditionally, not behind is_admin()).
  • Protected-meta filter and _wp_attached_file basename-only storage are correct and well covered. 50 new tests; full suite green.

Only optional defense-in-depth nits, none blocking:

  1. is_public_media_url() link-local check is IPv4-only (169.254.); IPv6 link-local/ULA/metadata (fe80::, fc00::/7, fd00:ec2::254) aren't covered. Low risk as these URLs are stored, not server-side-fetched.
  2. gethostbyname() validates once at store time — DNS-rebinding/TOCTOU; acceptable for a hotfix.
  3. On DNS-resolution failure gethostbyname() returns the hostname, which passes the link-local check; harmless since wp_http_validate_url() already validated.

LGTM to merge as part of 2.5.1.

@subodhr258
subodhr258 merged commit fe03fef into main Oct 26, 2026
9 of 10 checks passed
@subodhr258
subodhr258 deleted the fix/media-file-paths branch October 26, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants