|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the log libraries. |
| 3 | + */ |
| 4 | + |
| 5 | +private import python |
| 6 | +private import semmle.python.dataflow.new.DataFlow |
| 7 | +private import semmle.python.dataflow.new.TaintTracking |
| 8 | +private import semmle.python.dataflow.new.RemoteFlowSources |
| 9 | +private import experimental.semmle.python.Concepts |
| 10 | +private import semmle.python.frameworks.Flask |
| 11 | +private import semmle.python.ApiGraphs |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides models for Python's log-related libraries. |
| 15 | + */ |
| 16 | +private module log { |
| 17 | + /** |
| 18 | + * Log output method list. |
| 19 | + * |
| 20 | + * See https://docs.python.org/3/library/logging.html#logger-objects |
| 21 | + */ |
| 22 | + private class LogOutputMethods extends string { |
| 23 | + LogOutputMethods() { |
| 24 | + this in ["info", "error", "warn", "warning", "debug", "critical", "exception", "log"] |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * The class used to find the log output method of the `logging` module. |
| 30 | + * |
| 31 | + * See `LogOutputMethods` |
| 32 | + */ |
| 33 | + private class LoggingCall extends DataFlow::CallCfgNode, LogOutput::Range { |
| 34 | + LoggingCall() { |
| 35 | + this = API::moduleImport("logging").getMember(any(LogOutputMethods m)).getACall() |
| 36 | + } |
| 37 | + |
| 38 | + override DataFlow::Node getAnInput() { |
| 39 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and |
| 40 | + result in [this.getArg(_), this.getArgByName(_)] // this includes the arg named "msg" |
| 41 | + or |
| 42 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and |
| 43 | + result in [this.getArg(any(int i | i > 0)), this.getArgByName(any(string s | s != "level"))] |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The class used to find log output methods related to the `logging.getLogger` instance. |
| 49 | + * |
| 50 | + * See `LogOutputMethods` |
| 51 | + */ |
| 52 | + private class LoggerCall extends DataFlow::CallCfgNode, LogOutput::Range { |
| 53 | + LoggerCall() { |
| 54 | + this = |
| 55 | + API::moduleImport("logging") |
| 56 | + .getMember("getLogger") |
| 57 | + .getReturn() |
| 58 | + .getMember(any(LogOutputMethods m)) |
| 59 | + .getACall() |
| 60 | + } |
| 61 | + |
| 62 | + override DataFlow::Node getAnInput() { |
| 63 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and |
| 64 | + result in [this.getArg(_), this.getArgByName(_)] // this includes the arg named "msg" |
| 65 | + or |
| 66 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and |
| 67 | + result in [this.getArg(any(int i | i > 0)), this.getArgByName(any(string s | s != "level"))] |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * The class used to find the relevant log output method of the `flask.Flask.logger` instance (flask application). |
| 73 | + * |
| 74 | + * See `LogOutputMethods` |
| 75 | + */ |
| 76 | + private class FlaskLoggingCall extends DataFlow::CallCfgNode, LogOutput::Range { |
| 77 | + FlaskLoggingCall() { |
| 78 | + this = |
| 79 | + Flask::FlaskApp::instance() |
| 80 | + .getMember("logger") |
| 81 | + .getMember(any(LogOutputMethods m)) |
| 82 | + .getACall() |
| 83 | + } |
| 84 | + |
| 85 | + override DataFlow::Node getAnInput() { |
| 86 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and |
| 87 | + result in [this.getArg(_), this.getArgByName(_)] // this includes the arg named "msg" |
| 88 | + or |
| 89 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and |
| 90 | + result in [this.getArg(any(int i | i > 0)), this.getArgByName(any(string s | s != "level"))] |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * The class used to find the relevant log output method of the `django.utils.log.request_logger` instance (django application). |
| 96 | + * |
| 97 | + * See `LogOutputMethods` |
| 98 | + */ |
| 99 | + private class DjangoLoggingCall extends DataFlow::CallCfgNode, LogOutput::Range { |
| 100 | + DjangoLoggingCall() { |
| 101 | + this = |
| 102 | + API::moduleImport("django") |
| 103 | + .getMember("utils") |
| 104 | + .getMember("log") |
| 105 | + .getMember("request_logger") |
| 106 | + .getMember(any(LogOutputMethods m)) |
| 107 | + .getACall() |
| 108 | + } |
| 109 | + |
| 110 | + override DataFlow::Node getAnInput() { |
| 111 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and |
| 112 | + result in [this.getArg(_), this.getArgByName(_)] // this includes the arg named "msg" |
| 113 | + or |
| 114 | + this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and |
| 115 | + result in [this.getArg(any(int i | i > 0)), this.getArgByName(any(string s | s != "level"))] |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments