this post was submitted on 21 Jun 2024
422 points (99.1% liked)

Software Gore

962 readers
1 users here now

A community for posting software malfunctions

Deliberately bad software or bad design is not software gore, it must be something unintentional

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient and shear it



founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] frezik@midwest.social 72 points 5 months ago* (last edited 5 months ago) (4 children)

Those (?=...) bits are positive lookahead assertions:

Lookaround assertions are zero-width patterns which match a specific pattern without including it in $&. Positive assertions match when their subpattern matches, negative assertions match when their subpattern fails. Lookbehind matches text up to the current match position, lookahead matches text following the current match position.

The one (?!...) is a negative lookahead assertion.

The $& var doesn't really matter outside of Perl. It contains the text of the pattern you just matched, but even within Perl, capture groups are preferred. Once used at all, it will slow down your program every time a new regex is hit, which is especially bad in long running web server environments. Gets used sometimes in short scripts, though.

What really matters is that the lookaheads don't consume any text. In other words, the pointer that shows where in the text we are doesn't increment; once we're outside of the lookahead, we're still right back in the same place.

So let's break this down using the /x modifier to make it somewhat sane.

/^
(?!.*\s) # no whitespace allowed
(?=.{8,256}$) # between 8 and 256 characters (the '$' here indicating the end of the string)
(?=.*[a-z]) # has to be a lowercase ASCII alphabet char somewhere
(?=.*[A-Z]) # has to be an uppercase ASCII alphabet char somewhere
( # need a number, or a list of special chars on a US keyboard
    (?=.*[0-9]) 
    | (?=.*[~!@#$%^&*()-=_+[\]{}|;:,./<>?])
)
.* # consumes the whole string
$/x

Notes:

  • Doesn't make any allowances for non-English characters, or even non-US characters (like the "£" character in the UK)
  • There's a whole slew of utf8 characters out there that should count towards "special characters", but aren't considered here
  • There's no reason to deny whitespace; let people use passphrases if they want (but then, you also don't want to block those people for not using symbols)
  • Putting a limit at 256 is questionable, but may not necessarily be wrong

That last one has some nuance. We often say you shouldn't put any upper limit, but that's generally not true in the real world. You don't want someone flooding an indefinite amount of data into any field, password or not. A large limit like this is defensible.

Also, lots of devs are surprised to learn that bcrypt and scrypt have a length limit of 72 bytes. A way around this is to run your input through SHA256 before giving it to bcrypt or scrypt.

[–] dohpaz42@lemmy.world 11 points 5 months ago (1 children)

Honestly, white space is a character, and adds extra entropy to passwords. I do not understand why people do not want to promote using white space in passwords/passphrases. If I’m missing something intrinsically bad about white space in passwords, I’d love to know.

[–] frezik@midwest.social 5 points 5 months ago (1 children)

You're right on. As long as you're otherwise following best practices for storing passwords, there's no downside.

[–] shasta@lemm.ee 12 points 5 months ago (1 children)

But but but if I add it to the queryparams for my rest endpoint the space will break my URL!

[–] indepndnt@lemmy.world 3 points 4 months ago

Always urlencode your passwords!

Wait, that doesn't seem right...

[–] freebee@sh.itjust.works 10 points 5 months ago* (last edited 5 months ago) (1 children)
[–] tiredofsametab@kbin.run 10 points 5 months ago

As someone who spent many years as a Perl developer, I immediately recognized the incantations to the regex gods of old, heh. Great explanation!

[–] chickenf622@sh.itjust.works 1 points 5 months ago (1 children)

I don't see a reason to limit the length as long as the password hash can handle large values. I am green when it comes to the inner workings of password hashing, so I may be wrong.

[–] Vigge93@lemmy.world 6 points 5 months ago

Being able to handle it, and being able to handle it efficiently enough are two very distinct things. The hash method might be able to handle long strings, but it might take several seconds/minutes to process them, slowing down the application significantly. Imagine a malicious user being able to set a password with millions (or billions!) of characters.

Therefore, restricting it to a small, but still sufficiently big, number of characters might help prevent DoS-attacks without any notable reduction in security for regular users.