Repository navigation
Escape the database settings written into wp-tests-config.php - #355
Merged
kittenkamala merged 3 commits intoOct 22, 2026
Merged
Conversation
prepare.php copies WPT_DB_NAME, WPT_DB_USER, WPT_DB_PASSWORD, WPT_DB_HOST and WPT_TABLE_PREFIX into single-quoted PHP strings in wp-tests-config.php without escaping them. A password with a single quote, or one ending with a backslash, made the file a parse error, so the test run failed and cleanup-db.php could not read the settings. A double backslash was silently written as one. The values are now escaped with addcslashes( $value, "\\'" ), as WordPress does when it writes wp-config.php in wp-admin/setup-config.php, and the same goes for WP_PHP_BINARY. The placeholders are replaced with strtr() in a single pass: str_replace() ran each pair over the previous result, so a password containing "localhost" had that part replaced with WPT_DB_HOST. The CI job now uses a password with a quote, a backslash and the word "localhost", and checks that DB_PASSWORD in the generated file equals WPT_DB_PASSWORD. The mysql image puts MYSQL_PASSWORD into SQL unescaped, so a step sets the password with ALTER USER instead.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @jazzsequence. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
chrisdavidmiles
approved these changes
Oct 14, 2026
kittenkamala
approved these changes
Oct 21, 2026
jazzsequence
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #354.
Problem
prepare.phpcopies the database settings into single-quoted PHP strings inwp-tests-config.phpwithout escaping them. A password with a single quote, or one ending with a backslash, makes the file a parse error. A double backslash is silently written as one, and a password containinglocalhostgets that part replaced withWPT_DB_HOST, becausestr_replace()runs each placeholder over the result of the previous one. Details in #354.Fix
WPT_DB_NAME,WPT_DB_USER,WPT_DB_PASSWORD,WPT_DB_HOST,WPT_TABLE_PREFIXandWP_PHP_BINARYwithaddcslashes( $value, "\\'" ), as WordPress does when it writeswp-config.php(setup-config.php).strtr(), which works in a single pass and never rewrites a value it has already written.For values that contain no
', no\and no placeholder name such aslocalhost, the output does not change: the generatedwp-tests-config.phpfor a plain password, with the default table prefix and withhost_, is byte-identical to the onemasterwrites.CI
The CI job now uses the password
wp'te\st-localhostand checks, right afterprepare.php, thatDB_PASSWORDin the generated file equalsWPT_DB_PASSWORD. Run againstmaster'sprepare.php, that check fails with exit 255 (parse error). Themysqlimage putsMYSQL_PASSWORDinto SQL without escaping it, so the job sets the password withALTER USERin its own step.Verification
Config-writing code of
prepare.php,masterand this branch:WPT_DB_PASSWORDmasterplainpassmasterp'q"r$s#t uendswith\two\\backtwo\backmy-localhost-pwmy-127.0.0.1:33348-pwAgainst a real database, MariaDB 12.3 and MySQL 8.0: on
masterthe generated config does not load, andcleanup-db.phpstops with "could not parse the database settings", so the test tables stay. On this branch the config connects, andcleanup-db.phpdrops the tables.Full flow on this branch with
WPT_DB_PASSWORDset top'q"r$s#t u, against MariaDB 12.3 with PHP 8.5:prepare.phpsucceeds, the new CI check passes,test.phpwith--group optionruns 413 tests with no failures,report.phpsucceeds, andcleanup.phpdrops all 12 test tables.php -lpasses on PHP 7.4, 8.0 and 8.2, andphpcsis clean.Notes
WPT_DB_PASSWORDis still trimmed, as before, so spaces at either end of a password are still lost. Changing that could break a.envwith a stray space, so I left it out.prepare.phpwrites the config. Both merge cleanly with this branch and run after the replacement, so they are not affected.Use of AI
AI assistance: Yes
Tool(s): Claude Code and Codex
Used for: Investigation, implementation, verification harness, and PR wording. I reviewed the reasoning and test results, and I take responsibility for the contribution.