-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPath-Injection.java
More file actions
42 lines (32 loc) · 1.7 KB
/
XPath-Injection.java
File metadata and controls
42 lines (32 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
취약점 개요
XPath Injection
URL 매개변수와 같은 사용자 제공 데이터는 항상 신뢰할 수 없고 오염된 것으로 간주되어야 합니다.
오염된 데이터에서 직접 XPath 표현식을 구성하면 공격자가 표현식 자체의 초기 의미를 변경하는 특수하게 조작된 값을 주입할 수 있습니다. 성공적인 XPath 주입 공격은 XML 문서에서 중요한 정보를 읽을 수 있습니다.
*/
// 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);
}