Detect Caps Lock with JavaScript
davidwalsh.name
davidwalsh.name
Despite not using the site anymore, I appreciate how facebook handles this. If your password is incorrect, it checks again with the case swapped to account for caps lock being on.
I love this idea, going to add this to future projects.
It means that the passwords have far less entropy.
It means the uppercase you password when generating a hash. Good lord.
And for weak passwords... Well, it's not like that one extra bit would have made a difference.
Also, can someone please tell me that Facebook doesn't really do this?
It exchanges a few bits of entropy for a much smoother user experience, and it doesn't actually aid an attacker much (they could just try the permutations themselves)
Obligatory XKCD: https://xkcd.com/936/
Don't forget that the shift key makes things lowercase when Caps Lock is turned on.
For an offline attack, where you have to attack Facebook's bespoke "password onion"[1], you would still have to compute both hashes. There is approximately zero impact on cracking strategy.
There may be an argument to be made that Facebook would have otherwise used a larger work factor had they not had to do double the work for wrong passwords, but the common case is the password being correct, so I doubt this was a significant consideration in the parameter selection.
Even for the online case, I really don't think it affects security in a nontrivial way.
1. https://bristolcrypto.blogspot.com/2015/01/password-hashing-...
- User types pA$$WORD1 and clicks Sign In
- Frontend sends that to the server
- Server responds "incorrect"
- Frontend tries again with Pa$$word1
- Server responds "correct" and user is logged in
You either have to double the number of failed attempts before a lockout or deal with users getting locked out much quicker.
Thus causing more frustration than if you'd done nothing at all to address the capslock issue (that they're going to have to notice and fix anyway at some point, when they inevitably need to type again)
No it doesn't. Say your password is "Abc1". When you sign up, they would hash "Abc1". Then when you login, say you type "aBC1". The site would first hash "aBC1" and find that it is incorrect. Then it would invert the case to "Abc1" and try again, and it would work. It's basically just automating a second attempt. In fact, I wonder if it even counts as a second failed attempt if both failed.
If someone were able to make brute force attempts on the website, it would still end up evaluating the passwords the same number of times as if the inverted case wasn't checked (the attacker would just need to invert it themselves) and in fact would likely slow down the attack because they are forced into checking inverterted case passwords. But that's moot anyway because the website limits login attempts. And if the database leaked, it's not stored any differently than if they didn't invert the case.
Of course I'm making assumptions about the implementation (I didn't know they did this until this comment) and it could be done poorly but I would hope a company as big as meta/Facebook put at least this amount of thought into it.
This makes more sense, and obviously a better idea.
0: https://en.wikipedia.org/wiki/Letter_case#Bicameral_script
Making it more convenient for everybody equally is an admirable goal, but should a trivial change that makes life a bit easier for many people be scrapped just because making it easier for everyone else is difficult? Specifically here where the experience doesn’t significantly regress for anyone.
No, if CapsLock was set to on when you pressed the key, getModifierState('CapsLock') returns true. It is true that this doesn't let you query the caps lock status directly at will, instead you have to wait for a keyboard event. But it is not merely telling you that Caps Lock itself was pressed, rather that a key was pressed while CapsLock was enabled.
This is exactly how it's working for me as I test it on this textarea.
So if you wanted to add one of those "Hey, Caps Lock is on" warnings to a password field, you could do so, you'd just have to wait for the first key to be pressed.
* consecutive uppercase alpha characters are entered
* the shift property on the keyboard event is false
Why consecutive? Only one uppercase character without the shift key being used should be sufficient.
That being said, it's probably not the best method as the users can also copy/paste passwords, or use a password manager.
That would be up to the User Agent (the browser), not the website.
Websites could have been simple to make with basic markup, leaving UX niceties to the browser vendors.
The world we live in is about as far from that as you get, with the stock UI for <input> elements being about par for 1992 UI toolkits, if even that.
It just didn't pan out to tablets and desktop computers. But it might not be too late ?
Safari has a caps lock indicator on password fields on every platform, and has had it for several years - at least since Safari 12 on macOS 10.12 (circa 2018), possibly longer, but I don't have an older VM to test on.
Gecko has an open feature request for this, from 2 years ago: https://bugzilla.mozilla.org/show_bug.cgi?id=1757348
Chromium has an open feature request for this, from 3.5 years ago: https://issues.chromium.org/issues/40722752
Relevant: Safari has done this for ages.
docs: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/...
That being said, I think it would be better if browsers would implement this; that would make it more efficient and more predictable.
True, but I was trying to avoid handlers being fired constantly. Just something to consider.
So I take it none of the OS/browser vendors besides Apple are doing this natively yet?
I know that it is about security stuff, but for the games in js is a bad situation when the canvas lost the focus and the keys pressed change.
Our robots can be driven remotely via a web app. But there’s ways to accidentally get buttons stuck down because keyup is never guaranteed and you can’t get key state. Two cases: holding down forward key and blur the tab, or open contextmenu.
There’s workarounds of course. But it’s comical spending so much time on something so simplistic.
https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API...