將敏感數據存儲在 String 對象中使系統無法從內存中可靠地清除數據。
如果在使用敏感數據(例如密碼、社會保障號碼、信用卡號等)后不清除內存,則存儲在內存中的這些數據可能會泄漏。通常而言,String 是所用的存儲敏感數據,然而,由于 String 對象不可改變,因此用戶只能使用 JVM 垃圾收集器來從內存中清除 String 的值。除非 JVM 內存不足,否則系統不要求運行垃圾收集器,因此垃圾收集器何時運行并無保證。如果發生應用程序崩潰,則應用程序的內存轉儲操作可能會導致敏感數據泄漏。
例 1:下列代碼可將密碼從字符數組轉換為 String。
private JPasswordField pf;
...
final char[] password = pf.getPassword();
...
String passwordAsString = new String(password);
[1] Standards Mapping - OWASP Top 10 2010 - (OWASP 2010) A7 Insecure Cryptographic Storage
[2] Standards Mapping - OWASP Top 10 2007 - (OWASP 2007) A8 Insecure Cryptographic Storage
[3] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A8 Insecure Storage
[4] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3230.2 CAT II
[5] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3230.2 CAT II
[6] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 226
[7] Standards Mapping - FIPS200 - (FISMA) IA
[8] L. Gong, G. Ellison, and M. Dageforde Inside Java 2 Platform Security:Architecture, API Design, and Implementation, 2nd ed. Addison-Wesley
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 3.4, Requirement 6.3.1.3, Requirement 6.5.8, Requirement 8.4
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 3.4, Requirement 6.5.3, Requirement 8.4
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 3.4, Requirement 6.5.8, Requirement 8.4
[12] M. S. Ware Writing secure Java code:taxonomy of heuristics and an evaluation of static analysis tools