|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `csv` PyPI package. |
| 3 | + * See https://docs.python.org/3/library/csv.html |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import semmle.python.ApiGraphs |
| 8 | +private import experimental.semmle.python.Concepts |
| 9 | + |
| 10 | +/** |
| 11 | + * Provides models for the `csv` PyPI package. |
| 12 | + * |
| 13 | + * See |
| 14 | + * - https://docs.python.org/3/library/csv.html |
| 15 | + */ |
| 16 | +private module Csv { |
| 17 | + private module Writer { |
| 18 | + abstract class InstanceSource extends DataFlow::LocalSourceNode { } |
| 19 | + |
| 20 | + /** A direct instantiation of `csv.writer` or `csv.DictWriter`. */ |
| 21 | + private class ClassInstantiation extends InstanceSource, DataFlow::CfgNode { |
| 22 | + ClassInstantiation() { |
| 23 | + this = API::moduleImport("csv").getMember(["writer", "DictWriter"]).getACall() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** Gets a reference to an `csv.writer` or `csv.DictWriter` instance. */ |
| 28 | + private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) { |
| 29 | + t.start() and |
| 30 | + result instanceof InstanceSource |
| 31 | + or |
| 32 | + exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t)) |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets a reference to an `csv.writer` or `csv.DictWriter` instance. */ |
| 36 | + DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) } |
| 37 | + |
| 38 | + /** |
| 39 | + * See: |
| 40 | + * - https://docs.python.org/3/library/csv.html#csvwriter.writerow |
| 41 | + * - https://docs.python.org/3/library/csv.html#csvwriter.writerows |
| 42 | + */ |
| 43 | + private class CsvWriteCall extends CsvWriter::Range, DataFlow::CallCfgNode { |
| 44 | + string methodName; |
| 45 | + |
| 46 | + CsvWriteCall() { |
| 47 | + methodName in ["writerow", "writerows"] and |
| 48 | + this.(DataFlow::MethodCallNode).calls(Writer::instance(), methodName) |
| 49 | + } |
| 50 | + |
| 51 | + override DataFlow::Node getAnInput() { |
| 52 | + result = this.getArg(0) |
| 53 | + or |
| 54 | + methodName = "writerow" and |
| 55 | + result = this.getArgByName("row") |
| 56 | + or |
| 57 | + methodName = "writerows" and |
| 58 | + result = this.getArgByName("rows") |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * See: https://docs.python.org/3/library/csv.html#csv.DictWriter |
| 64 | + */ |
| 65 | + private class DictWriterInstance extends CsvWriter::Range, DataFlow::CallCfgNode { |
| 66 | + DictWriterInstance() { this = API::moduleImport("csv").getMember("DictWriter").getACall() } |
| 67 | + |
| 68 | + override DataFlow::Node getAnInput() { |
| 69 | + result in [this.getArg(1), this.getArgByName("fieldnames")] |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments