I'm working on a Java service that had a security bug logged against it, concerning a secret stored in a Java String object.
The concern is that once a password is stored in a String
, it stays in the string pool until it's garbage collected, and could show up in a dump as clear text.
I understand that Java's String
class is immutable, and it is preferred that secrets or passwords be stored in a mutable class, so it can be removed from memory as soon as possible after use.
References
- Why is char[] preferred over String for passwords?
- Java equivalent of SecureString
- In Java, is there still a point in using char[] instead of String to store passwords?
- Answer by StephenC to similar question
However, I find this impractical when working with 3rd party libraries, as many of them want the password or secret passed in the request as a String object, which no matter how safe I am, in my code, ultimately converts the char[]
array into a String
, which takes us back to the original problem, doesn't it?
So, is there anyway to convert a char[]
, or StringBuffer
to a String
, without getting back to the original problem?
EDIT: As pointed out by the comments in this question and other similar questions, many believe this is not a real concern, as there's no easy way to avoid the issue if you don't own ALL the code in your application (ie don't pass secrets to any 3rd party libraries). So given this, maybe the question becomes
How can I refute the bug? is there a good solid reference from Oracle/Owasp or whoever that says "unsolvable problem" ?