Skip to content

Commit

Permalink
fix(core): only escape "%" with the SQL LIKE operator
Browse files Browse the repository at this point in the history
  • Loading branch information
cgx committed Jan 31, 2022
1 parent 88b0b6a commit 7c81e3a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
12 changes: 6 additions & 6 deletions SoObjects/Appointments/SOGoAppointmentFolder.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2007-2019 Inverse inc.
Copyright (C) 2007-2022 Inverse inc.
Copyright (C) 2004-2005 SKYRIX Software AG

This file is part of SOGo.
Expand Down Expand Up @@ -794,7 +794,7 @@ - (NSArray *) bareFetchFields: (NSArray *) fields
if ([title length])
[baseWhere
addObject: [NSString stringWithFormat: @"c_title isCaseInsensitiveLike: '%%%@%%'",
[title asSafeSQLString]]];
[title asSafeSQLLikeString]]];

if (component)
{
Expand Down Expand Up @@ -1577,14 +1577,14 @@ - (NSArray *) fetchFields: (NSArray *) _fields
if ([filters isEqualToString:@"title_Category_Location"] || [filters isEqualToString:@"entireContent"])
{
[baseWhere addObject: [NSString stringWithFormat: @"(c_title isCaseInsensitiveLike: '%%%@%%' OR c_category isCaseInsensitiveLike: '%%%@%%' OR c_location isCaseInsensitiveLike: '%%%@%%')",
[title asSafeSQLString],
[title asSafeSQLString],
[title asSafeSQLString]]];
[title asSafeSQLLikeString],
[title asSafeSQLLikeString],
[title asSafeSQLLikeString]]];
}
}
else
[baseWhere addObject: [NSString stringWithFormat: @"c_title isCaseInsensitiveLike: '%%%@%%'",
[title asSafeSQLString]]];
[title asSafeSQLLikeString]]];
}

/* prepare mandatory fields */
Expand Down
6 changes: 3 additions & 3 deletions SoObjects/Contacts/SOGoContactGCSFolder.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2006-2019 Inverse inc.
Copyright (C) 2006-2022 Inverse inc.
This file is part of SOGo.
Expand Down Expand Up @@ -208,7 +208,7 @@ - (EOQualifier *) qualifierForFilter: (NSString *) filter
qualifier = nil;
if ([filter length] > 0)
{
filter = [filter asSafeSQLString];
filter = [filter asSafeSQLLikeString];
filters = [NSMutableArray array];
filterFormat = [NSString stringWithFormat: @"(%%@ isCaseInsensitiveLike: '%%%%%@%%%%')", filter];
if (criteria)
Expand Down Expand Up @@ -356,7 +356,7 @@ - (NSDictionary *) lookupContactWithName: (NSString *) aName
if (aName && [aName length] > 0)
{
aName = [aName asSafeSQLString];
qs = [NSString stringWithFormat: @"(c_name='%@')", aName];
qs = [NSString stringWithFormat: @"(c_name = '%@')", aName];
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
dbRecords = [[self ocsFolder] fetchFields: folderListingFields
matchingQualifier: qualifier];
Expand Down
3 changes: 2 additions & 1 deletion SoObjects/SOGo/NSString+Utilities.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* NSString+Utilities.h - this file is part of SOGo
*
* Copyright (C) 2006-2019 Inverse inc.
* Copyright (C) 2006-2022 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -51,6 +51,7 @@

/* SQL safety */
- (NSString *) asSafeSQLString;
- (NSString *) asSafeSQLLikeString;

/* Unicode safety */
- (NSString *) safeString;
Expand Down
12 changes: 8 additions & 4 deletions SoObjects/SOGo/NSString+Utilities.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* NSString+Utilities.m - this file is part of SOGo
*
* Copyright (C) 2006-2019 Inverse inc.
* Copyright (C) 2006-2022 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -725,9 +725,13 @@ - (id) objectFromJSONString

- (NSString *) asSafeSQLString
{
return [[[self stringByReplacingString: @"\\" withString: @"\\\\"]
stringByReplacingString: @"'" withString: @"\\'"]
stringByReplacingString: @"\%" withString: @"\\%"];
return [[self stringByReplacingString: @"\\" withString: @"\\\\"]
stringByReplacingString: @"'" withString: @"\\'"];
}

- (NSString *) asSafeSQLLikeString
{
return [[self asSafeSQLString] stringByReplacingString: @"\%" withString: @"\\%"];
}

- (NSUInteger) countOccurrencesOfString: (NSString *) substring
Expand Down
9 changes: 4 additions & 5 deletions SoObjects/SOGo/SOGoGCSFolder.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SOGoGCSFolder.m - this file is part of SOGo
*
* Copyright (C) 2004-2005 SKYRIX Software AG
* Copyright (C) 2006-2014 Inverse inc.
* Copyright (C) 2006-2022 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1969,8 +1969,8 @@ - (NSArray *) _fetchComponentsWithNames: (NSArray *) cNames
if (sqlFilter)
{
filterString = [NSMutableString stringWithCapacity: 8192];
[filterString appendFormat: @"(c_name='%@')",
[cNames componentsJoinedByString: @"' OR c_name='"]];
[filterString appendFormat: @"(c_name = '%@')",
[cNames componentsJoinedByString: @"' OR c_name = '"]];
if ([sqlFilter length] > 0)
[filterString appendFormat: @" AND (%@)", sqlFilter];
qualifier = [EOQualifier qualifierWithQualifierFormat: filterString];
Expand Down Expand Up @@ -2012,8 +2012,7 @@ - (NSArray *) _fetchComponentsMatchingObjectNames: (NSArray *) cNames
{
currentName = [[cNames objectAtIndex: count] asSafeSQLString];
queryNameLength = idQueryOverhead + [currentName length];
if ((currentSize + queryNameLength)
> maxQuerySize)
if ((currentSize + queryNameLength) > maxQuerySize)
{
records = [self _fetchComponentsWithNames: currentNames fields: fields];
[components addObjectsFromArray: records];
Expand Down
2 changes: 1 addition & 1 deletion SoObjects/SOGo/SQLSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ - (NSArray *) fetchContactsMatching: (NSString *) filter
if ([filter length])
{
lowerFilter = [filter lowercaseString];
lowerFilter = [lowerFilter asSafeSQLString];
lowerFilter = [lowerFilter asSafeSQLLikeString];
filterFormat = [NSString stringWithFormat: @"LOWER(%%@) LIKE '%%%%%@%%%%'", lowerFilter];
if (criteria)
criteriaList = [criteria objectEnumerator];
Expand Down

0 comments on commit 7c81e3a

Please sign in to comment.