설명
Core Data 또는 Realm 로컬 데이터베이스에 비밀번호, 토큰, API 키 같은 민감 값을 평문으로 저장하면 백업, 디버그 추출, 탈옥된 기기에서 값이 노출될 수 있습니다.
잠재적 영향
- 인증 토큰 또는 세션 탈취
- 로컬 백업과 디버그 추출을 통한 민감 정보 노출
- 탈취한 값 재사용을 통한 계정 접근
해결 방법
- 민감한 값은 가능하면 기기에 저장하지 마세요. 꼭 보관해야 하는 토큰이나 비밀번호는 Keychain에 저장하세요.
- 데이터베이스가 필요하면 SQLCipher나 암호화를 설정한 Realm을 사용하거나, 값을 암호화한 뒤 저장하세요. 암호화 키는 데이터베이스나 소스 코드에 두지 말고 Keychain 같은 별도 저장소에서 관리하세요.
예시
Core Data에서 access_token 속성은 Binary Data 형식이어야 합니다. 예시에서는 키 조회와 컨텍스트 저장을 생략했습니다.
변경 전
swift
let user = NSEntityDescription.insertNewObject(forEntityName: "User", into: context)
user.setValue(accessToken, forKey: "access_token")
변경 후
swift
import CoreData
import CryptoKit
enum StorageEncryptionError: Error {
case missingCombinedRepresentation
}
func storeToken(
context: NSManagedObjectContext,
accessToken: Data,
databaseKey: SymmetricKey
) throws {
let sealedBox = try AES.GCM.seal(accessToken, using: databaseKey)
guard let encryptedToken = sealedBox.combined else {
throw StorageEncryptionError.missingCombinedRepresentation
}
let user = NSEntityDescription.insertNewObject(
forEntityName: "User", into: context)
user.setValue(encryptedToken, forKey: "access_token")
}
storeToken은 AES-GCM으로 토큰을 암호화하고 sealedBox.combined를 access_token에 넣습니다. 결합 데이터를 얻지 못하면 오류를 던집니다.