Make WordPress Core

Opened 7 years ago

Last modified 7 days ago

#49639 new enhancement

Add a filter on wp_insert_user function regarding $user_pass

Reported by: stokim Owned by:
Priority: normal Milestone: Awaiting Review
Component: Users Version:
Severity: normal Keywords: good-first-bug has-patch 2nd-opinion has-unit-tests
Cc: Focuses:

Description (last modified by SergeyBiryukov)

/5.3/src/wp-includes/user.php
function wp_insert_user ( $userdata ) 1542 line
Please apply the below filter so that I can add a rule on user typed password before hashing the password.

$pre_user_password = apply_filters(  'pre_user_password', $user_pass );

Thank you.

Best regards,
Jen

Attachments (2)

49639.patch​ (420 bytes ) - added by tomdevisser 5 years ago.
Added the filter
49639-2.diff​ (2.2 KB ) - added by ilovecats7 5 years ago.

Download all attachments as: .zip

Change History (14)

#1 @SergeyBiryukov
7 years ago

  • Component Formatting → Users

#2 @johnbillion
5 years ago

  • Keywords needs-patch good-first-bug added
  • Version 5.4

@tomdevisser
5 years ago

Added the filter

#3 @tomdevisser
5 years ago

  • Focuses privacy added
  • Keywords has-patch dev-feedback 2nd-opinion added; needs-patch good-first-bug removed

I added the filter and changed the name of the variable to $pre_hash_password as I thought that would be a more descriptive name for a hook.

I would like to know if this goes against any security protocols, as you're giving site and plugin developers access to a non-hashed password of users without permission.

Last edited 5 years ago by tomdevisser (previous) (diff)

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


5 years ago

#5 @jorbin
5 years ago

  • Focuses privacy removed
  • Keywords needs-patch added; has-patch removed

Thanks for the first pass @tomjdevisser. A few notes

All filters need a doc block ​https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#4-hooks-actions-and-filters

We also need to make sure that the results of the filter are being used. In this pass, $pre_hash_password is set but then goes nowhere. I think it might also be good to check after that it isn't a falsy value and return a wp_error if that is the case.

I also think this needs to take into account updating users and not just inserting them.

As for the privacy concerns, plugins already have access to this from the global $_POST.

#6 @SergeyBiryukov
5 years ago

  • Description modified (diff)

#7 @johnbillion
5 years ago

  • Keywords good-first-bug added; dev-feedback 2nd-opinion removed

@ilovecats7
5 years ago

#8 @ilovecats7
5 years ago

  • Keywords has-patch 2nd-opinion added; needs-patch removed

Hello,

I submitted my first patch. Not sure if I'm supposed to modify the ticket?

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


2 years ago
#9

  • Keywords has-unit-tests added

This PR adds filter on user typed password before hashing the password.
It also includes error check for empty password and a backslash. Unit test included for both.

Trac ticket: 49639

​@TimothyBlynJacobs commented on ​PR #7384:


2 years ago
#10

Thanks for the pr @mklute101! We need to fix the PHPCS issues. You should be able to run something like this:

./vendor/bin/phpcbf src/wp-includes/user.php

This ticket was mentioned in ​PR #9093 on ​WordPress/wordpress-develop by ​NabhishekSingh.


16 months ago
#11

The PR add a new pre_hash_password filter in wp_insert_user() and wp_update_user() functions, allowing anyone to modify or validate passwords before hashing.

#12 @logicrays
7 days ago

Hello Team,
Thanks for working on this - a pre-hash password filter is a useful enhancement for password-policy / generation plugins.

A few blockers before this can move forward:

  1. Needs a rebase against the current trunk. @since 6.7.0 is outdated.
  2. Empty-password handling conflicts with [63770] / r60650: trunk intentionally allows creating a user with an empty user_pass (warning + password-reset flow). Returning WP_Error here would break that BC. Please drop/realign the hard empty-password error.
  3. The leftover backslash validation in wp_update_user() should be removed for consistency with insert (as previously noted).
  4. Please avoid empty( $pre_hash_password ) - it rejects the valid password "0". Prefer a strict '' === (string) ... check if validation stays.
  5. Please add a unit test that asserts the filter actually mutates the password before hashing (current tests only cover the empty case).
  6. Naming: ticket proposed pre_user_password; this PR uses pre_hash_password. Worth aligning with the existing pre_user_*. convention and documenting the final name.
  7. Docblock: @param string $userdata['user_pass'] is invalid; also consider passing $userdata (or at least insert vs. update context) as an additional argument.

Happy to re-review after a refreshed patch.

Note: See TracTickets for help on using tickets.