Skip to content

Commit 3502ab6

Browse files
author
Alvaro Muñoz
committed
fix missing QLDocs and refactor ServiceInterface
1 parent afa6b1c commit 3502ab6

File tree

2 files changed

+26
-13
lines changed
  • go/ql
    • lib/semmle/go/frameworks
    • test/library-tests/semmle/go/frameworks/Twirp

2 files changed

+26
-13
lines changed

go/ql/lib/semmle/go/frameworks/Twirp.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
/** Provides models of commonly used functions and types in the twirp packages. */
2+
13
import go
24
import semmle.go.security.RequestForgery
35

6+
/** Provides models of commonly used functions and types in the twirp packages. */
47
module Twirp {
58
/**
69
* A *.pb.go file generated by Twirp.
@@ -47,17 +50,27 @@ module Twirp {
4750
/**
4851
* An interface type representing a Twirp service.
4952
*/
50-
class ServiceInterface extends NamedType {
53+
class ServiceInterface extends InterfaceType {
54+
NamedType serviceInterface;
55+
5156
ServiceInterface() {
5257
exists(TypeEntity te |
53-
te.getType() = this and
54-
// To match an Interface type we need to use a NamedType whose getUnderlying type is an InterfaceType
55-
this.getUnderlyingType() instanceof InterfaceType and
58+
te.getType() = serviceInterface and
59+
this instanceof InterfaceType and
60+
serviceInterface.getUnderlyingType() = this and
5661
te.getDeclaration().getLocation().getFile() instanceof ServicesGeneratedFile
5762
)
5863
}
5964

60-
InterfaceType getInterfaceType() { result = this.getUnderlyingType() }
65+
/**
66+
* Returns the name of the interface
67+
*/
68+
override string getName() { result = serviceInterface.getName() }
69+
70+
/**
71+
* Returns the named type on top of this interface type
72+
*/
73+
NamedType getNamedType() { result = serviceInterface }
6174
}
6275

6376
/**
@@ -68,7 +81,7 @@ module Twirp {
6881

6982
ServiceClient() {
7083
exists(ServiceInterface i |
71-
pointerType.implements(i.getInterfaceType()) and
84+
pointerType.implements(i) and
7285
this = pointerType.getBaseType() and
7386
this.getName().toLowerCase() = i.getName().toLowerCase() + ["protobuf", "json"] + "client"
7487
)
@@ -81,7 +94,7 @@ module Twirp {
8194
class ServiceServer extends NamedType {
8295
ServiceServer() {
8396
exists(ServiceInterface i |
84-
this.implements(i.getInterfaceType()) and
97+
this.implements(i) and
8598
this.getName().toLowerCase() = i.getName().toLowerCase() + "server"
8699
)
87100
}
@@ -106,9 +119,9 @@ module Twirp {
106119
*/
107120
class ServerConstructor extends Function {
108121
ServerConstructor() {
109-
exists(ServiceServer c |
122+
exists(ServiceServer c, ServiceInterface i |
110123
this.getName().toLowerCase() = "new" + c.getName().toLowerCase() and
111-
this.getParameter(0).getType() instanceof ServiceInterface
124+
this.getParameter(0).getType() = i.getNamedType()
112125
)
113126
}
114127
}
@@ -139,10 +152,10 @@ module Twirp {
139152
exists(DataFlow::CallNode call, Type handlerType, ServiceInterface i |
140153
call.getTarget() instanceof ServerConstructor and
141154
call.getArgument(0).getType() = handlerType and
142-
handlerType.implements(i.getInterfaceType()) and
155+
handlerType.implements(i) and
143156
this = handlerType.getMethod(_) and
144157
this.implements(m) and
145-
i.getMethod(_) = m
158+
i.getNamedType().getMethod(_) = m
146159
)
147160
}
148161
}

go/ql/test/library-tests/semmle/go/frameworks/Twirp/tests.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ query predicate passingPositiveTests(string res, string expectation, InlineTest
5252
exists(Twirp::ProtobufMessage n | t.inType(n))
5353
or
5454
expectation = "serviceInterface" and
55-
exists(Twirp::ServiceInterface n | t.inType(n))
55+
exists(Twirp::ServiceInterface n | t.inType(n.getNamedType()))
5656
or
5757
expectation = "serviceClient" and
5858
exists(Twirp::ServiceClient n | t.inType(n))
@@ -85,7 +85,7 @@ query predicate failingPositiveTests(string res, string expectation, InlineTest
8585
not exists(Twirp::ProtobufMessage n | t.inType(n))
8686
or
8787
expectation = "serviceInterface" and
88-
not exists(Twirp::ServiceInterface n | t.inType(n))
88+
not exists(Twirp::ServiceInterface n | t.inType(n.getNamedType()))
8989
or
9090
expectation = "serviceClient" and
9191
not exists(Twirp::ServiceClient n | t.inType(n))

0 commit comments

Comments
 (0)