Skip to content

Commit 89db17f

Browse files
committed
Fix plugin crash when accessing non-query messages
1 parent 2bb6279 commit 89db17f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

plugin/plugin.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plugin
33
import (
44
"bytes"
55
"context"
6+
"encoding/base64"
67
"encoding/json"
78
"net/http"
89

@@ -69,18 +70,34 @@ func (p *Plugin) OnTrafficFromClient(
6970
p.Logger.Info("Failed to handle client message", "error", err)
7071
}
7172

73+
// Get the client request from the GatewayD request.
7274
request := cast.ToString(sdkPlugin.GetAttr(req, "request", ""))
7375
if request == "" {
7476
return req, nil
7577
}
7678

77-
query, err := postgres.GetQueryFromRequest(request)
78-
if err != nil {
79-
p.Logger.Error("Failed to get query from request", "error", err)
79+
// Get the query from the request.
80+
query := cast.ToString(sdkPlugin.GetAttr(req, "query", ""))
81+
if query == "" {
82+
p.Logger.Debug("Failed to get query from request, possibly not a SQL query request")
8083
return req, nil
8184
}
8285
p.Logger.Info("Query", "query", query)
8386

87+
// Decode the query.
88+
decodedQuery, err := base64.StdEncoding.DecodeString(query)
89+
if err != nil {
90+
return req, err
91+
}
92+
p.Logger.Info("Decoded Query", "decodedQuery", decodedQuery)
93+
94+
// Unmarshal query into a map.
95+
var queryMap map[string]interface{}
96+
if err := json.Unmarshal(decodedQuery, &queryMap); err != nil {
97+
p.Logger.Error("Failed to unmarshal query", "error", err)
98+
return req, nil
99+
}
100+
84101
model, err := tf.LoadSavedModel("sqli_model", []string{"serve"}, nil)
85102
if err != nil {
86103
p.Logger.Error("Failed to load model", "error", err)
@@ -90,7 +107,7 @@ func (p *Plugin) OnTrafficFromClient(
90107

91108
// Create the JSON body from the map.
92109
body, err := json.Marshal(map[string]interface{}{
93-
"query": query,
110+
"query": queryMap["String"],
94111
})
95112
if err != nil {
96113
p.Logger.Error("Failed to marshal query", "error", err)

0 commit comments

Comments
 (0)