forked from narrative-bi/traefik-graphql-limits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
222 lines (187 loc) · 5.21 KB
/
Copy pathmain.go
File metadata and controls
222 lines (187 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Package traefikgraphqllimits provides a Traefik plugin to limit the depth of a GraphQL query
package traefikgraphqllimits
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
"github.com/graphql-go/graphql/language/parser"
"github.com/graphql-go/graphql/language/visitor"
)
const errorBodyReadResponse = `{
"errors": [
{
"code": 400,
"message": "Failed to read request body"
}
]
}`
const errorGraphqlParsingResponse = `{
"errors": [
{
"code": 400,
"message": "Not a valid graphql query"
}
]
}`
func buildGraphqlMaxDepthError(maxDepth, depthLimit int) string {
errorBody := fmt.Sprintf(`{
"errors": [
{
"code": 400,
"message": "Query has depth of %d, which exceeds max depth of %d"
}
] }`, maxDepth, depthLimit)
return errorBody
}
func buildGraphqlBatchLimitError(batchCount, batchLimit int) string {
errorBody := fmt.Sprintf(`{
"errors": [
{
"code": 400,
"message": "Query batch limit of %d, which exceeds limit of %d"
}
] }`, batchCount, batchLimit)
return errorBody
}
func buildGraphqlNodeLimitError(nodeCount, nodeLimit int) string {
errorBody := fmt.Sprintf(`{
"errors": [
{
"code": 400,
"message": "Query node limit of %d, which exceeds limit of %d"
}
] }`, nodeCount, nodeLimit)
return errorBody
}
// QueryMetrics the query metrics for check.
type QueryMetrics struct {
maxDepth int
batchCount int
nodeCount int
}
// CreateQueryMetrics creates the default query metrics.
func (queryMetrics QueryMetrics) CreateQueryMetrics() QueryMetrics {
queryMetrics.maxDepth = 0
queryMetrics.batchCount = 0
queryMetrics.nodeCount = 0
return queryMetrics
}
// Config the plugin configuration.
type Config struct {
GraphQLPath string
DepthLimit int
BatchLimit int
NodeLimit int
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
GraphQLPath: "/graphql",
DepthLimit: 0,
BatchLimit: 0,
NodeLimit: 0,
}
}
// GraphqlLimit plugin configuration structure.
type GraphqlLimit struct {
next http.Handler
name string
graphQLPath string
depthLimit int
batchLimit int
nodeLimit int
}
func calculateQueryMetrics(astDoc *ast.Document) QueryMetrics {
queryMetrics := new(QueryMetrics).CreateQueryMetrics()
v := &visitor.VisitorOptions{
KindFuncMap: map[string]visitor.NamedVisitFuncs{
kinds.SelectionSet: {
Enter: func(p visitor.VisitFuncParams) (string, interface{}) {
// NOTE: We do not calculate initial query depth here, so we start at -1
depth := -1
for _, element := range p.Path {
if element == kinds.SelectionSet {
depth++
}
}
// NOTE: Top level query is start of new batch, otherwise it is a node
if depth == 0 {
queryMetrics.batchCount++
} else {
queryMetrics.nodeCount++
}
if depth > queryMetrics.maxDepth {
queryMetrics.maxDepth = depth
}
return visitor.ActionNoChange, nil
},
},
},
}
_ = visitor.Visit(astDoc, v, nil)
return queryMetrics
}
// New created a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &GraphqlLimit{
next: next,
name: name,
graphQLPath: config.GraphQLPath,
depthLimit: config.DepthLimit,
batchLimit: config.BatchLimit,
nodeLimit: config.NodeLimit,
}, nil
}
func respondWithJSONError(rw http.ResponseWriter, json string) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusBadRequest)
_, err := rw.Write([]byte(json))
if err != nil {
log.Printf("Error with response: %v", err)
}
}
func isGraphqlRequest(req *http.Request, path string) bool {
return req.Method == "POST" && req.URL.Path == path
}
func needToCheckLimits(depthLimit, batchLimit, nodeLimit int) bool {
return depthLimit > 0 || batchLimit > 0 || nodeLimit > 0
}
func (d *GraphqlLimit) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
body, err := io.ReadAll(req.Body)
if err != nil {
log.Printf("Error reading body: %v", err)
respondWithJSONError(rw, errorBodyReadResponse)
return
}
if isGraphqlRequest(req, d.graphQLPath) && needToCheckLimits(d.depthLimit, d.batchLimit, d.nodeLimit) {
params := parser.ParseParams{
Source: string(body),
Options: parser.ParseOptions{
NoLocation: true,
},
}
parseResults, err := parser.Parse(params)
if err != nil {
respondWithJSONError(rw, errorGraphqlParsingResponse)
return
}
queryMetrics := calculateQueryMetrics(parseResults)
if d.depthLimit > 0 && queryMetrics.maxDepth > d.depthLimit {
respondWithJSONError(rw, buildGraphqlMaxDepthError(queryMetrics.maxDepth, d.depthLimit))
return
}
if d.batchLimit > 0 && queryMetrics.batchCount > d.batchLimit {
respondWithJSONError(rw, buildGraphqlBatchLimitError(queryMetrics.batchCount, d.batchLimit))
}
if d.nodeLimit > 0 && queryMetrics.nodeCount > d.nodeLimit {
respondWithJSONError(rw, buildGraphqlNodeLimitError(queryMetrics.nodeCount, d.nodeLimit))
}
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
d.next.ServeHTTP(rw, req)
}