Skip to content

Commit cb1156a

Browse files
swissspidyclaude
andcommitted
Make wp db users a standalone command
`DB_Users_Command` extended `DB_Command`, so every public method of it became a subcommand of `wp db users` too: `wp db users drop` dropped the WordPress database, and so on. It is now a standalone command whose only subcommand is `create`. The statements are passed to the mysql client through STDIN, so the new user's password no longer shows up in the process list or in --debug output. If --password is omitted, it is read from STDIN when piped and prompted for otherwise, and creating a user without a password is an error. NO_BACKSLASH_ESCAPES is turned off for the session so that quoting the password and account name is reliable, and `_` and `%` are escaped in the database name so that GRANT only matches that database. FLUSH PRIVILEGES is not needed after GRANT and is dropped. SQLite is refused with a clear error, and the unrelated changes to the db command registration, the SQLite trait and phpcs.xml.dist are reverted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 7eb293c commit cb1156a

7 files changed

Lines changed: 331 additions & 159 deletions

File tree

‎db-command.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
require_once $wpcli_db_autoloader;
1010
}
1111

12-
WP_CLI::add_command( 'db', DB_Command::class );
13-
WP_CLI::add_command( 'db users', \WP_CLI\Db\DB_Users_Command::class );
12+
WP_CLI::add_command( 'db', 'DB_Command' );
13+
WP_CLI::add_command( 'db users', 'WP_CLI\DB\Users_Command' );

‎features/db-users.feature‎

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,110 @@
1-
@skip-sqlite
21
Feature: Manage database users
32

4-
Scenario: Create database user without privileges
5-
Given an empty directory
6-
And WP files
7-
And wp-config.php
3+
@require-mysql-or-mariadb
4+
Scenario: Create a database user without privileges
5+
Given a WP install
6+
And I run `wp db query "DROP USER IF EXISTS 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
87

9-
When I try `wp db create`
10-
And I run `wp db users create testuser localhost --password=testpass123`
11-
Then STDOUT should contain:
8+
When I run `wp db users create wpcli_dbusers_1 '%' --password=first-Pa55 --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
9+
Then STDOUT should be:
1210
"""
13-
Success: Database user 'testuser'@'localhost' created.
11+
Success: Created database user 'wpcli_dbusers_1'@'%'.
1412
"""
1513

16-
When I run `wp db query "SELECT User, Host FROM mysql.user WHERE User='testuser'"`
17-
Then STDOUT should contain:
14+
When I run `wp db query "SELECT User FROM mysql.user WHERE User = 'wpcli_dbusers_1'" --skip-column-names --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
15+
Then STDOUT should be:
1816
"""
19-
testuser
17+
wpcli_dbusers_1
2018
"""
2119

22-
Scenario: Create database user with privileges
23-
Given an empty directory
24-
And WP files
25-
And wp-config.php
20+
When I try `wp db query "SELECT 1" --dbuser=wpcli_dbusers_1 --dbpass=first-Pa55`
21+
Then the return code should be 1
22+
And STDERR should contain:
23+
"""
24+
denied
25+
"""
2626

27-
When I try `wp db create`
28-
And I run `wp db users create appuser localhost --password=secret123 --grant-privileges`
29-
Then STDOUT should contain:
27+
When I try `wp db users create wpcli_dbusers_1 '%' --password=first-Pa55 --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
28+
Then the return code should be 1
29+
And STDERR should contain:
30+
"""
31+
Error:
32+
"""
33+
34+
When I run `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
35+
36+
@require-mysql-or-mariadb
37+
Scenario: Create a database user with privileges on the WordPress database
38+
Given a WP install
39+
And I run `wp db query "DROP USER IF EXISTS 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
40+
41+
When I run `echo "it's a \"tricky\" pass\\word;" | wp db users create wpcli_dbusers_2 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
42+
Then STDOUT should be:
3043
"""
31-
created with privileges on database
44+
Success: Created database user 'wpcli_dbusers_2'@'%' with all privileges on '{DB_NAME}'.
3245
"""
33-
And STDOUT should contain:
46+
47+
When I run `wp db query "SELECT option_value FROM wp_options WHERE option_name = 'siteurl'" --skip-column-names --dbuser=wpcli_dbusers_2 --dbpass="it's a \"tricky\" pass\\word;"`
48+
Then STDOUT should be:
3449
"""
35-
appuser
50+
https://example.com
3651
"""
3752

38-
Scenario: Create database user with custom host
39-
Given an empty directory
40-
And WP files
41-
And wp-config.php
53+
When I run `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
4254

43-
When I try `wp db create`
44-
And I run `wp db users create remoteuser '%' --password=remote123`
45-
Then STDOUT should contain:
55+
@require-mysql-or-mariadb
56+
Scenario: The new user's password is not leaked into the debug output
57+
Given a WP install
58+
And I run `wp db query "DROP USER IF EXISTS 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
59+
60+
When I try `wp db users create wpcli_dbusers_3 '%' --password=leak-Ch3ck-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} --debug=db`
61+
Then the return code should be 0
62+
And STDERR should contain:
63+
"""
64+
Creating database user 'wpcli_dbusers_3'@'%'.
4665
"""
47-
Success: Database user 'remoteuser'@'%' created.
66+
And STDERR should not contain:
4867
"""
68+
leak-Ch3ck-Value
69+
"""
70+
71+
When I run `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
4972

50-
Scenario: Create database user with no password
51-
Given an empty directory
52-
And WP files
53-
And wp-config.php
73+
@require-mysql-or-mariadb
74+
Scenario: A password is required
75+
Given a WP install
5476

55-
When I try `wp db create`
56-
And I run `wp db users create nopassuser localhost`
77+
When I try `wp db users create wpcli_dbusers_4 '%' --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} < /dev/null`
78+
Then the return code should be 1
79+
And STDERR should contain:
80+
"""
81+
Error: A password is required.
82+
"""
83+
84+
@require-mysql-or-mariadb
85+
Scenario: Only the create subcommand is available
86+
Given a WP install
87+
88+
When I try `wp db users drop --yes`
89+
Then the return code should be 1
90+
And STDERR should contain:
91+
"""
92+
is not a registered subcommand of 'db users'
93+
"""
94+
95+
When I run `wp db check`
5796
Then STDOUT should contain:
5897
"""
59-
Success: Database user 'nopassuser'@'localhost' created.
98+
Success: Database checked.
99+
"""
100+
101+
@require-sqlite
102+
Scenario: Database users are not supported with SQLite
103+
Given a WP install
104+
105+
When I try `wp db users create someone --password=some-Pa55`
106+
Then the return code should be 1
107+
And STDERR should be:
108+
"""
109+
Error: Database users are not supported with SQLite.
60110
"""

‎phpcs.xml.dist‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
5656
<exclude-pattern>*/src/DB_Command\.php$</exclude-pattern>
5757
</rule>
58+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedTraitFound">
59+
<exclude-pattern>*/src/DB_Command_SQLite\.php$</exclude-pattern>
60+
</rule>
5861

5962
<exclude-pattern>/tests/phpstan/scan-files</exclude-pattern>
6063
</ruleset>

‎src/DB_Command.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
use WP_CLI\Process;
55
use WP_CLI\Utils;
66
use cli\table\Column;
7-
use WP_CLI\Db\DB_Command_SQLite;
7+
8+
require_once __DIR__ . '/DB_Command_SQLite.php';
89

910
/**
1011
* Performs basic database operations using credentials stored in wp-config.php.
@@ -2234,7 +2235,7 @@ private static function is_text_col( $type ) {
22342235
*
22352236
* @phpstan-return ($idents is string ? string : array)
22362237
*/
2237-
protected static function esc_sql_ident( $idents ) {
2238+
private static function esc_sql_ident( $idents ) {
22382239
$backtick = static function ( $v ) {
22392240
// Escape any backticks in the identifier by doubling.
22402241
return '`' . str_replace( '`', '``', $v ) . '`';

‎src/DB_Command_SQLite.php‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<?php
22

3-
namespace WP_CLI\Db;
4-
53
use WP_CLI\Formatter;
64
use WP_CLI\Utils;
7-
use WP_CLI;
8-
use Exception;
95

106
/**
117
* SQLite-specific database operations for DB_Command.

‎src/DB_Users_Command.php‎

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)