Skip to content

Commit 0e9499a

Browse files
authored
Fix raw endpoint PDF file headers (#19825) (#19826)
1 parent 675f658 commit 0e9499a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

modules/typesniffer/typesniffer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ import (
1717
// Use at most this many bytes to determine Content Type.
1818
const sniffLen = 1024
1919

20-
// SvgMimeType MIME type of SVG images.
21-
const SvgMimeType = "image/svg+xml"
20+
const (
21+
// SvgMimeType MIME type of SVG images.
22+
SvgMimeType = "image/svg+xml"
23+
// ApplicationOctetStream MIME type of binary files.
24+
ApplicationOctetStream = "application/octet-stream"
25+
)
2226

2327
var svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)
2428
var svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)

routers/common/repo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
8787
}
8888
if (st.IsImage() || st.IsPDF()) && (setting.UI.SVG.Enabled || !st.IsSvgImage()) {
8989
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
90-
if st.IsSvgImage() {
90+
if st.IsSvgImage() || st.IsPDF() {
9191
ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox")
9292
ctx.Resp.Header().Set("X-Content-Type-Options", "nosniff")
93-
ctx.Resp.Header().Set("Content-Type", typesniffer.SvgMimeType)
93+
if st.IsSvgImage() {
94+
ctx.Resp.Header().Set("Content-Type", typesniffer.SvgMimeType)
95+
} else {
96+
ctx.Resp.Header().Set("Content-Type", typesniffer.ApplicationOctetStream)
97+
}
9498
}
9599
} else {
96100
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))

0 commit comments

Comments
 (0)