|
| 1 | +/** |
| 2 | + * Provides class and predicates to track external data that |
| 3 | + * may represent malicious XSLT query objects. |
| 4 | + * |
| 5 | + * This module is intended to be imported into a taint-tracking query |
| 6 | + * to extend `TaintKind` and `TaintSink`. |
| 7 | + */ |
| 8 | + |
| 9 | +import python |
| 10 | +import semmle.python.dataflow.TaintTracking |
| 11 | +import semmle.python.web.HttpRequest |
| 12 | + |
| 13 | +/** Models XSLT Injection related classes and functions */ |
| 14 | +module XSLTInjection { |
| 15 | + /** Returns a class value which refers to `lxml.etree` */ |
| 16 | + Value etree() { result = Value::named("lxml.etree") } |
| 17 | + |
| 18 | + /** A generic taint sink that is vulnerable to XSLT injection. */ |
| 19 | + abstract class XSLTInjectionSink extends TaintSink { } |
| 20 | + |
| 21 | + /** |
| 22 | + * A kind of "taint", representing an untrusted XML string |
| 23 | + */ |
| 24 | + private class ExternalXmlStringKind extends ExternalStringKind { |
| 25 | + ExternalXmlStringKind() { this = "etree.XML string" } |
| 26 | + |
| 27 | + override TaintKind getTaintForFlowStep(ControlFlowNode fromnode, ControlFlowNode tonode) { |
| 28 | + etreeXML(fromnode, tonode) and result instanceof ExternalXmlKind |
| 29 | + or |
| 30 | + etreeFromStringList(fromnode, tonode) and result instanceof ExternalXmlKind |
| 31 | + or |
| 32 | + etreeFromString(fromnode, tonode) and result instanceof ExternalXmlKind |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * A kind of "taint", representing a XML encoded string |
| 38 | + */ |
| 39 | + class ExternalXmlKind extends TaintKind { |
| 40 | + ExternalXmlKind() { this = "lxml etree xml" } |
| 41 | + } |
| 42 | + |
| 43 | + private predicate etreeXML(ControlFlowNode fromnode, CallNode tonode) { |
| 44 | + // etree.XML("<xmlContent>") |
| 45 | + exists(CallNode call | call.getFunction().(AttrNode).getObject("XML").pointsTo(etree()) | |
| 46 | + call.getArg(0) = fromnode and |
| 47 | + call = tonode |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + private predicate etreeFromString(ControlFlowNode fromnode, CallNode tonode) { |
| 52 | + // etree.fromstring(text, parser=None) |
| 53 | + exists(CallNode call | call.getFunction().(AttrNode).getObject("fromstring").pointsTo(etree()) | |
| 54 | + call.getArg(0) = fromnode and |
| 55 | + call = tonode |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + private predicate etreeFromStringList(ControlFlowNode fromnode, CallNode tonode) { |
| 60 | + // etree.fromstringlist(strings, parser=None) |
| 61 | + exists(CallNode call | |
| 62 | + call.getFunction().(AttrNode).getObject("fromstringlist").pointsTo(etree()) |
| 63 | + | |
| 64 | + call.getArg(0) = fromnode and |
| 65 | + call = tonode |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * A Sink representing an argument to the `etree.XSLT` call. |
| 71 | + * |
| 72 | + * from lxml import etree |
| 73 | + * root = etree.XML("<xmlContent>") |
| 74 | + * find_text = etree.XSLT("`sink`") |
| 75 | + */ |
| 76 | + private class EtreeXSLTArgument extends XSLTInjectionSink { |
| 77 | + override string toString() { result = "lxml.etree.XSLT" } |
| 78 | + |
| 79 | + EtreeXSLTArgument() { |
| 80 | + exists(CallNode call | call.getFunction().(AttrNode).getObject("XSLT").pointsTo(etree()) | |
| 81 | + call.getArg(0) = this |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + override predicate sinks(TaintKind kind) { kind instanceof ExternalXmlKind } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * A Sink representing an argument to the `XSLT` call to a parsed xml document. |
| 90 | + * |
| 91 | + * from lxml import etree |
| 92 | + * from io import StringIO |
| 93 | + * `sink` = etree.XML(xsltQuery) |
| 94 | + * tree = etree.parse(f) |
| 95 | + * result_tree = tree.xslt(`sink`) |
| 96 | + */ |
| 97 | + private class ParseXSLTArgument extends XSLTInjectionSink { |
| 98 | + override string toString() { result = "lxml.etree.parse.xslt" } |
| 99 | + |
| 100 | + ParseXSLTArgument() { |
| 101 | + exists( |
| 102 | + CallNode parseCall, CallNode xsltCall, ControlFlowNode obj, Variable var, AssignStmt assign |
| 103 | + | |
| 104 | + parseCall.getFunction().(AttrNode).getObject("parse").pointsTo(etree()) and |
| 105 | + assign.getValue().(Call).getAFlowNode() = parseCall and |
| 106 | + xsltCall.getFunction().(AttrNode).getObject("xslt") = obj and |
| 107 | + var.getAUse() = obj and |
| 108 | + assign.getATarget() = var.getAStore() and |
| 109 | + xsltCall.getArg(0) = this |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + override predicate sinks(TaintKind kind) { kind instanceof ExternalXmlKind } |
| 114 | + } |
| 115 | +} |
0 commit comments