Skip to content

Commit 236e81d

Browse files
committed
feat: initial GoMicro modelling
1 parent 3fed279 commit 236e81d

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import go
2+
3+
module GoMicro {
4+
class GoMicroServerType extends Type {
5+
GoMicroServerType() { this.hasQualifiedName("go-micro.dev/v4/server", "Server") }
6+
}
7+
8+
/**
9+
* A file that is generated by the protobuf compiler.
10+
*/
11+
class ProtocGeneratedFile extends File {
12+
ProtocGeneratedFile() {
13+
exists(string prefix, File f |
14+
prefix = this.getBaseName().regexpCapture("^(.*)\\.pb(\\.micro)?\\.go$", 1) and
15+
this.getParentContainer() = f.getParentContainer()
16+
)
17+
}
18+
}
19+
20+
/**
21+
* A type that is generated by the protobuf compiler.
22+
*/
23+
class ProtocMessageType extends Type {
24+
ProtocMessageType() {
25+
exists(TypeEntity te |
26+
te.getType() = this and
27+
te.getDeclaration().getLocation().getFile() instanceof ProtocGeneratedFile and
28+
exists(MethodDecl md |
29+
md.getName() = "ProtoMessage" and
30+
this = md.getReceiverDecl().getTypeExpr().getAChild().(TypeName).getType()
31+
)
32+
)
33+
}
34+
35+
override string getName() { result = this.getUnderlyingType().getName() }
36+
}
37+
38+
/**
39+
* Server internal
40+
*/
41+
class ServiceInterfaceType extends InterfaceType {
42+
NamedType namedType;
43+
44+
ServiceInterfaceType() {
45+
exists(TypeEntity te |
46+
te.getType() = namedType and
47+
namedType.getUnderlyingType() = this and
48+
te.getDeclaration().getLocation().getFile() instanceof ProtocGeneratedFile
49+
)
50+
}
51+
52+
/**
53+
* Gets the name of the interface.
54+
*/
55+
override string getName() { result = namedType.getName() }
56+
57+
/**
58+
* Gets the named type on top of this interface type.
59+
*/
60+
NamedType getNamedType() { result = namedType }
61+
}
62+
63+
/**
64+
* A service server type.
65+
*/
66+
class ServiceServerType extends NamedType {
67+
ServiceServerType() {
68+
exists(ServiceInterfaceType i, TypeEntity te |
69+
this.implements(i) and
70+
this.getName().regexpMatch("(?i).*Handler") and
71+
te.getType() = this and
72+
te.getDeclaration().getLocation().getFile() instanceof ProtocGeneratedFile
73+
)
74+
}
75+
}
76+
77+
/**
78+
* A service register handler.
79+
*/
80+
class ServiceRegisterHandler extends Function {
81+
ServiceRegisterHandler() {
82+
exists(ServiceServerType c, GoMicroServerType s |
83+
this.getName().regexpMatch("(?i)register" + c.getName()) and
84+
this.getParameterType(0) = s and
85+
this.getDeclaration().getLocation().getFile() instanceof ProtocGeneratedFile
86+
)
87+
}
88+
}
89+
90+
/**
91+
* A service handler.
92+
*/
93+
class ServiceHandler extends Method {
94+
ServiceHandler() {
95+
exists(DataFlow::CallNode call, Type handlerType, ServiceInterfaceType i |
96+
call.getTarget() instanceof ServiceRegisterHandler and
97+
call.getArgument(1).getType() = handlerType and
98+
this = handlerType.getMethod(_) and
99+
this.implements(i.getNamedType().getMethod(_))
100+
)
101+
}
102+
}
103+
104+
/**
105+
* Remote / Untrusted requests coming to the service handler.
106+
*/
107+
class Request extends UntrustedFlowSource::Range instanceof DataFlow::ParameterNode {
108+
Request() {
109+
exists(FuncDef c, ServiceHandler handler | handler.getFuncDecl() = c |
110+
this.asParameter().isParameterOf(c, 1) and
111+
handler.getParameterType(0).hasQualifiedName("context", "Context") and
112+
this.getType().(PointerType).getBaseType() instanceof ProtocMessageType
113+
)
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)