-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebform.go
274 lines (230 loc) · 6.86 KB
/
webform.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2021 Teal.Finance/Garcon contributors
// This file is part of Teal.Finance/Garcon,
// an API and website server under the MIT License.
// SPDX-License-Identifier: MIT
package garcon
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/teal-finance/garcon/gg"
)
type WebForm struct {
Writer Writer
Notifier gg.Notifier
Redirect string
// TextLimits are used as security limits
// to avoid being flooded by large web forms
// and unexpected field names.
// The map key is the input field name.
// The map value is a pair of integers:
// the max length and the max line breaks.
// Zero (or negative) value for unlimited value size.
TextLimits map[string][2]int
// FileLimits is similar to TextLimits
// but for uploaded files.
// The map value is a pair of integers:
// the max size in runes of one file
// and the max occurrences having same field name.
// Zero (or negative) value for unlimited file size.
FileLimits map[string][2]int
// MaxBodyBytes limits someone hogging the host resources.
// Zero (or negative) value disables this security check.
MaxBodyBytes int64
// MaxMDBytes includes the form fields and browser fingerprints.
// Zero (or negative) value disables this security check.
MaxMDBytes int
maxFieldNameLength int
}
func (g *Garcon) NewContactForm(redirectURL string) WebForm {
return NewContactForm(g.Writer, redirectURL)
}
// NewContactForm initializes a new WebForm with the default contact-form settings.
func NewContactForm(gw Writer, redirectURL string) WebForm {
return WebForm{
Writer: gw,
Notifier: nil,
Redirect: redirectURL,
TextLimits: DefaultContactSettings(),
FileLimits: DefaultFileSettings(),
MaxBodyBytes: 5555,
MaxMDBytes: 4000,
maxFieldNameLength: 0,
}
}
// DefaultContactSettings is compliant with standard names for web form input fields:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#inappropriate-for-the-control
func DefaultContactSettings() map[string][2]int {
return map[string][2]int{
"name": {60, 1},
"email": {60, 1},
"text": {3000, 80},
"org-type": {20, 1},
"tel": {30, 1},
"want-call": {10, 1},
}
}
// DefaultFileSettings sets FileLimits with only "file".
func DefaultFileSettings() map[string][2]int {
return map[string][2]int{
"file": {1_000_000, 1}, // max: 1 file weighting 1 MB
}
}
func (wf *WebForm) init() {
if wf.TextLimits == nil {
wf.TextLimits = DefaultContactSettings()
log.Info("Middleware WebForm: empty TextLimits => use", wf.TextLimits)
}
if wf.FileLimits == nil {
wf.FileLimits = DefaultFileSettings()
log.Info("Middleware WebForm: empty FileLimits => use", wf.FileLimits)
}
wf.maxFieldNameLength = 0
for name := range wf.TextLimits {
if wf.maxFieldNameLength < len(name) {
wf.maxFieldNameLength = len(name)
}
}
for name := range wf.FileLimits {
if wf.maxFieldNameLength < len(name) {
wf.maxFieldNameLength = len(name)
}
}
log.Info("Middleware WebForm redirects to", wf.Redirect)
}
// Notify returns a handler that
// converts the received web-form into markdown format
// and sends it to the notifierURL.
func (wf *WebForm) Notify(notifierURL string) func(w http.ResponseWriter, r *http.Request) {
wf.init()
notifier := gg.NewNotifier(notifierURL)
return func(w http.ResponseWriter, r *http.Request) {
if wf.MaxBodyBytes > 0 {
r.Body = http.MaxBytesReader(w, r.Body, wf.MaxBodyBytes)
}
err := r.ParseForm()
if err != nil {
log.Warn("WebForm ParseForm:", err)
wf.Writer.WriteErr(w, r, http.StatusBadRequest, "cannot parse the webform", "reason", err.Error())
return
}
md := wf.toMarkdown(r)
err = notifier.Notify(md)
if err != nil {
log.Warn("WebForm Notify:", err)
}
http.Redirect(w, r, wf.Redirect, http.StatusFound)
}
}
func (wf *WebForm) toMarkdown(r *http.Request) string {
log.Infof("WebForm with %d input fields", len(r.Form))
md := wf.formMD(r.Form) + FingerprintMD(r)
if extra := overflow25(len(md), wf.MaxMDBytes); extra > 0 {
md = md[:wf.MaxMDBytes] + "\n\n" +
"(trimmed last " + strconv.Itoa(extra) + " characters)"
}
return md
}
func (wf *WebForm) formMD(fields url.Values) string {
md := ""
for name, values := range fields {
if !wf.valid(name, values) {
continue
}
max, ok := wf.TextLimits[name]
maxLen, maxLines := max[0], max[1]
if !ok {
log.Warningf("WebForm: reject name=%s not in allowlist", name)
continue
}
if extra := overflow25(len(values[0]), maxLen); extra > 0 {
values[0] = values[0][:maxLen] + "\n" +
"(trimmed last " + strconv.Itoa(extra) + " characters)"
maxLines++
}
if md == "" { // no break line at first loop
md += "- **"
} else {
md += "\n" + "- **" // double star -> bold
}
md += name + "**: " + wf.bulletParagraph(values[0], maxLines)
}
return md
}
func (wf *WebForm) valid(name string, values []string) bool {
if len(values) == 1 && values[0] == "" {
return false // skip empty values
}
if len(values) != 1 {
log.Warningf("WebForm: reject name=%s because "+
"received %d input field(s) while expected only one", name, len(values))
return false
}
return wf.validName(name)
}
func (wf *WebForm) validName(name string) bool {
nLen := len(name)
if nLen > wf.maxFieldNameLength {
name = gg.Sanitize(name)
state := "(sanitized)"
maxDisplay := 8 * wf.maxFieldNameLength
if nLen > maxDisplay+10 {
name = name[:maxDisplay]
state = "(sanitized and trimmed)"
}
log.Warningf("WebForm: reject name=%q %s too long (%d > %d)", name, state, nLen, wf.maxFieldNameLength)
return false
}
if p := gg.Printable(name); p >= 0 {
log.Warningf("WebForm: reject name=%q contains a bad character at position %d", gg.Sanitize(name), p)
return false
}
if _, ok := wf.FileLimits[name]; ok {
log.Warningf("WebForm: skip name=%s because file not yet supported (TODO)", name)
return false
}
return true
}
// overflow25 returns the overflow if n is 25% above max, else returns zero.
// max=0 means max is infinite.
func overflow25(n, max int) int {
if (n > max+max/4) && (max > 0) {
return n - max
}
return 0
}
// Markdown encoding.
const (
lineBreak = " " // trailing double space -> line break
bulletIndent = " " // leading spaces -> bullet indent
)
func (wf *WebForm) bulletParagraph(str string, maxLines int) string {
md := ""
count := 0
blank := false
txt := gg.SplitCleanedLines(str)
for i := range txt {
// skip top blank lines, redundant blank lines and bottom blank lines
if txt[i] == "" {
if md != "" {
blank = true
}
continue
}
if blank {
blank = false
md += lineBreak + "\n\n" + bulletIndent
} else if md != "" {
md += lineBreak + "\n" + bulletIndent
}
md += txt[i]
count++
remaining := len(txt) - i
if (count > maxLines) && (maxLines > 0) && (remaining > maxLines/2) {
md += fmt.Sprintf("\n (skip %d lines)", remaining)
break
}
}
return md
}