|
| 1 | +import python |
| 2 | + |
| 3 | + |
| 4 | +import semmle.python.security.TaintTracking |
| 5 | +import semmle.python.security.strings.Untrusted |
| 6 | +import semmle.python.web.Http |
| 7 | +import semmle.python.web.bottle.General |
| 8 | + |
| 9 | +private Object theBottleRequestObject() { |
| 10 | + result = theBottleModule().getAttribute("request") |
| 11 | +} |
| 12 | + |
| 13 | +class BottleRequestKind extends TaintKind { |
| 14 | + |
| 15 | + BottleRequestKind() { |
| 16 | + this = "bottle.request" |
| 17 | + } |
| 18 | + |
| 19 | + override TaintKind getTaintOfAttribute(string name) { |
| 20 | + result instanceof BottleFormsDict and |
| 21 | + (name = "cookies" or name = "query" or name = "form") |
| 22 | + or |
| 23 | + result instanceof UntrustedStringKind and |
| 24 | + (name = "query_string" or name = "url_args") |
| 25 | + or |
| 26 | + result.(DictKind).getValue() instanceof FileUpload and |
| 27 | + name = "files" |
| 28 | + } |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +private class RequestSource extends TaintSource { |
| 33 | + |
| 34 | + RequestSource() { |
| 35 | + this.(ControlFlowNode).refersTo(theBottleRequestObject()) |
| 36 | + } |
| 37 | + |
| 38 | + override predicate isSourceOf(TaintKind kind) { |
| 39 | + kind instanceof BottleRequestKind |
| 40 | + } |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +class BottleFormsDict extends TaintKind { |
| 46 | + |
| 47 | + BottleFormsDict() { |
| 48 | + this = "bottle.FormsDict" |
| 49 | + } |
| 50 | + |
| 51 | + override TaintKind getTaintForFlowStep(ControlFlowNode fromnode, ControlFlowNode tonode) { |
| 52 | + /* Cannot use `getTaintOfAttribute(name)` as it wouldn't bind `name` */ |
| 53 | + exists(string name | |
| 54 | + fromnode = tonode.(AttrNode).getObject(name) and |
| 55 | + result instanceof UntrustedStringKind |
| 56 | + | |
| 57 | + name != "get" and name != "getunicode" and name != "getall" |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + override TaintKind getTaintOfMethodResult(string name) { |
| 62 | + (name = "get" or name = "getunicode") and |
| 63 | + result instanceof UntrustedStringKind |
| 64 | + or |
| 65 | + name = "getall" and result.(SequenceKind).getItem() instanceof UntrustedStringKind |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class FileUpload extends TaintKind { |
| 70 | + |
| 71 | + FileUpload() { |
| 72 | + this = "bottle.FileUpload" |
| 73 | + } |
| 74 | + |
| 75 | + override TaintKind getTaintOfAttribute(string name) { |
| 76 | + name = "filename" and result instanceof UntrustedStringKind |
| 77 | + or |
| 78 | + name = "raw_filename" and result instanceof UntrustedStringKind |
| 79 | + or |
| 80 | + name = "file" and result instanceof UntrustedFile |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +class UntrustedFile extends TaintKind { |
| 86 | + |
| 87 | + UntrustedFile() { this = "Untrusted file" } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +// |
| 92 | +// TO DO.. File uploads -- Should check about file uploads for other frameworks as well. |
| 93 | +// Move UntrustedFile to shared location |
| 94 | +// |
| 95 | + |
| 96 | + |
| 97 | +/** Parameter to a bottle request handler function */ |
| 98 | +class BottleRequestParameter extends TaintSource { |
| 99 | + |
| 100 | + BottleRequestParameter() { |
| 101 | + exists(BottleRoute route | |
| 102 | + route.getNamedArgument() = this.(ControlFlowNode).getNode() |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + override predicate isSourceOf(TaintKind kind) { |
| 107 | + kind instanceof UntrustedStringKind |
| 108 | + } |
| 109 | + |
| 110 | + override string toString() { |
| 111 | + result = "bottle handler function argument" |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
0 commit comments