Fix: Prevents invalid wp-config.php when passwords contain double quotes#203
Fix: Prevents invalid wp-config.php when passwords contain double quotes#203mrsdizzie merged 6 commits intowp-cli:mainfrom
Conversation
|
This looks mostly good thank you for working on it! Another side effect of removing
Then either of these work fine: define( 'DB_PASSWORD', 'my\\password' );define( 'DB_PASSWORD', 'my\password' );In the first case, PHP sees \ which it interprets as an escaped backslash (a single \ at runtime). In the second case, \p is not a recognized escape sequence in single-quoted strings, so both the \ and p are treated as literals, giving the same result. However, if your password has two backslashes in it like:
Then the new code doesn't work, because it will insert: define( 'DB_PASSWORD', 'my\\password' );When it needs to be this to work properly: define( 'DB_PASSWORD', 'my\\\\password' );https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
So to keep the existing behavior while avoiding the issue with double-quotes (which addslashes() unnecessarily escapes), I think the change needs to be: if ( is_string( $value ) ) {
$value = str_replace( '\\', '\\\\', $value ); // Escape backslashes first
$value = str_replace( "'", "\\'", $value ); // Then escape single quotes
return $value;
}With a test for a password with |
This seems working with the following complex passwords as well: |
|
Great! Can you add the suggested test for the backslashes as well (Just so we verify it works and then track it going forward)? Then I think this is good. |
|
^ Just to re-trigger workflows :-) |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
grazie 😊 |
Replace addslashes() with str_replace() to avoid over-escaping double quotes in database passwords. Tested using the following complex passwords:
define( 'DB_PASSWORD', 'abcd\"efgh' );define( 'DB_PASSWORD', 'PasswordWith'SingleQuotes'' );define( 'DB_PASSWORD', 'p@(ss){w0r?d><}"!With"DoubleQuotes' );Issue #180