Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Need help with syntax error in CGI script (security hole)

by Ovid (Cardinal)
on Aug 02, 2004 at 01:47 UTC ( #379169=note: print w/replies, xml ) Need Help??


in reply to Need help with syntax error in CGI script

In addition to what the others have said, I'd like to point out a huge security hole. I've reformatted the code for clarity, but the following is logically equivalent to what you have:

my $dbh = DBI->connect( 'DBI:mysql:*********', '*****', '*****', {RaiseError => 1 , AutoCommit => 1} ) || die "Can't Connect: $!"; my $sth = $dbh->prepare(<<END_SQL); UPDATE inventory SET img="$short_name", thumb="$short_tname" WHERE partno="$img_name"; ENDSQL $sth->execute;

Never allow user to be able to send data directly to the database like this. If you do, you open yourself up to SQL injection attacks where the attacker can insert their own SQL and run it arbitrarily against the server. You can protect against this by using the $dbh->quote method on the variables before you insert them. However, a cleaner strategy is to always use placeholders:

my $sth = $dbh->prepare(<<END_SQL); UPDATE inventory SET img = ?, thumb = ? WHERE partno = ?; ENDSQL $sth->execute($short_name, $short_tname, $img_name);

Read "Placeholders and Bind Values" in the DBI documentation for more information.

Cheers,
Ovid

New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://379169]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-09-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?