Skip to content

Commit 6acf92b

Browse files
dbarrosopfenollp
andauthored
openapi3filter: use FileBodyDecoder if the format is specified as binary (#1088)
* use FileBodyDecoder if the format is specified as binary * added tests * clean up zip test * Update openapi3filter/req_resp_decoder.go --------- Co-authored-by: Pierre Fenoll <[email protected]>
1 parent 4358c4a commit 6acf92b

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

openapi3filter/req_resp_decoder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,13 @@ var headerCT = http.CanonicalHeaderKey("Content-Type")
12331233

12341234
const prefixUnsupportedCT = "unsupported content type"
12351235

1236+
func isBinary(schema *openapi3.SchemaRef) bool {
1237+
if schema == nil || schema.Value == nil {
1238+
return false
1239+
}
1240+
return schema.Value.Type.Is("string") && schema.Value.Format == "binary"
1241+
}
1242+
12361243
// decodeBody returns a decoded body.
12371244
// The function returns ParseError when a body is invalid.
12381245
func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (
@@ -1246,8 +1253,13 @@ func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef,
12461253
contentType = "text/plain"
12471254
}
12481255
}
1256+
12491257
mediaType := parseMediaType(contentType)
12501258
decoder, ok := bodyDecoders[mediaType]
1259+
if !ok && isBinary(schema) {
1260+
ok, decoder = true, FileBodyDecoder
1261+
}
1262+
12511263
if !ok {
12521264
return "", nil, &ParseError{
12531265
Kind: KindUnsupportedFormat,
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package openapi3filter_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io"
7+
"mime/multipart"
8+
"net/http"
9+
"net/textproto"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/getkin/kin-openapi/openapi3"
15+
"github.com/getkin/kin-openapi/openapi3filter"
16+
"github.com/getkin/kin-openapi/routers/gorillamux"
17+
)
18+
19+
func TestValidateUploadArbitraryBinaryFile(t *testing.T) {
20+
const spec = `
21+
openapi: 3.0.0
22+
info:
23+
title: 'Validator'
24+
version: 0.0.1
25+
paths:
26+
/test:
27+
post:
28+
requestBody:
29+
required: true
30+
content:
31+
multipart/form-data:
32+
schema:
33+
type: object
34+
required:
35+
- file
36+
properties:
37+
file:
38+
type: string
39+
format: binary
40+
responses:
41+
'200':
42+
description: Created
43+
`
44+
45+
loader := openapi3.NewLoader()
46+
doc, err := loader.LoadFromData([]byte(spec))
47+
require.NoError(t, err)
48+
49+
err = doc.Validate(loader.Context)
50+
require.NoError(t, err)
51+
52+
router, err := gorillamux.NewRouter(doc)
53+
require.NoError(t, err)
54+
55+
tests := []struct {
56+
zipData []byte
57+
wantErr bool
58+
}{
59+
{
60+
[]byte{
61+
0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x7d, 0x23, 0x56, 0xcd, 0xfd, 0x67, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x09, 0x00, 0x1c, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x09, 0x00, 0x03, 0xac, 0xce, 0xb3, 0x63, 0xaf, 0xce, 0xb3, 0x63, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xf7, 0x01, 0x00, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x7d, 0x23, 0x56, 0xcd, 0xfd, 0x67, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x09, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x00, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x05, 0x00, 0x03, 0xac, 0xce, 0xb3, 0x63, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xf7, 0x01, 0x00, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00,
62+
},
63+
false,
64+
},
65+
{
66+
[]byte{
67+
0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68+
}, // No entry
69+
false,
70+
},
71+
}
72+
for _, tt := range tests {
73+
body := &bytes.Buffer{}
74+
writer := multipart.NewWriter(body)
75+
76+
{ // Add file data
77+
h := make(textproto.MIMEHeader)
78+
h.Set("Content-Disposition", `form-data; name="file"; filename="hello.zip"`)
79+
h.Set("Content-Type", "application/zip")
80+
81+
fw, err := writer.CreatePart(h)
82+
require.NoError(t, err)
83+
_, err = io.Copy(fw, bytes.NewReader(tt.zipData))
84+
85+
require.NoError(t, err)
86+
}
87+
88+
writer.Close()
89+
90+
req, err := http.NewRequest(http.MethodPost, "/test", bytes.NewReader(body.Bytes()))
91+
require.NoError(t, err)
92+
93+
req.Header.Set("Content-Type", writer.FormDataContentType())
94+
95+
route, pathParams, err := router.FindRoute(req)
96+
require.NoError(t, err)
97+
98+
if err = openapi3filter.ValidateRequestBody(
99+
context.Background(),
100+
&openapi3filter.RequestValidationInput{
101+
Request: req,
102+
PathParams: pathParams,
103+
Route: route,
104+
},
105+
route.Operation.RequestBody.Value,
106+
); err != nil {
107+
if !tt.wantErr {
108+
t.Errorf("got %v", err)
109+
}
110+
continue
111+
}
112+
if tt.wantErr {
113+
t.Errorf("want err")
114+
}
115+
}
116+
}

openapi3filter/zip_file_upload_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818

1919
func TestValidateZipFileUpload(t *testing.T) {
2020
openapi3filter.RegisterBodyDecoder("application/zip", openapi3filter.ZipFileBodyDecoder)
21+
t.Cleanup(func() {
22+
openapi3filter.UnregisterBodyDecoder("application/zip")
23+
})
2124

2225
const spec = `
2326
openapi: 3.0.0

0 commit comments

Comments
 (0)