Re: SQL injection, php and queueing multiple statement - Mailing list pgsql-general

From Chris Browne
Subject Re: SQL injection, php and queueing multiple statement
Date
Msg-id 60lk3kw3co.fsf@dba2.int.libertyrms.com
Whole thread Raw
In response to SQL injection, php and queueing multiple statement  (Ivan Sergio Borgonovo <mail@webthatworks.it>)
List pgsql-general
mail@webthatworks.it (Ivan Sergio Borgonovo) writes:
> Is there a switch (php side or pg side) to avoid things like:
>
> pg_query("select id from table1 where a=$i");
>
> into becoming
>
> pg_query("select id from table1 where a=1 and 1=1; do something
> nasty; -- ");
>
> So that every
> pg_query(...) can contain no more than one statement?

The conventional approach to this sort of thing is to use prepared
statements:

http://ca3.php.net/manual/en/function.pg-prepare.php

In effect, you set up the query beforehand, pre-parameterizing.

<?php
// Connect to a database named "mary"
$dbconn = pg_connect("dbname=mary");

// Prepare a query for execution
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM shops WHERE name = $1');

// Execute the prepared query.  Note that it is not necessary to escape
// the string "Joe's Widgets" in any way
$result = pg_execute($dbconn, "my_query", array("Joe's Widgets"));

// Execute the same prepared query, this time with a different parameter
$result = pg_execute($dbconn, "my_query", array("Clothes Clothes Clothes"));

?>

Assuming that PHP is actually using PostgreSQL prepared statements
(and not just faking things behind your back), this should nicely
address the problem of injection attacks.
--
(reverse (concatenate 'string "ofni.sesabatadxunil" "@" "enworbbc"))
http://linuxfinances.info/info/linuxdistributions.html
The  average woman would rather   have beauty than  brains because the
average man can see better than he can think.

pgsql-general by date:

Previous
From: Stefan Sturm
Date:
Subject: Re: PostgreSQL Processes on a linux box
Next
From: Ivan Sergio Borgonovo
Date:
Subject: Re: SQL injection, php and queueing multiple statement