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