Skip to main content

Sensitive information sent insecurely

Need

Secure transmission of sensitive information

Context

  • Usage of Swift for iOS and macOS app development
  • Usage of UIKit for building modern and responsive user interfaces
  • Usage of Alamofire for making network requests in iOS development

Description

Non compliant code

import UIKit
import Alamofire

class ViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

@IBAction func loginButtonTapped(_ sender: Any) {
let username = usernameTextField.text
let password = passwordTextField.text

let parameters: [String: Any] = [
"username": username,
"password": password
]

Alamofire.request("http://example.com/api/login", method: .post, parameters: parameters, encoding: URLEncoding.default)
.responseJSON { response in
print(response)
}
}
}

The above Swift code is for a simple login screen in an iOS application. The user enters their username and password, which are then sent as parameters in a POST request to a login API.

The vulnerability lies in the fact that the application is sending sensitive user data (username and password) over an insecure channel (HTTP). HTTP does not encrypt the data being sent, which means that it could potentially be intercepted and read by malicious parties. This is a major security risk, as it could lead to unauthorized access to user accounts.

Additionally, the application is using Alamofire, a networking library, to send the request. While Alamofire is a powerful and commonly used library, it does not automatically encrypt data being sent, so it is up to the developer to ensure that data is being sent securely.

In the current state, the application is not ensuring the confidentiality or integrity of the user's sensitive data, making it vulnerable to attacks such as Man-in-the-Middle (MitM), where an attacker intercepts and potentially alters the communication between the user and the server.

Steps

  • Use HTTPS instead of HTTP for sending sensitive information.
  • Implement SSL/TLS to encrypt the communication between the client and the server.
  • Ensure that the server has a valid SSL/TLS certificate.
  • Verify the server's identity during the SSL/TLS handshake.
  • Avoid sending sensitive information in the URL parameters.
  • Use a secure encoding method (e.g., JSON or form data) to send the sensitive information in the request body.
  • Implement proper authentication and authorization mechanisms to protect sensitive endpoints.
  • Regularly update and patch the server to fix any security vulnerabilities.

Compliant code

import UIKit
import Alamofire

class ViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

@IBAction func loginButtonTapped(_ sender: Any) {
let username = usernameTextField.text
let password = passwordTextField.text

let parameters: [String: Any] = [
"username": username,
"password": password
]

Alamofire.request("https://example.com/api/login", method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
.responseJSON { response in
print(response)
}
}
}

The updated code now uses HTTPS instead of HTTP when making the request to the server. This ensures that the communication between the client and the server is encrypted, protecting the confidentiality and integrity of the sensitive information being sent.

The parameters are now being sent in the body of the request using URLEncoding.httpBody instead of being included in the URL. This prevents the sensitive information from being exposed in the URL, which could be logged or cached.

The server should have a valid SSL/TLS certificate and the client should verify the server's identity during the SSL/TLS handshake to prevent man-in-the-middle attacks.

The server should also implement proper authentication and authorization mechanisms to protect sensitive endpoints and regularly update and patch the server to fix any security vulnerabilities.

References