-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpolicy.go
204 lines (169 loc) · 4.06 KB
/
policy.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
package secure
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
type (
// Secure is a middleware that helps setup a few basic security features. A single secure.Options struct can be
// provided to configure which features should be enabled, and the ability to override a few of the default values.
policy struct {
// Customize Secure with an Options struct.
config Config
fixedHeaders []header
}
header struct {
key string
value []string
}
)
// Constructs a new Policy instance with supplied options.
func newPolicy(config Config) *policy {
policy := &policy{}
policy.loadConfig(config)
return policy
}
func (p *policy) loadConfig(config Config) {
p.config = config
p.fixedHeaders = make([]header, 0, 5)
// Frame Options header.
if len(config.CustomFrameOptionsValue) > 0 {
p.addHeader("X-Frame-Options", config.CustomFrameOptionsValue)
} else if config.FrameDeny {
p.addHeader("X-Frame-Options", "DENY")
}
// Content Type Options header.
if config.ContentTypeNosniff {
p.addHeader("X-Content-Type-Options", "nosniff")
}
// XSS Protection header.
if config.BrowserXssFilter {
p.addHeader("X-Xss-Protection", "1; mode=block")
}
// Content Security Policy header.
if len(config.ContentSecurityPolicy) > 0 {
p.addHeader("Content-Security-Policy", config.ContentSecurityPolicy)
}
if len(config.ReferrerPolicy) > 0 {
p.addHeader("Referrer-Policy", config.ReferrerPolicy)
}
// Strict Transport Security header.
if config.STSSeconds != 0 {
stsSub := ""
if config.STSIncludeSubdomains {
stsSub = "; includeSubdomains"
}
if config.STSPreload {
stsSub = "; preload"
}
// TODO
// "max-age=%d%s" refactor
p.addHeader(
"Strict-Transport-Security",
fmt.Sprintf("max-age=%d%s", config.STSSeconds, stsSub))
}
// X-Download-Options header.
if config.IENoOpen {
p.addHeader("X-Download-Options", "noopen")
}
// FeaturePolicy header.
if len(config.FeaturePolicy) > 0 {
p.addHeader("Feature-Policy", config.FeaturePolicy)
}
}
func (p *policy) addHeader(key string, value string) {
p.fixedHeaders = append(p.fixedHeaders, header{
key: key,
value: []string{value},
})
}
func (p *policy) applyToContext(c *gin.Context) bool {
if !p.config.IsDevelopment {
p.writeSecureHeaders(c)
if !p.checkAllowHosts(c) {
return false
}
if !p.checkSSL(c) {
return false
}
}
return true
}
func (p *policy) writeSecureHeaders(c *gin.Context) {
header := c.Writer.Header()
for _, pair := range p.fixedHeaders {
header[pair.key] = pair.value
}
}
func (p *policy) checkAllowHosts(c *gin.Context) bool {
if len(p.config.AllowedHosts) == 0 {
return true
}
host := c.Request.Host
if len(host) == 0 {
host = c.Request.URL.Host
}
for _, allowedHost := range p.config.AllowedHosts {
if strings.EqualFold(allowedHost, host) {
return true
}
}
if p.config.BadHostHandler != nil {
p.config.BadHostHandler(c)
} else {
c.AbortWithStatus(403)
}
return false
}
// checks if a host (possibly with trailing port) is an IPV4 address
func isIPV4(host string) bool {
if index := strings.IndexByte(host, ':'); index != -1 {
host = host[:index]
}
return net.ParseIP(host) != nil
}
func (p *policy) isSSLRequest(req *http.Request) bool {
if strings.EqualFold(req.URL.Scheme, "https") || req.TLS != nil {
return true
}
for h, v := range p.config.SSLProxyHeaders {
hv, ok := req.Header[h]
if !ok {
continue
}
if strings.EqualFold(hv[0], v) {
return true
}
}
if p.config.DontRedirectIPV4Hostnames && isIPV4(req.Host) {
return true
}
return false
}
func (p *policy) checkSSL(c *gin.Context) bool {
if !p.config.SSLRedirect {
return true
}
req := c.Request
isSSLRequest := p.isSSLRequest(req)
if isSSLRequest {
return true
}
// TODO
// req.Host vs req.URL.Host
url := req.URL
url.Scheme = "https"
url.Host = req.Host
if len(p.config.SSLHost) > 0 {
url.Host = p.config.SSLHost
}
status := http.StatusMovedPermanently
if p.config.SSLTemporaryRedirect {
status = http.StatusTemporaryRedirect
}
c.Redirect(status, url.String())
c.Abort()
return false
}