-
Notifications
You must be signed in to change notification settings - Fork 14
/
options.go
410 lines (341 loc) · 11.9 KB
/
options.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
// package rkmidsec provide auth related options
package rkmidsec
import (
"fmt"
"github.com/rookie-ninja/rk-entry/middleware"
"github.com/rs/xid"
"net/http"
"strings"
)
// ***************** OptionSet Interface *****************
// OptionSetInterface mainly for testing purpose
type OptionSetInterface interface {
GetEntryName() string
GetEntryType() string
Before(*BeforeCtx)
BeforeCtx(*http.Request) *BeforeCtx
}
// ***************** OptionSet Implementation *****************
// optionSet which is used for middleware implementation
type optionSet struct {
// EntryName name of entry
entryName string
// EntryType type of entry
entryType string
// IgnorePrefix ignoring paths prefix
ignorePrefix []string
// XSSProtection provides protection against cross-site scripting attack (XSS)
// by setting the `X-XSS-Protection` header.
// Optional. Default value "1; mode=block".
xssProtection string
// ContentTypeNosniff provides protection against overriding Content-Type
// header by setting the `X-Content-Type-Options` header.
// Optional. Default value "nosniff".
contentTypeNosniff string
// XFrameOptions can be used to indicate whether or not a browser should
// be allowed to render a page in a <frame>, <iframe> or <object> .
// Sites can use this to avoid clickjacking attacks, by ensuring that their
// content is not embedded into other sites.provides protection against
// clickjacking.
// Optional. Default value "SAMEORIGIN".
// Possible values:
// - "SAMEORIGIN" - The page can only be displayed in a frame on the same origin as the page itself.
// - "DENY" - The page cannot be displayed in a frame, regardless of the site attempting to do so.
// - "ALLOW-FROM uri" - The page can only be displayed in a frame on the specified origin.
xFrameOptions string
// HSTSMaxAge sets the `Strict-Transport-Security` header to indicate how
// long (in seconds) browsers should remember that this site is only to
// be accessed using HTTPS. This reduces your exposure to some SSL-stripping
// man-in-the-middle (MITM) attacks.
// Optional. Default value 0.
hstsMaxAge int
// HSTSExcludeSubdomains won't include subdomains tag in the `Strict Transport Security`
// header, excluding all subdomains from security policy. It has no effect
// unless HSTSMaxAge is set to a non-zero value.
// Optional. Default value false.
hstsExcludeSubdomains bool
// ContentSecurityPolicy sets the `Content-Security-Policy` header providing
// security against cross-site scripting (XSS), clickjacking and other code
// injection attacks resulting from execution of malicious content in the
// trusted web page context.
// Optional. Default value "".
contentSecurityPolicy string
// HSTSPreloadEnabled will add the preload tag in the `Strict Transport Security`
// header, which enables the domain to be included in the HSTS preload list
// maintained by Chrome (and used by Firefox and Safari): https://hstspreload.org/
// Optional. Default value false.
hstsPreloadEnabled bool
// CSPReportOnly would use the `Content-Security-Policy-Report-Only` header instead
// of the `Content-Security-Policy` header. This allows iterative updates of the
// content security policy by only reporting the violations that would
// have occurred instead of blocking the resource.
// Optional. Default value false.
cspReportOnly bool
// ReferrerPolicy sets the `Referrer-Policy` header providing security against
// leaking potentially sensitive request paths to third parties.
// Optional. Default value "".
referrerPolicy string
mock OptionSetInterface
}
// NewOptionSet Create new optionSet with options.
func NewOptionSet(opts ...Option) OptionSetInterface {
set := &optionSet{
entryName: xid.New().String(),
entryType: "",
xssProtection: "1; mode=block",
contentTypeNosniff: "nosniff",
xFrameOptions: "SAMEORIGIN",
hstsPreloadEnabled: false,
ignorePrefix: make([]string, 0),
}
for i := range opts {
opts[i](set)
}
if set.mock != nil {
return set.mock
}
return set
}
// GetEntryName returns entry name
func (set *optionSet) GetEntryName() string {
return set.entryName
}
// GetEntryType returns entry type
func (set *optionSet) GetEntryType() string {
return set.entryType
}
// BeforeCtx should be created before Before()
func (set *optionSet) BeforeCtx(req *http.Request) *BeforeCtx {
ctx := NewBeforeCtx()
if req != nil && req.URL != nil && req.Header != nil {
ctx.Input.UrlPath = req.URL.Path
ctx.Input.isTLS = req.TLS != nil
ctx.Input.xForwardedProto = req.Header.Get(rkmid.HeaderXForwardedProto)
}
return ctx
}
// Before should run before user handler
func (set *optionSet) Before(ctx *BeforeCtx) {
// normalize
if ctx == nil || set.ignore(ctx.Input.UrlPath) {
return
}
// Add X-XSS-Protection header
if set.xssProtection != "" {
ctx.Output.HeadersToReturn[rkmid.HeaderXXSSProtection] = set.xssProtection
}
// Add X-Content-Type-Options header
if set.contentTypeNosniff != "" {
ctx.Output.HeadersToReturn[rkmid.HeaderXContentTypeOptions] = set.contentTypeNosniff
}
// Add X-Frame-Options header
if set.xFrameOptions != "" {
ctx.Output.HeadersToReturn[rkmid.HeaderXFrameOptions] = set.xFrameOptions
}
// Add Strict-Transport-Security header
if (ctx.Input.isTLS || (ctx.Input.xForwardedProto == "https")) && set.hstsMaxAge != 0 {
subdomains := ""
if !set.hstsExcludeSubdomains {
subdomains = "; includeSubdomains"
}
if set.hstsPreloadEnabled {
subdomains = fmt.Sprintf("%s; preload", subdomains)
}
ctx.Output.HeadersToReturn[rkmid.HeaderStrictTransportSecurity] = fmt.Sprintf("max-age=%d%s", set.hstsMaxAge, subdomains)
}
// Add Content-Security-Policy-Report-Only or Content-Security-Policy header
if set.contentSecurityPolicy != "" {
if set.cspReportOnly {
ctx.Output.HeadersToReturn[rkmid.HeaderContentSecurityPolicyReportOnly] = set.contentSecurityPolicy
} else {
ctx.Output.HeadersToReturn[rkmid.HeaderContentSecurityPolicy] = set.contentSecurityPolicy
}
}
// Add Referrer-Policy header
if set.referrerPolicy != "" {
ctx.Output.HeadersToReturn[rkmid.HeaderReferrerPolicy] = set.referrerPolicy
}
}
// Ignore determine whether auth should be ignored based on path
func (set *optionSet) ignore(path string) bool {
for i := range set.ignorePrefix {
if strings.HasPrefix(path, set.ignorePrefix[i]) {
return true
}
}
return rkmid.IgnorePrefixGlobal(path)
}
// ***************** OptionSet Mock *****************
// NewOptionSetMock for testing purpose
func NewOptionSetMock(before *BeforeCtx) OptionSetInterface {
return &optionSetMock{
before: before,
}
}
type optionSetMock struct {
before *BeforeCtx
}
// GetEntryName returns entry name
func (mock *optionSetMock) GetEntryName() string {
return "mock"
}
// GetEntryType returns entry type
func (mock *optionSetMock) GetEntryType() string {
return "mock"
}
// BeforeCtx should be created before Before()
func (mock *optionSetMock) BeforeCtx(request *http.Request) *BeforeCtx {
return mock.before
}
// Before should run before user handler
func (mock *optionSetMock) Before(ctx *BeforeCtx) {
return
}
// ***************** Context *****************
// NewBeforeCtx create new BeforeCtx with fields initialized
func NewBeforeCtx() *BeforeCtx {
ctx := &BeforeCtx{}
ctx.Output.HeadersToReturn = make(map[string]string)
return ctx
}
// BeforeCtx context for Before() function
type BeforeCtx struct {
Input struct {
UrlPath string
xForwardedProto string
isTLS bool
}
Output struct {
HeadersToReturn map[string]string
}
}
// ***************** BootConfig *****************
// BootConfig for YAML
type BootConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
IgnorePrefix []string `yaml:"ignorePrefix" json:"ignorePrefix"`
XssProtection string `yaml:"xssProtection" json:"xssProtection"`
ContentTypeNosniff string `yaml:"contentTypeNosniff" json:"contentTypeNosniff"`
XFrameOptions string `yaml:"xFrameOptions" json:"xFrameOptions"`
HstsMaxAge int `yaml:"hstsMaxAge" json:"hstsMaxAge"`
HstsExcludeSubdomains bool `yaml:"hstsExcludeSubdomains" json:"hstsExcludeSubdomains"`
HstsPreloadEnabled bool `yaml:"hstsPreloadEnabled" json:"hstsPreloadEnabled"`
ContentSecurityPolicy string `yaml:"contentSecurityPolicy" json:"contentSecurityPolicy"`
CspReportOnly bool `yaml:"cspReportOnly" json:"cspReportOnly"`
ReferrerPolicy string `yaml:"referrerPolicy" json:"referrerPolicy"`
}
// ToOptions convert BootConfig into Option list
func ToOptions(config *BootConfig, entryName, entryType string) []Option {
opts := make([]Option, 0)
if config.Enabled {
opts = append(opts,
WithEntryNameAndType(entryName, entryType),
WithXSSProtection(config.XssProtection),
WithContentTypeNosniff(config.ContentTypeNosniff),
WithXFrameOptions(config.XFrameOptions),
WithHSTSMaxAge(config.HstsMaxAge),
WithHSTSExcludeSubdomains(config.HstsExcludeSubdomains),
WithHSTSPreloadEnabled(config.HstsPreloadEnabled),
WithContentSecurityPolicy(config.ContentSecurityPolicy),
WithCSPReportOnly(config.CspReportOnly),
WithReferrerPolicy(config.ReferrerPolicy),
WithIgnorePrefix(config.IgnorePrefix...))
}
return opts
}
// ***************** Option *****************
// Option
type Option func(*optionSet)
// WithEntryNameAndType provide entry name and entry type.
func WithEntryNameAndType(entryName, entryType string) Option {
return func(set *optionSet) {
set.entryName = entryName
set.entryType = entryType
}
}
// WithXSSProtection provide X-XSS-Protection header value.
// Optional. Default value "1; mode=block".
func WithXSSProtection(val string) Option {
return func(opt *optionSet) {
if len(val) > 0 {
opt.xssProtection = val
}
}
}
// WithContentTypeNosniff provide X-Content-Type-Options header value.
// Optional. Default value "nosniff".
func WithContentTypeNosniff(val string) Option {
return func(opt *optionSet) {
if len(val) > 0 {
opt.contentTypeNosniff = val
}
}
}
// WithXFrameOptions provide X-Frame-Options header value.
// Optional. Default value "SAMEORIGIN".
func WithXFrameOptions(val string) Option {
return func(opt *optionSet) {
if len(val) > 0 {
opt.xFrameOptions = val
}
}
}
// WithHSTSMaxAge provide Strict-Transport-Security header value.
func WithHSTSMaxAge(val int) Option {
return func(opt *optionSet) {
opt.hstsMaxAge = val
}
}
// WithHSTSExcludeSubdomains provide excluding subdomains of HSTS.
func WithHSTSExcludeSubdomains(val bool) Option {
return func(opt *optionSet) {
opt.hstsExcludeSubdomains = val
}
}
// WithHSTSPreloadEnabled provide enabling HSTS preload.
// Optional. Default value false.
func WithHSTSPreloadEnabled(val bool) Option {
return func(opt *optionSet) {
opt.hstsPreloadEnabled = val
}
}
// WithContentSecurityPolicy provide Content-Security-Policy header value.
// Optional. Default value "".
func WithContentSecurityPolicy(val string) Option {
return func(opt *optionSet) {
opt.contentSecurityPolicy = val
}
}
// WithCSPReportOnly provide Content-Security-Policy-Report-Only header value.
// Optional. Default value false.
func WithCSPReportOnly(val bool) Option {
return func(set *optionSet) {
set.cspReportOnly = val
}
}
// WithReferrerPolicy provide Referrer-Policy header value.
// Optional. Default value "".
func WithReferrerPolicy(val string) Option {
return func(opt *optionSet) {
if len(val) > 0 {
opt.referrerPolicy = val
}
}
}
// WithIgnorePrefix provide paths prefix that will ignore.
// Mainly used for swagger main page and RK TV entry.
func WithIgnorePrefix(paths ...string) Option {
return func(set *optionSet) {
set.ignorePrefix = append(set.ignorePrefix, paths...)
}
}
// WithMockOptionSet provide mock OptionSetInterface
func WithMockOptionSet(mock OptionSetInterface) Option {
return func(set *optionSet) {
set.mock = mock
}
}