From 865d3d188817dcd03e2496cd850856ed94ee0b06 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Fri, 12 Jul 2024 19:55:41 +0800 Subject: [PATCH] update --- upload/filetype_detector.go | 23 +++++++++++++++++------ upload/filetype_detector_test.go | 23 +---------------------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/upload/filetype_detector.go b/upload/filetype_detector.go index 06ddb82..988ce68 100644 --- a/upload/filetype_detector.go +++ b/upload/filetype_detector.go @@ -6,18 +6,29 @@ import ( "regexp" "strings" + "github.com/admpub/safesvg" "github.com/h2non/filetype" "github.com/h2non/filetype/matchers" "github.com/h2non/filetype/types" svg "github.com/h2non/go-is-svg" - "github.com/hamochi/safesvg" ) -var ReadHeadSizeBytes = 261 -var SVGMaxSizeBytes int64 = 5 * 1024 * 1024 -var ErrIncorrectFileFormat = errors.New(`file format is incorrect`) -var defaultSafeSVGValidator = safesvg.NewValidator() -var svgHeadRegex = regexp.MustCompile(`(?i)^\s*(?:<\?xml[^>]*>\s*)?(?:]*>\s*)?]*>`) +var ( + ReadHeadSizeBytes = 261 + SVGMaxSizeBytes int64 = 5 * 1024 * 1024 + ErrIncorrectFileFormat = errors.New(`file format is incorrect`) + defaultSafeSVGValidator = safesvg.NewValidator() + svgHeadRegex = regexp.MustCompile(`(?i)^\s*(?:<\?xml[^>]*>\s*)?(?:]*>\s*)?]*>`) +) + +func init() { + defaultSafeSVGValidator.WhitelistElements(`style`) + defaultSafeSVGValidator.WhitelistAttributes(`http://www.w3.org/1999/xlink:href`, `data-name`) +} + +func SafeSVGValidator() safesvg.Validator { + return defaultSafeSVGValidator +} func ReadHeadBytes(r io.Reader, readSizes ...int) ([]byte, error) { readSize := ReadHeadSizeBytes diff --git a/upload/filetype_detector_test.go b/upload/filetype_detector_test.go index 58bac3d..b112158 100644 --- a/upload/filetype_detector_test.go +++ b/upload/filetype_detector_test.go @@ -7,28 +7,7 @@ import ( "github.com/stretchr/testify/assert" ) -var svgBytes = []byte(` - - - - - - - - - - - - - - - - - - - - -`) +var svgBytes = []byte(`.TOP`) func TestIsSVG(t *testing.T) { r := IsSVGImage(svgBytes)