Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^10: CGI Action call

by tultalk (Monk)
on Mar 20, 2018 at 09:30 UTC ( [id://1211281]=note: print w/replies, xml ) Need Help??


in reply to Re^9: CGI Action call
in thread CGI Action call

See what you are saying

If the stmt was

my $stmt = "SELECT * FROM users WHERE ? = ? ORDER BY ? +ASC";

instead, it would fail with

$sth->execute($searchfield, $searchterm, $searchfield) or die "Unable +to execute + query: " . $sth->errstr;

But this works as the field name is assigned early.

[Mon Mar 19 19:01:04 2018] update_tables.cgi: statement = 'SELECT * F +ROM users WHERE lastname = ? ORDER BY ? ASC' at update_tables.cgi lin +e 462.

So what would be lost by simply using:

'SELECT * F +ROM users WHERE $searchfield = $searchterm ORDER BY $searchfield ASC'

where all the parameter would be defined at prepare?

Replies are listed 'Best First'.
Re^11: CGI Action call
by Corion (Patriarch) on Mar 20, 2018 at 09:50 UTC

    Because $searchterm is user-supplied, I could supply O'Reilly to break your SQL query or  1; delete from users -- to wipe all users from the user table or  1; update users set is_admin=1 -- to make all accounts administrator accounts.

    Interpolating user-supplied data into SQL statements is a problematic thing and best avoided.

      You say: Interpolating user-supplied data into SQL statements is a problematic thing and best avoided.

      How do you avoid having a user (administrator only in this case) enter a user supplied search term like a last name?

      Perhaps I don't understand your statement.

        my $kind = $query->param('kind'); my $searchterm = $query->param('searchterm'); my $searchfield; if ($kind == 0) { $searchfield = 'user_id'; } elsif ($kind == 1) { $searchfield = 'lastname'; } elsif ($kind == 2) $searchfield = 'business'; } my $stmt = " SELECT * FROM users WHERE $searchfield = ? ORDER by $searchfield"; my $sth = $dbh->prepare($stmt); $sth->execute($searchterm);

        In the above, the interpolated field $searchfield is not user supplied. The user supplied $searchterm uses a placeholder so no problem searching for the lastname O'Reilly.

        poj
        You say: Interpolating user-supplied data into SQL statements is a problematic thing and best avoided.

        No. He says: User data may, maliciously or accidentally, including programmer error, be problematic and must be sanitised.

        Regards,

        John Davies

Re^11: CGI Action call
by marto (Cardinal) on Mar 20, 2018 at 10:08 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-25 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found