Skip to content

Commit 2b8f72b

Browse files
author
Charles-Antoine Mathieu
committed
Add security headers to getFileHandler to avoid HTML rendring in web browser (src-d#162)
1 parent daeb202 commit 2b8f72b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

server/handlers/getFile.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"io"
3535
"net/http"
3636
"strconv"
37+
"strings"
3738

3839
"github.com/root-gg/plik/server/Godeps/_workspace/src/github.com/gorilla/mux"
3940
"github.com/root-gg/plik/server/Godeps/_workspace/src/github.com/root-gg/juliet"
@@ -119,8 +120,24 @@ func GetFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
119120
}
120121
}
121122

123+
// Avoid rendering HTML in browser
124+
if strings.Contains(file.Type, "html") {
125+
file.Type = "text/plain"
126+
}
127+
128+
if file.Type == "" || strings.Contains(file.Type, "flash") {
129+
file.Type = "application/octet-stream"
130+
}
131+
122132
// Set content type and print file
123133
resp.Header().Set("Content-Type", file.Type)
134+
135+
/* Additional security headers for possibly unsafe content */
136+
resp.Header().Set("X-Content-Type-Options", "nosniff")
137+
resp.Header().Set("X-XSS-Protection", "1; mode=block")
138+
resp.Header().Set("X-Frame-Options", "DENY")
139+
resp.Header().Set("Content-Security-Policy", "default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; connect-src 'none'; font-src 'none'; object-src 'none'; media-src 'none'; child-src 'none'; form-action 'none'; frame-ancestors 'none'; plugin-types ''; sandbox ''")
140+
124141
if file.CurrentSize > 0 {
125142
resp.Header().Set("Content-Length", strconv.Itoa(int(file.CurrentSize)))
126143
}

0 commit comments

Comments
 (0)