|
| 1 | +import python |
| 2 | +private import semmle.python.web.Http |
| 3 | + |
| 4 | +ClassValue httpConnectionClass() { |
| 5 | + // Python 2 |
| 6 | + result = Value::named("httplib.HTTPConnection") |
| 7 | + or |
| 8 | + result = Value::named("httplib.HTTPSConnection") |
| 9 | + or |
| 10 | + // Python 3 |
| 11 | + result = Value::named("http.client.HTTPConnection") |
| 12 | + or |
| 13 | + result = Value::named("http.client.HTTPSConnection") |
| 14 | + or |
| 15 | + // six |
| 16 | + result = Value::named("six.moves.http_client.HTTPConnection") |
| 17 | + or |
| 18 | + result = Value::named("six.moves.http_client.HTTPSConnection") |
| 19 | +} |
| 20 | + |
| 21 | +class HttpConnectionHttpRequest extends Client::HttpRequest, CallNode { |
| 22 | + CallNode constructor_call; |
| 23 | + CallableValue func; |
| 24 | + |
| 25 | + HttpConnectionHttpRequest() { |
| 26 | + exists(ClassValue cls, AttrNode call_origin, Value constructor_call_value | |
| 27 | + cls = httpConnectionClass() and |
| 28 | + func = cls.lookup("request") and |
| 29 | + this = func.getACall() and |
| 30 | + // since you can do `r = conn.request; r('GET', path)`, we need to find the origin |
| 31 | + this.getFunction().pointsTo(_, _, call_origin) and |
| 32 | + // Since HTTPSConnection is a subtype of HTTPConnection, up until this point, `cls` could be either class, |
| 33 | + // because `HTTPSConnection.request == HTTPConnection.request`. To avoid generating 2 results, we filter |
| 34 | + // on the actual class used as the constructor |
| 35 | + call_origin.getObject().pointsTo(_, constructor_call_value, constructor_call) and |
| 36 | + cls = constructor_call_value.getClass() and |
| 37 | + constructor_call = cls.getACall() |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + override ControlFlowNode getAUrlPart() { |
| 42 | + result = func.getNamedArgumentForCall(this, "url") |
| 43 | + or |
| 44 | + result = constructor_call.getArg(0) |
| 45 | + or |
| 46 | + result = constructor_call.getArgByName("host") |
| 47 | + } |
| 48 | + |
| 49 | + override string getMethodUpper() { |
| 50 | + exists(string method | |
| 51 | + result = method.toUpperCase() and |
| 52 | + func.getNamedArgumentForCall(this, "method").pointsTo(Value::forString(method)) |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
0 commit comments