Problem/Motivation

Currently, Drupal silently trims leading and trailing whitespace from password fields ... which probably saves many more headaches than it causes. But if a user does try, for some reason, to intentionally put a leading or trailing space in their password; the whitespace will be stripped and the password stored in the database without the spaces. However, because we also trim the inputs, the password *with* the spaces still works, so the end user has no idea that this change has taken place.

Example:
User A sets password of "   abc   ".
User A logs into site with "   abc   ", thinking they have 9 characters of entrophy.
Malicious User B comes along and brute-forces the password with "abc".

Steps to reproduce

Create a user with the password " a b c "
Try to login with that user but with the password "a b c", it works but it shouldn't

Note: spaces inside the password is working ( if you create a password " a b c " you can enter with "a b c" but not with "abc" )

Proposed resolution

Not automatically trim the password, we should either explicitly fail or warn that there is leading or trailing whitespace and disallow it, or just allow it.

Remaining tasks

Create Patch
Create Tests
Test it
Commit

User interface changes

None

API changes

Change the way we handle whitespaces in the password.

Data model changes

None

Issue fork drupal-1921576

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

floydm’s picture

I verified the same behavior exists in D8. If you sign up with the password " abc ", you can log back in later with either " abc " or the trimmed string "abc".

floydm’s picture

Issue summary: View changes

Updated issue summary.

charles belov’s picture

If it does no harm for the user to have entered the spaces (that is, it doesn't prevent the user from logging in) is there a reason to expose this system quirk to the user?

As long as the trimmed spaces do not contribute to a greater password complexity rating, the user will see that " abc " is not complex enough and hopefully increase the complexity of their password. (In 7.x, they do contribute, so I've filed issue #2254235 and related it to this issue.)

Alternatively, a less technical explanation would be a warning such as "Leading and trailing spaces do not improve password strength."

aohrvetpv’s picture

As long as the trimmed spaces do not contribute to a greater password complexity rating, the user will see that " abc " is not complex enough and hopefully increase the complexity of their password. (In 7.x, they do contribute, so I've filed issue #2254235 and related it to this issue.)

It can still be misleading and give users a false sense of security. Suppose a user's password strength is already at the maximum according to the password-strength indicator*, but they add some spaces to further increase the strength of their password. Those spaces are not actually stored, against the expectation of the user.

There is another unusual behavior caused by this problem unrelated to password-strength indication: If a user attempts to save a password of all spaces, the password appears to save, yet their password remains unchanged.

* I assume the password-strength indicator has a maximum indication in D8 as it does in D7.

rooby’s picture

There is another unusual behavior caused by this problem unrelated to password-strength indication: If a user attempts to save a password of all spaces, the password appears to save, yet their password remains unchanged.

Yes this is definitely an issue and it has caught me out in the past when I wasted a bunch of time investigating it. It gives a very strange user experience and is also a security issue of sorts.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dpi’s picture

Title: Warn user when trimming leading/trailing whitespace from password fields » Don't trim whitespace when changing user password

Confirmed issue is still valid in 8.3.x

Its probably better we don't trim whitespace: Password managers will record the whitespace. (confirmed with 1Password).

Taking the position of removing spaces means we are adding arbitrary password rules/requirements.

Perhaps spaces make a password harder to brute force because they are unexpected characters!

dpi’s picture

Title: Don't trim whitespace when changing user password » Don't trim whitespace when setting user password
scott_euser’s picture

If a site is upgrading, it already has users which potentially have their passwords trimmed from spaces; however, the users do not necessarily know it is trimmed as their input on login is also trimmed each time. I think we need to maintain that behaviour for existing sites and only apply this to fresh installs.

If I understand correctly, the config/install/* will be installed when a site is first created, but existing sites will have NULL as the password_trim_spaces value and if NULL, we can set it to TRUE to maintain the existing behaviour.

Thoughts?

dpi’s picture

password_trim_spaces

We don't really do backwards compatibility flags in Drupal. I'd say go all in on removing trimming, or defer to next major version (9.x)

already has users which potentially have their passwords trimmed from spaces;

If they put spaces in it originally, then it is likely they will continue to put spaces in passwords both before and after the patch. This means there is no noticable diffference at all.

At worst, a password reset is required if they originally used spaces, but for some reason they know of this bug and stopped using spaces thereafter.

jibran’s picture

Version: 8.2.x-dev » 8.4.x-dev
Status: Active » Needs work

We should maintain the existing behavior. If not then this is a bug report. Anyway, we need functional tests for the new config and we also need upgrade path tests.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mlncn’s picture

Category: Task » Bug report

This is a bug as it violates people's reasonable expectations. If someone has included leading or trailing spaces in their password, they expect them to be used.

floydm’s picture

I'd venture to guess that most of the time when people include a leading or trailing space it is because they copy and pasted a password and didn't notice the space, but I have no data to support that hunch.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hmendes’s picture

Issue summary: View changes
hmendes’s picture

Issue summary: View changes
hmendes’s picture

Issue summary: View changes
quietone’s picture

Status: Needs work » Needs review
Issue tags: +Bug Smash Initiative
StatusFileSize
new6.48 KB
new7.6 KB

@hmendes, thank you for updating the Issue Summary. I find that really helpful.

First, a reroll. I did make one additional change, to add password_trim_spaces to user.schema.yml because \Drupal\Tests\user\Functional\UserCancelTest::testUserCancelWithoutPermission was failing. Testing locally and the patch doesn't work, setting password_trim-spaces to false and updating config, clear cache and the password was still trimmed. But then, this is my first patch touching javascript so the reroll could be wrong anyway.

quietone’s picture

StatusFileSize
new1.88 KB
new9.77 KB

This is working for me, although I did minimal testing. Still needs tests, plus and update test.

cilefen’s picture

If some portion of site users won’t be able to log in, isn’t that a sort of backwards compatibility break? Ironically the behavior change would hurt the same users—those that cleverly lead or follow a pass phrase with spaces—that it tries to help.

To me, this would have to release in a major version, and it needs to be mentioned in release notes.

bnjmnm’s picture

I think the issue summary is correct in saying the current functionality "probably saves many more headaches than it causes". I think there may be a way to preserve these benefits while addressing the concerns expressed here:

  • Add validation that disallows leading/trailing whitespace at either side of a newly submitted password
  • Add logic in the password strength meter to warn users that trailing/leading whitespace is disallowed. This prevents validation-frustration, and is a small addition to logic that has already been implemented.

Something like this could address the concerns without being as invasive. It also doesn't require the multiple instances of conditional logic necessary to apply different behavior depending on when the user was created. From what I can see, the changes in the patch do this nicely, but an unexpected use case would impact an incredibly crucial piece of a sites functionality.

cilefen’s picture

Obviously I did not read the patch over carefully! It’s a new default, but opt-in for existing sites?

Status: Needs review » Needs work

The last submitted patch, 26: 1921576-26.patch, failed testing. View results

paulocs’s picture

I prefer the approach suggested in #28 because people oft add white-spaces wrongly

paulocs’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new3.54 KB

I wrote a patch with the approach proposed in #28.
It still need tests.

paulocs’s picture

StatusFileSize
new3.54 KB
new723 bytes

Ops! I only checked JS lint.

Status: Needs review » Needs work

The last submitted patch, 33: 1921576-33.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jiskra made their first commit to this issue’s fork.

oily made their first commit to this issue’s fork.

oily’s picture

Added test coverage. Test out leading whitespace. If that works, can use same pattern to test trailing whitespace.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.