|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `lxml` PyPI package. |
| 3 | + * |
| 4 | + * See |
| 5 | + * - https://pypi.org/project/lxml/ |
| 6 | + * - https://lxml.de/tutorial.html |
| 7 | + */ |
| 8 | + |
| 9 | +private import python |
| 10 | +private import semmle.python.dataflow.new.DataFlow |
| 11 | +private import semmle.python.Concepts |
| 12 | +private import semmle.python.ApiGraphs |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides classes modeling security-relevant aspects of the `lxml` PyPI package |
| 16 | + * |
| 17 | + * See |
| 18 | + * - https://pypi.org/project/lxml/ |
| 19 | + * - https://lxml.de/tutorial.html |
| 20 | + */ |
| 21 | +private module Lxml { |
| 22 | + /** |
| 23 | + * A class constructor compiling an XPath expression. |
| 24 | + * |
| 25 | + * from lxml import etree |
| 26 | + * root = etree.XML("<xmlContent>") |
| 27 | + * find_text = etree.XPath("`sink`") |
| 28 | + * find_text = etree.ETXPath("`sink`") |
| 29 | + * |
| 30 | + * See |
| 31 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.XPath |
| 32 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.ETXPath |
| 33 | + */ |
| 34 | + private class XPathClassCall extends XPathConstruction::Range, DataFlow::CallCfgNode { |
| 35 | + XPathClassCall() { |
| 36 | + this = API::moduleImport("lxml").getMember("etree").getMember(["XPath", "ETXPath"]).getACall() |
| 37 | + } |
| 38 | + |
| 39 | + override DataFlow::Node getXPath() { result in [this.getArg(0), this.getArgByName("path")] } |
| 40 | + |
| 41 | + override string getName() { result = "Lxml.etree" } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * A call to the `xpath` method of a parsed document. |
| 46 | + * |
| 47 | + * from lxml import etree |
| 48 | + * root = etree.fromstring(file(XML_DB).read(), XMLParser()) |
| 49 | + * find_text = root.xpath("`sink`") |
| 50 | + * |
| 51 | + * See https://lxml.de/apidoc/lxml.etree.html#lxml.etree._ElementTree.xpath |
| 52 | + * as well as |
| 53 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.parse |
| 54 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.fromstring |
| 55 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.fromstringlist |
| 56 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.HTML |
| 57 | + * - https://lxml.de/apidoc/lxml.etree.html#lxml.etree.XML |
| 58 | + */ |
| 59 | + class XPathCall extends XPathExecution::Range, DataFlow::CallCfgNode { |
| 60 | + XPathCall() { |
| 61 | + this = |
| 62 | + API::moduleImport("lxml") |
| 63 | + .getMember("etree") |
| 64 | + .getMember(["parse", "fromstring", "fromstringlist", "HTML", "XML"]) |
| 65 | + .getReturn() |
| 66 | + .getMember("xpath") |
| 67 | + .getACall() |
| 68 | + } |
| 69 | + |
| 70 | + override DataFlow::Node getXPath() { result in [this.getArg(0), this.getArgByName("_path")] } |
| 71 | + |
| 72 | + // TODO: implement when we get call nodes |
| 73 | + override DataFlow::Node getTree() { none() } |
| 74 | + |
| 75 | + override string getName() { result = "Lxml.etree" } |
| 76 | + } |
| 77 | +} |
0 commit comments