You need to sign in to do that
Don't have an account?
Stuck on prevent SOQL injection trailhead
Hi all, I am stuck on the prevent SOQL injection trailhead, where I am looking to determine which portions of the code I would need to invoke escapeSingleQuotes or whitelist to enable. I've made several attempts at investigation to little avail. I have appended my code are there any steps I would need to take. My goal is to assess the code to see how it works and what I would need to do.
Hope it helps.
Hope it helps.
public class Prevent_SOQL_Injection_Challenge { public string textOne {get; set;} public string textTwo {get; set;} public string comparator {get; set;} public string numberOne {get; set;} public List<Supply__c> whereclause_records {get; set;} public PageReference stringSearchOne(){ string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c'; string whereClause = ''; if(textOne != null && textOne!=''){ whereClause += 'name like \'%'+textOne+'%\' '; } if(whereClause != ''){ whereclause_records = database.query(query+' where '+whereClause+' Limit 10'); } return null; } public PageReference stringSearchTwo(){ string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c'; string whereClause = ''; if(textTwo != null && textTwo!=''){ whereClause += 'Storage_Location__r.name like \'%'+textTwo+'%\' '; } if(whereClause != ''){ whereclause_records = database.query(query+' where '+whereClause+' Limit 10'); } return null; } public PageReference numberSearchOne(){ string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c'; string whereClause = ''; if(numberOne != null && comparator != null){ whereClause += 'Quantity__c '+comparator+' '+numberOne+' '; } if(whereClause != ''){ whereclause_records = database.query(query+' where '+whereClause+' Limit 10'); } return null; } }
Here is a general guide for the changes I made to the class which eventually passed the assessment:
- Used string.escapeSingleQuote for textOne
- Used string.escapeSingleQuote for textTwo
- Used value whitelisting for the comparator to ensure values were <,>, or =.
Try removing the replaceAll from your statements on textOne and textTwo and I think it may work for you as you already have the whitelisting set for the comparator item.All Answers
For example :-
These seem to test out via the form, not with check challenge.
Any ideas?
Here is a general guide for the changes I made to the class which eventually passed the assessment:
- Used string.escapeSingleQuote for textOne
- Used string.escapeSingleQuote for textTwo
- Used value whitelisting for the comparator to ensure values were <,>, or =.
Try removing the replaceAll from your statements on textOne and textTwo and I think it may work for you as you already have the whitelisting set for the comparator item.Thanks for the info, but I'm still not seeing what's wrong. I've tried several combinations and find this frustrating.
I've paried it down to the simplest of code and still cannot get the code to pass. Here is my current version.
I've also tried this verson with forced whitelisting:
Any pointers will be greatly appreciated.
I have escaped textOne and textTwo, changed numberOne to Integer and verified comparator values in the if condition but still got the error...
Could you please advise ?
Thanks a lot.
Can you share your code for the challenge? I can then provide some specific guidance on the differences between that and the code I used to complete the module.
-Mike
See if changing line 47 to this makes a difference for you:
Very odd indeed but I successfully pass the challenge.
Thanks a lot for your help.
If you write if(numberOne != null) { if (comparator == '=' || comparator == '>' || comparator == '<') { : it does not work !!!
public class Prevent_SOQL_Injection_Challenge {
public string textOne {get; set;}
public string textTwo {get; set;}
public string comparator {get; set;}
public string numberOne {get; set;}
public List<Supply__c> whereclause_records {get; set;}
public PageReference stringSearchOne(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(textOne != null && textOne!=''){
whereClause += 'name like \'%'+String.escapeSingleQuotes(textOne)+'%\' ';
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
public PageReference stringSearchTwo(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(textTwo != null && textTwo!=''){
whereClause += 'Storage_Location__r.name like \'%'+String.escapeSingleQuotes(textTwo)+'%\' ';
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
public PageReference numberSearchOne(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(numberOne != null && comparator != null && (comparator == '<' || comparator == '>' || comparator == '=')){
whereClause += 'Quantity__c '+String.escapeSingleQuotes(comparator)+' '+String.valueOf(Integer.valueOf(numberOne))+' ';
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
}
The problem with my approach was that whereClause string needed to have single-quotes in it.
It is unnecessary to use String.valueOf() for the Integer though. Maybe he's an experienced C/C++ programmer and there's a big difference between '7' and 7, but in Apex, they are both equal.
public class Prevent_SOQL_Injection_Challenge {
public string textOne {get; set;}
public string textTwo {get; set;}
public string comparator {get; set;}
public string numberOne {get; set;}
public List<Supply__c> whereclause_records {get; set;}
public PageReference stringSearchOne(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(textOne != null && textOne!=''){
whereClause += 'name like \'%'+String.escapeSingleQuotes(textOne)+'%\' ';
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
public PageReference stringSearchTwo(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(textTwo != null && textTwo!=''){
whereClause += 'Storage_Location__r.name like \'%'+String.escapeSingleQuotes(textTwo)+'%\' ';
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
public PageReference numberSearchOne(){
string query = 'SELECT Id,Name,Quantity__c,Storage_Location__c,Storage_Location__r.Castle__c,Type__c FROM Supply__c';
string whereClause = '';
if(numberOne != null && comparator != null && (comparator == '<' || comparator == '>' || comparator == '=')){
whereClause += 'Quantity__c '+comparator+String.valueOf(Integer.valueOf(numberOne));
}
if(whereClause != ''){
whereclause_records = database.query(query+' where '+whereClause+' Limit 10');
}
return null;
}
}