敏感信息在本地数据库中的明文存储¶
ID: swift/cleartext-storage-database
Kind: path-problem
Security severity: 7.5
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-312
Query suites:
- swift-code-scanning.qls
- swift-security-extended.qls
- swift-security-and-quality.qls
在数据库中未加密存储的敏感信息可被获取该数据库访问权限的攻击者访问。例如,信息可能被根设备中的任何进程或用户访问,或通过其他漏洞暴露。
建议¶
对整个数据库进行加密,或确保在存储之前对每条敏感信息进行加密。通常,只在需要以明文形式使用敏感信息时才解密。如果不需要保留敏感信息,请避免存储它们。
示例¶
以下示例展示了使用 Core Data 库存储信息的三个案例。在“BAD”案例中,存储的数据是敏感的(信用卡号码),并且未加密。在“GOOD”案例中,数据要么不敏感,要么受到加密保护。
func storeMyData(databaseObject : NSManagedObject, faveSong : String, creditCardNo : String) {
// ...
// GOOD: not sensitive information
databaseObject.setValue(faveSong, forKey: "myFaveSong")
// BAD: sensitive information saved in cleartext
databaseObject.setValue(creditCardNo, forKey: "myCreditCardNo")
// GOOD: encrypted sensitive information saved
databaseObject.setValue(encrypt(creditCardNo), forKey: "myCreditCardNo")
// ...
}
参考资料¶
OWASP Top 10:2021: A02:2021 � 加密失败.
通用弱点枚举: CWE-312.