Custom searches and LoadForCriteria

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
psterritt
Ensign (ENS)
Ensign (ENS)
Posts: 2
Joined: Sun Apr 11, 2010 9:17 pm

Custom searches and LoadForCriteria

Post by psterritt » Sun Apr 11, 2010 10:08 pm

I'm trying to get better search results for a customer who wants to continue to use the standard (not advanced) search. They want the search to return results where the search criteria are found in product name, description or SearchKeywords.

I've had some luck by modifying SearchPage.ascx.cs. I'm parsing the words out of the entered keywords, splitting them out by spaces, and building a query statement using 'LIKE' statements for each of the fields, so a search for 'valve cover' would generate

(Name LIKE '%valve%' and Name LIKE '%cover%') or (Description LIKE ''%valve%' and Description LIKE '%cover%')or (SearchKeywords LIKE ''%valve%' and SearchKeywords LIKE '%cover%')

The resulting string is passed to ProductDataSource.LoadForCriteria() and the List<Product> that results is used for the ProductList.DataSource that would normally be returned by ProductDataSource.NarrowSearch.

I assume an approach like this would have little risk of something like SQL injection attacks, since it breaks up the keywords, but are there any other possible security/attack vulnerabilites in this approach?

Are there any other drawbacks to this approach?

Thanks!

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Custom searches and LoadForCriteria

Post by mazhar » Mon Apr 12, 2010 8:10 am

You can write some custom SQL Injection detection regular expression and then before building criteria make sure entered text doesn't contain any SQL via validating through that regular expression.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Custom searches and LoadForCriteria

Post by jmestep » Tue Apr 13, 2010 7:25 am

There is also StringHelper.SafeSqlString()
In the source code, here is what it does:
public static string SafeSqlString(string str)
{
if (str == null)
{
return string.Empty;
}

return str.Replace("'", "''"); // that is doubleqoute, singlequote doubleqoute, doublequote, two single quotes, double quote
}
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

psterritt
Ensign (ENS)
Ensign (ENS)
Posts: 2
Joined: Sun Apr 11, 2010 9:17 pm

Re: Custom searches and LoadForCriteria

Post by psterritt » Wed Apr 14, 2010 9:28 am

Thanks, Judy. Amazing how many fonts make it impossible to distinguish single quotes from doubles!

Post Reply