Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Hackers > Re: Protection ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 56 Topic 9274 of 10401
Post > Topic >>

Re: Protection from SQL injection

by lists@[EMAIL PROTECTED] (PFC) Apr 28, 2008 at 11:00 PM

> As you know, "SQL injection" is the main security problem of databases  
> today.
>
> I think I found a solution: 'disabling literals'. Or you may call it
> 'enforcing the use of parameterized statements'. This means that SQL
> statements with embedded user input are rejected at runtime. My
> solution goes beyond saying "developers ~should~ use parameterized
> statements". That is not a solution because developers are lazy. My
> solution is: "developers MUST use parameterized statements". It goes
> like this: Literals are disabled using the SQL statement:

I have found that the little bit of code posted afterwards did eliminate  
SQL holes in my PHP applications with zero developer pain, actually it is 

MORE convenient to use than randomly pasting strings into queries.

You just call
db_query( "SELECT * FROM table WHERE column1=%s AND column2=%s",  
array( $var1, $var2 ));

It is inspired from the Python interface which performs the same (but  
slightly more elegantly).
I have removed the logging features for clarity.

function db_quote_query( $sql, $params=false )
{
	// if no params, send query raw
	if( $params === false )	return $sql;
	if( !is_array( $params )) $params = array( $params );

	// quote params
	foreach( $params as $key => $val )
	{
		if( is_array( $val )) $params[$key] = implode( ', ', array_map( intval, 

$val ));
		else	              $params[$key] =  
is_null($val)?'NULL':("'".pg_escape_string($val)."'");;
	}
	return vsprintf( $sql, $params );
}

function db_query( $sql, $params=false )
{
	// it's already a query
	if( is_resource( $sql ))
		return $sql;

	$sql = db_quote_query( $sql, $params );

	$r = pg_query( $sql );
	if( !$r )
	{
		echo "<div class=bigerror><b>Erreur PostgreSQL :</b><br  
/>".htmlspecialchars(pg_last_error())."<br /><br /><b>RequĂȘte</b> :<br  
/>".$sql."<br /><br /><b>Traceback </b>:<pre>";
		foreach( debug_backtrace() as $t ) xdump( $t );
		echo "</pre></div>";
		die();
	}
	return $r;
}






>
> SET ALLOW_LITERALS NONE;
>
> Afterwards, SQL statements with text are not allowed any more for this
> session. That means, SQL statement of the form "SELECT * FROM USERS
> WHERE PASSWORD='qerkllkj'" will fail with the exception 'Literals are
> not allowed, please use parameters'. It is like the database does not
> know what ='qerkllkj' means. Only statements of the secure form are
> allowed, for example "SELECT * FROM USERS WHERE PASSWORD=?". This
> solves the problem because SQL injection is almost impossible if user
> input is not directly embedded in SQL statements.
>
> The 'ALLOW_LITERALS NONE' mode is enabled by the developer itself, or
> by an administrator. It is still possible to generate SQL statements
> dynamically, and use the same APIs as before, as long as SQL
> statements don't include literals. Literals can still be used when
> using query tools, or in applications considered 'safe'. To ease
> converting the application to use parameterized queries, there should
> be a second mode where number literals are allowed: SET ALLOW_LITERALS
> NUMBERS. To allow all literals, execute SET ALLOW_LITERALS ALL (this
> is the default setting).
>
> So far this feature is implemented in my little database H2. More
> information about this feature is described here:
> http://www.h2database.com/html/advanced.html#sql_injection
>
> I know about the Perl taint mode, but this is only for Perl. I also
> know about disabling multi-statement commands (only solves part of the
> problem). PostgreSQL should also sup****t database level 'constants'
> that are similar to constants in other programming languages,
> otherwise application level constants (such as 'active') can't be used
> in queries directly (I propose to add new SQL statements CREATE
> CONSTANT ... VALUE ... and DROP CONSTANT ..., example: CREATE CONSTANT
> STATE_ACTIVE VALUE 'active'). I also know the 'disabling literals'
> feature does not solve SQL injection completely: for example 'ORDER BY
> injection' where an application dynamically adds the column to sort on
> based on a hidden 'sort column' field in a web app. To solve that I
> suggest to sup****t parameterized ORDER BY: ORDER BY ? where ? is an
> integer. Then, instead of using SET ALLOW_LITERALS NONE the use of
> literals should probably be two access right (REVOKE LITERAL_TEXT,
> LITERAL_NUMBER FROM APP_ROLE). Those are details that still need to be
> discussed.
>
> What do you think about it? Do you think it makes sense to implement
> this security feature in PostgreSQL as well? If not why not? Does
> PostgreSQL have another solution or plan to solve the SQL injection
> problem?
>
> Regards,
> Thomas
>
> P.S. I have send this proposal to pgsql-sql@[EMAIL PROTECTED]
 first and
> got replies, but I would like to get some feedback from the PostgreSQL
> developers as well.
>



-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
 




 56 Posts in Topic:
Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-28 20:55:34 
Re: Protection from SQL injection
josh@[EMAIL PROTECTED] (  2008-04-28 12:09:29 
Re: Protection from SQL injection
sfrost@[EMAIL PROTECTED]   2008-04-28 15:17:35 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-04-28 23:00:18 
Re: Protection from SQL injection
direvus@[EMAIL PROTECTED]  2008-04-29 09:03:33 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-04-29 09:26:24 
Re: Protection from SQL injection
sam@[EMAIL PROTECTED] (S  2008-04-29 00:15:44 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-29 13:37:37 
Re: Protection from SQL injection
kleptog@[EMAIL PROTECTED]  2008-04-29 15:16:12 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-04-29 15:29:44 
Re: Protection from SQL injection
tino@[EMAIL PROTECTED] (  2008-04-29 16:35:41 
Re: Protection from SQL injection
andrew@[EMAIL PROTECTED]   2008-04-29 11:05:54 
Re: Protection from SQL injection
tino@[EMAIL PROTECTED] (  2008-04-30 16:47:56 
Re: Protection from SQL injection
stark@[EMAIL PROTECTED]   2008-04-29 09:36:02 
Re: Protection from SQL injection
pgsql@[EMAIL PROTECTED]   2008-04-29 19:44:29 
Re: Protection from SQL injection
tgl@[EMAIL PROTECTED] (T  2008-04-29 10:58:17 
Re: Protection from SQL injection
aidan@[EMAIL PROTECTED]   2008-04-29 11:23:04 
Re: Protection from SQL injection
josh@[EMAIL PROTECTED] (  2008-04-29 11:39:09 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-04-29 21:06:18 
Re: Protection from SQL injection
stark@[EMAIL PROTECTED]   2008-04-29 14:19:51 
Re: Protection from SQL injection
aidan@[EMAIL PROTECTED]   2008-04-29 16:01:44 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-29 21:29:41 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-29 22:18:48 
Re: Protection from SQL injection
andrew@[EMAIL PROTECTED]   2008-04-29 16:33:01 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-04-29 16:55:21 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-04-29 17:23:39 
Re: Protection from SQL injection
josh@[EMAIL PROTECTED] (  2008-04-29 15:24:10 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-05-01 19:00:25 
Re: Protection from SQL injection
andrew@[EMAIL PROTECTED]   2008-05-01 13:09:38 
Re: Protection from SQL injection
tgl@[EMAIL PROTECTED] (T  2008-05-01 13:25:07 
Re: Protection from SQL injection
josh@[EMAIL PROTECTED] (  2008-04-29 14:10:20 
Re: Protection from SQL injection
adsmail@[EMAIL PROTECTED]  2008-04-30 02:19:21 
Re: Protection from SQL injection
singh.gurjeet@[EMAIL PROT  2008-04-30 06:17:03 
Re: Protection from SQL injection
stark@[EMAIL PROTECTED]   2008-04-29 21:02:30 
Re: Protection from SQL injection
josh@[EMAIL PROTECTED] (  2008-04-29 18:20:36 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-04-30 10:20:09 
Re: Protection from SQL injection
kleptog@[EMAIL PROTECTED]  2008-04-30 22:58:34 
Re: Protection from SQL injection
tgl@[EMAIL PROTECTED] (T  2008-04-30 17:33:38 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-05-01 09:13:27 
Re: Protection from SQL injection
hannu@[EMAIL PROTECTED]   2008-04-29 23:21:10 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-04-30 12:55:06 
Re: Protection from SQL injection
books@[EMAIL PROTECTED]   2008-04-30 10:08:25 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-30 14:42:51 
Re: Protection from SQL injection
thomas.tom.mueller@[EMAIL  2008-04-30 17:22:50 
Re: Protection from SQL injection
singh.gurjeet@[EMAIL PROT  2008-04-30 22:41:08 
Re: Protection from SQL injection
tgl@[EMAIL PROTECTED] (T  2008-04-30 13:28:19 
Re: Protection from SQL injection
singh.gurjeet@[EMAIL PROT  2008-04-30 23:07:55 
Re: Protection from SQL injection
aidan@[EMAIL PROTECTED]   2008-04-30 13:50:25 
Re: Protection from SQL injection
stark@[EMAIL PROTECTED]   2008-05-01 11:07:08 
Re: Protection from SQL injection
tgl@[EMAIL PROTECTED] (T  2008-05-01 11:26:21 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-05-01 11:47:04 
Re: Protection from SQL injection
lists@[EMAIL PROTECTED]   2008-05-01 18:33:07 
Re: Protection from SQL injection
ajs@[EMAIL PROTECTED] (A  2008-05-01 15:17:37 
Re: Protection from SQL injection
fw@[EMAIL PROTECTED] (Fl  2008-05-04 19:38:45 
Re: Protection from SQL injection
Chris Browne <cbbrowne  2008-05-05 10:28:12 
Re: Protection from SQL injection
andrew@[EMAIL PROTECTED]   2008-05-05 14:00:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 6:51:37 CDT 2008.