|
| 1 | +/** |
| 2 | + * @name Use of constant `state` value in OAuth 2.0 URL |
| 3 | + * @description Using a constant value for the `state` in the OAuth 2.0 URL makes the application |
| 4 | + * susceptible to CSRF attacks. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision high |
| 8 | + * @id go/constant-oauth2-state |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-352 |
| 11 | + */ |
| 12 | + |
| 13 | +import go |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/** |
| 17 | + * A method that creates a new URL that will send the user |
| 18 | + * to the OAuth 2.0 authorization dialog of the provider. |
| 19 | + */ |
| 20 | +class AuthCodeURL extends Method { |
| 21 | + AuthCodeURL() { this.hasQualifiedName("golang.org/x/oauth2", "Config", "AuthCodeURL") } |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * A flow of a constant string value to a call to `AuthCodeURL` as the |
| 26 | + * `state` parameter. |
| 27 | + */ |
| 28 | +class ConstantStateFlowConf extends DataFlow::Configuration { |
| 29 | + ConstantStateFlowConf() { this = "ConstantStateFlowConf" } |
| 30 | + |
| 31 | + predicate isSink(DataFlow::Node sink, DataFlow::CallNode call) { |
| 32 | + exists(AuthCodeURL m | call = m.getACall() | sink = call.getArgument(0)) |
| 33 | + } |
| 34 | + |
| 35 | + override predicate isSource(DataFlow::Node source) { |
| 36 | + source.isConst() and |
| 37 | + not DataFlow::isReturnedWithError(source) and |
| 38 | + // Avoid duplicate paths by not considering reads from constants as sources themselves: |
| 39 | + ( |
| 40 | + source.asExpr() instanceof StringLit |
| 41 | + or |
| 42 | + source.asExpr() instanceof AddExpr |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Holds if `pred` writes a URL to the `RedirectURL` field of the `succ` `Config` object. |
| 51 | + * |
| 52 | + * This propagates flow from the RedirectURL field to the whole Config object. |
| 53 | + */ |
| 54 | +predicate isUrlTaintingConfigStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 55 | + exists(Write w, Field f | f.hasQualifiedName("golang.org/x/oauth2", "Config", "RedirectURL") | |
| 56 | + w.writesField(succ.(DataFlow::PostUpdateNode).getPreUpdateNode(), f, pred) |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Gets a URL or pseudo-URL that suggests an out-of-band OAuth2 flow or use of a transient |
| 62 | + * local listener to receive an OAuth2 redirect. |
| 63 | + */ |
| 64 | +bindingset[result] |
| 65 | +string getAnOobOauth2Url() { |
| 66 | + // The following are pseudo-URLs seen in the wild to indicate the authenticating site |
| 67 | + // should display a code for the user to manually convey, rather than directing: |
| 68 | + result in ["urn:ietf:wg:oauth:2.0:oob", "urn:ietf:wg:oauth:2.0:oob:auto", "oob", "code"] or |
| 69 | + // Alternatively some non-web tools will create a temporary local webserver to handle the |
| 70 | + // OAuth2 redirect: |
| 71 | + result.matches("%://localhost%") or |
| 72 | + result.matches("%://127.0.0.1%") |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * A flow of a URL indicating the OAuth redirect doesn't point to a publicly |
| 77 | + * accessible address, to the receiver of an `AuthCodeURL` call. |
| 78 | + * |
| 79 | + * Note we accept localhost and 127.0.0.1 on the assumption this is probably a transient |
| 80 | + * listener; if it actually is a persistent server then that really is vulnerable to CSRF. |
| 81 | + */ |
| 82 | +class PrivateUrlFlowsToAuthCodeUrlCall extends DataFlow::Configuration { |
| 83 | + PrivateUrlFlowsToAuthCodeUrlCall() { this = "PrivateUrlFlowsToConfig" } |
| 84 | + |
| 85 | + override predicate isSource(DataFlow::Node source) { |
| 86 | + source.getStringValue() = getAnOobOauth2Url() and |
| 87 | + // Avoid duplicate paths by excluding constant variable references from |
| 88 | + // themselves being sources: |
| 89 | + ( |
| 90 | + source.asExpr() instanceof StringLit |
| 91 | + or |
| 92 | + source.asExpr() instanceof AddExpr |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + override predicate isAdditionalFlowStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 97 | + // Propagate from a RedirectURL field to a whole Config |
| 98 | + isUrlTaintingConfigStep(pred, succ) |
| 99 | + or |
| 100 | + // Propagate across deref and address-taking steps |
| 101 | + TaintTracking::referenceStep(pred, succ) |
| 102 | + or |
| 103 | + // Propagate across Sprintf and similar calls |
| 104 | + TaintTracking::functionModelStep(any(Fmt::Sprinter s), pred, succ) |
| 105 | + } |
| 106 | + |
| 107 | + predicate isSink(DataFlow::Node sink, DataFlow::CallNode call) { |
| 108 | + exists(AuthCodeURL m | call = m.getACall() | sink = call.getReceiver()) |
| 109 | + } |
| 110 | + |
| 111 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Holds if a URL indicating the OAuth redirect doesn't point to a publicly |
| 116 | + * accessible address, to the receiver of an `AuthCodeURL` call. |
| 117 | + * |
| 118 | + * Note we accept localhost and 127.0.0.1 on the assumption this is probably a transient |
| 119 | + * listener; if it actually is a persistent server then that really is vulnerable to CSRF. |
| 120 | + */ |
| 121 | +predicate privateUrlFlowsToAuthCodeUrlCall(DataFlow::CallNode call) { |
| 122 | + exists(PrivateUrlFlowsToAuthCodeUrlCall flowConfig, DataFlow::Node receiver | |
| 123 | + flowConfig.hasFlowTo(receiver) and |
| 124 | + flowConfig.isSink(receiver, call) |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +/** A flow from `golang.org/x/oauth2.Config.AuthCodeURL`'s result to a logging function. */ |
| 129 | +class FlowToPrint extends DataFlow::Configuration { |
| 130 | + FlowToPrint() { this = "FlowToPrint" } |
| 131 | + |
| 132 | + predicate isSink(DataFlow::Node sink, DataFlow::CallNode call) { |
| 133 | + exists(LoggerCall logCall | call = logCall | sink = logCall.getAMessageComponent()) |
| 134 | + } |
| 135 | + |
| 136 | + override predicate isSource(DataFlow::Node source) { |
| 137 | + source = any(AuthCodeURL m).getACall().getResult() |
| 138 | + } |
| 139 | + |
| 140 | + override predicate isSink(DataFlow::Node sink) { isSink(sink, _) } |
| 141 | +} |
| 142 | + |
| 143 | +/** Holds if the provided `CallNode`'s result flows to an argument of a printer call. */ |
| 144 | +predicate resultFlowsToPrinter(DataFlow::CallNode authCodeURLCall) { |
| 145 | + exists(FlowToPrint cfg, DataFlow::PathNode source, DataFlow::PathNode sink | |
| 146 | + cfg.hasFlowPath(source, sink) and |
| 147 | + authCodeURLCall.getResult() = source.getNode() |
| 148 | + ) |
| 149 | +} |
| 150 | + |
| 151 | +/** Get a data-flow node that reads the value of `os.Stdin`. */ |
| 152 | +DataFlow::Node getAStdinNode() { |
| 153 | + exists(ValueEntity v | |
| 154 | + v.hasQualifiedName("os", "Stdin") and result = globalValueNumber(v.getARead()).getANode() |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Gets a call to a scanner function that reads from `os.Stdin`, or which creates a scanner |
| 160 | + * instance wrapping `os.Stdin`. |
| 161 | + */ |
| 162 | +DataFlow::CallNode getAScannerCall() { |
| 163 | + result = any(Fmt::Scanner f).getACall() |
| 164 | + or |
| 165 | + exists(Fmt::FScanner f | |
| 166 | + result = f.getACall() and f.getReader().getNode(result) = getAStdinNode() |
| 167 | + ) |
| 168 | + or |
| 169 | + exists(Bufio::NewScanner f | |
| 170 | + result = f.getACall() and f.getReader().getNode(result) = getAStdinNode() |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * Holds if the provided `CallNode` is within the same root as a call |
| 176 | + * to a scanner that reads from `os.Stdin`. |
| 177 | + */ |
| 178 | +predicate containsCallToStdinScanner(FuncDef funcDef) { getAScannerCall().getRoot() = funcDef } |
| 179 | + |
| 180 | +/** |
| 181 | + * Holds if the `authCodeURLCall` seems to be done within a terminal |
| 182 | + * because there are calls to a printer (`fmt.Println` and similar), |
| 183 | + * and a call to a scanner (`fmt.Scan` and similar), |
| 184 | + * all of which are typically done within a terminal session. |
| 185 | + */ |
| 186 | +predicate seemsLikeDoneWithinATerminal(DataFlow::CallNode authCodeURLCall) { |
| 187 | + resultFlowsToPrinter(authCodeURLCall) and |
| 188 | + containsCallToStdinScanner(authCodeURLCall.getRoot()) |
| 189 | +} |
| 190 | + |
| 191 | +from |
| 192 | + ConstantStateFlowConf cfg, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 193 | + DataFlow::CallNode sinkCall |
| 194 | +where |
| 195 | + cfg.hasFlowPath(source, sink) and |
| 196 | + cfg.isSink(sink.getNode(), sinkCall) and |
| 197 | + // Exclude cases that seem to be oauth flows done from within a terminal: |
| 198 | + not seemsLikeDoneWithinATerminal(sinkCall) and |
| 199 | + not privateUrlFlowsToAuthCodeUrlCall(sinkCall) |
| 200 | +select sink.getNode(), source, sink, "Using a constant $@ to create oauth2 URLs.", source.getNode(), |
| 201 | + "state string" |
0 commit comments