![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I am just wondering how to have safe parameters avoiding to use parametrized queries. How to behave quote_literal against sql injection attempts? Is it adequate or must we use something stronger? Like for example (Perl): sub sanitize{ my $p=shift; $p=~ s/[\\"'\*\#]+//g; $p=~ s/-+/-/g; return $p; } particularly I am wondering how quote_literal behaves against sql remarks. |
#3
| |||
| |||
|
|
"news" wrote: Hi, I am just wondering how to have safe parameters avoiding to use parametrized queries. How to behave quote_literal against sql injection attempts? Is it adequate or must we use something stronger? Like for example (Perl): sub sanitize{ my $p=shift; $p=~ s/[\\"'\*\#]+//g; $p=~ s/-+/-/g; return $p; } particularly I am wondering how quote_literal behaves against sql remarks. quote_literal surrounds the string with ', doubles any ' in the string, and if the string contains a backslash, that backslash will be doubled and the string prefixed with E. The idea is to modify a string so that it can safely be used in dynamic SQL, like EXECUTE 'SELECT * FROM ' || quote_literal(user_input) Your code sample has nothing to do with that, it removes certain characters from the string and will render it useless. |
|
quote_literal will protect you against SQL injection. Just make sure you don't forget to use it in your code. |
#4
| |||
| |||
|
|
Hi, I am just wondering how to have safe parameters avoiding to use parametrized queries. How to behave quote_literal against sql injection attempts? Is it adequate or must we use something stronger? Like for example (Perl): |
#5
| |||
| |||
|
|
Hi, I am just wondering how to have safe parameters avoiding to use parametrized queries. How to behave quote_literal against sql injection attempts? Is it adequate or must we use something stronger? Like for example (Perl): sub sanitize{ my $p=shift; $p=~ s/[\\"'\*\#]+//g; $p=~ s/-+/-/g; return $p; } Your code sample has nothing to do with that, it removes certain characters from the string and will render it useless. why useless? It simply removes dangerous chars from user input(remarks in other sql dialects are included too): ' " # * \ * is removed for /* */ remarks style any multiple dash like -- or --- is turned in a single dash in my opinion those chars are arguably eligible as content in a database text field, so they can be removed without worries. Of course any value parsed in this way must be surrounded by single quotes and this method is viable only for text fields. |
|
quote_literal will protect you against SQL injection. Just make sure you don't forget to use it in your code. I made a test ended in a scary way: root@pluto:~# ./testinjection.pl test input string: '); drop table IDoNotExist; -- dynamic query string: select quote_literal('$test') as result query issued to Postgres: select quote_literal(''); drop table IDoNotExist; --') as result DBD::Pg::st execute failed: ERROR: table "idonotexist" does not exist DBD::Pg::st fetchall_arrayref failed: no statement executing |
#6
| |||
| |||
|
|
$sth = $dbh->prepare('SELECT quote_literal(?)'); $sth->execute($test); Then fetch the query result into $quoted and use it like this: my $sql = 'DELETE FROM ' . $quoted; $dbh->do($sql); and no SQL injection will occur (All code samples untested). |
#7
| |||
| |||
|
|
query issued to Postgres: select quote_literal(''); drop table IDoNotExist; --') as result DBD::Pg::st execute failed: ERROR: table "idonotexist" does not exist DBD::Pg::st fetchall_arrayref failed: no statement executing |
#8
| |||
| |||
|
|
I wrote: [an example for quoting in dynamic sql] $sth = $dbh->prepare('SELECT quote_literal(?)'); $sth->execute($test); Then fetch the query result into $quoted and use it like this: my $sql = 'DELETE FROM ' . $quoted; $dbh->do($sql); and no SQL injection will occur (All code samples untested). That delete statement is nonsense, sorry. I mixed up quote_identifier and quote_literal. The following is more appropriate: my $sql = 'DELETE FROM tab WHERE textcol = ' . $quoted; |

#9
| |||
| |||
|
|
On 2010-03-30, Anselmo Canfora<nobody (AT) nowhere (DOT) net> wrote: query issued to Postgres: select quote_literal(''); drop table IDoNotExist; --') as result DBD::Pg::st execute failed: ERROR: table "idonotexist" does not exist DBD::Pg::st fetchall_arrayref failed: no statement executing http://search.cpan.org/~turnstep/DBD....1/Pg.pm#quote |
thanks!
#10
| |||
| |||
|
|
On Mon, 29 Mar 2010 18:44:12 +0200, news wrote: Hi, I am just wondering how to have safe parameters avoiding to use parametrized queries. How to behave quote_literal against sql injection attempts? Is it adequate or must we use something stronger? Like for example (Perl): The best way to avoid SQL injection is not to form SQL statements dynamically, from input. You should use bind variables instead. |

![]() |
| Thread Tools | |
| Display Modes | |
| |