堆检查

7gyucuyw  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(431)
  1. public DataSource dsDatasource(Environment env) throws Exception {
  2. HikariDataSource dataSource = new HikariDataSource();
  3. dataSource.setUsername(username);
  4. String password = env.getProperty(convertToHashicorpLabel());
  5. dataSource.setPassword(password != null ? password : dbPassword);
  6. if (dataSource.getPassword() == null) {
  7. throw new Exception("Datasource password is null");
  8. }
  9. dataSource.setJdbcUrl(url);
  10. dataSource.setDriverClassName(driverClassName);
  11. dataSource.setMaximumPoolSize(maxPoolSize);
  12. dataSource.setMinimumIdle(minPoolSize);
  13. dataSource.setPoolName(poolName);
  14. return dataSource;
  15. }
  16. private String convertToHashicorpLabel() {
  17. return username + "_label";
  18. }

}
上面是java方法,当我运行checkmarx报告时,它在此行显示一个堆检查漏洞,字符串password=env.getproperty(converttohashicorplabel());。请帮我修一下好吗。

sh7euo9m

sh7euo9m1#

这里的风险是字符串是不可变的,并且敏感信息(在本例中为密码)可能保留在内存中,攻击者可以通过访问主机来检索这些信息。
如@luk2302所指出的,使用更安全的类型,如sealedobject与char[]结合使用

  1. char[] password;
  2. Key key = KeyGenerator.getInstance("AES").generateKey();
  3. Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding");
  4. c.init(Cipher.ENCRYPT_MODE, key);
  5. List<Character> characterList = Arrays.asList(password);
  6. SealedObject soPassword = new SealedObject((Serializable) characterList, c);
  7. Arrays.fill(password, '\0');

相关问题