Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/server/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ func (s *Server) browseFile(w http.ResponseWriter, r *http.Request, ecosystem, n
}
defer func() { _ = fileReader.Close() }()

// Set content type based on file extension
contentType := detectContentType(filePath)
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Security-Policy", "sandbox")
w.Header().Set("X-Content-Type-Options", "nosniff")

// Set filename for download
_, filename := path.Split(filePath)
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", filename))

Expand All @@ -368,8 +368,8 @@ func detectContentType(filename string) string {
// Text formats
case ".txt", ".md", ".markdown":
return contentTypePlainText
case ".html", ".htm":
return "text/html; charset=utf-8"
case ".html", ".htm", ".xhtml":
return contentTypePlainText
case ".css":
return "text/css; charset=utf-8"
case ".js", ".mjs":
Expand Down Expand Up @@ -423,7 +423,7 @@ func detectContentType(filename string) string {
case ".gif":
return "image/gif"
case ".svg":
return "image/svg+xml"
return contentTypePlainText
case ".ico":
return "image/x-icon"

Expand Down
4 changes: 4 additions & 0 deletions internal/server/browse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func TestDetectContentType(t *testing.T) {
{"file.go", "text/x-go; charset=utf-8"},
{"file.py", "text/x-python; charset=utf-8"},
{"file.rs", "text/x-rust; charset=utf-8"},
{"file.html", contentTypePlainText},
{"file.htm", contentTypePlainText},
{"file.xhtml", contentTypePlainText},
{"file.svg", contentTypePlainText},
{"file.png", "image/png"},
{"file.jpg", "image/jpeg"},
{"README", contentTypePlainText},
Expand Down