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 | |
Re: Mind the meta!
by LanX (Saint) on Mar 03, 2016 at 22:08 UTC | |
by Anonymous Monk on Mar 04, 2016 at 11:24 UTC | |
by LanX (Saint) on Mar 04, 2016 at 12:02 UTC | |
by Your Mother (Archbishop) on Mar 04, 2016 at 12:34 UTC | |
by tye (Sage) on Mar 04, 2016 at 14:50 UTC | |
| |
by LanX (Saint) on Mar 04, 2016 at 12:46 UTC | |
| |
by Anonymous Monk on Mar 04, 2016 at 13:29 UTC | |
Re: Mind the meta!
by mr_mischief (Monsignor) on Mar 03, 2016 at 15:14 UTC |