调用REST API时出现SSRF漏洞

问题描述

我正在使用一种方法,该方法调用一个REST API从数据库中检索ID。当我对该类运行veracode扫描时,下面一行显示安全漏洞“服务器端请求伪造”。

response =  resttemplate.getForEntity(resturl,String.class);

不确定如何解决此问题。任何帮助表示赞赏。下面是该方法的完整代码

public static String getIDFromDB(String resturl) {

  String id = null;
  RestTemplate resttemplate = new RestTemplate();
  ResponseEntity<String> response = new ResponseEntity<>(HTTPStatus.OK)
  try {
        response =  resttemplate.getForEntity(resturl,String.class);
        if (response.getStatusCode == HTTPStatus.OK && response.getBody.trim() != null) {
        id = response.getBody.trim() ;
      }
  } Catch(Exception e) {
     log.error("Failed to get msgiD: {}",e);
  }
}

解决方法

这是因为您在代码中允许完全在代码中传递resturl,因此攻击者可以绕过URL并将其路由到其预期的目的地。

为避免这种情况,应该外部化并使用配置文件或dB中具有操作名称的具有域和应用程序上下文的URL