Skip to content

Commit 8083d35

Browse files
committed
Check for root earlier
1 parent f701f40 commit 8083d35

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

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
@@ -1070,39 +1070,6 @@ public function init_config() {
10701070
$this->required_files['runtime'] = $this->config['require'];
10711071
}
10721072

1073-
private function check_root() {
1074-
if ( $this->config['allow-root'] || getenv( 'WP_CLI_ALLOW_ROOT' ) ) {
1075-
return; # they're aware of the risks!
1076-
}
1077-
if ( count( $this->arguments ) >= 2 && 'cli' === $this->arguments[0] && in_array( $this->arguments[1], [ 'update', 'info' ], true ) ) {
1078-
return; # make it easier to update root-owned copies
1079-
}
1080-
if ( ! function_exists( 'posix_geteuid' ) ) {
1081-
return; # posix functions not available
1082-
}
1083-
if ( posix_geteuid() !== 0 ) {
1084-
return; # not root
1085-
}
1086-
1087-
WP_CLI::error(
1088-
"YIKES! It looks like you're running this as root. You probably meant to " .
1089-
"run this as the user that your WordPress installation exists under.\n" .
1090-
"\n" .
1091-
"If you REALLY mean to run this as root, we won't stop you, but just " .
1092-
'bear in mind that any code on this site will then have full control of ' .
1093-
"your server, making it quite DANGEROUS.\n" .
1094-
"\n" .
1095-
"If you'd like to continue as root, please run this again, adding this " .
1096-
"flag: --allow-root\n" .
1097-
"\n" .
1098-
"If you'd like to run it as the user that this site is under, you can " .
1099-
"run the following to become the respective user:\n" .
1100-
"\n" .
1101-
" sudo -u USER -i -- wp <command>\n" .
1102-
"\n"
1103-
);
1104-
}
1105-
11061073
private function run_alias_group( $aliases ) {
11071074
Utils\check_proc_available( 'group alias' );
11081075

@@ -1150,7 +1117,6 @@ public function start() {
11501117
WP_CLI::debug( $this->project_config_path_debug, 'bootstrap' );
11511118
WP_CLI::debug( 'argv: ' . implode( ' ', $GLOBALS['argv'] ), 'bootstrap' );
11521119

1153-
$this->check_root();
11541120
if ( $this->alias ) {
11551121
if ( '@all' === $this->alias && ! isset( $this->aliases['@all'] ) ) {
11561122
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)