|
| 1 | +private import ruby |
| 2 | +private import codeql.ruby.DataFlow |
| 3 | +private import codeql.ruby.CFG |
| 4 | +private import codeql.ruby.Concepts |
| 5 | +private import codeql.ruby.Frameworks |
| 6 | +private import codeql.ruby.frameworks.ActionController |
| 7 | +private import codeql.ruby.frameworks.ActionView |
| 8 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 9 | +private import codeql.ruby.dataflow.BarrierGuards |
| 10 | +import codeql.ruby.dataflow.internal.DataFlowDispatch |
| 11 | +private import codeql.ruby.typetracking.TypeTracker |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides default sources, sinks and sanitizers for detecting |
| 15 | + * "reflected server-side cross-site scripting" |
| 16 | + * vulnerabilities, as well as extension points for adding your own. |
| 17 | + */ |
| 18 | +module ReflectedXSS { |
| 19 | + /** |
| 20 | + * A data flow source for "reflected server-side cross-site scripting" vulnerabilities. |
| 21 | + */ |
| 22 | + abstract class Source extends DataFlow::Node { } |
| 23 | + |
| 24 | + /** |
| 25 | + * A data flow sink for "reflected server-side cross-site scripting" vulnerabilities. |
| 26 | + */ |
| 27 | + abstract class Sink extends DataFlow::Node { } |
| 28 | + |
| 29 | + /** |
| 30 | + * A sanitizer for "reflected server-side cross-site scripting" vulnerabilities. |
| 31 | + */ |
| 32 | + abstract class Sanitizer extends DataFlow::Node { } |
| 33 | + |
| 34 | + /** |
| 35 | + * A sanitizer guard for "reflected server-side cross-site scripting" vulnerabilities. |
| 36 | + */ |
| 37 | + abstract class SanitizerGuard extends DataFlow::BarrierGuard { } |
| 38 | + |
| 39 | + /** |
| 40 | + * A source of remote user input, considered as a flow source. |
| 41 | + */ |
| 42 | + class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { } |
| 43 | + |
| 44 | + private class ErbOutputMethodCallArgumentNode extends DataFlow::Node { |
| 45 | + private MethodCall call; |
| 46 | + |
| 47 | + ErbOutputMethodCallArgumentNode() { |
| 48 | + exists(ErbOutputDirective d | |
| 49 | + call = d.getTerminalStmt() and |
| 50 | + this.asExpr().getExpr() = call.getAnArgument() |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + MethodCall getCall() { result = call } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * An `html_safe` call marking the output as not requiring HTML escaping, |
| 59 | + * considered as a flow sink. |
| 60 | + */ |
| 61 | + class HtmlSafeCallAsSink extends Sink { |
| 62 | + HtmlSafeCallAsSink() { |
| 63 | + exists(HtmlSafeCall c, ErbOutputDirective d | |
| 64 | + this.asExpr().getExpr() = c.getReceiver() and |
| 65 | + c = d.getTerminalStmt() |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * An argument to a call to the `raw` method, considered as a flow sink. |
| 72 | + */ |
| 73 | + class RawCallArgumentAsSink extends Sink, ErbOutputMethodCallArgumentNode { |
| 74 | + RawCallArgumentAsSink() { this.getCall() instanceof RawCall } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * A argument to a call to the `link_to` method, which does not expect |
| 79 | + * unsanitized user-input, considered as a flow sink. |
| 80 | + */ |
| 81 | + class LinkToCallArgumentAsSink extends Sink, ErbOutputMethodCallArgumentNode { |
| 82 | + LinkToCallArgumentAsSink() { |
| 83 | + this.asExpr().getExpr() = this.getCall().(LinkToCall).getPathArgument() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * An HTML escaping, considered as a sanitizer. |
| 89 | + */ |
| 90 | + class HtmlEscapingAsSanitizer extends Sanitizer { |
| 91 | + HtmlEscapingAsSanitizer() { this = any(HtmlEscaping esc).getOutput() } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * A comparison with a constant string, considered as a sanitizer-guard. |
| 96 | + */ |
| 97 | + class StringConstCompareAsSanitizerGuard extends SanitizerGuard, StringConstCompare { } |
| 98 | + |
| 99 | + /** |
| 100 | + * An inclusion check against an array of constant strings, considered as a sanitizer-guard. |
| 101 | + */ |
| 102 | + class StringConstArrayInclusionCallAsSanitizerGuard extends SanitizerGuard, |
| 103 | + StringConstArrayInclusionCall { } |
| 104 | + |
| 105 | + /** |
| 106 | + * A `VariableWriteAccessCfgNode` that is not succeeded (locally) by another |
| 107 | + * write to that variable. |
| 108 | + */ |
| 109 | + private class FinalInstanceVarWrite extends CfgNodes::ExprNodes::InstanceVariableWriteAccessCfgNode { |
| 110 | + private InstanceVariable var; |
| 111 | + |
| 112 | + FinalInstanceVarWrite() { |
| 113 | + var = this.getExpr().getVariable() and |
| 114 | + not exists(CfgNodes::ExprNodes::InstanceVariableWriteAccessCfgNode succWrite | |
| 115 | + succWrite.getExpr().getVariable() = var |
| 116 | + | |
| 117 | + succWrite = this.getASuccessor+() |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + InstanceVariable getVariable() { result = var } |
| 122 | + |
| 123 | + AssignExpr getAnAssignExpr() { result.getLeftOperand() = this.getExpr() } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * An additional step that is taint-preserving in the context of reflected XSS. |
| 128 | + */ |
| 129 | + predicate isAdditionalXSSTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 130 | + // node1 is a `locals` argument to a render call... |
| 131 | + exists(RenderCall call, Pair kvPair, string hashKey | |
| 132 | + call.getLocals().getAKeyValuePair() = kvPair and |
| 133 | + kvPair.getValue() = node1.asExpr().getExpr() and |
| 134 | + kvPair.getKey().(StringlikeLiteral).getValueText() = hashKey and |
| 135 | + // `node2` appears in the rendered template file |
| 136 | + call.getTemplateFile() = node2.getLocation().getFile() and |
| 137 | + ( |
| 138 | + // node2 is an element reference against `local_assigns` |
| 139 | + exists( |
| 140 | + CfgNodes::ExprNodes::ElementReferenceCfgNode refNode, DataFlow::Node argNode, |
| 141 | + CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
| 142 | + | |
| 143 | + refNode = node2.asExpr() and |
| 144 | + argNode.asExpr() = refNode.getArgument(0) and |
| 145 | + refNode.getReceiver().getExpr().(MethodCall).getMethodName() = "local_assigns" and |
| 146 | + argNode.getALocalSource() = DataFlow::exprNode(strNode) and |
| 147 | + strNode.getExpr().getValueText() = hashKey |
| 148 | + ) |
| 149 | + or |
| 150 | + // ...node2 is a "method call" to a "method" with `hashKey` as its name |
| 151 | + // TODO: This may be a variable read in reality that we interpret as a method call |
| 152 | + exists(MethodCall varAcc | |
| 153 | + varAcc = node2.asExpr().(CfgNodes::ExprNodes::MethodCallCfgNode).getExpr() and |
| 154 | + varAcc.getMethodName() = hashKey |
| 155 | + ) |
| 156 | + ) |
| 157 | + ) |
| 158 | + or |
| 159 | + // instance variables in the controller |
| 160 | + exists( |
| 161 | + ActionControllerActionMethod action, VariableReadAccess viewVarRead, AssignExpr ae, |
| 162 | + FinalInstanceVarWrite controllerVarWrite |
| 163 | + | |
| 164 | + viewVarRead = node2.asExpr().(CfgNodes::ExprNodes::VariableReadAccessCfgNode).getExpr() and |
| 165 | + action.getDefaultTemplateFile() = viewVarRead.getLocation().getFile() and |
| 166 | + // match read to write on variable name |
| 167 | + viewVarRead.getVariable().getName() = controllerVarWrite.getVariable().getName() and |
| 168 | + // propagate taint from assignment RHS expr to variable read access in view |
| 169 | + ae = controllerVarWrite.getAnAssignExpr() and |
| 170 | + node1.asExpr().getExpr() = ae.getRightOperand() and |
| 171 | + ae.getParent+() = action |
| 172 | + ) |
| 173 | + or |
| 174 | + // flow from template into controller helper method |
| 175 | + exists( |
| 176 | + ErbFile template, ActionControllerHelperMethod helperMethod, |
| 177 | + CfgNodes::ExprNodes::MethodCallCfgNode helperMethodCall, int argIdx |
| 178 | + | |
| 179 | + template = node1.getLocation().getFile() and |
| 180 | + helperMethod.getName() = helperMethodCall.getExpr().getMethodName() and |
| 181 | + helperMethod.getControllerClass() = getAssociatedControllerClass(template) and |
| 182 | + helperMethodCall.getArgument(argIdx) = node1.asExpr() and |
| 183 | + helperMethod.getParameter(argIdx) = node2.asExpr().getExpr() |
| 184 | + ) |
| 185 | + or |
| 186 | + // flow out of controller helper method into template |
| 187 | + exists( |
| 188 | + ErbFile template, ActionControllerHelperMethod helperMethod, |
| 189 | + CfgNodes::ExprNodes::MethodCallCfgNode helperMethodCall |
| 190 | + | |
| 191 | + template = node2.getLocation().getFile() and |
| 192 | + helperMethod.getName() = helperMethodCall.getExpr().getMethodName() and |
| 193 | + helperMethod.getControllerClass() = getAssociatedControllerClass(template) and |
| 194 | + // `node1` is an expr node that may be returned by the helper method |
| 195 | + exprNodeReturnedFrom(node1, helperMethod) and |
| 196 | + // `node2` is a call to the helper method |
| 197 | + node2.asExpr() = helperMethodCall |
| 198 | + ) |
| 199 | + } |
| 200 | +} |
0 commit comments