-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
http_validator.go
226 lines (200 loc) · 6.09 KB
/
http_validator.go
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
223
224
225
226
package runn
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
legacyrouter "github.com/getkin/kin-openapi/routers/legacy"
)
type httpValidator interface {
ValidateRequest(ctx context.Context, req *http.Request) error
ValidateResponse(ctx context.Context, req *http.Request, res *http.Response) error
}
type UnsupportedError struct {
Cause error
}
func (e *UnsupportedError) Error() string {
return e.Cause.Error()
}
func (e UnsupportedError) Unwrap() error {
return e.Cause
}
func newHttpValidator(c *httpRunnerConfig) (httpValidator, error) {
if c.OpenApi3DocLocation != "" || c.openApi3Doc != nil {
return newOpenApi3Validator(c)
}
return newNopValidator(), nil
}
type nopValidator struct{}
func (v *nopValidator) ValidateRequest(ctx context.Context, req *http.Request) error {
return nil
}
func (v *nopValidator) ValidateResponse(ctx context.Context, req *http.Request, res *http.Response) error {
return nil
}
func newNopValidator() *nopValidator {
return &nopValidator{}
}
type openApi3Validator struct {
skipValidateRequest bool
skipValidateResponse bool
doc *openapi3.T
}
func newOpenApi3Validator(c *httpRunnerConfig) (*openApi3Validator, error) {
if c.OpenApi3DocLocation != "" {
l := c.OpenApi3DocLocation
ctx := context.Background()
loader := openapi3.NewLoader()
var doc *openapi3.T
switch {
case strings.HasPrefix(l, "https://") || strings.HasPrefix(l, "http://"):
u, err := url.Parse(l)
if err != nil {
return nil, err
}
doc, err = loader.LoadFromURI(u)
if err != nil {
return nil, err
}
default:
b, err := readFile(l)
if err != nil {
return nil, err
}
doc, err = loader.LoadFromData(b)
if err != nil {
return nil, err
}
}
if err := doc.Validate(ctx); err != nil {
return nil, fmt.Errorf("openapi3 document validation error: %w", err)
}
c.openApi3Doc = doc
}
if c.openApi3Doc == nil {
return nil, errors.New("cannot load openapi3 document")
}
// skip scheme://host:port validation
c.openApi3Doc.Servers = nil
return &openApi3Validator{
skipValidateRequest: c.SkipValidateRequest,
skipValidateResponse: c.SkipValidateResponse,
doc: c.openApi3Doc,
}, nil
}
// FIXME: better to depend on any library
// currently refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
var registerBodyMimeTypes = []string{
"text/css", "text/html", "text/csv", "text/xml", "text/javascript",
"image/apng", "image/avif", "image/gif", "image/jpeg", "image/png", "image/svg+xml", "image/webp",
"audio/wave", "audio/wav", "audio/x-wav", "audio/x-pn-wav", "audio/webm", "audio/ogg", "audio/mpeg", "audio/vorbis",
"video/webm", "video/ogg", "video/mp4",
"application/pdf", "application/pkcs8", "application/zip", "application/wasm", "application/ogg",
"font/woff", "font/ttf", "font/otf",
"model/3mf", "model/vrml",
}
func (v *openApi3Validator) ValidateRequest(ctx context.Context, req *http.Request) error {
if v.skipValidateRequest {
return nil
}
input, err := v.requestInput(req)
if err != nil {
return err
}
if err := openapi3filter.ValidateRequest(ctx, input); err != nil {
b, errr := httputil.DumpRequest(req, true)
if errr != nil {
return fmt.Errorf("runn error: %w", errr)
}
return fmt.Errorf("openapi3 validation error: %w\n-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n", err, string(b))
}
return nil
}
func (v *openApi3Validator) requestInput(req *http.Request) (*openapi3filter.RequestValidationInput, error) {
router, err := legacyrouter.NewRouter(v.doc)
if err != nil {
return nil, err
}
route, pathParams, err := router.FindRoute(req)
if err != nil {
return nil, fmt.Errorf("failed to find route: %w (%s %s)", err, req.Method, req.URL.Path)
}
return &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
AuthenticationFunc: openapi3filter.NoopAuthenticationFunc,
},
}, nil
}
func (v *openApi3Validator) responseInput(req *http.Request, res *http.Response) (*openapi3filter.ResponseValidationInput, error) {
reqInput, err := v.requestInput(req)
if err != nil {
return nil, err
}
var body io.ReadCloser
if res.Body != nil {
b, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
res.Body = io.NopCloser(bytes.NewBuffer(b))
body = io.NopCloser(bytes.NewBuffer(b))
}
return &openapi3filter.ResponseValidationInput{
RequestValidationInput: reqInput,
Status: res.StatusCode,
Header: res.Header,
Body: body,
Options: &openapi3filter.Options{IncludeResponseStatus: true},
}, nil
}
func (v *openApi3Validator) ValidateResponse(ctx context.Context, req *http.Request, res *http.Response) error {
if v.skipValidateResponse {
return nil
}
input, err := v.responseInput(req, res)
if err != nil {
return err
}
err = openapi3filter.ValidateResponse(ctx, input)
if err != nil {
var parseError *openapi3filter.ParseError
var responseError *openapi3filter.ResponseError
switch {
case errors.As(err, &parseError):
if parseError.Kind == openapi3filter.KindUnsupportedFormat {
return &UnsupportedError{Cause: err}
}
case errors.As(err, &responseError):
if errors.As(responseError.Err, &parseError) {
if parseError.Kind == openapi3filter.KindUnsupportedFormat {
return &UnsupportedError{Cause: err}
}
}
}
b, errr := httputil.DumpRequest(req, true)
if errr != nil {
return fmt.Errorf("runn error: %w", errr)
}
b2, errr := httputil.DumpResponse(res, true)
if errr != nil {
return fmt.Errorf("runn error: %w", errr)
}
return fmt.Errorf("openapi3 validation error: %w\n-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n-----START HTTP RESPONSE-----\n%s\n-----END HTTP RESPONSE-----\n", err, string(b), string(b2))
}
return nil
}
func init() {
for _, mime := range registerBodyMimeTypes {
openapi3filter.RegisterBodyDecoder(mime, openapi3filter.FileBodyDecoder)
}
}