堆检查

7gyucuyw  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(301)
public DataSource dsDatasource(Environment env) throws Exception {
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setUsername(username);
    String password = env.getProperty(convertToHashicorpLabel());
    dataSource.setPassword(password != null ? password : dbPassword);
    if (dataSource.getPassword() == null) {
        throw new Exception("Datasource password is null");
    }
    dataSource.setJdbcUrl(url);
    dataSource.setDriverClassName(driverClassName);
    dataSource.setMaximumPoolSize(maxPoolSize);
    dataSource.setMinimumIdle(minPoolSize);
    dataSource.setPoolName(poolName);
    return dataSource;
}

private String convertToHashicorpLabel() {
    return username + "_label";
}

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

sh7euo9m

sh7euo9m1#

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

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

相关问题