CodeQL 文档

应用程序偏好存储中敏感信息的明文存储

ID: swift/cleartext-storage-preferences
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

单击以查看 CodeQL 仓库中的查询

在应用程序偏好存储(如用户默认数据库或 iCloud 支持的无处不在的键值存储)中未加密存储的敏感信息可供获得该数据存储访问权限的攻击者访问。例如,该信息可以被任何进程或用户在已破解的设备中访问,可以被受损的应用程序扩展访问,或者可以通过另一个漏洞暴露。

建议

将数据存储在加密数据库中,或确保在存储之前对每条敏感信息进行加密。通常,仅在需要明文使用敏感信息时才对其进行解密。如果不需要保留敏感信息,请避免将其存储。

示例

以下示例展示了三种使用 UserDefaults 存储信息的情况。在“不良”情况下,存储的数据是敏感的(信用卡号),并且未加密。在“良好”情况下,数据要么不是敏感的,要么受到加密保护。


func storeMyData(faveSong : String, creditCardNo : String) {
	// ...

	// GOOD: not sensitive information
	UserDefaults.standard.set(faveSong, forKey: "myFaveSong")

	// BAD: sensitive information saved in cleartext
	UserDefaults.standard.set(creditCardNo, forKey: "myCreditCardNo")

	// GOOD: encrypted sensitive information saved
	UserDefaults.standard.set(encrypt(creditCardNo), forKey: "myCreditCardNo")

	// ...
}

参考资料

  • ©GitHub, Inc.
  • 条款
  • 隐私