From: Lauris BH Date: Sat, 28 May 2022 15:40:03 +0000 (+0300) Subject: Fix raw endpoint PDF file headers (#19825) (#19826) X-Git-Tag: v1.16.9~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0e9499ada7ecfe1fab9fd3714f3d4785d862b183;p=gitea.git Fix raw endpoint PDF file headers (#19825) (#19826) --- diff --git a/modules/typesniffer/typesniffer.go b/modules/typesniffer/typesniffer.go index 9e29b3557c..3cfbd92319 100644 --- a/modules/typesniffer/typesniffer.go +++ b/modules/typesniffer/typesniffer.go @@ -17,8 +17,12 @@ import ( // Use at most this many bytes to determine Content Type. const sniffLen = 1024 -// SvgMimeType MIME type of SVG images. -const SvgMimeType = "image/svg+xml" +const ( + // SvgMimeType MIME type of SVG images. + SvgMimeType = "image/svg+xml" + // ApplicationOctetStream MIME type of binary files. + ApplicationOctetStream = "application/octet-stream" +) var svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(||>))\s*)*\/]`) var svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(||>))\s*)*\/]`) diff --git a/routers/common/repo.go b/routers/common/repo.go index b0e14b63f5..1e9076a198 100644 --- a/routers/common/repo.go +++ b/routers/common/repo.go @@ -87,10 +87,14 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) } if (st.IsImage() || st.IsPDF()) && (setting.UI.SVG.Enabled || !st.IsSvgImage()) { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name)) - if st.IsSvgImage() { + if st.IsSvgImage() || st.IsPDF() { ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox") ctx.Resp.Header().Set("X-Content-Type-Options", "nosniff") - ctx.Resp.Header().Set("Content-Type", typesniffer.SvgMimeType) + if st.IsSvgImage() { + ctx.Resp.Header().Set("Content-Type", typesniffer.SvgMimeType) + } else { + ctx.Resp.Header().Set("Content-Type", typesniffer.ApplicationOctetStream) + } } } else { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))