Skip to content

Fail closed on malformed primary-provider meta (#944) - #996

Merged
kasparsd merged 1 commit into
WordPress:masterfrom
gunjanjaswal:fix/944-normalize-primary-provider-meta
Sep 26, 2026
Merged

kasparsd merged 1 commit into
WordPress:masterfrom
gunjanjaswal:fix/944-normalize-primary-provider-meta

Conversation

@gunjanjaswal

@gunjanjaswal gunjanjaswal commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

What?

Guards get_primary_provider_key_selected_for_user() so a malformed _two_factor_provider user meta value can no longer fatal while the primary provider is being resolved.

Fixes #944

Why?

_two_factor_provider is 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 throws TypeError: 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 before array_intersect(). This PR closes the remaining primary-provider gap and adds regression coverage for both, so #941 doesn't quietly regress later.

How?

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

  1. On a site with the plugin active, give a user two available providers (for example configure TOTP and leave Email enabled).
  2. Corrupt the primary selection directly in meta: update_user_meta( $user_id, '_two_factor_provider', array( 'Two_Factor_Totp' ) ); (an object value reproduces it too).
  3. Before this change, anything that resolves the primary provider (the Users list table, the profile screen, the login flow) fatals with a TypeError on PHP 8. After it, the page loads and the user falls back to the first available provider.

Automated: npm test. The new cases live in tests/class-two-factor-core.php: test_get_primary_provider_for_user_handles_malformed_primary_meta and test_get_enabled_providers_for_user_handles_malformed_stored_meta.

Changelog Entry

Fixed - Prevent a fatal error when a user's stored primary-provider meta is malformed (a non-string value); primary resolution now falls back to an available provider.

Open WordPress Playground Preview

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.
@github-actions

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: gunjanjaswal <[email protected]>
Co-authored-by: chubes4 <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
kasparsd merged commit 214751e into WordPress:master Sep 26, 2026
25 checks passed

@kasparsd kasparsd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to submit the review -- this looks good! Thanks for fixing this.

@masteradhoc masteradhoc added this to the 0.18.0 milestone Sep 28, 2026
@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks @kasparsd for the review and merge. Glad the fail-closed fix is in, malformed _two_factor_provider meta won't fatal primary resolution on PHP 8 anymore. And thanks to @chubes4 for the thorough fuzz report that surfaced both this and #941.

@gunjanjaswal
gunjanjaswal deleted the fix/944-normalize-primary-provider-meta branch September 28, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Normalize malformed primary-provider meta before array-key access

4 participants