Welcome to WebmasterWorld Guest from 54.227.101.214

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

accepting only Safe Characters in formmail

     

astounded

6:24 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



I am learning, and I put the snippet below into a formmail script so it would accept only safe characters. But when I test it, all other characters such as & * % also go through to the the text file it writes to, and to the email the formmail.php sends it to. What am I doing wrong on this thing?

$safe_vars = array("A-Z", "a-z", "0-9");
foreach ($_POST as $key => $value) {
$safe_vars[$key] = htmlspecialchars($value);
}

jatar_k

8:01 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The problem is that I don't think the snippet does anything that you think it does. Why not try explaining your methodology in words and we can see if we can't convert that to code.

astounded

9:27 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



What I'm trying to do is restrict the characters which can be susubmitted by the form in order to limit potential hacking to the text file it writes to.

I was trying to accept only letters and numerals, and also the symbols for:

at @, dash-, and underscore _

Perhaps I am going about this entirely wrong.

jatar_k

9:32 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think in your foreach you could have a regular expression that makes sure all chars in the var are in the allowed set.

I am not the regex guru here so maybe someone could offer one?

andreasfriedrich

9:40 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$save = preg_replace [php.net]("'[^a-zA-Z0-9]'", '', $unsave);

will delete all characters that are not simple letters and numbers.

However, whatīs save and what isnīt will depend on the context. When you output the input as HTML then htmlspecialchars [php.net] is all you really need to make sure that the output is save. When you pass the input to the shell you might want to use escapeshellcmd [php.net]. The method you suggested is rather brute force and eliminates more than really necessary in most cases.

Andreas

jatar_k

9:45 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Most of the time when I do data checking I use specific patterns/tests for specific fields. A phone number one way, an email another. Error checking always depends on what data you are receiving and what you plan on doing with it. Also on the possible vulnerabilities of any given action.

For example an upload field would need more rigorous checks to prevent malicious code being unserted onto your server.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month