-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathmultipart.go
124 lines (114 loc) · 3.37 KB
/
multipart.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
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package bodyprocessors
import (
"errors"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"os"
"strings"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
"github.com/corazawaf/coraza/v3/internal/collections"
"github.com/corazawaf/coraza/v3/internal/environment"
)
type multipartBodyProcessor struct{}
func (mbp *multipartBodyProcessor) ProcessRequest(reader io.Reader, v plugintypes.TransactionVariables, options plugintypes.BodyProcessorOptions) error {
mimeType := options.Mime
storagePath := options.StoragePath
mediaType, params, err := mime.ParseMediaType(mimeType)
if err != nil {
log.Fatalf("failed to parse media type: %s", err.Error())
}
if !strings.HasPrefix(mediaType, "multipart/") {
return errors.New("not a multipart body")
}
mr := multipart.NewReader(reader, params["boundary"])
totalSize := int64(0)
filesCol := v.Files()
filesTmpNamesCol := v.FilesTmpNames()
fileSizesCol := v.FilesSizes()
postCol := v.ArgsPost()
filesCombinedSizeCol := v.FilesCombinedSize()
filesNamesCol := v.FilesNames()
headersNames := v.MultipartPartHeaders()
for {
p, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
return err
}
partName := p.FormName()
for key, values := range p.Header {
for _, value := range values {
headersNames.Add(partName, fmt.Sprintf("%s: %s", key, value))
}
}
// if is a file
filename := originFileName(p)
if filename != "" {
var size int64
if environment.HasAccessToFS {
// Only copy file to temp when not running in TinyGo
temp, err := os.CreateTemp(storagePath, "crzmp*")
if err != nil {
return err
}
sz, err := io.Copy(temp, p)
if err != nil {
return err
}
size = sz
filesTmpNamesCol.Add("", temp.Name())
} else {
sz, err := io.Copy(io.Discard, p)
if err != nil {
return err
}
size = sz
}
totalSize += size
filesCol.Add("", filename)
fileSizesCol.SetIndex(filename, 0, fmt.Sprintf("%d", size))
filesNamesCol.Add("", p.FormName())
} else {
// if is a field
data, err := io.ReadAll(p)
if err != nil {
return err
}
totalSize += int64(len(data))
postCol.Add(p.FormName(), string(data))
}
filesCombinedSizeCol.(*collections.Single).Set(fmt.Sprintf("%d", totalSize))
}
return nil
}
func (mbp *multipartBodyProcessor) ProcessResponse(_ io.Reader, _ plugintypes.TransactionVariables, options plugintypes.BodyProcessorOptions) error {
return nil
}
var (
_ plugintypes.BodyProcessor = (*multipartBodyProcessor)(nil)
)
// OriginFileName returns the filename parameter of the Part's Content-Disposition header.
// This function is based on (multipart.Part).parseContentDisposition,
// See https://go.googlesource.com/go/+/refs/tags/go1.17.9/src/mime/multipart/multipart.go#87
// for the current implementation and also notice this function hasn't change since go1.4, as in
// https://go.googlesource.com/go/+/refs/tags/go1.4/src/mime/multipart/multipart.go#75
func originFileName(p *multipart.Part) string {
v := p.Header.Get("Content-Disposition")
_, dispositionParams, err := mime.ParseMediaType(v)
if err != nil {
return ""
}
return dispositionParams["filename"]
}
func init() {
RegisterBodyProcessor("multipart", func() plugintypes.BodyProcessor {
return &multipartBodyProcessor{}
})
}