forked from browseraudit/browseraudit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsp.go
More file actions
227 lines (182 loc) · 6.18 KB
/
Copy pathcsp.go
File metadata and controls
227 lines (182 loc) · 6.18 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
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
package main
import (
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"regexp"
)
type CSPTemplate struct {
SessionID string
CookieDomain string
Domain1 string
Subdomain1 string
Domain2 string
Subdomain2 string
}
func CSPServeHandler(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
session := store.Get(w, r)
id := mux.Vars(r)["id"]
r.ParseForm()
// If there is a "policy" parameter in the query string, send back the
// Base64-decoded Content-Security-Policy: HTTP header
if r.Form["policy"] != nil && r.Form["policy"][0] != "" {
policyBase64 := r.Form["policy"][0]
policy, err := base64.StdEncoding.DecodeString(policyBase64)
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Security-Policy", string(policy))
}
// If there is a "defaultResult" parameter in the query string, set the
// default result for this CSP test in the session data
if r.Form["defaultResult"] != nil && r.Form["defaultResult"][0] != "" {
defaultResult := r.Form["defaultResult"][0]
session.Set("csp"+id, defaultResult)
}
CSPServeFile(w, r)
}
func CSPPassHandler(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
session := store.Get(w, r)
id := mux.Vars(r)["id"]
r.ParseForm()
session.Set("csp"+id, "pass")
fmt.Println("Recording PASS for CSP test " + id)
CSPServeFile(w, r)
}
func CSPFailHandler(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
session := store.Get(w, r)
id := mux.Vars(r)["id"]
r.ParseForm()
session.Set("csp"+id, "fail")
fmt.Println("Recording FAIL for CSP test " + id)
CSPServeFile(w, r)
}
func CSPServeFile(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
// Parameters passed in the path of the URI:
// "id" specifies the CSP test ID
id := mux.Vars(r)["id"]
// "file" specifies a particular file to be served
file := mux.Vars(r)["file"]
r.ParseForm()
// Set the Access-Control-Allow-*: HTTP headers, if required
if r.Form["corsOrigin"] != nil && r.Form["corsOrigin"][0] != "" {
// "corsOrigin" specifies the Access-Control-Allow-Origin: HTTP
// header to be included in the response
w.Header().Set("Access-Control-Allow-Origin", r.Form["corsOrigin"][0])
}
if r.Form["corsMethod"] != nil && r.Form["corsMethod"][0] != "" {
// "corsMethod" specifies the Access-Control-Allow-Method: HTTP
// header to be included in the response
w.Header().Set("Access-Control-Allow-Method", r.Form["corsMethod"][0])
}
// Serve a file as directed by the "file" segment of the URI path
switch {
// First, handle the static files
case file == "emptyhtml":
http.ServeFile(w, r, "./csp/empty.html")
case file == "emptycss":
w.Header().Set("Content-Type", "text/css")
fmt.Fprintln(w, "/* Empty CSS */")
case file == "emptyjs":
w.Header().Set("Content-Type", "text/javascript")
fmt.Fprintln(w, "// Empty JS")
case file == "emptyes":
// We need to send a non-200 HTTP status code and/or a Content-Type
// other than text/event-stream to prevent Chrome from making
// endless requests to this "stream"
http.Error(w, "Method Not Allowed", 405)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "data: 1\n\n")
case file == "oktext":
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "ok")
case file == "png":
http.ServeFile(w, r, "./static/pixel.png")
case file == "swf":
http.ServeFile(w, r, "./csp/minimal.swf")
case file == "mp3":
http.ServeFile(w, r, "./csp/mpthreetest.mp3")
case file == "mp4":
http.ServeFile(w, r, "./csp/small.mp4")
case file == "eot":
//w.Header().Set("Content-Type", "application/vnd.ms-fontobject")
//fileToServe = "./csp/BrowserAuditTestFont.eot"
http.ServeFile(w, r, "./csp/BrowserAuditTestFont.eot")
case file == "woff":
//w.Header().Set("Content-Type", "application/font-woff")
//fileToServe = "./csp/BrowserAuditTestFont.woff"
http.ServeFile(w, r, "./csp/BrowserAuditTestFont.woff")
case file == "ttf":
//w.Header().Set("Content-Type", "application/x-font-ttf")
//fileToServe = "./csp/BrowserAuditTestFont.ttf"
http.ServeFile(w, r, "./csp/BrowserAuditTestFont.ttf")
case file == "svg":
//w.Header().Set("Content-Type", "image/svg+xml")
//fileToServe = "./csp/BrowserAuditTestFont.svg"
http.ServeFile(w, r, "./csp/BrowserAuditTestFont.svg")
// Now, handle the dynamic (i.e. parameterised) files
case regexp.MustCompile("^param-").MatchString(file):
fileToServe := ""
switch {
// param-html => iframe HTML file
case file == "param-html":
w.Header().Set("Content-Type", "text/html")
fileToServe = "./csp/" + id + ".html"
// param-htmlb => nested iframe HTML file
case file == "param-htmlb":
w.Header().Set("Content-Type", "text/html")
fileToServe = "./csp/" + id + "-b.html"
// param-css => external stylesheet
case file == "param-css":
w.Header().Set("Content-Type", "text/css")
fileToServe = "./csp/" + id + ".css"
// param-cssb => nested external stylesheet
case file == "param-cssb":
w.Header().Set("Content-Type", "text/css")
fileToServe = "./csp/" + id + "-b.css"
} // end switch
cookieDomain := ""
if r.Form["cookieDomain"] != nil && r.Form["cookieDomain"][0] != "" {
// "cookieDomain" is present for document.cookie
// allow-same-origin sandbox tests, and controls
// the cookie that should be attempted to be
// read by a script in the child iframe
cookieDomain = r.Form["cookieDomain"][0]
}
// Serve dynamic file. browseraudit cookie is guaranteed
// to exist, due to earlier call to addManualCookie()
session := store.Get(w, r)
p := &CSPTemplate{
SessionID: session.Id,
CookieDomain: cookieDomain,
Domain1: cfg.Domain.Domain1,
Subdomain1: cfg.Domain.Subdomain1,
Domain2: cfg.Domain.Domain2,
Subdomain2: cfg.Domain.Subdomain2,
}
t, err := template.ParseFiles(fileToServe)
if err != nil {
log.Println(err)
}
t.Execute(w, p)
} // end switch
}
func CSPResultHandler(w http.ResponseWriter, r *http.Request) {
DontCache(&w)
session := store.Get(w, r)
id := mux.Vars(r)["id"]
result, err := session.Get("csp" + id)
fmt.Printf("Fetching result for CSP test %s: %s\n", id, result)
if err != nil {
log.Printf("nil result csp%s\n", id)
result = "nil"
}
fmt.Fprintf(w, "%s", result)
}