logo

Database

Swift Sensitive Data In External Storage

Description

Identifies instances where sensitive data might be insecurely written to external storage in Swift applications. Writing sensitive information to external storage without proper encryption or protection mechanisms can expose confidential data to unauthorized access.

Weakness:

275 - Non-encrypted confidential information - Local data

Category: Information Collection

Detection Strategy

    Check if the Foundation framework is imported in the Swift code

    Look for method calls ending with '.write' that perform write operations

    Verify the write operation is performed using unsafe methods or insecure file handling functions

    Confirm the write function matches known patterns for file system operations

Vulnerable code example

import Foundation
import UIKit

func savePassword(passwordField: UITextField) {
    let password = passwordField.text ?? ""
    let filePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        .appending(path: "password.txt", directoryHint: .notDirectory)
    ...

✅ Secure code example

import Foundation
import UIKit
import Security

func savePassword(passwordField: UITextField) {
    let password = passwordField.text ?? ""
    
    // Store password securely in Keychain instead of plaintext file...