-
Notifications
You must be signed in to change notification settings - Fork 22
/
request_upload.go
234 lines (211 loc) · 6.22 KB
/
request_upload.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
227
228
229
230
231
232
233
234
package checkpoints
import (
"bytes"
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"io"
"net/http"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var multipartHeaderRegexp = regexp.MustCompile(`(?i)(?:^|\r\n)--+\w+\r\n((([\w-]+: .+)\r\n)+)`)
var multipartHeaderContentRangeRegexp = regexp.MustCompile(`/(\d+)`)
// RequestUploadCheckpoint ${requestUpload.arg}
type RequestUploadCheckpoint struct {
Checkpoint
}
func (this *RequestUploadCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
if this.RequestBodyIsEmpty(req) {
value = ""
return
}
value = ""
if param == "minSize" || param == "maxSize" {
value = 0
}
if req.WAFRaw().Method != http.MethodPost {
return
}
if req.WAFRaw().Body == nil {
return
}
hasRequestBody = true
var requestContentLength = req.WAFRaw().ContentLength
var fields []string
var minSize int64
var maxSize int64
var names []string
var extensions []string
if requestContentLength <= req.WAFMaxRequestSize() { // full read
if req.WAFRaw().MultipartForm == nil {
var bodyData = req.WAFGetCacheBody()
if len(bodyData) == 0 {
data, err := req.WAFReadBody(req.WAFMaxRequestSize())
if err != nil {
sysErr = err
return
}
bodyData = data
req.WAFSetCacheBody(data)
defer req.WAFRestoreBody(data)
}
var oldBody = req.WAFRaw().Body
req.WAFRaw().Body = io.NopCloser(bytes.NewBuffer(bodyData))
err := req.WAFRaw().ParseMultipartForm(req.WAFMaxRequestSize())
if err == nil {
for field, files := range req.WAFRaw().MultipartForm.File {
if param == "field" {
fields = append(fields, field)
} else if param == "minSize" {
for _, file := range files {
if minSize == 0 || minSize > file.Size {
minSize = file.Size
}
}
} else if param == "maxSize" {
for _, file := range files {
if maxSize < file.Size {
maxSize = file.Size
}
}
} else if param == "name" {
for _, file := range files {
if !lists.ContainsString(names, file.Filename) {
names = append(names, file.Filename)
}
}
} else if param == "ext" {
for _, file := range files {
if len(file.Filename) > 0 {
exit := strings.ToLower(filepath.Ext(file.Filename))
if !lists.ContainsString(extensions, exit) {
extensions = append(extensions, exit)
}
}
}
}
}
}
// 还原
req.WAFRaw().Body = oldBody
if err != nil {
userErr = err
return
}
if req.WAFRaw().MultipartForm == nil {
return
}
}
} else { // read first part
var bodyData = req.WAFGetCacheBody()
if len(bodyData) == 0 {
data, err := req.WAFReadBody(req.WAFMaxRequestSize())
if err != nil {
sysErr = err
return
}
bodyData = data
req.WAFSetCacheBody(data)
defer req.WAFRestoreBody(data)
}
var subMatches = multipartHeaderRegexp.FindAllSubmatch(bodyData, -1)
for _, subMatch := range subMatches {
var headers = bytes.Split(subMatch[1], []byte{'\r', '\n'})
var partContentLength int64 = -1
for _, header := range headers {
if len(header) > 2 {
var kv = bytes.SplitN(header, []byte{':'}, 2)
if len(kv) == 2 {
var k = kv[0]
var v = kv[1]
switch string(bytes.ToLower(k)) {
case "content-disposition":
var props = bytes.Split(v, []byte{';', ' '})
for _, prop := range props {
var propKV = bytes.SplitN(prop, []byte{'='}, 2)
if len(propKV) == 2 {
var propValue = string(propKV[1])
switch string(propKV[0]) {
case "name":
if param == "field" {
propValue, _ = strconv.Unquote(propValue)
fields = append(fields, propValue)
}
case "filename":
if param == "name" {
propValue, _ = strconv.Unquote(propValue)
names = append(names, propValue)
} else if param == "ext" {
propValue, _ = strconv.Unquote(propValue)
extensions = append(extensions, strings.ToLower(filepath.Ext(propValue)))
}
}
}
}
case "content-range":
if partContentLength <= 0 {
var contentRange = multipartHeaderContentRangeRegexp.FindSubmatch(v)
if len(contentRange) >= 2 {
partContentLength = types.Int64(string(contentRange[1]))
}
}
case "content-length":
if partContentLength <= 0 {
partContentLength = types.Int64(string(v))
}
}
}
}
}
// minSize & maxSize
if partContentLength > 0 {
if param == "minSize" && (minSize == 0 /** not set yet **/ || partContentLength < minSize) {
minSize = partContentLength
} else if param == "maxSize" && partContentLength > maxSize {
maxSize = partContentLength
}
}
}
}
if param == "field" { // field
value = strings.Join(fields, ",")
} else if param == "minSize" { // minSize
if minSize == 0 && requestContentLength > 0 {
minSize = requestContentLength
}
value = minSize
} else if param == "maxSize" { // maxSize
if maxSize == 0 && requestContentLength > 0 {
maxSize = requestContentLength
}
value = maxSize
} else if param == "name" { // name
value = strings.Join(names, ",")
} else if param == "ext" { // ext
value = strings.Join(extensions, ",")
}
return
}
func (this *RequestUploadCheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
if this.IsRequest() {
return this.RequestValue(req, param, options, ruleId)
}
return
}
func (this *RequestUploadCheckpoint) ParamOptions() *ParamOptions {
option := NewParamOptions()
option.AddParam("最小文件尺寸", "minSize")
option.AddParam("最大文件尺寸", "maxSize")
option.AddParam("扩展名(如.txt)", "ext")
option.AddParam("原始文件名", "name")
option.AddParam("表单字段名", "field")
return option
}
func (this *RequestUploadCheckpoint) CacheLife() utils.CacheLife {
return utils.CacheMiddleLife
}