debian-mirror-gitlab/workhorse/internal/headers/content_headers.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
3.8 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package headers
import (
"net/http"
"regexp"
2021-10-27 15:23:28 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/utils/svg"
2021-02-22 17:27:13 +05:30
)
var (
2022-06-21 17:19:12 +05:30
javaScriptTypeRegex = regexp.MustCompile(`^(text|application)\/javascript$`)
2021-02-22 17:27:13 +05:30
2022-06-21 17:19:12 +05:30
imageTypeRegex = regexp.MustCompile(`^image/*`)
svgMimeTypeRegex = regexp.MustCompile(`^image/svg\+xml$`)
2021-02-22 17:27:13 +05:30
2022-06-21 17:19:12 +05:30
textTypeRegex = regexp.MustCompile(`^text/*`)
2021-02-22 17:27:13 +05:30
2022-06-21 17:19:12 +05:30
videoTypeRegex = regexp.MustCompile(`^video/*`)
2021-02-22 17:27:13 +05:30
2022-06-21 17:19:12 +05:30
pdfTypeRegex = regexp.MustCompile(`application\/pdf`)
attachmentRegex = regexp.MustCompile(`^attachment`)
inlineRegex = regexp.MustCompile(`^inline`)
2021-02-22 17:27:13 +05:30
)
// Mime types that can't be inlined. Usually subtypes of main types
2022-06-21 17:19:12 +05:30
var forbiddenInlineTypes = []*regexp.Regexp{svgMimeTypeRegex}
2021-02-22 17:27:13 +05:30
// Mime types that can be inlined. We can add global types like "image/" or
// specific types like "text/plain". If there is a specific type inside a global
// allowed type that can't be inlined we must add it to the forbiddenInlineTypes var.
// One example of this is the mime type "image". We allow all images to be
// inlined except for SVGs.
2022-06-21 17:19:12 +05:30
var allowedInlineTypes = []*regexp.Regexp{imageTypeRegex, textTypeRegex, videoTypeRegex, pdfTypeRegex}
const (
svgContentType = "image/svg+xml"
textPlainContentType = "text/plain; charset=utf-8"
attachmentDispositionText = "attachment"
inlineDispositionText = "inline"
)
2021-02-22 17:27:13 +05:30
func SafeContentHeaders(data []byte, contentDisposition string) (string, string) {
contentType := safeContentType(data)
contentDisposition = safeContentDisposition(contentType, contentDisposition)
return contentType, contentDisposition
}
func safeContentType(data []byte) string {
// Special case for svg because DetectContentType detects it as text
if svg.Is(data) {
2022-06-21 17:19:12 +05:30
return svgContentType
2021-02-22 17:27:13 +05:30
}
// Override any existing Content-Type header from other ResponseWriters
contentType := http.DetectContentType(data)
2022-06-21 17:19:12 +05:30
// http.DetectContentType does not support JavaScript and would only
// return text/plain. But for cautionary measures, just in case they start supporting
// it down the road and start returning application/javascript, we want to handle it now
// to avoid regressions.
if isType(contentType, javaScriptTypeRegex) {
return textPlainContentType
}
2021-02-22 17:27:13 +05:30
// If the content is text type, we set to plain, because we don't
// want to render it inline if they're html or javascript
2022-06-21 17:19:12 +05:30
if isType(contentType, textTypeRegex) {
return textPlainContentType
2021-02-22 17:27:13 +05:30
}
return contentType
}
func safeContentDisposition(contentType string, contentDisposition string) string {
// If the existing disposition is attachment we return that. This allow us
// to force a download from GitLab (ie: RawController)
2022-06-21 17:19:12 +05:30
if attachmentRegex.MatchString(contentDisposition) {
2021-02-22 17:27:13 +05:30
return contentDisposition
}
// Checks for mime types that are forbidden to be inline
for _, element := range forbiddenInlineTypes {
if isType(contentType, element) {
return attachmentDisposition(contentDisposition)
}
}
// Checks for mime types allowed to be inline
for _, element := range allowedInlineTypes {
if isType(contentType, element) {
return inlineDisposition(contentDisposition)
}
}
// Anything else is set to attachment
return attachmentDisposition(contentDisposition)
}
func attachmentDisposition(contentDisposition string) string {
if contentDisposition == "" {
2022-06-21 17:19:12 +05:30
return attachmentDispositionText
2021-02-22 17:27:13 +05:30
}
2022-06-21 17:19:12 +05:30
if inlineRegex.MatchString(contentDisposition) {
return inlineRegex.ReplaceAllString(contentDisposition, attachmentDispositionText)
2021-02-22 17:27:13 +05:30
}
return contentDisposition
}
func inlineDisposition(contentDisposition string) string {
if contentDisposition == "" {
2022-06-21 17:19:12 +05:30
return inlineDispositionText
2021-02-22 17:27:13 +05:30
}
2022-06-21 17:19:12 +05:30
if attachmentRegex.MatchString(contentDisposition) {
return attachmentRegex.ReplaceAllString(contentDisposition, inlineDispositionText)
2021-02-22 17:27:13 +05:30
}
return contentDisposition
}
func isType(contentType string, mimeType *regexp.Regexp) bool {
return mimeType.MatchString(contentType)
}