XPath 인젝션 공격
Noncompliant Code Example
public boolean authenticate(javax.servlet.http.HttpServletRequest request, javax.xml.xpath.XPath xpath, org.w3c.dom.Document doc) throws XPathExpressionException {
String user = request.getParameter("user");
String pass = request.getParameter("pass");
String expression = "/users/user[@name='" + user + "' and @pass='" + pass + "']"; // Unsafe
// An attacker can bypass authentication by setting user to this special value
user = "' or 1=1 or ''='";
return (boolean)xpath.evaluate(expression, doc, XPathConstants.BOOLEAN); // Noncompliant
}
Compliant Solution
public boolean authenticate(javax.servlet.http.HttpServletRequest request, javax.xml.xpath.XPath xpath, org.w3c.dom.Document doc) throws XPathExpressionException {
String user = request.getParameter("user");
String pass = request.getParameter("pass");
String expression = "/users/user[@name=$user and @pass=$pass]";
xpath.setXPathVariableResolver(v -> {
switch (v.getLocalPart()) {
case "user":
return user;
case "pass":
return pass;
default:
throw new IllegalArgumentException();
}
});
return (boolean)xpath.evaluate(expression, doc, XPathConstants.BOOLEAN);
}
'Secure Coding' 카테고리의 다른 글
HTTP 응답 헤더 인젝션 (0) | 2021.09.17 |
---|---|
OS 명령 삽입 공격 (0) | 2021.09.17 |
데이터베이스에 연결 시 암호 보안 (0) | 2021.09.17 |
데이터베이스 쿼리 인젝션 공격 (0) | 2021.09.17 |
Reflected cross-site scripting (XSS) 공격 (0) | 2021.09.17 |
댓글