There are various questions and answers about how to manage passwords in Java code - for example, here and here.
Discussions tend to focus on the merits of using char[]
over String
.
But are there any ways to avoid passwords being stored in the JVM's heap, if it's a 3rd party library that is storing the password in a String?
For example, in the following three cases I think passwords will remain in the heap for the lifetime of the JVM:
Hikari Connection Pool 3.4
EDIT - example updated to be more relevant to my question:
// some values read in from a properties file, or similar:
String url = ...;
String user = ...;
String pw = ... ;
...
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(url);
ds.setUsername(user);
ds.setPassword(pw);
...
In the above example, my com.zaxxer.hikari.HikariDataSource
object contains the password in a String
. I can't see a way to null the data source without losing the connection pool.
Jetty 9.4 Database Adaptor
For example, if I create a JDBCSessionDataStoreFactory
, then the org.eclipse.jetty.server.session.DatabaseAdaptor
holds on to the connection credentials, including the password in a String
.
JNDI Connection in Tomcat 9
In this case the password can be found in org.apache.tomcat.util.descriptor.web.ContextResource
and maybe also in org.apache.tomcat.dbcp.dbcp2.BasicDataSource
, if I also choose to use that connection pool.
In other words, sensitive connection information can be found in various locations in my JVM's heap, regardless of how diligent I am about using char arrays in my code and overwriting/nulling sensitive data when it's no longer needed. I am not suggesting my efforts are wasted - I still want to protect what I can (for example a user's login credentials). But other credentials such as the database connection seem to be out of my control.
Are there any solutions or best practices for these cases?