-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathhttpMiddlewares.go
More file actions
117 lines (109 loc) · 3.93 KB
/
Copy pathhttpMiddlewares.go
File metadata and controls
117 lines (109 loc) · 3.93 KB
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
package main
import (
"net/http"
"net/url"
"slices"
"github.com/samber/lo"
"github.com/tiptophelmet/cspolicy"
"github.com/tiptophelmet/cspolicy/directives"
"github.com/tiptophelmet/cspolicy/directives/constraint"
"github.com/tiptophelmet/cspolicy/src"
"github.com/tiptophelmet/cspolicy/src/hashalg"
)
func noIndexHeader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "noindex")
next.ServeHTTP(w, r)
})
}
func fixHTTPHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.RawPath = ""
next.ServeHTTP(w, r)
})
}
func headAsGetHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead {
// Clone request and change method
newReq := new(http.Request)
*newReq = *r
newReq.Method = http.MethodGet
// Serve new request
next.ServeHTTP(w, newReq)
return
}
next.ServeHTTP(w, r)
})
}
func (a *goBlog) securityHeaders(next http.Handler) http.Handler {
allowedDomains := []string{a.cfg.Server.publicHost, a.cfg.Server.shortPublicHost, a.cfg.Server.mediaHost}
allowedDomains = append(allowedDomains, a.cfg.Server.altHosts...)
allowedDomains = append(allowedDomains, a.cfg.Server.CSPDomains...)
if mp := a.cfg.Micropub.MediaStorage; mp != nil && mp.MediaURL != "" {
if u, err := url.Parse(mp.MediaURL); err == nil {
allowedDomains = append(allowedDomains, u.Hostname())
}
}
allowedDomains = lo.Uniq(lo.Filter(allowedDomains, func(v string, _ int) bool { return v != "" }))
defaultSrcList := make([]src.SourceVal, 0, 2+len(allowedDomains))
defaultSrcList = append(defaultSrcList, src.Self(), src.Scheme("blob:"))
for _, d := range allowedDomains {
defaultSrcList = append(defaultSrcList, src.Host(d))
}
imgSrcList := make([]src.SourceVal, 0, 2+len(allowedDomains))
imgSrcList = append(imgSrcList, src.Self(), src.Scheme("data:"))
for _, d := range allowedDomains {
imgSrcList = append(imgSrcList, src.Host(d))
}
fac := &constraint.FrameAncestorsConstraint{}
fac.Sources(src.None())
// style-src must allow the inlined main stylesheet via its sha256 hash
styleSrcList := make([]src.SourceVal, 0, 3+len(allowedDomains))
styleSrcList = append(styleSrcList, src.Self())
if af, ok := a.assetFiles[a.assetFileNames["css/styles.css"]]; ok && af != nil {
styleSrcList = append(styleSrcList, src.HashAlgBase64(hashalg.Sha256(), af.sha256base64))
}
if af, ok := a.assetFiles[a.assetFileNames["css/admin.css"]]; ok && af != nil {
styleSrcList = append(styleSrcList, src.HashAlgBase64(hashalg.Sha256(), af.sha256base64))
}
for _, d := range allowedDomains {
styleSrcList = append(styleSrcList, src.Host(d))
}
csp := cspolicy.Build(
directives.DefaultSrc(defaultSrcList...),
directives.ImgSrc(imgSrcList...),
directives.StyleSrc(styleSrcList...),
directives.FrameAncestors(fac),
)
// Return handler
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000;")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Security-Policy", csp)
next.ServeHTTP(w, r)
})
}
func (a *goBlog) addOnionLocation(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if a.torAddress != "" {
w.Header().Set("Onion-Location", a.torAddress+r.URL.RequestURI())
}
next.ServeHTTP(w, r)
})
}
func keepSelectedQueryParams(paramsToKeep ...string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
for param := range query {
if !slices.Contains(paramsToKeep, param) {
query.Del(param)
}
}
r.URL.RawQuery = query.Encode()
next.ServeHTTP(w, r)
})
}
}