Skip to content

Commit 8f041e3

Browse files
committed
Make API address configurable
Send query to API in the body as JSON using POST
1 parent 0a43ce6 commit 8f041e3

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

gatewayd_plugin.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ plugins:
3030
- METRICS_ENABLED=True
3131
- METRICS_UNIX_DOMAIN_SOCKET=/tmp/gatewayd-plugin-sql-ids-ips.sock
3232
- METRICS_PATH=/metrics
33+
- API_ADDRESS=http://localhost:5000
3334
# Threshold determine the minimum prediction confidence
3435
# required to detect an SQL injection attack. Any value
3536
# between 0 and 1 is valid, and it is inclusive.

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func main() {
5454
pluginInstance.Impl.EnableLibinjection = cast.ToBool(cfg["enableLibinjection"])
5555
pluginInstance.Impl.LibinjectionPermissiveMode = cast.ToBool(
5656
cfg["libinjectionPermissiveMode"])
57+
pluginInstance.Impl.APIAddress = cast.ToString(cfg["apiAddress"])
5758
}
5859

5960
goplugin.Serve(&goplugin.ServeConfig{

plugin/module.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
"metricsUnixDomainSocket": sdkConfig.GetEnv(
3636
"METRICS_UNIX_DOMAIN_SOCKET", "/tmp/gatewayd-plugin-sql-ids-ips.sock"),
3737
"metricsEndpoint": sdkConfig.GetEnv("METRICS_ENDPOINT", "/metrics"),
38+
"apiAddress": sdkConfig.GetEnv("API_ADDRESS", "http://localhost:5000"),
3839
"threshold": sdkConfig.GetEnv("THRESHOLD", "0.8"),
3940
"modelPath": sdkConfig.GetEnv("MODEL_PATH", "sqli_model"),
4041
"enableLibinjection": sdkConfig.GetEnv("ENABLE_LIBINJECTION", "true"),

plugin/plugin.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package plugin
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/base64"
67
"encoding/json"
7-
"fmt"
88
"net/http"
9+
"net/url"
910

1011
"github.com/corazawaf/libinjection-go"
1112
tf "github.com/galeone/tensorflow/tensorflow/go"
@@ -27,6 +28,7 @@ type Plugin struct {
2728
Threshold float32
2829
EnableLibinjection bool
2930
LibinjectionPermissiveMode bool
31+
APIAddress string
3032
}
3133

3234
type InjectionDetectionPlugin struct {
@@ -144,9 +146,27 @@ func (p *Plugin) OnTrafficFromClient(ctx context.Context, req *v1.Struct) (*v1.S
144146
return req
145147
}
146148

147-
// Make an HTTP GET request to the tokenize service.
148-
resp, err := http.Get(
149-
fmt.Sprintf("http://localhost:5000/tokenize_and_sequence/%s", queryString))
149+
// Create a JSON body for the request.
150+
body, err := json.Marshal(map[string]interface{}{
151+
"query": queryString,
152+
})
153+
if err != nil {
154+
p.Logger.Error("Failed to marshal body", "error", err)
155+
if isSQLi(queryString) && !p.LibinjectionPermissiveMode {
156+
return errorResponse(), nil
157+
}
158+
return req, nil
159+
}
160+
// Make an HTTP POST request to the tokenize service.
161+
tokenizeEndpoint, err := url.JoinPath(p.APIAddress, "/tokenize_and_sequence")
162+
if err != nil {
163+
p.Logger.Error("Failed to join API address and path", "error", err)
164+
if isSQLi(queryString) && !p.LibinjectionPermissiveMode {
165+
return errorResponse(), nil
166+
}
167+
return req, nil
168+
}
169+
resp, err := http.Post(tokenizeEndpoint, "application/json", bytes.NewBuffer(body))
150170
if err != nil {
151171
p.Logger.Error("Failed to make GET request", "error", err)
152172
if isSQLi(queryString) && !p.LibinjectionPermissiveMode {

0 commit comments

Comments
 (0)