|
| 1 | +/** |
| 2 | + * @name Unsafe `CertificateValidationCallback` use. |
| 3 | + * @description Using a `CertificateValidationCallback` that always returns `true` is insecure, as it allows any certificate to be accepted as valid. |
| 4 | + * @kind alert |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id cs/unsafe-certificate-validation |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-295 |
| 10 | + * external/cwe/cwe-297 |
| 11 | + */ |
| 12 | + |
| 13 | +import csharp |
| 14 | + |
| 15 | +/** |
| 16 | + * Provides an abstract base class for properties related to certificate validation. |
| 17 | + */ |
| 18 | +abstract private class CertificateValidationProperty extends Property { } |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents the `ServerCertificateValidationCallback` property of the `ServicePointManager` class. |
| 22 | + */ |
| 23 | +private class ServicePointManagerServerCertificateValidationCallback extends CertificateValidationProperty |
| 24 | +{ |
| 25 | + ServicePointManagerServerCertificateValidationCallback() { |
| 26 | + this.getDeclaringType().hasFullyQualifiedName("System.Net", "ServicePointManager") and |
| 27 | + this.hasName("ServerCertificateValidationCallback") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents the `ServerCertificateCustomValidationCallback` property of the `HttpClientHandler` class. |
| 33 | + */ |
| 34 | +private class HttpClientHandlerServerCertificateCustomValidationCallback extends CertificateValidationProperty |
| 35 | +{ |
| 36 | + HttpClientHandlerServerCertificateCustomValidationCallback() { |
| 37 | + this.getDeclaringType().hasFullyQualifiedName("System.Net.Http", "HttpClientHandler") and |
| 38 | + this.hasName("ServerCertificateCustomValidationCallback") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Represents the creation of an `SslStream` object. |
| 44 | + */ |
| 45 | +private class SslStreamCreation extends ObjectCreation { |
| 46 | + SslStreamCreation() { |
| 47 | + this.getObjectType().hasFullyQualifiedName("System.Net.Security", "SslStream") |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the expression used as the server certificate validation callback argument |
| 52 | + * in the creation of the `SslStream` object. |
| 53 | + */ |
| 54 | + Expr getServerCertificateValidationCallback() { result = this.getArgument(2) } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Represents the `ServerCertificateValidationCallback` property of the `HttpWebRequest` class. |
| 59 | + */ |
| 60 | +private class HttpWebRequestServerCertificateValidationCallback extends CertificateValidationProperty |
| 61 | +{ |
| 62 | + HttpWebRequestServerCertificateValidationCallback() { |
| 63 | + this.getDeclaringType().hasFullyQualifiedName("System.Net", "HttpWebRequest") and |
| 64 | + this.hasName("ServerCertificateValidationCallback") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Holds if `c` always returns `true`. |
| 70 | + */ |
| 71 | +private predicate alwaysReturnsTrue(Callable c) { |
| 72 | + forex(ReturnStmt rs | rs.getEnclosingCallable() = c | |
| 73 | + rs.getExpr().(BoolLiteral).getBoolValue() = true |
| 74 | + ) |
| 75 | + or |
| 76 | + c.getBody().(BoolLiteral).getBoolValue() = true |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Gets the actual callable object referred to by expression `e`. |
| 81 | + * This can be a direct reference to a callable, a delegate creation, or an anonymous function. |
| 82 | + */ |
| 83 | +Callable getActualCallable(Expr e) { |
| 84 | + exists(Expr dcArg | dcArg = e.(DelegateCreation).getArgument() | |
| 85 | + result = dcArg.(CallableAccess).getTarget() or result = dcArg.(AnonymousFunctionExpr) |
| 86 | + ) |
| 87 | + or |
| 88 | + result = e.(Callable) |
| 89 | +} |
| 90 | + |
| 91 | +from Expr e, Callable c |
| 92 | +where |
| 93 | + [ |
| 94 | + any(SslStreamCreation yy).getServerCertificateValidationCallback(), |
| 95 | + any(CertificateValidationProperty xx).getAnAssignedValue() |
| 96 | + ] = e and |
| 97 | + getActualCallable(e) = c and |
| 98 | + alwaysReturnsTrue(c) |
| 99 | +select e, "$@ that is defined $@ and accepts any certificate as valid, is used here.", e, |
| 100 | + "This certificate callback", c, "here" |
0 commit comments