|
| 1 | +private import python |
| 2 | +private import semmle.python.dataflow.new.DataFlow |
| 3 | +private import semmle.python.ApiGraphs |
| 4 | + |
| 5 | +/** |
| 6 | + * A data-flow node that constructs a XSLT transformer. |
| 7 | + * |
| 8 | + * Extend this class to refine existing API models. If you want to model new APIs, |
| 9 | + * extend `TemplateConstruction::Range` instead. |
| 10 | + */ |
| 11 | +class XsltConstruction extends DataFlow::Node instanceof XsltConstruction::Range { |
| 12 | + /** Gets the argument that specifies the XSLT transformer. */ |
| 13 | + DataFlow::Node getXsltArg() { result = super.getXsltArg() } |
| 14 | +} |
| 15 | + |
| 16 | +/** Provides a class for modeling new system-command execution APIs. */ |
| 17 | +module XsltConstruction { |
| 18 | + /** |
| 19 | + * A data-flow node that constructs a XSLT transformer. |
| 20 | + * |
| 21 | + * Extend this class to model new APIs. If you want to refine existing API models, |
| 22 | + * extend `XsltConstruction` instead. |
| 23 | + */ |
| 24 | + abstract class Range extends DataFlow::Node { |
| 25 | + /** Gets the argument that specifies the XSLT transformer. */ |
| 26 | + abstract DataFlow::Node getXsltArg(); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A data-flow node that executes a XSLT transformer. |
| 32 | + * |
| 33 | + * Extend this class to refine existing API models. If you want to model new APIs, |
| 34 | + * extend `TemplateConstruction::Range` instead. |
| 35 | + */ |
| 36 | +class XsltExecution extends DataFlow::Node instanceof XsltExecution::Range { |
| 37 | + /** Gets the argument that specifies the XSLT transformer. */ |
| 38 | + DataFlow::Node getXsltArg() { result = super.getXsltArg() } |
| 39 | +} |
| 40 | + |
| 41 | +/** Provides a class for modeling new system-command execution APIs. */ |
| 42 | +module XsltExecution { |
| 43 | + /** |
| 44 | + * A data-flow node that executes a XSLT transformer. |
| 45 | + * |
| 46 | + * Extend this class to model new APIs. If you want to refine existing API models, |
| 47 | + * extend `XsltExecution` instead. |
| 48 | + */ |
| 49 | + abstract class Range extends DataFlow::Node { |
| 50 | + /** Gets the argument that specifies the XSLT transformer. */ |
| 51 | + abstract DataFlow::Node getXsltArg(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ----------------------------------------------------------------------------- |
| 56 | +/** |
| 57 | + * A call to `lxml.etree.XSLT`. |
| 58 | + * |
| 59 | + * ```py |
| 60 | + * from lxml import etree |
| 61 | + * xslt_tree = etree.parse(...) |
| 62 | + * doc = etree.parse(...) |
| 63 | + * transform = etree.XSLT(xslt_tree) |
| 64 | + * result = transform(doc) |
| 65 | + * ``` |
| 66 | + */ |
| 67 | +class LxmlEtreeXsltCall extends XsltConstruction::Range, API::CallNode { |
| 68 | + LxmlEtreeXsltCall() { |
| 69 | + this = API::moduleImport("lxml").getMember("etree").getMember("XSLT").getACall() |
| 70 | + } |
| 71 | + |
| 72 | + override DataFlow::Node getXsltArg() { result = this.getParameter(0, "xslt_input").asSink() } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * A call to `.xslt` on an lxml ElementTree object. |
| 77 | + * |
| 78 | + * ```py |
| 79 | + * from lxml import etree |
| 80 | + * xslt_tree = etree.parse(...) |
| 81 | + * doc = etree.parse(...) |
| 82 | + * result = doc.xslt(xslt_tree) |
| 83 | + * ``` |
| 84 | + */ |
| 85 | +class XsltAttributeCall extends XsltExecution::Range, API::CallNode { |
| 86 | + XsltAttributeCall() { this = elementTreeConstruction(_).getReturn().getMember("xslt").getACall() } |
| 87 | + |
| 88 | + override DataFlow::Node getXsltArg() { result = this.getParameter(0, "_xslt").asSink() } |
| 89 | +} |
| 90 | + |
| 91 | +// ----------------------------------------------------------------------------- |
| 92 | +API::CallNode elementTreeConstruction(DataFlow::Node inputArg) { |
| 93 | + // TODO: If we could, would be nice to model this as flow-summaries. But I'm not sure if we actually can :thinking: |
| 94 | + // see https://lxml.de/api/lxml.etree-module.html#fromstring |
| 95 | + result = API::moduleImport("lxml").getMember("etree").getMember("fromstring").getACall() and |
| 96 | + inputArg = result.getParameter(0, "text").asSink() |
| 97 | + or |
| 98 | + // see https://lxml.de/api/lxml.etree-module.html#fromstringlist |
| 99 | + result = API::moduleImport("lxml").getMember("etree").getMember("fromstringlist").getACall() and |
| 100 | + inputArg = result.getParameter(0, "strings").asSink() |
| 101 | + or |
| 102 | + // TODO: technically we should treat parse differently, since it takes a file as argument |
| 103 | + // see https://lxml.de/api/lxml.etree-module.html#parse |
| 104 | + result = API::moduleImport("lxml").getMember("etree").getMember("parse").getACall() and |
| 105 | + inputArg = result.getParameter(0, "source").asSink() |
| 106 | + or |
| 107 | + // see https://lxml.de/api/lxml.etree-module.html#XML |
| 108 | + result = API::moduleImport("lxml").getMember("etree").getMember("XML").getACall() and |
| 109 | + inputArg = result.getParameter(0, "text").asSink() |
| 110 | +} |
0 commit comments