|
| 1 | +/** |
| 2 | + * Provides models of the [Go Micro library](https://github.com/go-micro/go-micro). |
| 3 | + */ |
| 4 | + |
| 5 | +import go |
| 6 | +private import semmle.go.security.RequestForgeryCustomizations |
| 7 | + |
| 8 | +/** |
| 9 | + * Module for Go Micro framework. |
| 10 | + */ |
| 11 | +module GoMicro { |
| 12 | + /** |
| 13 | + * A GoMicro server type. |
| 14 | + */ |
| 15 | + class GoMicroServerType extends Type { |
| 16 | + GoMicroServerType() { this.hasQualifiedName("go-micro.dev/v4/server", "Server") } |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * A GoMicro client type. |
| 21 | + */ |
| 22 | + class GoMicroClientType extends Type { |
| 23 | + GoMicroClientType() { this.hasQualifiedName("go-micro.dev/v4/client", "Client") } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * A file that is generated by the protobuf compiler. |
| 28 | + */ |
| 29 | + class ProtocGeneratedFile extends File { |
| 30 | + ProtocGeneratedFile() { this.getBaseName().regexpMatch(".*\\.pb(\\.micro)?\\.go$") } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * A type that is generated by the protobuf compiler. |
| 35 | + */ |
| 36 | + class ProtocMessageType extends Type { |
| 37 | + ProtocMessageType() { |
| 38 | + this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) and |
| 39 | + exists(MethodDecl md | |
| 40 | + md.getName() = "ProtoMessage" and |
| 41 | + this = md.getReceiverDecl().getTypeExpr().getAChild().(TypeName).getType() |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A Server Interface type. |
| 48 | + */ |
| 49 | + class ServiceInterfaceType extends InterfaceType { |
| 50 | + NamedType namedType; |
| 51 | + |
| 52 | + ServiceInterfaceType() { |
| 53 | + this = namedType.getUnderlyingType() and |
| 54 | + namedType.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets the name of the interface. |
| 59 | + */ |
| 60 | + override string getName() { result = namedType.getName() } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the named type on top of this interface type. |
| 64 | + */ |
| 65 | + NamedType getNamedType() { result = namedType } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A Service server handler type. |
| 70 | + */ |
| 71 | + class ServiceServerType extends NamedType { |
| 72 | + ServiceServerType() { |
| 73 | + this.implements(any(ServiceInterfaceType i)) and |
| 74 | + this.getName().regexpMatch("(?i).*Handler") and |
| 75 | + this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * A Client server handler type. |
| 81 | + */ |
| 82 | + class ClientServiceType extends NamedType { |
| 83 | + ClientServiceType() { |
| 84 | + this.implements(any(ServiceInterfaceType i)) and |
| 85 | + this.getName().regexpMatch("(?i).*Service") and |
| 86 | + this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * A service register handler. |
| 92 | + */ |
| 93 | + class ServiceRegisterHandler extends Function { |
| 94 | + ServiceRegisterHandler() { |
| 95 | + this.getName().regexpMatch("(?i)register" + any(ServiceServerType c).getName()) and |
| 96 | + this.getParameterType(0) instanceof GoMicroServerType and |
| 97 | + this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * A service handler. |
| 103 | + */ |
| 104 | + class ServiceHandler extends Method { |
| 105 | + ServiceHandler() { |
| 106 | + exists(DataFlow::CallNode call | |
| 107 | + call.getTarget() instanceof ServiceRegisterHandler and |
| 108 | + this = call.getArgument(1).getType().getMethod(_) and |
| 109 | + this.implements(any(ServiceInterfaceType i).getNamedType().getMethod(_)) |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A client service function. |
| 116 | + */ |
| 117 | + class ClientService extends Function { |
| 118 | + ClientService() { |
| 119 | + this.getName().regexpMatch("(?i)new" + any(ClientServiceType c).getName()) and |
| 120 | + this.getParameterType(0) instanceof StringType and |
| 121 | + this.getParameterType(1) instanceof GoMicroClientType and |
| 122 | + this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * An SSRF sink for the Client service function. |
| 128 | + */ |
| 129 | + class ClientRequestUrlAsSink extends RequestForgery::Sink { |
| 130 | + ClientRequestUrlAsSink() { |
| 131 | + exists(DataFlow::CallNode call | |
| 132 | + call.getArgument(0) = this and |
| 133 | + call.getTarget() instanceof ClientService |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + override DataFlow::Node getARequest() { result = this } |
| 138 | + |
| 139 | + override string getKind() { result = "URL" } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * A set of remote requests from a service handler. |
| 144 | + */ |
| 145 | + class Request extends UntrustedFlowSource::Range instanceof DataFlow::ParameterNode { |
| 146 | + Request() { |
| 147 | + exists(ServiceHandler handler | |
| 148 | + this.asParameter().isParameterOf(handler.getFuncDecl(), 1) and |
| 149 | + handler.getParameterType(0).hasQualifiedName("context", "Context") and |
| 150 | + this.getType().(PointerType).getBaseType() instanceof ProtocMessageType |
| 151 | + ) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments