-
Notifications
You must be signed in to change notification settings - Fork 18
Hardening: Block path traversal in package install via composer.json name #237
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
base: main
Are you sure you want to change the base?
Changes from all commits
3cfa2c5
ef1653a
0d35231
0655ac3
538cad5
c4f7ef4
ab278f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| use Composer\Installer; | ||
| use Composer\Json\JsonFile; | ||
| use Composer\Package\BasePackage; | ||
| use Composer\Package\Loader\ValidatingArrayLoader; | ||
| use Composer\Package\PackageInterface; | ||
| use Composer\Package\Version\VersionSelector; | ||
| use Composer\Repository; | ||
|
|
@@ -302,6 +303,10 @@ public function install( $args, $assoc_args ) { | |
| // Move to a location based on the package name | ||
| $local_dir = rtrim( WP_CLI::get_runner()->get_packages_dir_path(), '/' ) . '/local/'; | ||
| $actual_dir_package = $local_dir . str_replace( '/', '-', $package_name ); | ||
| // Guard against path traversal: ensure destination stays within local_dir. | ||
| if ( ! self::is_child_path( $actual_dir_package, $local_dir ) ) { | ||
| throw new Exception( 'Invalid package: resolved destination path escapes the packages directory.' ); | ||
| } | ||
| Extractor::copy_overwrite_files( $dir_package, $actual_dir_package ); | ||
| Extractor::rmdir( $dir_package ); | ||
| // Behold, the extracted package | ||
|
|
@@ -1182,13 +1187,57 @@ private static function get_package_name_and_version_from_dir_package( $dir_pack | |
| WP_CLI::error( sprintf( "Invalid package: no name in composer.json file '%s'.", $composer_file ) ); | ||
| } | ||
| $package_name = $composer_data['name']; | ||
| $version = self::DEFAULT_DEV_BRANCH_CONSTRAINTS; | ||
| $naming_error = ValidatingArrayLoader::hasPackageNamingError( $package_name ); | ||
| if ( null !== $naming_error ) { | ||
| WP_CLI::error( sprintf( "Invalid package name '%s': %s", $package_name, $naming_error ) ); | ||
| } | ||
|
swissspidy marked this conversation as resolved.
|
||
| $version = self::DEFAULT_DEV_BRANCH_CONSTRAINTS; | ||
| if ( ! empty( $composer_data['version'] ) ) { | ||
| $version = $composer_data['version']; | ||
| } | ||
| return [ $package_name, $version ]; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a path is inside (or equal to) a given parent directory. | ||
| * | ||
| * Resolves '.' and '..' segments without touching the filesystem so it | ||
| * works even when the paths do not exist yet. Uses a case-insensitive | ||
| * comparison on Windows where the filesystem is case-insensitive. | ||
| * | ||
| * @param string $path Path to test. | ||
| * @param string $parent_dir Parent directory to test against. | ||
| * @return bool True when $path is inside $parent_dir. | ||
| */ | ||
| private static function is_child_path( $path, $parent_dir ) { | ||
| $normalized_path = self::resolve_dot_segments( rtrim( str_replace( '\\', '/', $path ), '/' ) ) . '/'; | ||
| $normalized_parent = self::resolve_dot_segments( rtrim( str_replace( '\\', '/', $parent_dir ), '/' ) ) . '/'; | ||
| if ( DIRECTORY_SEPARATOR === '\\' ) { | ||
| return 0 === stripos( $normalized_path, $normalized_parent ); | ||
| } | ||
| return 0 === strpos( $normalized_path, $normalized_parent ); | ||
| } | ||
|
swissspidy marked this conversation as resolved.
Comment on lines
+1212
to
+1219
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Consider resolving
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in ab278f6. Replaced
|
||
|
|
||
| /** | ||
| * Resolves '.' and '..' segments in a path without touching the filesystem. | ||
| * | ||
| * @param string $path Forward-slash path to resolve. | ||
| * @return string Resolved path. | ||
| */ | ||
| private static function resolve_dot_segments( $path ) { | ||
| $is_absolute = isset( $path[0] ) && '/' === $path[0]; | ||
| $result = []; | ||
| foreach ( explode( '/', $path ) as $part ) { | ||
| if ( '..' === $part ) { | ||
| array_pop( $result ); | ||
| } elseif ( '.' !== $part && '' !== $part ) { | ||
| $result[] = $part; | ||
| } | ||
| } | ||
| $resolved = implode( '/', $result ); | ||
| return $is_absolute ? '/' . $resolved : $resolved; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the WP-CLI packages composer.json object. | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.