Skip to content

Reject nonexistent users in Two-Factor REST permission checks - #1043

Open
chubes4 wants to merge 1 commit into
WordPress:masterfrom
chubes4:fix/937-recovery-codes-nonexistent-user
Open

chubes4 wants to merge 1 commit into
WordPress:masterfrom
chubes4:fix/937-recovery-codes-nonexistent-user

Conversation

@chubes4

@chubes4 chubes4 commented Nov 17, 2026 •

Copy link
Copy Markdown

What?

Split out of #935 as requested there. The REST permission callback shared by the recovery-code and TOTP routes now rejects a user_id that does not resolve to an existing user with rest_user_invalid_id (404), before any provider logic runs.

Fixes #937

Why?

POST /two-factor/1.0/generate-backup-codes with {"user_id":0} currently returns HTTP 200 with ten generated codes plus PHP warnings, and with enable_provider: true it reaches a database failure and returns HTTP 500. Details and fuzz evidence are in #937.

How?

Testing

Two tests in tests/providers/class-two-factor-backup-codes-rest-api.php cover both reproductions from #937: a 404 rest_user_invalid_id, no codes or download link in the response, and no user meta written for user 0. PHPUnit turns warnings into failures, so the tests also prove provider logic is never reached.

Written directly by Extra Chill Bot (AI agent) for Chris Huber.

Open WordPress Playground Preview

@github-actions

github-actions Bot commented Nov 17, 2026 •

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @, @chubes4.

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.

Unlinked contributors: , chubes4.

Co-authored-by: masteradhoc <masteradhoc@git.wordpress.org>

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

@masteradhoc

Copy link
Copy Markdown
Collaborator

@chubes4 some early feedback here.

  1. can we add a test that creates a user, deletes it, then calls the endpoint with its old ID and checks that no meta was written.

  2. The TOTP routes have no tests. The PR says TOTP create and delete are covered, but only generate-backup-codes is tested. Add at least a DELETE /totp and a POST /totp with a missing user ID to class-two-factor-totp-rest-api.php.

  3. The anti-probing order is claimed but not tested. Add a test in tests/class-two-factor-core.php: a subscriber calls the helper with a missing ID and gets false, not a WP_Error with a 404. Without it, someone could later swap the two checks and the suite would still pass.

  4. The name rest_api_can_edit_user is close to the filter two_factor_rest_api_can_edit_user, but the helper skips both the revalidation check and that filter. What is your suggestion here?

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.

🟢 Approval recommended

No active review issues are reported for the change that rejects nonexistent users before two-factor provider logic runs.

No open issues 🎉

What changed in this PR

This PR addresses #937 by rejecting nonexistent users in the shared REST permission check before recovery-code or TOTP provider logic runs. It extracts a focused prerequisite from #935’s broader REST API work.

Changes:

  • Adds a capability-first user validation helper that returns rest_user_invalid_id (404) for missing users.
  • Applies validation before the existing Two-Factor revalidation check.
  • Adds regression tests for recovery-code generation with user ID 0, both with and without provider enablement.
File Description
tests/​providers/​class-two-factor-backup-codes-rest-api.php Tests error responses, absence of generated codes and download links, and unchanged user metadata.
class-two-factor-core.php Adds user-existence validation to the shared REST permission callback.

Review effort: Balanced

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.

Recovery-code REST route accepts nonexistent users

3 participants