Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,11 @@ private function create_default_composer_json( $composer_path ) {

$composer_dir = pathinfo( $composer_path, PATHINFO_DIRNAME );
if ( ! is_dir( $composer_dir ) ) {
if ( ! @mkdir( $composer_dir, 0700, true ) ) { // @codingStandardsIgnoreLine
// 0755 rather than 0700: the risk being guarded against is another local user
// *writing* into the packages directory, whose `vendor/autoload.php` is required on
// every WP-CLI run. Read access was never the problem, and revoking it breaks setups
// that point `WP_CLI_PACKAGES_DIR` at a location shared between users.
if ( ! @mkdir( $composer_dir, 0755, true ) ) { // @codingStandardsIgnoreLine
Comment on lines +1286 to +1290

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🌐 Web query:

Composer 2.10.2 src Composer Autoload AutoloadGenerator generate filePutContentsIfModified vendor/autoload.php chmod

💡 Result:

In Composer 2.10.2, the AutoloadGenerator class is responsible for generating autoloader files within the vendor directory [1][2][3]. The process involves generating several files (e.g., autoload.php, autoload_static.php, autoload_real.php) using the filePutContentsIfModified method from the Filesystem utility class [1][3][4][5]. The filePutContentsIfModified method is implemented in src/Composer/Util/Filesystem.php as follows: public function filePutContentsIfModified(string $path, string $content) { $currentContent = Silencer::call('file_get_contents', $path); if (false === $currentContent || $currentContent!== $content) { return file_put_contents($path, $content); } return 0; } This method only performs a file write operation if the target file does not exist or if the content has changed, which helps avoid unnecessary disk I/O and maintains original file timestamps when no modification is required [4]. It does not explicitly call chmod to modify file permissions during the generation of these files; standard file permissions are determined by the PHP file_put_contents function and the system's default umask settings [4].

Citations:


🌐 Web query:

Composer 2.10.2 Package_Command require composer version autoload vendor autoload.php include chmod

💡 Result:

Composer 2.10.2 is a version of the Composer dependency manager released on July 1, 2026 [1][2]. The require command is used to add new packages to your composer.json file and install them into your project [3]. When you run composer require <package-name>, Composer updates your dependencies and regenerates the autoloading files, specifically the vendor/autoload.php entry point [4][5][6]. Regarding vendor/autoload.php and chmod permissions: 1. Purpose of vendor/autoload.php: This file is the central entry point for PHP's autoloading mechanism in Composer-managed projects [4][6]. By including it at the start of your application (e.g., require DIR . '/vendor/autoload.php';), PHP will automatically load any classes from your installed packages when needed, eliminating the need for manual require or include statements for individual class files [4][7][5][6]. 2. Permission Issues and chmod: If you encounter "Permission denied" or "Failed opening required" errors when attempting to include this file, it is typically due to incorrect file system permissions or ownership [8][9][10]. - Ownership: The vendor directory and its contents should be owned by the user or service account that executes your PHP scripts (such as www-data, apache, or your own user) [8][9][10]. You can fix ownership issues using chown (e.g., sudo chown -R <user>:<group> vendor) [10]. - Permissions: Ensure the directories and files are readable by the PHP process [8]. While chmod can be used to set permissions (e.g., chmod 755 -R vendor), it is generally better practice to manage ownership correctly rather than granting broad, insecure permissions like o+w (world-writable) [11][10]. If the vendor/autoload.php file is missing, ensure you have successfully run composer install or composer update in the project root [4][6]. If you change your project's autoload configuration in composer.json, you must run composer dump-autoload to regenerate the autoloader files [4][6].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== package composer constraints =="
git ls-files | rg '(^|/)composer\.(json|lock)$' || true
for f in $(git ls-files 'composer.*'); do
  echo "--- $f"
  sed -n '1,220p' "$f" | rg -n '"name"|"require"|"php"|"composer-plugin-api"|"composer-runtime-api"|WP_CLI' || true
done

echo "== autoload.php require paths =="
rg -n "vendor/autoload\.php|autoload\.php" src tests --glob '*.php' || true

echo "== package install/update autoloader generation hints =="
rg -n "dump-autoload|autoload|vendor/autoload|createDefaultComposer|create_default_composer|install\(|update\(" src tests/phpunit/ComposerJsonTest.php tests --glob '*.php' | head -n 200 || true

Repository: wp-cli/package-command

Length of output: 2955


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== composer.json =="
cat -n composer.json

echo "== Package_Command relevant sections =="
sed -n '1,90p' src/Package_Command.php
sed -n '220,265p' src/Package_Command.php
sed -n '680,720p' src/Package_Command.php
sed -n '800,850p' src/Package_Command.php
sed -n '1240,1340p' src/Package_Command.php
sed -n '1340,1390p' src/Package_Command.php

Repository: wp-cli/package-command

Length of output: 16208


Security Misconfiguration (CWE-732): Incorrect Permission Assignment for Critical Resource

Reachability: Internal

Protect generated autoload files, not only the packages directory.

0755 clears group/other write from the directory, but Composer 2.10.2 still writes generated PHP files through file_put_contents() without clearing file write bits. On a shared packages directory with a permissive umask, a local peer with file access can overwrite vendor/autoload.php even though they cannot create files there. Enforce that Composer-created PHP files under WP-CLI packages are not group- or world-writable, or keep the Composer root private until Composer applies stricter default permissions.

🤖 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 `@src/Package_Command.php` around lines 1286 - 1290, Update the package setup
flow around mkdir in Package_Command to protect Composer-generated PHP files,
including vendor/autoload.php, from group or world write access. Ensure the
Composer root remains private or enforce restrictive permissions on generated
PHP files after Composer runs, while preserving support for shared
WP_CLI_PACKAGES_DIR locations.

Source: MCP tools

$error = error_get_last();
WP_CLI::error( sprintf( "Composer directory '%s' for packages couldn't be created: %s", $composer_dir, $error['message'] ) );
}
Expand Down
10 changes: 9 additions & 1 deletion tests/phpunit/ComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ public function test_create_default_composer_json() {
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
$this->assertTrue( false !== strpos( file_get_contents( $actual ), 'wp-cli/wp-cli' ) );
if ( ! Utils\is_windows() ) {
$this->assertSame( '0700', substr( sprintf( '%o', fileperms( dirname( $actual ) ) ), -4 ) );
// The invariant that matters is that no other local user can write into the packages
// directory, whose `vendor/autoload.php` is required on every WP-CLI run. The exact
// mode depends on the umask, so assert the write bits rather than the literal mode.
$perms = fileperms( dirname( $actual ) ) & 0777;
$this->assertSame(
0,
$perms & 0022,
sprintf( 'Packages directory must not be group- or world-writable, got %o.', $perms )
);
Comment on lines +94 to +99
}
unlink( $actual );
rmdir( dirname( $actual ) );
Expand Down
Loading