![](/img/trans.png)
[英]Allowing column name to be specified makes it a potential SQL injection risk
[英]SQL injection risk on sort direction?
我正在生成一個動態的prepareStatement,如下所示(字段可能會根據請求輸入而更改)。 Veracode掃描不斷報告有關以下方面的SQL注入風險:CWE-89:對SQL命令中使用的特殊元素(“ SQL注入”)進行不正確的中和。
Select id from table where (USER_KEY=?) AND (TIMESTAMP between ? and ?) AND (APPLICATION_ID = ? OR APPLICATION_ID = ? )
AND (TITLE = ? ) order by "sortField" "sortDirection" limit "searchlimit"
因為以“ order by”開頭的所有內容都無法通過問號(?)進行參數化。 在連接到prepareStatment之前,我遵循了現有的文章對以“ order by”開頭的字符串進行清理。 為Order by使用准備好的語句來防止SQL注入java 。 排序字段和限制檢查邏輯很好,但排序方向檢查仍會觸發SQL注入風險警報。
StringBuilder whereBuilder = new StringBuilder();
...
if(!Utility.checkAllowedSortColumn(Utility.getSQLField(sortField))) {
throw new ServiceException("Invalid Request: bad SQL sort field resulted from - "+sortField);
}else {
whereBuilder.append(" order by ");
whereBuilder.append(Utility.getSQLField(sortField));
whereBuilder.append(" ");
}
/*
if(sortDirection.equalsIgnoreCase("DESC") || sortDirection.equalsIgnoreCase("ASC")){
whereBuilder.append(sortDirection);
}else {
throw new ServiceException("Invalid Request: bad sort direction - "+sortDirection);
}*/
if(Integer.parseInt(searchLimit) < 1){
throw new ServiceException("Invalid Request: query result limit must be greater than zero");
}
else {
whereBuilder.append(" limit ");
whereBuilder.append(searchLimit);
}
return whereBuilder.toString();
如果我像上面那樣注釋了sortDirection檢查邏輯,則Veracode掃描通過了prepareStatement(當然,它需要DB默認ASC)。 但是,我期望基於用戶輸入的DESC或ASC的sortDirection。 有沒有建議在沒有SQL注入風險的情況下使用檢查邏輯添加排序方向?
您的注釋塊不應給您的查詢帶來任何風險。 看來此Veracode工具不夠聰明,無法標記為安全。 您是否可以通過重組代碼來欺騙它,如下所示?
if(sortDirection.equalsIgnoreCase("DESC")) {
whereBuilder.append("DESC");
} else if (sortDirection.equalsIgnoreCase("ASC")) {
whereBuilder.append("ASC");
} else {
throw new ServiceException("Invalid Request: bad sort direction - "+sortDirection);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.