According to issue #1921576, Drupal trims leading and trailing whitespace from the password field. However, leading and trailing whitespace in the password field increase password strength rating.
Steps:
1. Go to https://[domain]/user/[user#]/edit
2. In Password field, type abcde
3. Note length of indicator bar in Password strength (approx. 25%) and rating of Weak
4. Place cursor immediately before the first character
5. Type a space.
Actual result: Length of indicator bar increases to about 75% and rating changes to Good
Expected result: Length of indicator bar remains at about 25% and rating remains as Weak.
6. Backspace to remove the first character, or else blank the field and retype abcde
7. Place cursor immediately following the last character.
8. Type a space.
Actual result: Length of indicator bar increases to about 75% and rating changes to Good
Expected result: Length of indicator bar remains at about 25% and rating remains as Weak.
Verified in 7.x, but haven't confirmed in 8.x.
Comments
Comment #1
pushpinderchauhan commentedComment #2
pushpinderchauhan commentedHere is patch added and fixed point: Length of indicator bar remains at about 25% and rating remains as Weak if either leading or trailing spaces is added. These extra spaces won't consider to calculate password strength.
Comment #3
pushpinderchauhan commentedComment #4
charles belovThe thing that bothers me about the current proposed solution code is that the same logic (stripping leading and trailing spaces) is duplicated in two places. I'm thinking that it would be better to have a single password filter function that is called by both the password saver and the strength checker. That way, if the filter is ever changed to allow keeping the beginning and end spaces, or to place other restrictions on the password, the processing will automatically change in both places. Does that make sense?
Comment #5
pushpinderchauhan commentedYes Charles, I will create a single function to manage both cases. I will upload the next patch soon.
Comment #6
pushpinderchauhan commentedI have reviewed my code once again and found only following code can help in password strength checker. Because we have trimmed password value before passing in
Drupal.evaluatePasswordStrength()function by$.trim(passwordInput.val()).After applying this code, I test this by creating new users and also edit existing users and its working fine as required.
Say I created a user with name "test", when user enter leading&trailing spaces in password fields then password strength checker dint comes into place. And at the time of saving in database, these leading&trailing spaces automatically get trimmed through module file. It means during password saver we don't required any function to deal this. So can we go with this single line or I need to write some other logic. Please correct me if I am wrong.
Comment #7
charles belovI'm sorry. I now see the problem. The trimming for password strength is done in the browser via user.js, while the trimming for the database storage is done server-side via user.module. There is no way for them to both call the same function to do the trimming. Having to code the trimming code twice is unavoidable.
Please ignore my comment #4. You're patch #3 looks fine to me then.
Selected functions need to be replicable server- and client-side without having to code them in both server- and client-side code. But that is outside the scope of this issue.
Comment #8
aohrvetpv commentedWhy not permit leading/trailing spaces in the password? Then there need be no special logic in the password-strength indicator, and users get the passwords they expect when they enter them.
Comment #9
aohrvetpv commentedI suppose some users might accidentally put spaces before or after their passwords, or assume that doing so has no effect. So it could also be confusing for some users if spaces are kept.
It seems it can be confusing no matter the password-strength indication (a problem probably more relevant to #1921576 than this issue).
Comment #10
charles belovRe #8: I don't have any concern about leading or trailing spaces in passwords. But given that Drupal does strip them, it needs to not contribute to password strength.
I don't know why Drupal strips leading and trailing spaces - my guess is that it is intended to deal with inadvertently added spaces, e.g., through copy & paste - but having it not do so would be a separate issue from this one.
Comment #11
nod_Instead of changing this in two different places in the code I would just add this line in
Drupal.evaluatePasswordStrengthfunction:And we can use the native trim function instead of using jQuery.
Also for reference if any issue is going to touch a javascript file can you please add the JavaScript tag to it so that the JavaScript team can monitor and review issues? Otherwise those kind of issues get lost in the queue.
Comment #12
mahtoranjeet commentedComment #13
mahtoranjeet commentedComment #14
yogen.prasad commentedWorking fine
Comment #15
aohrvetpv commentedindentation
Comment #16
mahtoranjeet commentedComment #17
nod_There is an eslint error:
Also usually when we change parameters like that we put this sort of thing at the very top of the function, before anything else gets done.
Comment #18
ashutoshsngh commentedComment #20
nod_Could you move the new line at the begining please?
Right before
var indicatorText;around line 112.Comment #21
yogen.prasad commentedComment #22
manjit.singhWorked as expected
Length of indicator bar remains at about 25% and rating remains as Weak.Attaching screencast after applying patch.
Comment #23
manjit.singhComment #24
aohrvetpv commentedDo you know an example of this offhand? It seems a little odd to me to call a function before variable declarations, though I am not an experienced JavaScript developer.
I see the logic of the suggestion--you look at the function parameters and can immediately see that parameter is being trimmed.
In any case, here is an updated patch that moves the line as you suggest. But the patch in #21 seems fine to me too.
Edit: I see now that nod_ is Drupal JavaScript maintainer so I defer to his judgment on the placement.
Comment #26
aohrvetpv commentedUpdate to apply on latest code.
Comment #27
aohrvetpv commentedIs it a problem that
trim()was introduced only in IE 9?Edit: No, IE 9 or greater is required for Drupal 8 per https://www.drupal.org/node/61509.
Comment #28
droplet commentedSimple fix should not take such long time.
Comment #31
aohrvetpv commentedBack to RTBC per #28
Comment #32
alexpottCommitted 4af9619 and pushed to 8.0.x. Thanks!
Comment #34
David_Rothstein commentedFrom the issue summary it looks like this was originally discovered as a Drupal 7 issue.
Comment #35
aohrvetpv commentedDrupal 7 requires IE 8 at minimum, but
trim()was introduced in IE 9. So this patch usesreplace()to accomplish nearly the same.**
trim()actually removes some other obscure whitespace characters. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global..., which gives polyfill code fortrim(). I doubt we care about them, but I have not investigated.Comment #36
droplet commentedD7 requires IE6+
jQuery.trim is your friend.
Comment #37
aohrvetpv commentedThanks. I removed the comment since it is more obvious what the line is doing. Maybe it would be worth commenting why we are using
jQuery.trim, though?Comment #38
aohrvetpv commentedThis "Browser Requirements" page seems to suggest IE 8 is the minimum:
https://www.drupal.org/node/61509
Do you know a source on 6 being the minimum supported IE version for Drupal 7? I don't think it matters for this issue, but it would be helpful to know.
Comment #39
droplet commentedIt's officially dropped in D8:
https://www.drupal.org/node/1569578
Comment #40
David_Rothstein commentedCommitted to 7.x - thanks!