Skip to content

Commit f26579b

Browse files
authored
Merge pull request #203 from rollybueno/fix/issue/180
Fix: Prevents invalid wp-config.php when passwords contain double quotes
2 parents 2052b38 + b17a5ad commit f26579b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

features/config-create.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,38 @@ Feature: Create a wp-config file
243243
PasswordWith'SingleQuotes'
244244
"""
245245
246+
Scenario: Passwords with special characters and double quotes
247+
Given an empty directory
248+
And WP files
249+
250+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass='p@(ss){w0r?d><}"!With"DoubleQuotes'`
251+
Then the wp-config.php file should contain:
252+
"""
253+
define( 'DB_PASSWORD', 'p@(ss){w0r?d><}"!With"DoubleQuotes' )
254+
"""
255+
256+
When I run `wp config get DB_PASSWORD`
257+
Then STDOUT should be:
258+
"""
259+
p@(ss){w0r?d><}"!With"DoubleQuotes
260+
"""
261+
262+
Scenario: Passwords with backslash should properly escaped
263+
Given an empty directory
264+
And WP files
265+
266+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass='my\\password'`
267+
Then the wp-config.php file should contain:
268+
"""
269+
define( 'DB_PASSWORD', 'my\\\\password' )
270+
"""
271+
272+
When I run `wp config get DB_PASSWORD`
273+
Then STDOUT should be:
274+
"""
275+
my\\password
276+
"""
277+
246278
@require-mysql @require-mysql-5.7
247279
Scenario: Configure with required SSL connection
248280
Given an empty directory

src/Config_Command.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,9 @@ private function escape_config_value( $key, $value ) {
12401240
}
12411241

12421242
if ( is_string( $value ) ) {
1243-
return addslashes( $value );
1243+
$value = str_replace( '\\', '\\\\', $value ); // Escape backslashes first
1244+
$value = str_replace( "'", "\\'", $value ); // Then escape single quotes
1245+
return $value;
12441246
}
12451247

12461248
return $value;

0 commit comments

Comments
 (0)