2013-07-17 62 views
1

我有一台运行我的应用程序我得到一个陷阱有以下原因时,使用下列字符串修复SQL注入核心数据

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"displayName == [cd] %@ AND emailAddress == [cd] %@" , displayName, emailAddress]; 

定期内置谓词:

Fatal Exception NSInvalidArgumentException 
unimplemented SQL generation for predicate : (0x64b5580 <x-coredata://3AA31AFB-35E4-468C-876D-03DEB56F38A3/aaa/p14> IN) 

看起来问题是由有效的SQL注入谓词语句引起的。

此处推荐的解决方法是使用以下格式的谓词吗?

[NSPredicate predicateWithFormat:@"(displayName == [cd] \"%@\") AND (emailAddress == [cd] \"%@\")" , displayName, emailAddress]; 

即,用双引号包围搜索目标吗?或者是在这里推荐的其他方法?

+1

一个AND复合谓语您可以验证,如果'displayName'和'emailAddress'时有效异常。占位符插入值的引号,因此不需要包含多余的引号。 – Anupdas

+0

我希望我能,但我只有crashlytics数据。你说的是正确的 - 我只是证实了我自己。我需要找到导致这种情况的输入数据。 – Lee

回答

5

如果您不确定这些值的完整性,请尝试使用NSCompoundPredicate。谓词添加到一个谓语阵列仅当这个值是有效的,并创建谓词阵列

NSMutableArray *subPredicates = [NSMutableArray array]; 

if (displayName) { 
    [subPredicates addObject:[NSPredicate predicateWithFormat:@"displayName == [cd] %@",displayName]]; 
} 

if (emailAddress) { 
    [subPredicates addObject:[NSPredicate predicateWithFormat:@"emailAddress == [cd] %@",emailAddress]]; 
} 

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; 
+1

感谢这看起来不错 - 数据来自电子邮件服务器的电子邮件地址/名称,所以他们可能是任何东西。 – Lee