Ngl I wrote a new application at work that (among other responsibilities) is going to take over the password authentication of customer accounts, and it sucked because I wanted it to Not Suck. And while I think there are still like theoretical timing attack vectors (according to ChatGPT’s Astra anyway, but Claude thought it was fine and I’m too busy with this whole other mission critical application we desperately needed to rewrite 5 years ago to look at a Schrodinger’s security flaw the girls are fighting over in an app that’s still not in production because of a chain of stupid and/or horrifying decisions made by departments that are not mine that I neither want nor should get into), it’s way fucking better than what’s currently which is what I will say is not awful, it certainly falls short of many a best practice. I also learned that most places are not following all the best practices and I would guess that it being Really Fucking Hard is a big reason why.
So, like, a recommended step is Unicode normalization (specifically NFC I believe) because it can prevent situations where a user is inputting what is essentially the same string of text but for reasons that are not their fault and practically impossible for them to understand or predict, it is not the same set of bytes originally used to generate the password hash which then causes it to not authenticate. That’s relatively simple to do though in my nice greenfield application except oh yeah I have a bunch of fucking existing hashes I must maintain compatibility for and they weren’t normalized. For that and a number of other reasons, it maintains support for all the existing password hashes and can do so in a way that won’t break anything as we transition over to using the new authentication system completely while also having all of the machinery necessary that we can flip a switch and have it silently start rehashing any password using an old hash when a user authenticatss with one. That sounds really nice in theory too, but it resulted in some ugly fucking code and interfaces that are going to be there forever unless we decide to invalidate all the old hashes at some point.
Also I’m becoming too fucking aware that the length of a string as far as computer programming goes is a situation where you really need to be specific and make sure that everything agrees. I’m actually pretty sure I fucked this up in one area and need to go back and fix it eventually. Password length for example you want it to be the length of the string in characters but in Golang, the len() function returns the number of bytes in a string. So, you have to do type conversions to make sure length is actually characters. I thought, naively, that the length of a VARCHAR column in MySQL. Like idk a lot of column widths are defined in terms of bytes (or even bits!), so I just assumed that like varchar(255) capped the length at 255 bytes. Nope, it’s also characters. Hashing algorithms, however, do care about the length of the input in bytes. Bcrypt (widely used algorithm) has a maximum length of 72 bytes. PBKDF2 (the one used by Discourse) seemingly doesn’t, but it seems like some libraries did implement limits because you could effectively DDoS a site using it by making it perform the computationally expensive hashing algorithm on a super long string. Argon2 is similar, but the implementations I’ve seen tend to leave maximum input size as an exercise for the programmer to sort out. I’m getting off topic. Point is I was needing to check that the length was valid in terms of characters in at least 3 different contexts and the length in bytes in at least one. And it wouldn’t surprise me if I subtly fucked that up somewhere along the way. Like I’m not sure if it’s validating the length of normalized or unnormalized secrets and I’m pretty sure normalization can affect the length. Also that validation happens before it knows what hashing algorithm it needs to use, but… I think with how we plan to use it we just need to make sure it checks lengths in the way the desired hashing algorithm wants because that’s what it’ll inevitably be hashed with. Remember when I said this was complicated?
Oh yeah also just fucking handling passwords securely inside your own application is a nightmare. You want to make sure they’re not logged or thrown into errors or stack traces or core dumps as much as is reasonably possible, but because there’s no existing type will essentially redact itself automatically, I had to make one. But I’m neither stupid nor ambitious enough to write my own cryptography package which means that I inevitably have to pass a secret into a built-in type so there are like microseconds where it could hypothetically be exposed if the exact wrong thing happened at the wrong time. Thankfully that’s not a viable attack vector really unless an attacker already has access to the application host machine which is game over anyway. I do still try to minimize even the risk of that happening I guess. (Although I’m also remembering instances now where I forgot to zero out the bytes after using the secret for the last time but fucking whatever I’ll get to that eventually probably). Did I mention this was complicated?