This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
193 lines (163 loc) · 6.26 KB
/
handler.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
package secure
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/volatile/core"
)
const (
// HPKPDefaultMaxAge provides a default HPKP Max-Age value of 30 days.
HPKPDefaultMaxAge = 30 * 24 * time.Hour
// HSTSDefaultMaxAge provides a default HSTS Max-Age value of 30 days.
HSTSDefaultMaxAge = 30 * 24 * time.Hour
// HSTSPreloadMinAge is the lowest max age usable with HSTS preload. See https://hstspreload.appspot.com.
HSTSPreloadMinAge = 10886400
)
// Options represents security options.
type Options struct {
AllowedHosts []string // AllowedHosts indicates which fully qualified domain names are allowed to point to this server. If none are set, all are allowed.
CSP string // CSP contains Content Security Policy for responses. See http://www.w3.org/TR/CSP/ and https://developer.mozilla.org/en-US/docs/Web/Security/CSP/Using_Content_Security_Policy.
FrameAllowed bool // FrameAllowed indicates whether the browsers can display the response in a frame, regardless of the site attempting to do so.
HPKP *HPKPOptions // HPKP contains the HTTP Public Key Pinning options.
HSTS *HSTSOptions // HPKP contains the HTTP Strict Transport Security options.
SSLForced bool // SSLForced indicates whether an insecure request must be redirected to the secure protocol.
}
// HPKPOptions represents HTTP Public Key Pinning options.
// See RFC 7469 and https://developer.mozilla.org/en-US/docs/Web/Security/Public_Key_Pinning.
type HPKPOptions struct {
Keys []string // Keys contains the Base64 encoded Subject Public Key Information (SPKI) fingerprints. This field is required.
MaxAge time.Duration // MaxAge indicates how long the browser should remember that this site is only to be accessed using one of the pinned keys. This field is required.
IncludeSubdomains bool // IncludeSubdomains indicates whether HPKP applies to all of the site's subdomains as well.
ReportURI string // ReportURI is the URL at which validation failures are reported to.
}
// HSTSOptions represents HTTP Strict Transport Security options.
// See RFC 6797 and https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security.
type HSTSOptions struct {
MaxAge time.Duration // MaxAge indicates how long the browser should remember that this site is only to be accessed using HTTPS. This field is required.
IncludeSubdomains bool // IncludeSubdomains indicates whether HSTS applies to all of the site's subdomains as well.
Preload bool // Preload indicates whether the browsers must use a secure connection. It's not a standard. See https://hstspreload.appspot.com.
}
// Use adds the handler to the default handlers stack.
func Use(options *Options) {
// Panic when options are invalid.
if options != nil {
if options.HPKP != nil {
if _, err := hpkpHeader(options); err != nil {
panic(err)
}
}
if options.HSTS != nil {
if _, err := hstsHeader(options); err != nil {
panic(err)
}
}
}
core.Use(func(c *core.Context) {
if options != nil {
if core.Production {
// Check if host is allowed.
if len(options.AllowedHosts) > 0 {
for _, host := range options.AllowedHosts {
if host == c.Request.URL.Host {
goto SSLOptions
}
}
http.NotFound(c.ResponseWriter, c.Request)
return
}
SSLOptions:
isSSL := (c.Request.URL.Scheme == "https" || c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https")
// If wanted, redirect permanently to the secure protocol.
if !isSSL && options.SSLForced {
url := c.Request.URL
url.Scheme = "https"
http.Redirect(c.ResponseWriter, c.Request, url.String(), http.StatusMovedPermanently)
return
}
// Set HPKP header, but only if connected by SSL and the HPKP options are valid.
if isSSL && options.HPKP != nil {
if v, err := hpkpHeader(options); err != nil {
panic(err)
} else {
c.ResponseWriter.Header().Set("Public-Key-Pins", v)
}
}
// HSTS header, but only if HSTS options are valid.
if options.HSTS != nil {
if v, err := hstsHeader(options); err != nil {
panic(err)
} else {
c.ResponseWriter.Header().Set("Strict-Transport-Security", v)
}
}
}
// Set Content Security Policy headers.
if options.CSP != "" {
c.ResponseWriter.Header().Set("Content-Security-Policy", options.CSP)
c.ResponseWriter.Header().Set("X-Content-Security-Policy", options.CSP)
c.ResponseWriter.Header().Set("X-WebKit-CSP", options.CSP)
}
}
// If not explicitly allowed, displaying content inside a frame of a different origin is forbidden.
if options == nil || !options.FrameAllowed {
c.ResponseWriter.Header().Set("X-Frame-Options", "SAMEORIGIN")
}
// Set some "good practice" default headers.
c.ResponseWriter.Header().Set("X-Content-Type-Options", "nosniff")
c.ResponseWriter.Header().Set("X-XSS-Protection", "1; mode=block")
c.Next()
})
}
func hpkpHeader(o *Options) (v string, err error) {
if len(o.HPKP.Keys) == 0 {
err = errors.New("secure: at least one key must be set when using HPKP")
return
}
if o.HPKP.MaxAge == 0 {
err = errors.New("secure: max age must be set when using HPKP")
return
}
for _, key := range o.HPKP.Keys {
if v != "" {
v += "; "
}
v += fmt.Sprintf("pin-sha256=%q", key)
}
v += fmt.Sprintf("; %.f", o.HPKP.MaxAge.Seconds())
if o.HPKP.IncludeSubdomains {
v += "; includeSubdomains"
}
if o.HPKP.ReportURI != "" {
v += fmt.Sprintf("; report-uri=%q", o.HPKP.ReportURI)
}
return
}
func hstsHeader(o *Options) (v string, err error) {
if !o.SSLForced {
err = errors.New("secure: SSLForced must be true when using HSTS")
return
}
if o.HSTS.MaxAge == 0 {
err = errors.New("secure: max age must be set when using HSTS")
return
}
if o.HSTS.Preload {
if o.HSTS.MaxAge < HSTSPreloadMinAge {
err = errors.New("secure: max age must be at least eighteen weeks when using HSTS preload")
return
}
if !o.HSTS.IncludeSubdomains {
err = errors.New("secure: subdomains must be included when using HSTS preload")
return
}
}
v += fmt.Sprintf("; %.f", o.HSTS.MaxAge.Seconds())
if o.HSTS.IncludeSubdomains {
v += "; includeSubdomains"
}
if o.HSTS.Preload {
v += "; preload"
}
return
}