|
| 1 | +/** |
| 2 | + * Provides the sources and taint-flow for HTTP servers defined using the standard library (stdlib). |
| 3 | + * Specifically, we model `HttpRequestTaintSource`s from instances of `BaseHTTPRequestHandler` |
| 4 | + * (or subclasses) and form parsing using `cgi.FieldStorage`. |
| 5 | + */ |
| 6 | +import python |
| 7 | +import semmle.python.security.TaintTracking |
| 8 | +import semmle.python.web.Http |
| 9 | + |
| 10 | +/** Source of BaseHTTPRequestHandler instances. */ |
| 11 | +class StdLibRequestSource extends HttpRequestTaintSource { |
| 12 | + StdLibRequestSource() { |
| 13 | + exists(ClassValue cls | |
| 14 | + cls.getABaseType+() = Value::named("BaseHTTPServer.BaseHTTPRequestHandler") |
| 15 | + or |
| 16 | + cls.getABaseType+() = Value::named("http.server.BaseHTTPRequestHandler") |
| 17 | + | |
| 18 | + this.(ControlFlowNode).pointsTo().getClass() = cls |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + override predicate isSourceOf(TaintKind kind) { kind instanceof BaseHTTPRequestHandlerKind } |
| 23 | +} |
| 24 | + |
| 25 | +/** TaintKind for an instance of BaseHTTPRequestHandler. */ |
| 26 | +class BaseHTTPRequestHandlerKind extends TaintKind { |
| 27 | + BaseHTTPRequestHandlerKind() { this = "BaseHTTPRequestHandlerKind" } |
| 28 | + |
| 29 | + override TaintKind getTaintOfAttribute(string name) { |
| 30 | + name in ["requestline", "path"] and |
| 31 | + result instanceof ExternalStringKind |
| 32 | + or |
| 33 | + name = "headers" and |
| 34 | + result instanceof HTTPMessageKind |
| 35 | + or |
| 36 | + name = "rfile" and |
| 37 | + result instanceof ExternalFileObject |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** TaintKind for headers (instance of HTTPMessage). */ |
| 42 | +class HTTPMessageKind extends ExternalStringDictKind { |
| 43 | + override TaintKind getTaintOfMethodResult(string name) { |
| 44 | + result = super.getTaintOfMethodResult(name) |
| 45 | + or |
| 46 | + name = "get_all" and |
| 47 | + result.(SequenceKind).getItem() = this.getValue() |
| 48 | + or |
| 49 | + name in ["as_bytes", "as_string"] and |
| 50 | + result instanceof ExternalStringKind |
| 51 | + } |
| 52 | + |
| 53 | + override TaintKind getTaintForFlowStep(ControlFlowNode fromnode, ControlFlowNode tonode) { |
| 54 | + result = super.getTaintForFlowStep(fromnode, tonode) |
| 55 | + or |
| 56 | + exists(ClassValue cls | cls = ClassValue::unicode() or cls = ClassValue::bytes() | |
| 57 | + tonode = cls.getACall() and |
| 58 | + tonode.(CallNode).getArg(0) = fromnode and |
| 59 | + result instanceof ExternalStringKind |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** Source of parsed HTTP forms (by using the `cgi` module). */ |
| 65 | +class CgiFieldStorageSource extends HttpRequestTaintSource { |
| 66 | + CgiFieldStorageSource() { this = Value::named("cgi.FieldStorage").getACall() } |
| 67 | + |
| 68 | + override predicate isSourceOf(TaintKind kind) { kind instanceof CgiFieldStorageFormKind } |
| 69 | +} |
| 70 | + |
| 71 | +/** TaintKind for a parsed HTTP form. */ |
| 72 | +class CgiFieldStorageFormKind extends TaintKind { |
| 73 | + /* |
| 74 | + * There is a slight difference between how we model form/fields and how it is handled by the code. |
| 75 | + * In the code |
| 76 | + * ``` |
| 77 | + * form = cgi.FieldStorage() |
| 78 | + * field = form['myfield'] |
| 79 | + * ``` |
| 80 | + * both `form` and `field` have the type `cgi.FieldStorage`. This allows the code to represent |
| 81 | + * nested forms as `form['nested_form']['myfield']`. However, since HTML forms can't be nested |
| 82 | + * we ignore that detail since it allows for a more clean modeling. |
| 83 | + */ |
| 84 | + CgiFieldStorageFormKind() { this = "CgiFieldStorageFormKind" } |
| 85 | + |
| 86 | + override TaintKind getTaintOfAttribute(string name) { |
| 87 | + name = "value" and result.(SequenceKind).getItem() instanceof CgiFieldStorageFieldKind |
| 88 | + } |
| 89 | + |
| 90 | + override TaintKind getTaintOfMethodResult(string name) { |
| 91 | + name = "getvalue" and |
| 92 | + ( |
| 93 | + result instanceof ExternalStringKind |
| 94 | + or |
| 95 | + result.(SequenceKind).getItem() instanceof ExternalStringKind |
| 96 | + ) |
| 97 | + or |
| 98 | + name = "getfirst" and |
| 99 | + result instanceof ExternalStringKind |
| 100 | + or |
| 101 | + name = "getlist" and |
| 102 | + result.(SequenceKind).getItem() instanceof ExternalStringKind |
| 103 | + } |
| 104 | + |
| 105 | + override TaintKind getTaintForFlowStep(ControlFlowNode fromnode, ControlFlowNode tonode) { |
| 106 | + tonode.(SubscriptNode).getObject() = fromnode and |
| 107 | + ( |
| 108 | + result instanceof CgiFieldStorageFieldKind |
| 109 | + or |
| 110 | + result.(SequenceKind).getItem() instanceof CgiFieldStorageFieldKind |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** TaintKind for the field of a parsed HTTP form. */ |
| 116 | +class CgiFieldStorageFieldKind extends TaintKind { |
| 117 | + CgiFieldStorageFieldKind() { this = "CgiFieldStorageFieldKind" } |
| 118 | + |
| 119 | + override TaintKind getTaintOfAttribute(string name) { |
| 120 | + name in ["filename", "value"] and result instanceof ExternalStringKind |
| 121 | + or |
| 122 | + name = "file" and result instanceof ExternalFileObject |
| 123 | + } |
| 124 | +} |
0 commit comments