当遇到错误/不存在的 spring-boot 端点时。代码级别的类详细信息被公开。这可能会被标记为安全问题。
示例
请求
localhost:8500/api/1.0/service/../msc -> this is a bad formatted endpoint, which does not exist.
回复
{
"timestamp": "2024-01-31T08:33:44.321+0000",
"status": 400,
"error": "Bad Request",
"message": "Failed to find lookupPath '/api/1.0/msc' within requestUri '/api/1.0/service/../msc'. This could be because the path has invalid encoded characters or isn't normalized.; nested exception is org.springframework.web.servlet.resource.ResourceUrlEncodingFilter$LookupPathIndexException: Failed to find lookupPath '/api/1.0/msc' within requestUri '/api/1.0/service/../msc'. This could be because the path has invalid encoded characters or isn't normalized.",
"path": "/api/1.0/service/../msc"
}
只要查看错误消息,我们就可以了解到有一个 spring-boot 应用程序在后台运行,由于消息中公开了代码级别的详细信息,因此它可能是一个漏洞。
我们如何向客户端发送通用消息而不是整个异常详细信息?
您可以使用多种方法的组合来处理该异常并且不泄漏它,请参阅https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc#recent-updates检查不同类型的处理程序。
您是否尝试在控制器级别建议上使用 ExceptionHandler? 然后处理具体的业务异常。 在处理程序上,您可以添加自定义业务消息,也可以尝试保留原始异常的一些详细信息。 您还可以有一个特定的异常处理程序,它可以充当捕获所有情况的功能。像下面这样,
@Configuration
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Response>
handleMethodArgumentException(MethodArgumentNotValidException
methodArgumentNotValidException)