|
| 1 | +import java.io.ByteArrayInputStream; |
| 2 | +import java.io.StringReader; |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import javax.servlet.http.HttpServletRequest; |
| 6 | +import javax.xml.namespace.QName; |
| 7 | +import javax.xml.parsers.DocumentBuilder; |
| 8 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 9 | +import javax.xml.xpath.XPath; |
| 10 | +import javax.xml.xpath.XPathConstants; |
| 11 | +import javax.xml.xpath.XPathExpression; |
| 12 | +import javax.xml.xpath.XPathExpressionException; |
| 13 | +import javax.xml.xpath.XPathFactory; |
| 14 | + |
| 15 | +import org.jaxen.pattern.Pattern; |
| 16 | +import org.dom4j.DocumentFactory; |
| 17 | +import org.dom4j.DocumentHelper; |
| 18 | +import org.dom4j.Namespace; |
| 19 | +import org.dom4j.Node; |
| 20 | +import org.dom4j.io.SAXReader; |
| 21 | +import org.dom4j.util.ProxyDocumentFactory; |
| 22 | +import org.dom4j.xpath.DefaultXPath; |
| 23 | +import org.dom4j.xpath.XPathPattern; |
| 24 | +import org.w3c.dom.Document; |
| 25 | +import org.xml.sax.InputSource; |
| 26 | + |
| 27 | +public class XPathInjectionTest { |
| 28 | + private static abstract class XPathImplStub implements XPath { |
| 29 | + public static XPathImplStub getInstance() { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public XPathExpression compile(String expression) throws XPathExpressionException { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Object evaluate(String expression, Object item, QName returnType) throws XPathExpressionException { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String evaluate(String expression, Object item) throws XPathExpressionException { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object evaluate(String expression, InputSource source, QName returnType) |
| 50 | + throws XPathExpressionException { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String evaluate(String expression, InputSource source) throws XPathExpressionException { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + private static class ProxyDocumentFactoryStub extends ProxyDocumentFactory { |
| 62 | + } |
| 63 | + |
| 64 | + private static class PatternStub extends Pattern { |
| 65 | + private String text; |
| 66 | + |
| 67 | + PatternStub(String text) { |
| 68 | + this.text = text; |
| 69 | + } |
| 70 | + |
| 71 | + public String getText() { |
| 72 | + return text; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void handle(HttpServletRequest request) throws Exception { |
| 77 | + String user = request.getParameter("user"); |
| 78 | + String pass = request.getParameter("pass"); |
| 79 | + String expression = "/users/user[@name='" + user + "' and @pass='" + pass + "']"; |
| 80 | + |
| 81 | + final String xmlStr = "<users>" + " <user name=\"aaa\" pass=\"pass1\"></user>" |
| 82 | + + " <user name=\"bbb\" pass=\"pass2\"></user>" + "</users>"; |
| 83 | + DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); |
| 84 | + DocumentBuilder builder = domFactory.newDocumentBuilder(); |
| 85 | + InputSource xmlSource = new InputSource(new StringReader(xmlStr)); |
| 86 | + Document doc = builder.parse(xmlSource); |
| 87 | + |
| 88 | + XPathFactory factory = XPathFactory.newInstance(); |
| 89 | + XPath xpath = factory.newXPath(); |
| 90 | + |
| 91 | + xpath.evaluate(expression, doc, XPathConstants.BOOLEAN); // $hasXPathInjection |
| 92 | + xpath.evaluateExpression(expression, xmlSource); // $hasXPathInjection |
| 93 | + xpath.compile("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 94 | + |
| 95 | + XPathImplStub xpathStub = XPathImplStub.getInstance(); |
| 96 | + xpathStub.evaluate(expression, doc, XPathConstants.BOOLEAN); // $hasXPathInjection |
| 97 | + xpathStub.evaluateExpression(expression, xmlSource); // $hasXPathInjection |
| 98 | + xpathStub.compile("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 99 | + |
| 100 | + StringBuffer sb = new StringBuffer("/users/user[@name="); |
| 101 | + sb.append(user); |
| 102 | + sb.append("' and @pass='"); |
| 103 | + sb.append(pass); |
| 104 | + sb.append("']"); |
| 105 | + String query = sb.toString(); |
| 106 | + |
| 107 | + xpath.compile(query); // $hasXPathInjection |
| 108 | + xpathStub.compile(query); // $hasXPathInjection |
| 109 | + |
| 110 | + String expression4 = "/users/user[@name=$user and @pass=$pass]"; |
| 111 | + xpath.setXPathVariableResolver(v -> { |
| 112 | + switch (v.getLocalPart()) { |
| 113 | + case "user": |
| 114 | + return user; |
| 115 | + case "pass": |
| 116 | + return pass; |
| 117 | + default: |
| 118 | + throw new IllegalArgumentException(); |
| 119 | + } |
| 120 | + }); |
| 121 | + xpath.evaluate(expression4, doc, XPathConstants.BOOLEAN); // Safe |
| 122 | + |
| 123 | + SAXReader reader = new SAXReader(); |
| 124 | + org.dom4j.Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes())); |
| 125 | + document.selectObject("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 126 | + document.selectNodes("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 127 | + document.selectNodes("/users/user[@name='test']", "/users/user[@pass='" + pass + "']"); // $hasXPathInjection |
| 128 | + document.selectSingleNode("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 129 | + document.valueOf("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 130 | + document.numberValueOf("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 131 | + document.matches("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 132 | + document.createXPath("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 133 | + |
| 134 | + new DefaultXPath("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 135 | + new XPathPattern("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 136 | + new XPathPattern(new PatternStub(user)); // $ MISSING: hasXPathInjection // Jaxen is not modeled yet |
| 137 | + |
| 138 | + DocumentFactory docFactory = DocumentFactory.getInstance(); |
| 139 | + docFactory.createPattern("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 140 | + docFactory.createXPath("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 141 | + docFactory.createXPathFilter("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 142 | + |
| 143 | + DocumentHelper.createPattern("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 144 | + DocumentHelper.createXPath("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 145 | + DocumentHelper.createXPathFilter("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 146 | + DocumentHelper.selectNodes("/users/user[@name='" + user + "' and @pass='" + pass + "']", new ArrayList<Node>()); // $hasXPathInjection |
| 147 | + DocumentHelper.sort(new ArrayList<Node>(), "/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 148 | + |
| 149 | + ProxyDocumentFactoryStub proxyDocFactory = new ProxyDocumentFactoryStub(); |
| 150 | + proxyDocFactory.createPattern("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 151 | + proxyDocFactory.createXPath("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 152 | + proxyDocFactory.createXPathFilter("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 153 | + |
| 154 | + Namespace namespace = new Namespace("prefix", "http://some.uri.io"); |
| 155 | + namespace.createPattern("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 156 | + namespace.createXPathFilter("/users/user[@name='" + user + "' and @pass='" + pass + "']"); // $hasXPathInjection |
| 157 | + |
| 158 | + org.jaxen.SimpleVariableContext svc = new org.jaxen.SimpleVariableContext(); |
| 159 | + svc.setVariableValue("user", user); |
| 160 | + svc.setVariableValue("pass", pass); |
| 161 | + String xpathString = "/users/user[@name=$user and @pass=$pass]"; |
| 162 | + org.dom4j.XPath safeXPath = document.createXPath(xpathString); // Safe |
| 163 | + safeXPath.setVariableContext(svc); |
| 164 | + safeXPath.selectSingleNode(document); // Safe |
| 165 | + } |
| 166 | +} |
0 commit comments