2020-06-30 12:48:07 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== 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);
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-09-17 13:50:03 +02:00
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|