从远程源转发 URL¶
ID: java/unvalidated-url-forward
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-552
Query suites:
- java-code-scanning.qls
- java-security-extended.qls
- java-security-and-quality.qls
在未验证输入的情况下直接将用户输入合并到 URL 转发请求中会导致文件信息泄露,因为攻击者可以访问未授权的 URL。
建议¶
为了防止不受信任的 URL 转发,应避免将用户输入直接放入转发的 URL 中。相反,应在服务器上维护一个授权 URL 列表,然后根据提供的用户输入从该列表中进行选择。
示例¶
以下示例显示了 HTTP 请求参数直接用于 URL 转发,而没有验证输入,这可能导致文件信息泄露。它还显示了如何通过针对已知的固定字符串验证用户输入来解决问题。
public class UrlForward extends HttpServlet {
private static final String VALID_FORWARD = "https://cwe.mitre.org/data/definitions/552.html";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletConfig cfg = getServletConfig();
ServletContext sc = cfg.getServletContext();
// BAD: a request parameter is incorporated without validation into a URL forward
sc.getRequestDispatcher(request.getParameter("target")).forward(request, response);
// GOOD: the request parameter is validated against a known fixed string
if (VALID_FORWARD.equals(request.getParameter("target"))) {
sc.getRequestDispatcher(VALID_FORWARD).forward(request, response);
}
}
}
参考¶
OWASP:未验证的重定向和转发备忘单.
常见漏洞枚举:CWE-552.