|
| 1 | +import csharp |
| 2 | +import semmle.code.csharp.frameworks.system.Net |
| 3 | +import semmle.code.csharp.frameworks.System |
| 4 | +import semmle.code.csharp.security.dataflow.flowsources.Remote |
| 5 | +import semmle.code.csharp.security.Sanitizers |
| 6 | + |
| 7 | +//If this leaves experimental this should probably go in semmle.code.csharp.frameworks.system.Net |
| 8 | +/** The `System.Net.WebClient` class. */ |
| 9 | +class SystemNetWebClientClass extends SystemNetClass { |
| 10 | + SystemNetWebClientClass() { this.hasName("WebClient") } |
| 11 | + |
| 12 | + /** Gets the `DownloadString` method. */ |
| 13 | + Method getDownloadStringMethod() { result = this.getAMethod("DownloadString") } |
| 14 | +} |
| 15 | + |
| 16 | +//If this leaves experimental this should probably go in semmle.code.csharp.frameworks.System |
| 17 | +//Extend the already existent SystemUriClass to not touch the stdlib. |
| 18 | +/** The `System.Uri` class. */ |
| 19 | +class SystemUriClassExtra extends SystemUriClass { |
| 20 | + /** Gets the `IsWellFormedUriString` method. */ |
| 21 | + Method getIsWellFormedUriStringMethod() { result = this.getAMethod("IsWellFormedUriString") } |
| 22 | +} |
| 23 | + |
| 24 | +//If this leaves experimental this should probably go in semmle.code.csharp.frameworks.system |
| 25 | +/** |
| 26 | + * A data flow source for uncontrolled data in path expression vulnerabilities. |
| 27 | + */ |
| 28 | +abstract class Source extends DataFlow::Node { } |
| 29 | + |
| 30 | +/** |
| 31 | + * A data flow sink for uncontrolled data in path expression vulnerabilities. |
| 32 | + */ |
| 33 | +abstract class Sink extends DataFlow::ExprNode { } |
| 34 | + |
| 35 | +/** |
| 36 | + * A sanitizer for uncontrolled data in path expression vulnerabilities. |
| 37 | + */ |
| 38 | +abstract class Sanitizer extends DataFlow::ExprNode { } |
| 39 | + |
| 40 | +/** |
| 41 | + * A taint-tracking configuration for uncontrolled data in path expression vulnerabilities. |
| 42 | + */ |
| 43 | +class TaintTrackingConfiguration extends TaintTracking::Configuration { |
| 44 | + TaintTrackingConfiguration() { this = "TaintedWebClientLib" } |
| 45 | + |
| 46 | + override predicate isSource(DataFlow::Node source) { source instanceof Source } |
| 47 | + |
| 48 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 49 | + |
| 50 | + override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer } |
| 51 | +} |
| 52 | + |
| 53 | +/** A source of remote user input. */ |
| 54 | +class RemoteSource extends Source { |
| 55 | + RemoteSource() { this instanceof RemoteFlowSource } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * A path argument to a `WebClient` method call that has an address argument. |
| 60 | + */ |
| 61 | +class WebClientSink extends Sink { |
| 62 | + WebClientSink() { |
| 63 | + exists(Method m | m = any(SystemNetWebClientClass f).getAMethod() | |
| 64 | + this.getExpr() = m.getACall().getArgumentForName("address") |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A call to `System.Uri.IsWellFormedUriString` that is considered to sanitize the input. |
| 71 | + */ |
| 72 | +class RequestMapPathSanitizer extends Sanitizer { |
| 73 | + RequestMapPathSanitizer() { |
| 74 | + exists(Method m | m = any(SystemUriClassExtra uri).getIsWellFormedUriStringMethod() | |
| 75 | + this.getExpr() = m.getACall().getArgument(0) |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private class SimpleTypeSanitizer extends Sanitizer, SimpleTypeSanitizedExpr { } |
| 81 | + |
| 82 | +private class GuidSanitizer extends Sanitizer, GuidSanitizedExpr { } |
0 commit comments