Skip to content

Commit 25e7dde

Browse files
GeekMasherowen-mc
andauthored
Apply suggestions from code review
Co-authored-by: Owen Mansel-Chan <[email protected]>
1 parent e31cce5 commit 25e7dde

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ module GoMicro {
108108
*/
109109
class ServiceHandler extends Method {
110110
ServiceHandler() {
111-
exists(DataFlow::CallNode call, Type handlerType, ServiceInterfaceType i |
111+
exists(DataFlow::CallNode call |
112112
call.getTarget() instanceof ServiceRegisterHandler and
113-
call.getArgument(1).getType() = handlerType and
114-
this = handlerType.getMethod(_) and
115-
this.implements(i.getNamedType().getMethod(_))
113+
this = call.getArgument(1).getType().getMethod(_) and
114+
this.implements(any(ServiceInterfaceType i).getNamedType().getMethod(_))
116115
)
117116
}
118117
}
@@ -122,12 +121,10 @@ module GoMicro {
122121
*/
123122
class ClientService extends Function {
124123
ClientService() {
125-
exists(ClientServiceType c |
126-
this.getName().regexpMatch("(?i)new" + c.getName()) and
127-
this.getParameterType(0) instanceof StringType and
128-
this.getParameterType(1) instanceof GoMicroClientType and
129-
this.getDeclaration().getLocation().getFile() instanceof ProtocGeneratedFile
130-
)
124+
this.getName().regexpMatch("(?i)new" + any(ClientServiceType c).getName()) and
125+
this.getParameterType(0) instanceof StringType and
126+
this.getParameterType(1) instanceof GoMicroClientType and
127+
this.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _)
131128
}
132129
}
133130

@@ -152,8 +149,8 @@ module GoMicro {
152149
*/
153150
class Request extends UntrustedFlowSource::Range instanceof DataFlow::ParameterNode {
154151
Request() {
155-
exists(FuncDef c, ServiceHandler handler | handler.getFuncDecl() = c |
156-
this.asParameter().isParameterOf(c, 1) and
152+
exists(ServiceHandler handler |
153+
this.asParameter().isParameterOf(handler.getFuncDecl(), 1) and
157154
handler.getParameterType(0).hasQualifiedName("context", "Context") and
158155
this.getType().(PointerType).getBaseType() instanceof ProtocMessageType
159156
)

go/ql/test/library-tests/semmle/go/frameworks/GoMicro/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
// context
1717
ctx := context.Background()
1818

19-
greeterService := pb.NewGreeterService("http://localhost:8000", service.Client())
19+
greeterService := pb.NewGreeterService("http://localhost:8000", service.Client()) // $ clientRequest="http:\/\/localhost:8000"
2020
// request
2121
req := pb.Request{Name: "Mona"}
2222
resp, err := greeterService.Hello(ctx, &req)
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
import go
2+
import TestUtilities.InlineExpectationsTest
23

3-
query predicate serverRequests(DataFlow::ParameterNode node) { node instanceof GoMicro::Request }
4+
module GoMicroTest implements TestSig {
5+
string getARelevantTag() { result = ["serverRequest", "clientRequest"] }
46

5-
query predicate clientRequests(DataFlow::Node node) {
6-
node instanceof GoMicro::ClientRequestUrlAsSink
7+
predicate hasActualResult(Location location, string element, string tag, string value) {
8+
exists(DataFlow::Node node |
9+
node.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
10+
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and
11+
(
12+
node instanceof GoMicro::Request and
13+
element = node.toString() and
14+
value = "\"" + node.toString() + "\"" and
15+
tag = "serverRequest"
16+
or
17+
node instanceof GoMicro::ClientRequestUrlAsSink and
18+
element = node.toString() and
19+
value = node.toString().replaceAll("/", "\\/") and
20+
tag = "clientRequest"
21+
)
22+
)
23+
}
724
}
25+
26+
import MakeTest<GoMicroTest>

go/ql/test/library-tests/semmle/go/frameworks/GoMicro/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
type Greeter struct{}
1717

18-
func (g *Greeter) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
18+
func (g *Greeter) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error { // $ serverRequest="definition of req"
1919
// var access
2020
name := req.Name
2121
fmt.Println("Name :: %s", name)

0 commit comments

Comments
 (0)