Repository navigation
Conversation
get_primary_provider_key_selected_for_user() used the stored _two_factor_provider value directly as an array key. A corrupted or imported value that isn't a string (array or object) fatals on PHP 8, and warns on 7.x, once a user has two or more available providers and primary resolution reaches the lookup. Guard it with is_string() so malformed meta resolves to "no selection" and the caller falls back to the first available provider rather than fataling the page. Adds regression coverage for the primary-provider path (WordPress#944) and for the already-normalized enabled-providers path (WordPress#941). Props chubes4. Fixes WordPress#944.
|
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 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. |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Fixes fatal errors caused by malformed provider metadata during primary-provider resolution.
Changes:
- Validates stored primary-provider values before array access.
- Adds regression tests for malformed provider metadata.
| File | Description |
|---|---|
tests/class-two-factor-core.php |
Adds regression coverage for malformed metadata. |
class-two-factor-core.php |
Safely handles malformed primary-provider metadata. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
kasparsd
left a comment
There was a problem hiding this comment.
Forgot to submit the review -- this looks good! Thanks for fixing this.
What?
Guards
get_primary_provider_key_selected_for_user()so a malformed_two_factor_provideruser meta value can no longer fatal while the primary provider is being resolved.Fixes #944
Why?
_two_factor_provideris untrusted persisted state. If it holds a non-string value (an array or object left behind by a legacy import, a corrupted row, or a fuzzed state), primary resolution passed it straight into$available_providers[ $primary_provider ]. On PHP 8 that throwsTypeError: Cannot access offset of type array/stdClass; on 7.x it's an illegal-offset warning. It only surfaces once a user has two or more available providers, since that's the branch that consults the stored primary at all.chubes4 reported this out of the multisite fuzz review of #933, together with #941. #941 (malformed
_two_factor_enabled_providers) is already handled on trunk:get_stored_provider_keys_for_user()normalizes non-array meta to an empty list beforearray_intersect(). This PR closes the remaining primary-provider gap and adds regression coverage for both, so #941 doesn't quietly regress later.How?
get_primary_provider_key_selected_for_user()now checksis_string( $primary_provider )before using it as an array key. A non-string value resolves to "no selection", soget_primary_provider_for_user()falls through to the first available provider. Authentication stays fail-closed: the user still gets a valid second factor and the page doesn't die.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: reproducing both issues against a local WP test harness and drafting the fix and tests. I reviewed the change, ran the full PHPUnit suite (262 tests, all green) and PHPCS locally, and I take responsibility for what's here.
Testing Instructions
update_user_meta( $user_id, '_two_factor_provider', array( 'Two_Factor_Totp' ) );(an object value reproduces it too).TypeErroron PHP 8. After it, the page loads and the user falls back to the first available provider.Automated:
npm test. The new cases live intests/class-two-factor-core.php:test_get_primary_provider_for_user_handles_malformed_primary_metaandtest_get_enabled_providers_for_user_handles_malformed_stored_meta.Changelog Entry