Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions features/bootstrap.feature
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,35 @@ Feature: Bootstrap WP-CLI
Given an empty directory
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=ini_set} cli info`
Then the return code should be 0

Scenario: Test early root detection

Given an empty directory
And a include.php file:
"""
<?php
namespace WP_CLI\Bootstrap;

// To override posix_geteuid in our namespace
function posix_geteuid() {
return 0;
}
?>
"""

And I try `WP_CLI_EARLY_REQUIRE=include.php wp cli version --debug`

Then STDERR should contain:
"""
WP_CLI\Bootstrap\CheckRoot
"""

And STDERR should not contain:
"""
WP_CLI\Bootstrap\IncludeRequestsAutoloader
"""

And STDERR should contain:
"""
YIKES!
"""
70 changes: 70 additions & 0 deletions php/WP_CLI/Bootstrap/CheckRoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace WP_CLI\Bootstrap;

use WP_CLI;
use WP_CLI\Utils;

/**
* Class CheckRoot.
*
* Check if the user is running as root and aborts with a warning if they are.
*
* @package WP_CLI\Bootstrap
*/
class CheckRoot implements BootstrapStep {

/**
* Process this single bootstrapping step.
*
* @param BootstrapState $state Contextual state to pass into the step.
*
* @return BootstrapState Modified state to pass to the next step.
*/
public function process( BootstrapState $state ) {
$config = $state->getValue( 'config', [] );
if ( array_key_exists( 'allow-root', $config ) && true === $config['allow-root'] ) {

Check warning on line 26 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L24-L26

Added lines #L24 - L26 were not covered by tests
// They're aware of the risks and set a flag to allow root.
return $state;

Check warning on line 28 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L28

Added line #L28 was not covered by tests
}

if ( getenv( 'WP_CLI_ALLOW_ROOT' ) ) {

Check warning on line 31 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L31

Added line #L31 was not covered by tests
// They're aware of the risks and set an environment variable to allow root.
return $state;

Check warning on line 33 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L33

Added line #L33 was not covered by tests
}

$args = $state->getValue( 'arguments', [] );
if ( count( $args ) >= 2 && 'cli' === $args[0] && in_array( $args[1], [ 'update', 'info' ], true ) ) {

Check warning on line 37 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L36-L37

Added lines #L36 - L37 were not covered by tests
// Make it easier to update root-owned copies.
return $state;

Check warning on line 39 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L39

Added line #L39 was not covered by tests
}

if ( ! function_exists( 'posix_geteuid' ) ) {

Check warning on line 42 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L42

Added line #L42 was not covered by tests
// POSIX functions not available.
return $state;

Check warning on line 44 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L44

Added line #L44 was not covered by tests
}

if ( posix_geteuid() !== 0 ) {

Check warning on line 47 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L47

Added line #L47 was not covered by tests
// Not root.
return $state;

Check warning on line 49 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L49

Added line #L49 was not covered by tests
}

WP_CLI::error(
"YIKES! It looks like you're running this as root. You probably meant to " .
"run this as the user that your WordPress installation exists under.\n" .
"\n" .
"If you REALLY mean to run this as root, we won't stop you, but just " .
'bear in mind that any code on this site will then have full control of ' .
"your server, making it quite DANGEROUS.\n" .
"\n" .
"If you'd like to continue as root, please run this again, adding this " .
"flag: --allow-root\n" .
"\n" .
"If you'd like to run it as the user that this site is under, you can " .
"run the following to become the respective user:\n" .
"\n" .
" sudo -u USER -i -- wp <command>\n" .
"\n"
);

Check warning on line 68 in php/WP_CLI/Bootstrap/CheckRoot.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/CheckRoot.php#L52-L68

Added lines #L52 - L68 were not covered by tests
}
}
4 changes: 4 additions & 0 deletions php/WP_CLI/Bootstrap/ConfigureRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
$runner = new RunnerInstance();
$runner()->init_config();

$state->setValue( 'config', $runner()->config );
$state->setValue( 'arguments', $runner()->arguments );
$state->setValue( 'assoc_args', $runner()->assoc_args );

Check warning on line 27 in php/WP_CLI/Bootstrap/ConfigureRunner.php

View check run for this annotation

Codecov / codecov/patch

php/WP_CLI/Bootstrap/ConfigureRunner.php#L25-L27

Added lines #L25 - L27 were not covered by tests

return $state;
}
}
34 changes: 0 additions & 34 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,39 +1103,6 @@ public function init_config() {
$this->required_files['runtime'] = $this->config['require'];
}

private function check_root() {
if ( $this->config['allow-root'] || getenv( 'WP_CLI_ALLOW_ROOT' ) ) {
return; # they're aware of the risks!
}
if ( count( $this->arguments ) >= 2 && 'cli' === $this->arguments[0] && in_array( $this->arguments[1], [ 'update', 'info' ], true ) ) {
return; # make it easier to update root-owned copies
}
if ( ! function_exists( 'posix_geteuid' ) ) {
return; # posix functions not available
}
if ( posix_geteuid() !== 0 ) {
return; # not root
}

WP_CLI::error(
"YIKES! It looks like you're running this as root. You probably meant to " .
"run this as the user that your WordPress installation exists under.\n" .
"\n" .
"If you REALLY mean to run this as root, we won't stop you, but just " .
'bear in mind that any code on this site will then have full control of ' .
"your server, making it quite DANGEROUS.\n" .
"\n" .
"If you'd like to continue as root, please run this again, adding this " .
"flag: --allow-root\n" .
"\n" .
"If you'd like to run it as the user that this site is under, you can " .
"run the following to become the respective user:\n" .
"\n" .
" sudo -u USER -i -- wp <command>\n" .
"\n"
);
}

private function run_alias_group( $aliases ) {
Utils\check_proc_available( 'group alias' );

Expand Down Expand Up @@ -1183,7 +1150,6 @@ public function start() {
WP_CLI::debug( $this->project_config_path_debug, 'bootstrap' );
WP_CLI::debug( 'argv: ' . implode( ' ', $GLOBALS['argv'] ), 'bootstrap' );

$this->check_root();
if ( $this->alias ) {
if ( '@all' === $this->alias && ! isset( $this->aliases['@all'] ) ) {
WP_CLI::error( "Cannot use '@all' when no aliases are registered." );
Expand Down
3 changes: 2 additions & 1 deletion php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
Bootstrap\DeclareAbstractBaseCommand::class,
Bootstrap\IncludeFrameworkAutoloader::class,
Bootstrap\ConfigureRunner::class,
Bootstrap\IncludeRequestsAutoloader::class,
Bootstrap\InitializeColorization::class,
Bootstrap\InitializeLogger::class,
Bootstrap\CheckRoot::class,
Bootstrap\IncludeRequestsAutoloader::class,

Check warning on line 28 in php/bootstrap.php

View check run for this annotation

Codecov / codecov/patch

php/bootstrap.php#L27-L28

Added lines #L27 - L28 were not covered by tests
Bootstrap\DefineProtectedCommands::class,
Bootstrap\LoadExecCommand::class,
Bootstrap\LoadRequiredCommand::class,
Expand Down