Perl和参数化语句
我正在寻找参数化语句没有被正确执行的确认。当前代码如下:
$ywant = "user supplied integer";
$select = "SELECT a.Authors, a.PublicationYear, a.Title, a.URL, a.ArticleID, f.Format ".
"FROM tbl_ETI_Article a, tbl_ETI_Format f ".
"WHERE a.PublicationYear = '".$ywant."' ".
"AND a.FID = f.FID ".
"ORDER BY a.PublicationYear DESC, a.Title ASC";
$sth = $dbh->prepare( " $select " );
$sth->execute();
我知道选择字符串中的 $ywant 变量应该替换为占位符 '?' 然后将用户提供的数据作为参数放置在执行语句中。
我的问题是上述内容是否仍然提供任何针对注入攻击的保护,因为它仍在“准备中”?
回答
这是一个有风险的 SQL,因为 $ywan 可能有恶意代码,如果你不能相信它的来源。所以最好这样做:
$ywant = "user supplied integer";
$select = qq{SELECT a.Authors, a.PublicationYear, a.Title, a.URL, a.ArticleID, f.Format
FROM tbl_ETI_Article a, tbl_ETI_Format f
WHERE a.PublicationYear = ?
AND a.FID = f.FID
ORDER BY a.PublicationYear DESC, a.Title ASC};
$sth = $dbh->prepare($select);
$sth->execute($ywant);
并避免任何 SQL 注入潜在问题。对于这种攻击,prepare 语句本身并不能保护您