Repository navigation
Move and delete attachment files only inside the uploads directory #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5d0f4e
fix: move and delete attachment files only inside the uploads directory
vytisbulkevicius c80a201
fix: replace only attachment files inside the uploads directory
vytisbulkevicius dcbbcbd
fix: check the scaled offload path and keep backslashes in folder names
vytisbulkevicius d6849fa
test: cover backslash folder names inside uploads and the replace err…
vytisbulkevicius f23a9e2
test: share the outside-uploads helpers and cover the replace scaled …
vytisbulkevicius ab2198d
fix: keep links to image sizes the rename leaves in place
vytisbulkevicius b7fe113
docs: use a type hint the coding standard accepts
vytisbulkevicius 36e4da7
perf: look up kept size suffixes by key
vytisbulkevicius 0207995
fix: rename size links only for size files the rename moved
vytisbulkevicius 6f70beb
test: cover escaped links of moved sizes and an empty moved list
vytisbulkevicius 4f55b0b
fix: keep links to a size whose file another size moved, and drop the…
vytisbulkevicius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| namespace OptimoleWP\Media; | ||
|
|
||
| /** | ||
| * Checks that attachment file paths stay inside the uploads directory. | ||
| * | ||
| * Paths built from attachment metadata are moved and deleted, so a `../` in that metadata | ||
| * must not reach files elsewhere on the server. | ||
| * | ||
| * @package OptimoleWP\Media | ||
| */ | ||
| class UploadsPath { | ||
| /** | ||
| * Check whether a file path, existing or not, is inside the uploads directory. | ||
| * | ||
| * The folder holding the file must exist; `..` and symlinks in that folder are resolved before the check. | ||
| * A symlink at the file itself is not followed: creating one needs write access to the server files. | ||
| * | ||
| * @param string $path Absolute file path. | ||
| * | ||
| * @return bool True when the file is inside the uploads directory. | ||
| */ | ||
| public static function contains( string $path ): bool { | ||
| $name = basename( $path ); | ||
| if ( '' === $name || '.' === $name || '..' === $name ) { | ||
| return false; | ||
| } | ||
|
pirate-bot marked this conversation as resolved.
Resolved
|
||
|
|
||
| $uploads = realpath( wp_get_upload_dir()['basedir'] ); | ||
| $dir = realpath( dirname( $path ) ); | ||
| if ( false === $uploads || false === $dir ) { | ||
| return false; | ||
| } | ||
|
|
||
| // realpath() returns backslashes on Windows. Elsewhere a backslash is a filename character, so only the | ||
| // separator of this system is normalized. | ||
| $dir = str_replace( DIRECTORY_SEPARATOR, '/', $dir ); | ||
| $uploads = str_replace( DIRECTORY_SEPARATOR, '/', $uploads ); | ||
|
|
||
| return 0 === strpos( trailingslashit( $dir ), trailingslashit( $uploads ) ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.