我试图通过提供一个搜索字符串来搜索帐户表。我正在构建一个基于不同条件的查询,然后将返回的记录添加到列表中。
该功能工作正常,但我想知道如何进一步优化下面的方法。
public PageReference search(){
clear();
runningUserId = Userinfo.getUserId();
String searchquery;
Boolean existUser = AccSearchAccess__c.getValues(runningUserId) !=null? true : false;
if(existUser){
searchquery='Select Name,Id,AccessLevel__c FROM Account where Name like \'%'+accountName+'%\'';
}
else{
set<id> accountIds = new set<id>();
List<WMAccess__c> wmaLst = [Select id,Account__c from WMAccess__c where WM_User__c = : UserInfo.getUserId()];
for(WMAccess__c wm : wmaLst){
accountIds.add(wm.Account__c);
}
if(!accountIds.isEmpty()){
searchquery='Select Name,Id,AccessLevel__c FROM Account where Name like \'%'+accountName+'%\' AND id IN:accountIds';
}
}
accList= Database.query(searchquery);
if(accList.size()>0)
for(Account acc : accList){
AccountWrapper aw = new AccountWrapper(acc, false);
aw.accountId = acc.id;
aw.accountName = acc.Name;
aw.accountAccess = acc.AccessLevel__c;
accountsList.add(aw);
}
return null;
}发布于 2016-07-26 16:44:53
您已经暴露了一个注入漏洞。
'WHERE Name like \'%'+accountName+'%\''当您将用户输入合并到查询中时,应该调用String.escapeSingleQuotes,或者更好的是使用动态绑定。
// OK
searchQuery += 'WHERE Name LIKE \'%' + String.escapeSingleQuotes(accountName) + '%\'';
// BETTER
String fuzzySearchTerm = '%' + accountName + '%';
searchQuery += 'WHERE Name LIKE :fuzzySearchTerm';https://codereview.stackexchange.com/questions/135833
复制相似问题