How to Check Input Text for SQL Injection Attacks in Swift

Cloudmersive
2 min readApr 26, 2021

--

Did you know that having a website or application which allows users to utilize a search bar or fill out a feedback, checkout, or business form makes you particularly susceptible to SQL injection attacks? These provide an open line between the user and business, giving an attacker the opportunity to insert malicious SQL commands in an incorrectly coded field. To guard against these threats, you can use the following API in Swift to detect SQLI attacks from specific text.

All that is needed for the operation is the target text, API key, and detection level (optional — default is Normal); once this information is entered into the code, we can call the function:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "\"<string>\""
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/text-input/check/sql-injection")!,timeoutInterval: Double.infinity)
request.addValue("<string>", forHTTPHeaderField: "detectionLevel")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

Your response will indicate if an SQL injection has been detected so you can immediately address the issue if needed.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet