|
| 1 | +/** |
| 2 | + * Provides classes and predicates for detecting insecure cookies. |
| 3 | + */ |
| 4 | + |
| 5 | +import csharp |
| 6 | +import semmle.code.csharp.frameworks.microsoft.AspNetCore |
| 7 | + |
| 8 | +/** |
| 9 | + * Holds if the expression is a variable with a sensitive name. |
| 10 | + */ |
| 11 | +predicate isCookieWithSensitiveName(Expr cookieExpr) { |
| 12 | + exists(DataFlow::Node sink | |
| 13 | + AuthCookieName::flowTo(sink) and |
| 14 | + sink.asExpr() = cookieExpr |
| 15 | + ) |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Configuration for tracking if a variable with a sensitive name is used as an argument. |
| 20 | + */ |
| 21 | +private module AuthCookieNameConfig implements DataFlow::ConfigSig { |
| 22 | + private predicate isAuthVariable(Expr expr) { |
| 23 | + exists(string val | |
| 24 | + ( |
| 25 | + val = expr.getValue() or |
| 26 | + val = expr.(Access).getTarget().getName() |
| 27 | + ) and |
| 28 | + val.regexpMatch("(?i).*(session|login|token|user|auth|credential).*") and |
| 29 | + not val.regexpMatch("(?i).*(xsrf|csrf|forgery).*") |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + predicate isSource(DataFlow::Node source) { isAuthVariable(source.asExpr()) } |
| 34 | + |
| 35 | + predicate isSink(DataFlow::Node sink) { exists(Call c | sink.asExpr() = c.getAnArgument()) } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Tracks if a variable with a sensitive name is used as an argument. |
| 40 | + */ |
| 41 | +private module AuthCookieName = DataFlow::Global<AuthCookieNameConfig>; |
| 42 | + |
| 43 | +/** |
| 44 | + * Configuration module tracking creation of `CookieOptions` to `IResponseCookies.Append(String, String, CookieOptions)` |
| 45 | + * calls as a third parameter. |
| 46 | + */ |
| 47 | +private module CookieOptionsTrackingConfig implements DataFlow::ConfigSig { |
| 48 | + predicate isSource(DataFlow::Node source) { |
| 49 | + source.asExpr().(ObjectCreation).getType() instanceof MicrosoftAspNetCoreHttpCookieOptions |
| 50 | + } |
| 51 | + |
| 52 | + predicate isSink(DataFlow::Node sink) { |
| 53 | + exists(MicrosoftAspNetCoreHttpResponseCookies iResponse, MethodCall mc | |
| 54 | + iResponse.getAppendMethod() = mc.getTarget() and |
| 55 | + mc.getArgument(2) = sink.asExpr() |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Tracking creation of `CookieOptions` to `IResponseCookies.Append(String, String, CookieOptions)` |
| 62 | + * calls as a third parameter. |
| 63 | + */ |
| 64 | +module CookieOptionsTracking = DataFlow::Global<CookieOptionsTrackingConfig>; |
| 65 | + |
| 66 | +/** |
| 67 | + * Looks for property value of `CookiePolicyOptions` passed to `app.UseCookiePolicy` in `Startup.Configure`. |
| 68 | + */ |
| 69 | +Expr getAValueForCookiePolicyProp(string prop) { |
| 70 | + exists(Method m, MethodCall mc, ObjectCreation oc, Expr val | |
| 71 | + m.getName() = "Configure" and |
| 72 | + m.getDeclaringType().getName() = "Startup" and |
| 73 | + m.getBody().getAChild+() = mc and |
| 74 | + mc.getTarget() = |
| 75 | + any(MicrosoftAspNetCoreBuilderCookiePolicyAppBuilderExtensions e).getUseCookiePolicyMethod() and |
| 76 | + oc.getType() instanceof MicrosoftAspNetCoreBuilderCookiePolicyOptions and |
| 77 | + getAValueForProp(oc, _, prop) = val and |
| 78 | + result = val |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A simplistic points-to alternative: given an object creation and a property name, get the values that property can be assigned. |
| 84 | + * |
| 85 | + * Assumptions: |
| 86 | + * - we don't reassign the variable that the creation is stored in |
| 87 | + * - we always access the creation through the same variable it is initially assigned to |
| 88 | + * |
| 89 | + * This should cover most typical patterns... |
| 90 | + */ |
| 91 | +Expr getAValueForProp(ObjectCreation create, Assignment a, string prop) { |
| 92 | + // values set in object init |
| 93 | + exists(MemberInitializer init, Expr src, PropertyAccess pa | |
| 94 | + a.getLValue() = pa and |
| 95 | + pa.getTarget().hasName(prop) and |
| 96 | + init = create.getInitializer().(ObjectInitializer).getAMemberInitializer() and |
| 97 | + init.getLValue() = pa and |
| 98 | + DataFlow::localExprFlow(src, init.getRValue()) and |
| 99 | + result = src |
| 100 | + ) |
| 101 | + or |
| 102 | + // values set on var that create is assigned to |
| 103 | + exists(Expr src, PropertyAccess pa | |
| 104 | + a.getLValue() = pa and |
| 105 | + pa.getTarget().hasName(prop) and |
| 106 | + DataFlow::localExprFlow(create, pa.getQualifier()) and |
| 107 | + DataFlow::localExprFlow(src, a.getRValue()) and |
| 108 | + result = src |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Checks if the given property was explicitly set to a value. |
| 114 | + */ |
| 115 | +predicate isPropertySet(ObjectCreation oc, string prop) { exists(getAValueForProp(oc, _, prop)) } |
| 116 | + |
| 117 | +private signature string propertyName(); |
| 118 | + |
| 119 | +/** |
| 120 | + * Configuration for tracking if a callback used in `OnAppendCookie` sets a cookie property to `true`. |
| 121 | + */ |
| 122 | +private module OnAppendCookieTrackingConfig<propertyName/0 getPropertyName> implements |
| 123 | + DataFlow::ConfigSig |
| 124 | +{ |
| 125 | + /** |
| 126 | + * Specifies the cookie property name to track. |
| 127 | + */ |
| 128 | + predicate isSource(DataFlow::Node source) { |
| 129 | + exists(PropertyWrite pw, Assignment delegateAssign, Callable c | |
| 130 | + pw.getProperty().getName() = "OnAppendCookie" and |
| 131 | + pw.getProperty().getDeclaringType() instanceof MicrosoftAspNetCoreBuilderCookiePolicyOptions and |
| 132 | + delegateAssign.getLValue() = pw and |
| 133 | + ( |
| 134 | + exists(LambdaExpr lambda | |
| 135 | + delegateAssign.getRValue() = lambda and |
| 136 | + lambda = c |
| 137 | + ) |
| 138 | + or |
| 139 | + exists(DelegateCreation delegate | |
| 140 | + delegateAssign.getRValue() = delegate and |
| 141 | + delegate.getArgument().(CallableAccess).getTarget() = c |
| 142 | + ) |
| 143 | + ) and |
| 144 | + c.getParameter(0) = source.asParameter() |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + predicate isSink(DataFlow::Node sink) { |
| 149 | + exists(PropertyWrite pw, Assignment a | |
| 150 | + pw.getProperty().getDeclaringType() instanceof MicrosoftAspNetCoreHttpCookieOptions and |
| 151 | + pw.getProperty().getName() = getPropertyName() and |
| 152 | + a.getLValue() = pw and |
| 153 | + exists(Expr val | |
| 154 | + DataFlow::localExprFlow(val, a.getRValue()) and |
| 155 | + val.getValue() = "true" |
| 156 | + ) and |
| 157 | + sink.asExpr() = pw.getQualifier() |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 162 | + node2.asExpr() = |
| 163 | + any(PropertyRead pr | |
| 164 | + pr.getQualifier() = node1.asExpr() and |
| 165 | + pr.getProperty().getDeclaringType() instanceof |
| 166 | + MicrosoftAspNetCoreCookiePolicyAppendCookieContext |
| 167 | + ) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +private string getPropertyNameSecure() { result = "Secure" } |
| 172 | + |
| 173 | +/** |
| 174 | + * Configuration module for tracking if a callback used in `OnAppendCookie` sets `Secure` to `true`. |
| 175 | + */ |
| 176 | +private module OnAppendCookieSecureTrackingConfig = |
| 177 | + OnAppendCookieTrackingConfig<getPropertyNameSecure/0>; |
| 178 | + |
| 179 | +/** |
| 180 | + * Tracks if a callback used in `OnAppendCookie` sets `Secure` to `true`. |
| 181 | + */ |
| 182 | +module OnAppendCookieSecureTracking = DataFlow::Global<OnAppendCookieSecureTrackingConfig>; |
| 183 | + |
| 184 | +private string getPropertyNameHttpOnly() { result = "HttpOnly" } |
| 185 | + |
| 186 | +/** |
| 187 | + * Configuration module for tracking if a callback used in `OnAppendCookie` sets `HttpOnly` to `true`. |
| 188 | + */ |
| 189 | +private module OnAppendCookieHttpOnlyTrackingConfig = |
| 190 | + OnAppendCookieTrackingConfig<getPropertyNameHttpOnly/0>; |
| 191 | + |
| 192 | +/** |
| 193 | + * Tracks if a callback used in `OnAppendCookie` sets `HttpOnly` to `true`. |
| 194 | + */ |
| 195 | +module OnAppendCookieHttpOnlyTracking = DataFlow::Global<OnAppendCookieHttpOnlyTrackingConfig>; |
0 commit comments