Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Mind the meta!

by Anonymous Monk
on Mar 01, 2016 at 16:08 UTC ( [id://1156540]=perlmeditation: print w/replies, xml ) Need Help??

I was a little surprised that in a recent thread no one seemed to flinch at code that was essentially the equivalent of $logged_in = $expect_pass=~/$FORM{pass}/i;.

If you happen to be wondering "Why is that bad?" - Because $FORM{pass} most likely comes from an HTML form (or at the very least could contain user input), and its contents will be interpolated into and interpreted as a regular expression, leaving the user free to input ".*", which of course matches anything, and so the user is always logged in!

One solution is to use /\Q$FORM{pass}\E/, which causes characters that would normally be special in a regular expression to be escaped - see quotemeta. In addition, the regex should probably be anchored, to prevent partial matches: /^\Q$FORM{pass}\E$/. Of course, in this example the much easier solution is to just use $expect_pass eq $FORM{pass} instead of a regex!

I feel like \Q...\E is forgotten too often when interpolating variables into regexes, and it seems to be often overlooked here on PerlMonks when people post code like the above. In fact, I see it forgotten often enough that I think instead \Q...\E should be everyone's default thought when interpolating variables into regular expressions, only to be left off when one has a good reason to (like dynamically building regexes - but of course doing that with unchecked user input is dangerous!).

So please, mind the meta(characters) and remember \Q...\E!

Replies are listed 'Best First'.
Re: Mind the meta!
by Eily (Monsignor) on Mar 02, 2016 at 08:48 UTC

    If you need to check that a string is included in another, index will often do both a faster and safer job. You'll need to lc both variables for a case insensitive comparison though.

    And when it comes to security with user input, Taint mode is probably what you should rely on, rather than just expecting to never leave a security hole through multiples evolutions and corrections.

Re: Mind the meta!
by mr_mischief (Monsignor) on Mar 03, 2016 at 15:14 UTC

    Far better would be to hash the password and check that the hash exactly matches the expected hash of the password, and to only store the password pre-hashed.

Re: Mind the meta!
by LanX (Saint) on Mar 03, 2016 at 22:08 UTC
    Since when is a regex better than eq to check a password?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Understand what you're saying, but the fc and \F are since v5.16.

      "Since when is it better to case-fold passwords?" :^)

        OK I missed the case insensitive /i part, mea culpa!

        > "Since when is it better to case-fold passwords?" :^)

        For me case folding of passwords is a reason to ignore a discussion thread ... ;-)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1156540]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found