-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
135 lines (117 loc) · 3.32 KB
/
handlers.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
package zhttp
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"zgo.at/zlog"
)
// HandlerRobots writes a simple robots.txt.
func HandlerRobots(rules [][]string) func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
for _, r := range rules {
buf.WriteString(fmt.Sprintf("%s\n%s\n\n", r[0], r[1]))
}
text := buf.Bytes()
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Cache-Control", "public,max-age=31536000")
w.WriteHeader(200)
w.Write(text)
}
}
// HandlerJSErr logs JavaScript errors from window.onerror.
func HandlerJSErr() func(w http.ResponseWriter, r *http.Request) {
type Error struct {
Msg string `json:"msg"`
URL string `json:"url"`
Loc string `json:"loc"`
Line string `json:"line"`
Column string `json:"column"`
Stack string `json:"stack"`
UserAgent string `json:"ua"`
}
return func(w http.ResponseWriter, r *http.Request) {
var args Error
_, err := Decode(r, &args)
if err != nil {
zlog.Error(err)
return
}
zlog.Fields(zlog.F{
"url": args.URL,
"loc": args.Loc,
"line": args.Line,
"column": args.Column,
"stack": args.Stack,
"userAgent": args.UserAgent,
}).Errorf("JavaScript error: %s", args.Msg)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNoContent)
}
}
// CSP errors.
type (
CSPError struct {
Report Report `json:"csp-report"`
}
Report struct {
BlockedURI string `json:"blocked-uri"`
ColumnNumber int `json:"column-number"`
DocumentURI string `json:"document-uri"`
LineNumber int `json:"line-number"`
Referrer string `json:"referrer"`
SourceFile string `json:"source-file"`
Violated string `json:"violated-directive"`
}
)
// HandlerCSP handles CSP errors.
func HandlerCSP() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
d, _ := ioutil.ReadAll(r.Body)
var csp CSPError
err := json.Unmarshal(d, &csp)
// Probably an extension or something.
if err == nil && !noise(csp.Report) {
fmt.Println(string(d))
zlog.Fields(zlog.F{
"BlockedURI": csp.Report.BlockedURI,
"ColumnNumber": csp.Report.ColumnNumber,
"DocumentURI": csp.Report.DocumentURI,
"LineNumber": csp.Report.LineNumber,
"Referrer": csp.Report.Referrer,
"SourceFile": csp.Report.SourceFile,
"Violated": csp.Report.Violated,
}).Errorf("CSP error")
}
w.WriteHeader(202)
}
}
func noise(r Report) bool {
// Probably some extension or whatnot that injected a script.
if r.ColumnNumber == 1 && r.LineNumber == 1 &&
r.Violated == "script-src" &&
(r.BlockedURI == "inline" || r.BlockedURI == "eval" || r.BlockedURI == "data") {
return true
}
if strings.HasPrefix(r.SourceFile, "safari-extension://") || strings.HasPrefix(r.SourceFile, "moz-extension://") {
return true
}
return false
}
// HandlerRedirectHTTP redirects all HTTP requests to HTTPS.
func HandlerRedirectHTTP(port string) http.HandlerFunc {
if port == "" {
port = "443"
}
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Encode()
if len(q) > 0 {
q = "?" + q
}
w.Header().Set("Location", fmt.Sprintf("https://%s:%s%s%s", r.Host, port, r.URL.Path, q))
w.WriteHeader(301)
}
}