|
1 | 1 | /** |
2 | | - * Provides classes modeling security-relevant aspects of the `django` package. |
| 2 | + * Provides classes modeling security-relevant aspects of the `django` PyPI package. |
| 3 | + * See https://www.djangoproject.com/. |
3 | 4 | */ |
4 | 5 |
|
5 | 6 | private import python |
6 | 7 | private import experimental.dataflow.DataFlow |
7 | 8 | private import experimental.dataflow.RemoteFlowSources |
8 | 9 | private import experimental.semmle.python.Concepts |
9 | 10 |
|
10 | | -private module Django { } |
| 11 | +/** |
| 12 | + * Provides models for the `django` PyPI package. |
| 13 | + * See https://www.djangoproject.com/. |
| 14 | + */ |
| 15 | +private module Django { |
| 16 | + // --------------------------------------------------------------------------- |
| 17 | + // django |
| 18 | + // --------------------------------------------------------------------------- |
| 19 | + /** Gets a reference to the `django` module. */ |
| 20 | + private DataFlow::Node django(DataFlow::TypeTracker t) { |
| 21 | + t.start() and |
| 22 | + result = DataFlow::importNode("django") |
| 23 | + or |
| 24 | + exists(DataFlow::TypeTracker t2 | result = django(t2).track(t2, t)) |
| 25 | + } |
| 26 | + |
| 27 | + /** Gets a reference to the `django` module. */ |
| 28 | + DataFlow::Node django() { result = django(DataFlow::TypeTracker::end()) } |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets a reference to the attribute `attr_name` of the `django` module. |
| 32 | + * WARNING: Only holds for a few predefined attributes. |
| 33 | + */ |
| 34 | + private DataFlow::Node django_attr(DataFlow::TypeTracker t, string attr_name) { |
| 35 | + attr_name in ["urls"] and |
| 36 | + ( |
| 37 | + t.start() and |
| 38 | + result = DataFlow::importNode("django" + "." + attr_name) |
| 39 | + or |
| 40 | + t.startInAttr(attr_name) and |
| 41 | + result = DataFlow::importNode("django") |
| 42 | + ) |
| 43 | + or |
| 44 | + // Due to bad performance when using normal setup with `django_attr(t2, attr_name).track(t2, t)` |
| 45 | + // we have inlined that code and forced a join |
| 46 | + exists(DataFlow::TypeTracker t2 | |
| 47 | + exists(DataFlow::StepSummary summary | |
| 48 | + django_attr_first_join(t2, attr_name, result, summary) and |
| 49 | + t = t2.append(summary) |
| 50 | + ) |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + pragma[nomagic] |
| 55 | + private predicate django_attr_first_join( |
| 56 | + DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res, DataFlow::StepSummary summary |
| 57 | + ) { |
| 58 | + DataFlow::StepSummary::step(django_attr(t2, attr_name), res, summary) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets a reference to the attribute `attr_name` of the `django` module. |
| 63 | + * WARNING: Only holds for a few predefined attributes. |
| 64 | + */ |
| 65 | + private DataFlow::Node django_attr(string attr_name) { |
| 66 | + result = django_attr(DataFlow::TypeTracker::end(), attr_name) |
| 67 | + } |
| 68 | + |
| 69 | + /** Provides models for the `django` module. */ |
| 70 | + module django { |
| 71 | + /** Gets a reference to the `django.urls` module. */ |
| 72 | + DataFlow::Node urls() { result = django_attr("urls") } |
| 73 | + |
| 74 | + // ------------------------------------------------------------------------- |
| 75 | + // django.urls |
| 76 | + // ------------------------------------------------------------------------- |
| 77 | + /** Provides models for the `django.urls` module */ |
| 78 | + module urls { |
| 79 | + /** |
| 80 | + * Gets a reference to the attribute `attr_name` of the `urls` module. |
| 81 | + * WARNING: Only holds for a few predefined attributes. |
| 82 | + */ |
| 83 | + private DataFlow::Node urls_attr(DataFlow::TypeTracker t, string attr_name) { |
| 84 | + attr_name in ["path", "re_path"] and |
| 85 | + ( |
| 86 | + t.start() and |
| 87 | + result = DataFlow::importNode("django.urls" + "." + attr_name) |
| 88 | + or |
| 89 | + t.startInAttr(attr_name) and |
| 90 | + result = DataFlow::importNode("django.urls") |
| 91 | + or |
| 92 | + t.startInAttr(attr_name) and |
| 93 | + result = django::urls() |
| 94 | + ) |
| 95 | + or |
| 96 | + // Due to bad performance when using normal setup with `urls_attr(t2, attr_name).track(t2, t)` |
| 97 | + // we have inlined that code and forced a join |
| 98 | + exists(DataFlow::TypeTracker t2 | |
| 99 | + exists(DataFlow::StepSummary summary | |
| 100 | + urls_attr_first_join(t2, attr_name, result, summary) and |
| 101 | + t = t2.append(summary) |
| 102 | + ) |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + pragma[nomagic] |
| 107 | + private predicate urls_attr_first_join( |
| 108 | + DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res, |
| 109 | + DataFlow::StepSummary summary |
| 110 | + ) { |
| 111 | + DataFlow::StepSummary::step(urls_attr(t2, attr_name), res, summary) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets a reference to the attribute `attr_name` of the `urls` module. |
| 116 | + * WARNING: Only holds for a few predefined attributes. |
| 117 | + */ |
| 118 | + private DataFlow::Node urls_attr(string attr_name) { |
| 119 | + result = urls_attr(DataFlow::TypeTracker::end(), attr_name) |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Gets a reference to the `django.urls.path` function. |
| 124 | + * See https://docs.djangoproject.com/en/3.0/ref/urls/#path |
| 125 | + */ |
| 126 | + DataFlow::Node path() { result = urls_attr("path") } |
| 127 | + |
| 128 | + /** |
| 129 | + * Gets a reference to the `django.urls.re_path` function. |
| 130 | + * See https://docs.djangoproject.com/en/3.0/ref/urls/#re_path |
| 131 | + */ |
| 132 | + DataFlow::Node re_path() { result = urls_attr("re_path") } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private class DjangoPathRouteSetup extends HTTP::Server::RouteSetup::Range, DataFlow::CfgNode { |
| 137 | + override CallNode node; |
| 138 | + |
| 139 | + DjangoPathRouteSetup() { node.getFunction() = django::urls::path().asCfgNode() } |
| 140 | + |
| 141 | + override string getUrlPattern() { |
| 142 | + exists(StrConst str, ControlFlowNode urlPatternArg | |
| 143 | + urlPatternArg = [node.getArg(0), node.getArgByName("route")] |
| 144 | + | |
| 145 | + DataFlow::localFlow(DataFlow::exprNode(str), |
| 146 | + any(DataFlow::Node n | n.asCfgNode() = urlPatternArg)) and |
| 147 | + result = str.getText() |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + override Function getARouteHandler() { none() } |
| 152 | + |
| 153 | + override Parameter getARoutedParameter() { none() } |
| 154 | + } |
| 155 | + |
| 156 | + private class DjangoRePathRouteSetup extends HTTP::Server::RouteSetup::Range, DataFlow::CfgNode { |
| 157 | + override CallNode node; |
| 158 | + |
| 159 | + DjangoRePathRouteSetup() { node.getFunction() = django::urls::re_path().asCfgNode() } |
| 160 | + |
| 161 | + override string getUrlPattern() { |
| 162 | + exists(StrConst str, ControlFlowNode urlPatternArg | |
| 163 | + urlPatternArg = [node.getArg(0), node.getArgByName("route")] |
| 164 | + | |
| 165 | + DataFlow::localFlow(DataFlow::exprNode(str), |
| 166 | + any(DataFlow::Node n | n.asCfgNode() = urlPatternArg)) and |
| 167 | + result = str.getText() |
| 168 | + ) |
| 169 | + } |
| 170 | + |
| 171 | + override Function getARouteHandler() { none() } |
| 172 | + |
| 173 | + override Parameter getARoutedParameter() { none() } |
| 174 | + } |
| 175 | +} |
0 commit comments