Make WordPress Core

Opened 14 months ago

Closed 23 hours ago

#63927 closed enhancement (fixed)

Send email notification when an application password is added

Reported by: johnbillion Owned by: johnbillion
Priority: normal Milestone: 7.2
Component: Application Passwords Version:
Severity: normal Keywords: needs-dev-note add-to-field-guide needs-user-docs has-patch has-unit-tests
Cc: Focuses:

Description

When the account password or email address of a user is changed (either by the user themselves or by an administrator) an email gets sent to the email address of the user informing them of the change. This email helps prevent a user account takeover from going unnoticed.

No such email is sent when an application password is added to a user's account. An application password is almost as privileged as the user's main password as it can be used to perform actions via the REST API, even though it can't be used to log in to wp-admin.

An email should be sent to the user when an application password is added to their account. If the creation of the application password is unexpected then this informs the user about it.

Web services such as GitHub send an email to a user informing them about newly granted access to their account for third party apps. The wording of the email sent by WordPress could be similar, framing it as an app now having access via a newly created application password. One important difference is that the name of the application password is controlled by the user adding the password, unlike services where a third party app is prohibited from using a name that might imply that it's a first party connection. If an attacker adds an application password with the name "WordPress" or the name of the site itself, it would be confusing for the user to receive an email which says "WordPress now has access to your user account".

That's a long way of saying that we need to be careful of how this email is phrased.

Change History (32)

#1 @spacedmonkey
14 months ago

+1 to this idea.

This ticket was mentioned in ​PR #9795 on ​WordPress/wordpress-develop by ​@prasadkarmalkar.


14 months ago
#2

  • Keywords has-patch added; needs-patch removed

This pull request enhances WordPress security by notifying users via email whenever a new application password is added to their account.

  • Sends an email to the user’s registered email address immediately after an application password is created.
  • The email includes the name of the application password and site details, and advises the user to review their account if the creation was unexpected.

Trac ticket: https://core.trac.wordpress.org/ticket/63927

#3 @prasadkarmalkar
14 months ago

Hi team,
I’ve created a PR to send an email notification to the user when an application password is created. Please review and let me know if any changes are needed in the email structure.

​@prasadkarmalkar commented on ​PR #9795:


14 months ago
#4

Hi @johnbillion, Thanks for the suggestions. I have updated the code as per the requested changes.

#5 @johnbillion
3 months ago

  • Milestone Awaiting Review → 7.2
  • Owner set to johnbillion
  • Status new → reviewing

​@johnbillion commented on ​PR #9795:


2 months ago
#6

@prasadkarmalkar I adjusted the text of the email so it's closer to ​the password reset email text. What do you think?

#7 @audrasjb
2 months ago

  • Keywords needs-dev-note add-to-field-guide needs-user-docs added

The implementation proposed in the PR looks great to me.
Adding some docs workflow related keywords.

#8 @johnbillion
2 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 63582:

Application Passwords: Send email notification when an application password is added.

This introduces a filterable email notification so users get alerted when an application password is added to their account.

The email can be filtered via the wp_application_password_created_email filter and disabled via the wp_send_application_password_created_email filter.

Props prasadkarmalkar, johnbillion, audrasjb.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9795

Fixes #63927

#9 follow-up: @beaulebens
2 months ago

Nice addition/improvement. Is there a good reason for this to be filterable though? It seems like a gap in the security aspect -- if I were attacking a site, I'd filter this to false before adding the Application Password.

#10 in reply to: ↑ 9 @johnbillion
2 months ago

Replying to beaulebens:

if I were attacking a site, I'd filter this to false before adding the Application Password.

That presumes that you have code-level access on the site (and it would also apply to the existing password and email address change notifications).

If a plugin on the site wants to register an application password without prior consent, there's no protection that core could put in to realistically protect against it doing so silently. If this email wasn't filterable then the plugin could go down a level and modify the user meta row in the database directly to add an application password.

Similarly to the notification about a change of password or change of email address, this is intended to alert a user about unexpected account access or takeover rather than guard against that has code-level access. Does that make sense?

#11 @beaulebens
2 months ago

Yep, good points 👍

#12 @johnbillion
5 weeks ago

  • Keywords has-patch removed
  • Resolution fixed
  • Status closed → reopened

Reopening to address two bugs:

  1. ###ADMIN_EMAIL### is not replaced in the email
  2. The application password name does not have its slashes stripped in the email

I'm working on a PR for this, the slashes issue might need a wider fix.

This ticket was mentioned in ​PR #14118 on ​WordPress/wordpress-develop by ​@johnbillion.


5 weeks ago
#13

  • Keywords has-patch added

Two fixes to the email sent by wp_application_password_created_notification():

  • ###ADMIN_EMAIL### was never replaced in the message.
  • The application password name was output with slashes. The wp_create_application_password action receives the name slashed, as passed to WP_Application_Passwords::create_new_application_password().

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5.1
Used for: Implementation and this description; reviewed by me.

#14 @audrasjb
5 weeks ago

  • Keywords commit added

#15 @peterwilsoncc
5 weeks ago

  • Keywords commit removed

I've removed the commit keyword as I've left a ​change request on the PR.

As is the PR sends the application name to the wp_application_password_created_email filter in it's slashed form: I've asked that this either be changed or documented.

Last edited 5 weeks ago by peterwilsoncc (previous) (diff)

#16 @johnbillion
5 weeks ago

In 64253:

Users: Fix the admin email placeholder in the Application Password Created email notification.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14118

Props johnbillion, peterwilsoncc, audrasjb.

See #63927

​@johnbillion commented on ​PR #14118:


5 weeks ago
#17

I've committed the ###ADMIN_EMAIL### fix. Going to look more into the slash issue.

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


3 weeks ago

This ticket was mentioned in ​PR #14721 on ​WordPress/wordpress-develop by ​@ravipatel.


3 weeks ago
#19

  • Keywords has-unit-tests added

Summary

wp_application_password_created_notification() was added in 7.2 ([63582]) without tests. The ###ADMIN_EMAIL### placeholder was then found to be left unreplaced and fixed in [64253], also without a test. This PR adds tests for the function. No source changes.

The new tests cover:

  • an email is sent to the user when an application password is created through WP_Application_Passwords::create_new_application_password()
  • every placeholder is replaced (username, application password name, admin email, user email, site name, site URL), and the subject includes the site name
  • wp_send_application_password_created_email returning false prevents the email, and the filter receives the user and the application password details
  • wp_application_password_created_email can change the recipient, subject and message, and placeholders in a filtered message are still replaced
  • no email is sent for a user that doesn't exist, an unnamed application password, or a user with an invalid email address

I confirmed that the placeholder test fails if the [64253] fix is reverted.

The slashed application password name discussed in #14118 is intentionally not covered, since @johnbillion mentioned it will get a separate ticket.

Testing

  • phpunit --filter Tests_Functions_wpApplicationPasswordCreatedNotification: OK (9 tests, 23 assertions)
  • The same with -c tests/phpunit/multisite.xml: OK (9 tests, 23 assertions)
  • phpcs on the new file: no issues

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the untested function, drafting the tests and this description. I reviewed every test, ran them on single site and multisite, and confirmed they catch the earlier bug.

#20 @ravipatel
3 weeks ago

Follow-up PR adding tests for wp_application_password_created_notification(): ​https://github.com/WordPress/wordpress-develop/pull/14721. It covers sending, placeholder replacement (the test fails if [64253] is reverted), both filters, and the no-email cases. The slashed-name issue from #14118 is left out, as that is getting its own ticket.

Last edited 3 weeks ago by ravipatel (previous) (diff)

#21 @ravipatel
3 weeks ago

.

Last edited 3 weeks ago by ravipatel (previous) (diff)

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


2 weeks ago

#23 @lancewillett
11 days ago

In 65091:

Tests: Cover application password creation emails.

Cover notification delivery, placeholder replacement, filtering, and invalid inputs in wp_application_password_created_notification(). Protect the admin-email placeholder fix.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14721

Props ravipatel.
See #63927.

#25 @peterwilsoncc
6 days ago

  • Keywords has-patch has-unit-tests removed

Keyword management.

On ​PR#14118, I added a note that the new hooks receive slashed data which is currently pending investigation.

For what it's worth, wp_create_application_password is also receiving slashed data so there is an argument that consistency with the other application password hooks is the best approach. If that's the case then any further commits on the ticket can be a docs change.

Either way, an intentional decision for the new hooks is desirable.

#26 @jorbin
5 days ago

I think consistency and clear documentation that this data is unslashed is better than different data being passed to similiar hooks but I don't feel super strong on this.

This ticket was mentioned in ​PR #15608 on ​WordPress/wordpress-develop by ​@peterwilsoncc.


5 days ago
#27

  • Keywords has-patch has-unit-tests added

Handles slashed and unslashed data in application password email and hooks:

  • Improves documentation of all hooks to explain what data is slashed and unslahed
  • Ensures that the email contains the application password in it's unslashed form.

Initial push contains just the test for the latter.

Use of AI Tools

#28 @peterwilsoncc
5 days ago

I've created ​PR #15608:

  • Follows Jorbin's suggestion of consistency across the application password hooks
  • Documents which data is slashed vs unslashed in the new and existing hooks
  • Fixes the site name placeholder replacement to unslash the application name

​@peterwilsoncc commented on ​PR #15608:


5 days ago
#29

@probsbot add @aaronjorbin

​@johnbillion commented on ​PR #15608:


3 days ago
#30

Actually I'm going to propose some further adjustments to this.

#31 @ssebuwufumoses
39 hours ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/15608 (commit c909211)

Environment

  • WordPress: 7.2-beta4-65231-src (trunk at 83c101d)
  • PHP: 8.3.30
  • Server: Apache/2.4.66 (Laragon)
  • Database: MySQL 8.4.3
  • OS: Windows 11
  • Theme: Ipsum
  • MU-Plugins: one to allow application passwords on the local http site (wp_is_application_passwords_available), and one to send mail to Mailpit
  • Plugins: None activated

Steps to Reproduce

  1. Create an application password whose name has an apostrophe, for example Moses's Before App, from the profile screen (REST API).
  2. Check the "Application Password Created" email in Mailpit.
  3. Apply the patch and create another one, Moses's After App.

Actual Results

Email "Application password name:" line
Before the patch Moses\'s Before App ❌
After the patch Moses's After App ✅

The stored names on the profile screen are correct before and after, so only the email was affected.

Unit tests with the patch applied:

  • --filter Tests_Functions_wpApplicationPasswordCreatedNotification: OK (10 tests, 25 assertions)

With only the new test applied (the functions.php change removed), test_email_contains_unslashed_application_password_name fails as expected, because the email doesn't contain Bob's "Tool" \ Backslash.

Additional Notes

  • The patch still applies cleanly to current trunk.
  • Unslashing only at the point where the name goes into the email keeps the data passed to the existing hooks unchanged, and the new docs make clear which values are slashed. The new test also covers a real backslash in the name, which is kept.

AI assistance: I used Claude to help review the code and draft this report. I reproduced the bug and ran the tests myself.

#32 @johnbillion
23 hours ago

  • Resolution → fixed
  • Status reopened → closed

In 65361:

Application Passwords: Pass unslashed data to hooks.

The name of an application password appeared with backslashes in the "Application Password Created" email, so a name such as Bob's App showed as Bob\'s App. This happened because WP_Application_Passwords::create_new_application_password() and ::update_application_password() expect slashed data and have passed it to their actions unchanged since 5.6.0. Both methods now unslash their input.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15608

Follow-up to [63582], [64253], [65091].

Props peterwilsoncc, jorbin, audrasjb, ssebuwufumoses.
Fixes #63927.

Note: See TracTickets for help on using tickets.