【发布时间】:2020-09-09 12:08:49
【问题描述】:
我的 Spring Boot 应用程序中有方法从系统环境变量中获取数据,该方法按预期工作,但 sonarQube 说“确保在此处安全使用环境变量”, 我试图找到解决此问题的替代方法,但找不到解决方案,方法如下:
我该如何处理这个安全问题,除了从环境变量中获取值之外,我无法使用任何其他方法。
public Map<String, Object> getConfigurations() {
Map<String, Object> result = new HashMap<>();
HttpResponse response = null;
try {
String xVaultToken = System.getenv("XVaultToken");
String cityAppConfig = System.getenv("CityApp_Config");
@SuppressWarnings("deprecation")
HttpClient client = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSslcontext(
new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build())
.build();
Map<String, Object> headerDatas = new HashMap<>();
headerDatas.put("Content-Type", "application/json");
headerDatas.put("X-Vault-Token", xVaultToken);
HttpGet get = new HttpGet(cityAppConfig);
Set<String> keys = headerDatas.keySet();
for (String key : keys) {
get.setHeader(key, headerDatas.get(key).toString());
}
response = client.execute(get);
try(BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))){
String responseData = rd.readLine();
result.put(Constants.RESPONSE, responseData);
}
int statusCode = response.getStatusLine().getStatusCode();
result.put(Constants.STATUS, statusCode);
} catch (Exception e) {
logger.info("error is local settings getConfigurations" + e);
}
return result;
}
}
【问题讨论】:
-
Sonnar 是否提出了一些替代方案?
-
您不需要寻找替代品,只需“确保在此处安全使用环境变量”,如声纳 qube 消息所述。您是否阅读了声纳文档,了解为什么这会被视为漏洞以及如何处理?
-
@JoãoZarate 不,它没有提供任何替代方案,它只是说“确保在此处安全使用环境变量”
-
这不是关于sanitizing this like any other input吗?如果 OP sqays 他们被 env 变量困住了,我想这就是与那些认为将令牌存储为 env var 是一个好主意和可能的 //NOSONAR
-
为什么不让 Spring Boot 处理这个问题。如果此类由 Spring 管理,您可以注入
Environment并执行getProperty或仅在实例字段上使用@Value("${XVaultToken}"。额外的好处,您现在还可以将此配置移动到application.properties或 Spring Boot 支持的任何方式(系统环境就是其中之一)。
标签: java spring-boot environment-variables