|
| 1 | +/** |
| 2 | + * @name CORS misconfiguration |
| 3 | + * @description If a CORS policy is configured to accept an origin value obtained from the request data, |
| 4 | + * or is set to `*` or `null`, and it allows credential sharing, then the users of the |
| 5 | + * application are vulnerable to the same range of attacks as in XSS (credential stealing, etc.). |
| 6 | + * @kind problem |
| 7 | + * @problem.severity warning |
| 8 | + * @id go/cors-misconfiguration |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-942 |
| 11 | + * external/cwe/cwe-346 |
| 12 | + */ |
| 13 | + |
| 14 | +import go |
| 15 | +import semmle.go.security.InsecureFeatureFlag::InsecureFeatureFlag |
| 16 | + |
| 17 | +/** |
| 18 | + * A flag indicating a check for satisfied permissions or test configuration. |
| 19 | + */ |
| 20 | +class AllowedFlag extends FlagKind { |
| 21 | + AllowedFlag() { this = "allowed" } |
| 22 | + |
| 23 | + bindingset[result] |
| 24 | + override string getAFlagName() { |
| 25 | + result.regexpMatch("(?i).*(allow|match|check|debug|devel|insecure).*") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Provides the name of the `Access-Control-Allow-Origin` header key. |
| 31 | + */ |
| 32 | +string headerAllowOrigin() { result = "Access-Control-Allow-Origin".toLowerCase() } |
| 33 | + |
| 34 | +/** |
| 35 | + * Provides the name of the `Access-Control-Allow-Credentials` header key. |
| 36 | + */ |
| 37 | +string headerAllowCredentials() { result = "Access-Control-Allow-Credentials".toLowerCase() } |
| 38 | + |
| 39 | +/** |
| 40 | + * An `Access-Control-Allow-Origin` header write. |
| 41 | + */ |
| 42 | +class AllowOriginHeaderWrite extends HTTP::HeaderWrite { |
| 43 | + AllowOriginHeaderWrite() { this.getHeaderName() = headerAllowOrigin() } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * An `Access-Control-Allow-Credentials` header write. |
| 48 | + */ |
| 49 | +class AllowCredentialsHeaderWrite extends HTTP::HeaderWrite { |
| 50 | + AllowCredentialsHeaderWrite() { this.getHeaderName() = headerAllowCredentials() } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * A taint-tracking configuration for reasoning about when an UntrustedFlowSource |
| 55 | + * flows to a HeaderWrite that writes an `Access-Control-Allow-Origin` header's value. |
| 56 | + */ |
| 57 | +class FlowsUntrustedToAllowOriginHeader extends TaintTracking::Configuration { |
| 58 | + FlowsUntrustedToAllowOriginHeader() { this = "from-untrusted-to-allow-origin-header-value" } |
| 59 | + |
| 60 | + override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource } |
| 61 | + |
| 62 | + predicate isSink(DataFlow::Node sink, AllowOriginHeaderWrite hw) { sink = hw.getValue() } |
| 63 | + |
| 64 | + override predicate isSanitizer(DataFlow::Node node) { |
| 65 | + exists(ControlFlow::ConditionGuardNode cgn | |
| 66 | + cgn.ensures(any(AllowedFlag f).getAFlag().getANode(), _) |
| 67 | + | |
| 68 | + cgn.dominates(node.getBasicBlock()) |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Holds if the provided `allowOriginHW` HeaderWrite's parent ResponseWriter |
| 77 | + * also has another HeaderWrite that sets a `Access-Control-Allow-Credentials` |
| 78 | + * header to `true`. |
| 79 | + */ |
| 80 | +predicate allowCredentialsIsSetToTrue(AllowOriginHeaderWrite allowOriginHW) { |
| 81 | + exists(AllowCredentialsHeaderWrite allowCredentialsHW | |
| 82 | + allowCredentialsHW.getHeaderValue().toLowerCase() = "true" |
| 83 | + | |
| 84 | + allowOriginHW.getResponseWriter() = allowCredentialsHW.getResponseWriter() |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Holds if the provided `allowOriginHW` HeaderWrite's value is set using an |
| 90 | + * UntrustedFlowSource. |
| 91 | + * The `message` parameter is populated with the warning message to be returned by the query. |
| 92 | + */ |
| 93 | +predicate flowsFromUntrustedToAllowOrigin(AllowOriginHeaderWrite allowOriginHW, string message) { |
| 94 | + exists(FlowsUntrustedToAllowOriginHeader cfg, DataFlow::Node sink | |
| 95 | + cfg.hasFlowTo(sink) and |
| 96 | + cfg.isSink(sink, allowOriginHW) |
| 97 | + | |
| 98 | + message = |
| 99 | + headerAllowOrigin() + " header is set to a user-defined value, and " + |
| 100 | + headerAllowCredentials() + " is set to `true`" |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Holds if the provided `allowOriginHW` HeaderWrite is for a `Access-Control-Allow-Origin` |
| 106 | + * header and the value is set to `null`. |
| 107 | + */ |
| 108 | +predicate allowOriginIsNull(AllowOriginHeaderWrite allowOriginHW, string message) { |
| 109 | + allowOriginHW.getHeaderValue().toLowerCase() = "null" and |
| 110 | + message = |
| 111 | + headerAllowOrigin() + " header is set to `" + allowOriginHW.getHeaderValue() + "`, and " + |
| 112 | + headerAllowCredentials() + " is set to `true`" |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * A read on a map type. |
| 117 | + */ |
| 118 | +class MapRead extends DataFlow::ElementReadNode { |
| 119 | + MapRead() { this.getBase().getType() instanceof MapType } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * A taint-tracking configuration for reasoning about when an UntrustedFlowSource |
| 124 | + * flows somewhere. |
| 125 | + */ |
| 126 | +class FlowsFromUntrusted extends TaintTracking::Configuration { |
| 127 | + FlowsFromUntrusted() { this = "from-untrusted" } |
| 128 | + |
| 129 | + override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource } |
| 130 | + |
| 131 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 132 | + |
| 133 | + predicate isSink(DataFlow::Node sink, ControlFlow::ConditionGuardNode cgn) { |
| 134 | + exists(IfStmt ifs | |
| 135 | + exists(Expr operand | |
| 136 | + operand = ifs.getCond().getAChildExpr*() and |
| 137 | + ( |
| 138 | + exists(DataFlow::CallExpr call | call = operand | |
| 139 | + call.getTarget().hasQualifiedName("strings", "HasSuffix") and |
| 140 | + sink.asExpr() = call.getArgument(0) |
| 141 | + ) |
| 142 | + or |
| 143 | + exists(MapRead mapRead | |
| 144 | + operand = mapRead.asExpr() and |
| 145 | + sink = mapRead.getIndex().getAPredecessor*() |
| 146 | + // TODO: add _, ok : map[untrusted]; ok |
| 147 | + ) |
| 148 | + or |
| 149 | + exists(EqlExpr comp | |
| 150 | + operand = comp and |
| 151 | + ( |
| 152 | + sink.asExpr() = comp.getLeftOperand() and |
| 153 | + not comp.getRightOperand().(StringLit).getStringValue() = "" |
| 154 | + or |
| 155 | + sink.asExpr() = comp.getRightOperand() and |
| 156 | + not comp.getLeftOperand().(StringLit).getStringValue() = "" |
| 157 | + ) |
| 158 | + ) |
| 159 | + ) |
| 160 | + ) |
| 161 | + | |
| 162 | + cgn.getCondition() = ifs.getCond() |
| 163 | + ) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Holds if the provided `dst` is also destination of a `UntrustedFlowSource`. |
| 169 | + */ |
| 170 | +predicate flowsToGuardedByCheckOnUntrusted(AllowOriginHeaderWrite allowOriginHW) { |
| 171 | + exists(FlowsFromUntrusted cfg, DataFlow::Node sink, ControlFlow::ConditionGuardNode cgn | |
| 172 | + cfg.hasFlowTo(sink) and cfg.isSink(sink, cgn) |
| 173 | + | |
| 174 | + cgn.dominates(allowOriginHW.getBasicBlock()) |
| 175 | + ) |
| 176 | +} |
| 177 | + |
| 178 | +from AllowOriginHeaderWrite allowOriginHW, string message |
| 179 | +where |
| 180 | + allowCredentialsIsSetToTrue(allowOriginHW) and |
| 181 | + ( |
| 182 | + flowsFromUntrustedToAllowOrigin(allowOriginHW, message) |
| 183 | + or |
| 184 | + allowOriginIsNull(allowOriginHW, message) |
| 185 | + ) and |
| 186 | + not flowsToGuardedByCheckOnUntrusted(allowOriginHW) and |
| 187 | + not exists(ControlFlow::ConditionGuardNode cgn | |
| 188 | + cgn.ensures(any(AllowedFlag f).getAFlag().getANode(), _) |
| 189 | + | |
| 190 | + cgn.dominates(allowOriginHW.getBasicBlock()) |
| 191 | + ) |
| 192 | +select allowOriginHW, message |
0 commit comments