This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
This rule detects potential NoSQL injection vulnerabilities in iOS applications using the Realm database. The vulnerability occurs when a Realm query predicate is constructed by concatenating a static string with untrusted user input.
An attacker could provide specially crafted input that alters the logic of the NoSQL query. A successful exploit could allow the attacker to bypass authentication, access or modify sensitive data, or disrupt the application’s functionality.
To remediate this, avoid building queries using string concatenation. Instead, use parameterized queries with NSPredicate, which safely separates the query logic from user-provided values. For example, use NSPredicate(format: "name = %@", userInput) instead of "name = '\(userInput)'".
Non-Compliant Code Examples
importRealmSwiftclassUser:Object{@objcdynamicvarusername=""@objcdynamicvarisAdmin=false}funcfindUser(username:String){letrealm=try!Realm()// --- NON-COMPLIANT ---// The query predicate is built by concatenating a string with user input// *directly inside the filter call*. This pattern is detected by the rule.letresults=realm.objects(User.self).filter("username = '"+username+"'")print("Found \(results.count) users.")}// Example usagefindUser(username:"guest' OR isAdmin = true")
Compliant Code Examples
importFoundationimportRealmSwiftclassUser:Object{@objcdynamicvarusername=""@objcdynamicvarisAdmin=false}funcfindUserSafely(username:String){letrealm=try!Realm()// --- COMPLIANT ---// The query uses NSPredicate, which safely handles user input.// There is no `additive_expression` here for the rule to find.letsafePredicate=NSPredicate(format:"username = %@",username)letresults=realm.objects(User.self).filter(safePredicate)print("Found \(results.count) users.")}// Example usagefindUserSafely(username:"guest' OR isAdmin = true")
1
2
rulesets:- swift-security # Rules to enforce Unknown security.