perlmeditation
talexb
<p>This is a followup to my original question [id://661249]. When I run the following code on my system..</p>
<readmore><c>
#!/usr/bin/perl -w
use DBI;
# Test SQL injection attack. The test table 'jobs' was created with
# the command 'create table jobs (j_id integer, j_value text);' in
# all three databases.
my @databases = ( 'mysql:test', 'Pg:dbname=test', 'SQLite:test.sq3' );
my @data = (
{ id => 44, value => "Some benign text" },
{ id => 55, value => "Just regular data" },
{ id => 66, value => "Evil data');DELETE FROM jobs;" }
);
{
foreach my $thisDbName ( @databases ) {
my $dbh = DBI->connect("DBI:$thisDbName", undef, undef )
or die "Unable to connect to $thisDbName: " . $DBI::errstr;
print "Connected OK to $thisDbName.\n";
# Clean up test table before we start ..
my $cmd = "DELETE FROM jobs";
my $sth = $dbh->prepare($cmd);
print "Clear out existing data from the test table ..\n";
$sth->execute or die "Problem executing $cmd: " . $sth->errstr;
$cmd = "INSERT INTO jobs (j_id, j_value) VALUES (?,?)";
$sth = $dbh->prepare($cmd);
# Add test data into table ..
foreach my $hashref ( @data ) {
print "Add "
."($hashref->{'id'},$hashref->{'value'})"
." to the test table ..\n";
$sth->execute($hashref->{'id'}, $hashref->{'value'})
or die "Problem executing $cmd: " . $sth->errstr;
}
# Dump out the resulting tables.
$cmd = "SELECT * FROM jobs";
$sth = $dbh->prepare($cmd);
print "Dump out the result.\n";
$sth->execute or die "Problem executing $cmd: " . $sth->errstr;
DBI::dump_results($sth);
print "\n";
}
}
</c></readmore>
<p>I get the following results:</p>
<readmore><c>
Connected OK to mysql:test.
Clear out existing data from the test table ..
Add (44,Some benign text) to the test table ..
Add (55,Just regular data) to the test table ..
Add (66,Evil data');DELETE FROM jobs;) to the test table ..
Dump out the result.
'44', 'Some benign text'
'55', 'Just regular data'
'66', 'Evil data');DELETE FROM jobs;'
3 rows
Connected OK to Pg:dbname=test.
Clear out existing data from the test table ..
Add (44,Some benign text) to the test table ..
Add (55,Just regular data) to the test table ..
Add (66,Evil data');DELETE FROM jobs;) to the test table ..
Dump out the result.
'44', 'Some benign text'
'55', 'Just regular data'
'66', 'Evil data');DELETE FROM jobs;'
3 rows
Connected OK to SQLite:test.sq3.
Clear out existing data from the test table ..
Add (44,Some benign text) to the test table ..
Add (55,Just regular data) to the test table ..
Add (66,Evil data');DELETE FROM jobs;) to the test table ..
Dump out the result.
44, 'Some benign text'
55, 'Just regular data'
66, 'Evil data');DELETE FROM jobs;'
3 rows
</c></readmore>
<p>So, as far as I'm concerned, placeholders are enough to prevent SQL injection attacks for MySQL, PostgreSQL and SQLite. If anyone can point out something obvious that I've missed, please let me know.</p>
<p><b>Update:</b> Thanks to the monks who have contributed, we now have the following results:
<table>
<tr><th>Pass</th><th>Fail</th></tr>
<tr>
<td valign="top"><ul>
<li>MySQL, PostgreSQL and SQLite (from this node)</li>
<li>Oracle 10g [olus]</li>
<li>MS Access and SQLServer (both 2000, I believe) [rhesa]</li>
<li>IBM UDB version 9.5.0 on Linux 2.6.9 i386 [andreas1234567]</li>
<li>CSV, DBM -- In theory that means that should pass with any of the [cpan://SQL::Statement] DBDs. [jZed]</li>
<li>DBD::Sybase 1.08, Sybase ASE 15.0.2 and Sybase OpenClient 15 [mpeppler]</li>
</ul></td>
<td valign="top"><ul>
<li>Nothing yet.</li>
</ul></td>
</tr>
</table>
</p>
<p><b>Update 2:</b> As per [tye]'s [id://661491|reply below], please note that this is obviously not a rigorous test that proves conclusively that placeholders <i>always</i> prevent SQL injection attacks, but rather a quick check that an obvious test does indeed work properly.</p>
<div class="pmsig"><div class="pmsig-131279">
<p>Alex / [talexb] / Toronto</p>
<p><small>"<a href="http://www.groklaw.net">Groklaw</a> is the open-source mentality applied to legal research" ~ Linus Torvalds</small></p>
</div></div>