-
Notifications
You must be signed in to change notification settings - Fork 137
/
secure.go
327 lines (307 loc) · 10.3 KB
/
secure.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package middlewares
import (
"fmt"
"net/url"
"strings"
"time"
build "github.com/cozy/cozy-stack/pkg/config"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/labstack/echo/v4"
"golang.org/x/net/idna"
)
type (
// CSPSource type are the different types of CSP headers sources definitions.
// Each source type defines a different acess policy.
CSPSource int
// SecureConfig defines the config for Secure middleware.
SecureConfig struct {
HSTSMaxAge time.Duration
CSPDefaultSrc []CSPSource
CSPScriptSrc []CSPSource
CSPFrameSrc []CSPSource
CSPConnectSrc []CSPSource
CSPFontSrc []CSPSource
CSPImgSrc []CSPSource
CSPManifestSrc []CSPSource
CSPMediaSrc []CSPSource
CSPObjectSrc []CSPSource
CSPStyleSrc []CSPSource
CSPWorkerSrc []CSPSource
CSPFrameAncestors []CSPSource
CSPBaseURI []CSPSource
CSPFormAction []CSPSource
CSPDefaultSrcAllowList string
CSPScriptSrcAllowList string
CSPFrameSrcAllowList string
CSPConnectSrcAllowList string
CSPFontSrcAllowList string
CSPImgSrcAllowList string
CSPManifestSrcAllowList string
CSPMediaSrcAllowList string
CSPObjectSrcAllowList string
CSPStyleSrcAllowList string
CSPWorkerSrcAllowList string
CSPFrameAncestorsAllowList string
CSPBaseURIAllowList string
CSPFormActionAllowList string
// context_name -> source -> allow_list
CSPPerContext map[string]map[string]string
}
)
const (
// CSPSrcSelf is the 'self' option of a CSP source.
CSPSrcSelf CSPSource = iota
// CSPSrcNone is the 'none' option. It denies all domains as an eligible
// source.
CSPSrcNone
// CSPSrcData is the 'data:' option of a CSP source.
CSPSrcData
// CSPSrcBlob is the 'blob:' option of a CSP source.
CSPSrcBlob
// CSPSrcParent adds the parent domain as an eligible CSP source.
CSPSrcParent
// CSPSrcWS adds the parent domain eligible for websocket.
CSPSrcWS
// CSPSrcSiblings adds all the siblings subdomains as eligibles CSP
// sources.
CSPSrcSiblings
// CSPSrcAny is the '*' option. It allows any domain as an eligible source.
CSPSrcAny
// CSPUnsafeInline is the 'unsafe-inline' option. It allows to have inline
// styles or scripts to be injected in the page.
CSPUnsafeInline
// CSPAllowList inserts a allowList of domains.
CSPAllowList
)
// Secure returns a Middlefunc that can be used to define all the necessary
// secure headers. It is configurable with a SecureConfig object.
func Secure(conf *SecureConfig) echo.MiddlewareFunc {
var hstsHeader string
if conf.HSTSMaxAge > 0 {
hstsHeader = fmt.Sprintf("max-age=%.f; includeSubDomains",
conf.HSTSMaxAge.Seconds())
}
conf.CSPDefaultSrc, conf.CSPDefaultSrcAllowList =
validCSPList(conf.CSPDefaultSrc, conf.CSPDefaultSrc, conf.CSPDefaultSrcAllowList)
conf.CSPScriptSrc, conf.CSPScriptSrcAllowList =
validCSPList(conf.CSPScriptSrc, conf.CSPDefaultSrc, conf.CSPScriptSrcAllowList)
conf.CSPFrameSrc, conf.CSPFrameSrcAllowList =
validCSPList(conf.CSPFrameSrc, conf.CSPDefaultSrc, conf.CSPFrameSrcAllowList)
conf.CSPConnectSrc, conf.CSPConnectSrcAllowList =
validCSPList(conf.CSPConnectSrc, conf.CSPDefaultSrc, conf.CSPConnectSrcAllowList)
conf.CSPFontSrc, conf.CSPFontSrcAllowList =
validCSPList(conf.CSPFontSrc, conf.CSPDefaultSrc, conf.CSPFontSrcAllowList)
conf.CSPImgSrc, conf.CSPImgSrcAllowList =
validCSPList(conf.CSPImgSrc, conf.CSPDefaultSrc, conf.CSPImgSrcAllowList)
conf.CSPManifestSrc, conf.CSPManifestSrcAllowList =
validCSPList(conf.CSPManifestSrc, conf.CSPDefaultSrc, conf.CSPManifestSrcAllowList)
conf.CSPMediaSrc, conf.CSPMediaSrcAllowList =
validCSPList(conf.CSPMediaSrc, conf.CSPDefaultSrc, conf.CSPMediaSrcAllowList)
conf.CSPObjectSrc, conf.CSPObjectSrcAllowList =
validCSPList(conf.CSPObjectSrc, nil, conf.CSPObjectSrcAllowList)
conf.CSPStyleSrc, conf.CSPStyleSrcAllowList =
validCSPList(conf.CSPStyleSrc, conf.CSPDefaultSrc, conf.CSPStyleSrcAllowList)
conf.CSPWorkerSrc, conf.CSPWorkerSrcAllowList =
validCSPList(conf.CSPWorkerSrc, conf.CSPDefaultSrc, conf.CSPWorkerSrcAllowList)
conf.CSPFormAction, conf.CSPFormActionAllowList =
validCSPList(conf.CSPFormAction, nil, conf.CSPFormActionAllowList)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
isSecure := !build.IsDevRelease()
h := c.Response().Header()
if isSecure && hstsHeader != "" {
h.Set(echo.HeaderStrictTransportSecurity, hstsHeader)
}
var cspHeader string
host, err := idna.ToUnicode(c.Request().Host)
if err != nil {
return err
}
parent, _, siblings := config.SplitCozyHost(host)
parent, err = idna.ToASCII(parent)
if err != nil {
return err
}
var contextName string
if conf.CSPPerContext != nil {
contextName = GetInstance(c).ContextName
}
b := cspBuilder{
parent: parent,
siblings: siblings,
isSecure: isSecure,
contextName: contextName,
perContext: conf.CSPPerContext,
}
cspHeader += b.makeCSPHeader("default-src", conf.CSPDefaultSrcAllowList, conf.CSPDefaultSrc)
cspHeader += b.makeCSPHeader("script-src", conf.CSPScriptSrcAllowList, conf.CSPScriptSrc)
cspHeader += b.makeCSPHeader("frame-src", conf.CSPFrameSrcAllowList, conf.CSPFrameSrc)
cspHeader += b.makeCSPHeader("connect-src", conf.CSPConnectSrcAllowList, conf.CSPConnectSrc)
cspHeader += b.makeCSPHeader("font-src", conf.CSPFontSrcAllowList, conf.CSPFontSrc)
cspHeader += b.makeCSPHeader("img-src", conf.CSPImgSrcAllowList, conf.CSPImgSrc)
cspHeader += b.makeCSPHeader("manifest-src", conf.CSPManifestSrcAllowList, conf.CSPManifestSrc)
cspHeader += b.makeCSPHeader("media-src", conf.CSPMediaSrcAllowList, conf.CSPMediaSrc)
cspHeader += b.makeCSPHeader("object-src", conf.CSPObjectSrcAllowList, conf.CSPObjectSrc)
cspHeader += b.makeCSPHeader("style-src", conf.CSPStyleSrcAllowList, conf.CSPStyleSrc)
cspHeader += b.makeCSPHeader("worker-src", conf.CSPWorkerSrcAllowList, conf.CSPWorkerSrc)
cspHeader += b.makeCSPHeader("frame-ancestors", conf.CSPFrameAncestorsAllowList, conf.CSPFrameAncestors)
cspHeader += b.makeCSPHeader("base-uri", conf.CSPBaseURIAllowList, conf.CSPBaseURI)
cspHeader += b.makeCSPHeader("form-action", conf.CSPFormActionAllowList, conf.CSPFormAction)
if cspHeader != "" {
h.Set(echo.HeaderContentSecurityPolicy, cspHeader)
}
h.Set(echo.HeaderXContentTypeOptions, "nosniff")
return next(c)
}
}
}
func validCSPList(sources, defaults []CSPSource, allowList string) ([]CSPSource, string) {
allowListFields := strings.Fields(allowList)
allowListFilter := allowListFields[:0]
for _, s := range allowListFields {
u, err := url.Parse(s)
if err != nil {
continue
}
if !build.IsDevRelease() {
if u.Scheme == "ws" {
u.Scheme = "wss"
} else if u.Scheme == "http" {
u.Scheme = "https"
}
}
if u.Path == "" {
u.Path = "/"
}
// For custom links like cozydrive:, we want to allow the whole protocol
if !strings.HasPrefix(u.Scheme, "cozy") {
s = u.String()
}
allowListFilter = append(allowListFilter, s)
}
if len(allowListFilter) > 0 {
allowList = strings.Join(allowListFilter, " ")
sources = append(sources, CSPAllowList)
} else {
allowList = ""
}
if len(sources) == 0 && allowList == "" {
return nil, ""
}
sources = append(sources, defaults...)
sourcesUnique := sources[:0]
for _, source := range sources {
var found bool
for _, s := range sourcesUnique {
if s == source {
found = true
break
}
}
if !found {
sourcesUnique = append(sourcesUnique, source)
}
}
return sourcesUnique, allowList
}
type cspBuilder struct {
parent string
siblings string
contextName string
perContext map[string]map[string]string
isSecure bool
}
func (b cspBuilder) makeCSPHeader(header, cspAllowList string, sources []CSPSource) string {
headers := make([]string, len(sources), len(sources)+1)
for i, src := range sources {
switch src {
case CSPSrcSelf:
headers[i] = "'self'"
case CSPSrcNone:
headers[i] = "'none'"
case CSPSrcData:
headers[i] = "data:"
case CSPSrcBlob:
headers[i] = "blob:"
case CSPSrcParent:
if b.isSecure {
headers[i] = "https://" + b.parent
} else {
headers[i] = "http://" + b.parent
}
case CSPSrcWS:
if b.isSecure {
headers[i] = "wss://" + b.parent
} else {
headers[i] = "ws://" + b.parent
}
case CSPSrcSiblings:
if b.isSecure {
headers[i] = "https://" + b.siblings
} else {
headers[i] = "http://" + b.siblings
}
case CSPSrcAny:
headers[i] = "*"
case CSPUnsafeInline:
headers[i] = "'unsafe-inline'"
case CSPAllowList:
headers[i] = cspAllowList
}
}
if b.contextName != "" {
if context, ok := b.perContext[b.contextName]; ok {
var src string
switch header {
case "default-src":
src = "default"
case "img-src":
src = "img"
case "script-src":
src = "script"
case "connect-src":
src = "connect"
case "style-src":
src = "style"
case "font-src":
src = "font"
case "media-src":
src = "font"
case "frame-src":
src = "frame"
}
if list, ok := context[src]; ok && list != "" {
headers = append(headers, list)
}
}
}
if len(headers) == 0 {
return ""
}
return header + " " + strings.Join(headers, " ") + ";"
}
// AppendCSPRule allows to patch inline the CSP headers to add a new rule.
func AppendCSPRule(c echo.Context, ruleType string, appendedValues ...string) {
currentRules := c.Response().Header().Get(echo.HeaderContentSecurityPolicy)
newRules := appendCSPRule(currentRules, ruleType, appendedValues...)
c.Response().Header().Set(echo.HeaderContentSecurityPolicy, newRules)
}
func appendCSPRule(currentRules, ruleType string, appendedValues ...string) (newRules string) {
ruleIndex := strings.Index(currentRules, ruleType)
if ruleIndex >= 0 {
ruleTerminationIndex := strings.Index(currentRules[ruleIndex:], ";")
if ruleTerminationIndex <= 0 {
return
}
ruleFields := strings.Fields(currentRules[ruleIndex : ruleIndex+ruleTerminationIndex])
if len(ruleFields) == 2 && ruleFields[1] == "'none'" {
ruleFields = ruleFields[:1]
}
ruleFields = append(ruleFields, appendedValues...)
newRules = currentRules[:ruleIndex] + strings.Join(ruleFields, " ") +
currentRules[ruleIndex+ruleTerminationIndex:]
} else {
newRules = currentRules + ruleType + " " + strings.Join(appendedValues, " ") + ";"
}
return
}