Rendering: Remove SVG sanitization (#109797)

This commit is contained in:
Mariell Hoversholm
2025-08-19 11:34:12 +02:00
committed by GitHub
parent 1a87679dc7
commit 0fc29cbaae
15 changed files with 49 additions and 344 deletions
+6 -29
View File
@@ -2,46 +2,23 @@ package store
import (
"context"
"errors"
"mime"
"path/filepath"
"github.com/grafana/grafana/pkg/infra/filestorage"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/store/sanitizer"
"github.com/grafana/grafana/pkg/services/user"
)
func (s *standardStorageService) sanitizeContents(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) ([]byte, error) {
func (s *standardStorageService) sanitizeUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error) {
if req.EntityType == EntityTypeImage {
ext := filepath.Ext(req.Path)
if ext == ".svg" {
resp, err := sanitizer.SanitizeSVG(ctx, &rendering.SanitizeSVGRequest{
Filename: storagePath,
Content: req.Contents,
})
if err != nil {
if s.cfg != nil && s.cfg.AllowUnsanitizedSvgUpload {
grafanaStorageLogger.Debug("Allowing unsanitized svg upload", "filename", req.Path, "sanitizationError", err)
return req.Contents, nil
} else {
grafanaStorageLogger.Debug("Disallowing unsanitized svg upload", "filename", req.Path, "sanitizationError", err)
return nil, err
}
}
return resp.Sanitized, nil
if ext == ".svg" && !s.cfg.AllowUnsanitizedSvgUpload {
grafanaStorageLogger.Debug("Disallowing svg upload", "filename", req.Path)
return nil, errors.New("SVG uploads are not allowed")
}
}
return req.Contents, nil
}
func (s *standardStorageService) sanitizeUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error) {
contents, err := s.sanitizeContents(ctx, user, req, storagePath)
if err != nil {
return nil, err
}
// we have already validated that the file contents match the extension in `./validate.go`
mimeType := mime.TypeByExtension(filepath.Ext(req.Path))
if mimeType == "" {
@@ -51,7 +28,7 @@ func (s *standardStorageService) sanitizeUploadRequest(ctx context.Context, user
return &filestorage.UpsertFileCommand{
Path: storagePath,
Contents: contents,
Contents: req.Contents,
MimeType: mimeType,
CacheControl: req.CacheControl,
ContentDisposition: req.ContentDisposition,
-23
View File
@@ -1,23 +0,0 @@
package sanitizer
import (
"context"
"errors"
"github.com/grafana/grafana/pkg/services/rendering"
)
// workaround for cyclic dep between the store and the renderer
type Provider struct{}
var SanitizeSVG = func(ctx context.Context, req *rendering.SanitizeSVGRequest) (*rendering.SanitizeSVGResponse, error) {
return nil, errors.New("not implemented")
}
func ProvideService(
renderer rendering.Service,
) *Provider {
SanitizeSVG = renderer.SanitizeSVG
return &Provider{}
}