Skip to content

Commit 7e8b548

Browse files
authored
Merge pull request #5987 from wp-cli/fix/check-for-root-earlier
2 parents 0175f13 + fd86708 commit 7e8b548

File tree

5 files changed

+108
-35
lines changed

5 files changed

+108
-35
lines changed

features/bootstrap.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,35 @@ Feature: Bootstrap WP-CLI
506506
Given an empty directory
507507
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=ini_set} cli info`
508508
Then the return code should be 0
509+
510+
Scenario: Test early root detection
511+
512+
Given an empty directory
513+
And a include.php file:
514+
"""
515+
<?php
516+
namespace WP_CLI\Bootstrap;
517+
518+
// To override posix_geteuid in our namespace
519+
function posix_geteuid() {
520+
return 0;
521+
}
522+
?>
523+
"""
524+
525+
And I try `WP_CLI_EARLY_REQUIRE=include.php wp cli version --debug`
526+
527+
Then STDERR should contain:
528+
"""
529+
WP_CLI\Bootstrap\CheckRoot
530+
"""
531+
532+
And STDERR should not contain:
533+
"""
534+
WP_CLI\Bootstrap\IncludeRequestsAutoloader
535+
"""
536+
537+
And STDERR should contain:
538+
"""
539+
YIKES!
540+
"""

php/WP_CLI/Bootstrap/CheckRoot.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace WP_CLI\Bootstrap;
4+
5+
use WP_CLI;
6+
use WP_CLI\Utils;
7+
8+
/**
9+
* Class CheckRoot.
10+
*
11+
* Check if the user is running as root and aborts with a warning if they are.
12+
*
13+
* @package WP_CLI\Bootstrap
14+
*/
15+
class CheckRoot implements BootstrapStep {
16+
17+
/**
18+
* Process this single bootstrapping step.
19+
*
20+
* @param BootstrapState $state Contextual state to pass into the step.
21+
*
22+
* @return BootstrapState Modified state to pass to the next step.
23+
*/
24+
public function process( BootstrapState $state ) {
25+
$config = $state->getValue( 'config', [] );
26+
if ( array_key_exists( 'allow-root', $config ) && true === $config['allow-root'] ) {
27+
// They're aware of the risks and set a flag to allow root.
28+
return $state;
29+
}
30+
31+
if ( getenv( 'WP_CLI_ALLOW_ROOT' ) ) {
32+
// They're aware of the risks and set an environment variable to allow root.
33+
return $state;
34+
}
35+
36+
$args = $state->getValue( 'arguments', [] );
37+
if ( count( $args ) >= 2 && 'cli' === $args[0] && in_array( $args[1], [ 'update', 'info' ], true ) ) {
38+
// Make it easier to update root-owned copies.
39+
return $state;
40+
}
41+
42+
if ( ! function_exists( 'posix_geteuid' ) ) {
43+
// POSIX functions not available.
44+
return $state;
45+
}
46+
47+
if ( posix_geteuid() !== 0 ) {
48+
// Not root.
49+
return $state;
50+
}
51+
52+
WP_CLI::error(
53+
"YIKES! It looks like you're running this as root. You probably meant to " .
54+
"run this as the user that your WordPress installation exists under.\n" .
55+
"\n" .
56+
"If you REALLY mean to run this as root, we won't stop you, but just " .
57+
'bear in mind that any code on this site will then have full control of ' .
58+
"your server, making it quite DANGEROUS.\n" .
59+
"\n" .
60+
"If you'd like to continue as root, please run this again, adding this " .
61+
"flag: --allow-root\n" .
62+
"\n" .
63+
"If you'd like to run it as the user that this site is under, you can " .
64+
"run the following to become the respective user:\n" .
65+
"\n" .
66+
" sudo -u USER -i -- wp <command>\n" .
67+
"\n"
68+
);
69+
}
70+
}

php/WP_CLI/Bootstrap/ConfigureRunner.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function process( BootstrapState $state ) {
2222
$runner = new RunnerInstance();
2323
$runner()->init_config();
2424

25+
$state->setValue( 'config', $runner()->config );
26+
$state->setValue( 'arguments', $runner()->arguments );
27+
$state->setValue( 'assoc_args', $runner()->assoc_args );
28+
2529
return $state;
2630
}
2731
}

php/WP_CLI/Runner.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,39 +1103,6 @@ public function init_config() {
11031103
$this->required_files['runtime'] = $this->config['require'];
11041104
}
11051105

1106-
private function check_root() {
1107-
if ( $this->config['allow-root'] || getenv( 'WP_CLI_ALLOW_ROOT' ) ) {
1108-
return; # they're aware of the risks!
1109-
}
1110-
if ( count( $this->arguments ) >= 2 && 'cli' === $this->arguments[0] && in_array( $this->arguments[1], [ 'update', 'info' ], true ) ) {
1111-
return; # make it easier to update root-owned copies
1112-
}
1113-
if ( ! function_exists( 'posix_geteuid' ) ) {
1114-
return; # posix functions not available
1115-
}
1116-
if ( posix_geteuid() !== 0 ) {
1117-
return; # not root
1118-
}
1119-
1120-
WP_CLI::error(
1121-
"YIKES! It looks like you're running this as root. You probably meant to " .
1122-
"run this as the user that your WordPress installation exists under.\n" .
1123-
"\n" .
1124-
"If you REALLY mean to run this as root, we won't stop you, but just " .
1125-
'bear in mind that any code on this site will then have full control of ' .
1126-
"your server, making it quite DANGEROUS.\n" .
1127-
"\n" .
1128-
"If you'd like to continue as root, please run this again, adding this " .
1129-
"flag: --allow-root\n" .
1130-
"\n" .
1131-
"If you'd like to run it as the user that this site is under, you can " .
1132-
"run the following to become the respective user:\n" .
1133-
"\n" .
1134-
" sudo -u USER -i -- wp <command>\n" .
1135-
"\n"
1136-
);
1137-
}
1138-
11391106
private function run_alias_group( $aliases ) {
11401107
Utils\check_proc_available( 'group alias' );
11411108

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

1186-
$this->check_root();
11871153
if ( $this->alias ) {
11881154
if ( '@all' === $this->alias && ! isset( $this->aliases['@all'] ) ) {
11891155
WP_CLI::error( "Cannot use '@all' when no aliases are registered." );

php/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ function get_bootstrap_steps() {
2222
Bootstrap\DeclareAbstractBaseCommand::class,
2323
Bootstrap\IncludeFrameworkAutoloader::class,
2424
Bootstrap\ConfigureRunner::class,
25-
Bootstrap\IncludeRequestsAutoloader::class,
2625
Bootstrap\InitializeColorization::class,
2726
Bootstrap\InitializeLogger::class,
27+
Bootstrap\CheckRoot::class,
28+
Bootstrap\IncludeRequestsAutoloader::class,
2829
Bootstrap\DefineProtectedCommands::class,
2930
Bootstrap\LoadExecCommand::class,
3031
Bootstrap\LoadRequiredCommand::class,

0 commit comments

Comments
 (0)