I'm trying to use this method in a dynamic SOQL query. Follow the sample code:
public static String getRowById(String sobjName, id id) {
String query = 'SELECT ';
Map<String, Schema.SObjectField> objectFields =
Schema.getGlobalDescribe().get(sobjName).getDescribe().fields.getMap();
for (String f : objectFields.keySet()) {
query += f;
query += ',';
}
query = query.substring(0, query.length() - 1);
query += ' FROM '+ sobjName;
query += ' WHERE id = \'' + String.escapeSingleQuotes(id) + '\'';
query += ' LIMIT 1';
return query;
}
The problem is that this is still considered as a SOQL Injection vulnerability.
Am I missing something here? I also tried to use it as a parameter like this:
getRowById('SFT_Brand__c',
String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('id')));

sobjNameargument to be the bigger injection risk if it is coming from the query string parameters. Also, I'd be surprised in the Id type could contain single quote characters. Look at validating if sobjName is a valid sObject? – Daniel Ballinger Feb 14 at 19:19sObjNamein theSchema.getGlobalDescribe()call verify it's valid? It would throw an exception there if it wasn't. If OP is always hard-coding the object name in his calls, I can't see an injection vulnerability. – tomlogic Feb 14 at 20:43Schema.getGlobalDescribe().get(sobjName)returns null for an unknown type. – Daniel Ballinger Feb 14 at 21:28queryvariable using System.debug? – Suman Krishna Saha Feb 15 at 5:10select id,sft_brand_component_type__c,name,isdeleted,sft_brand_tire_type__c,createddate,sft_brand_vehicle_type__c,systemmodstamp,ownerid,lastmodifiedbyid,createdbyid,lastmodifieddate from SFT_Brand__c where id = 'a04U0000006id9NIAQ' limit 1– vanderlindo Feb 15 at 11:11