mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-01-23 11:14:48 +02:00
Borrow buffer from imagedata pool when sanitizing SVG
This commit is contained in:
parent
f95f57bb4d
commit
f5f4fb6487
@ -1,6 +1,8 @@
|
||||
package imagedata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/imgproxy/imgproxy/v3/bufpool"
|
||||
@ -86,3 +88,10 @@ func readAndCheckImage(r io.Reader, contentLength int) (*ImageData, error) {
|
||||
cancel: cancel,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func BorrowBuffer() (*bytes.Buffer, context.CancelFunc) {
|
||||
buf := downloadBufPool.Get(0, false)
|
||||
cancel := func() { downloadBufPool.Put(buf) }
|
||||
|
||||
return buf, cancel
|
||||
}
|
||||
|
@ -344,17 +344,14 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
|
||||
// Don't process SVG
|
||||
if originData.Type == imagetype.SVG {
|
||||
if config.SanitizeSvg {
|
||||
sanitized, svgErr := svg.Satitize(originData.Data)
|
||||
sanitized, svgErr := svg.Satitize(originData)
|
||||
checkErr(ctx, "svg_processing", svgErr)
|
||||
|
||||
// Since we'll replace origin data, it's better to close it to return
|
||||
// it's buffer to the pool
|
||||
originData.Close()
|
||||
|
||||
originData = &imagedata.ImageData{
|
||||
Data: sanitized,
|
||||
Type: imagetype.SVG,
|
||||
}
|
||||
originData = sanitized
|
||||
}
|
||||
|
||||
respondWithImage(reqID, r, rw, statusCode, originData, po, imageURL, originData)
|
||||
|
@ -291,11 +291,11 @@ func (s *ProcessingHandlerTestSuite) TestSkipProcessingSVG() {
|
||||
require.Equal(s.T(), 200, res.StatusCode)
|
||||
|
||||
actual := s.readBody(res)
|
||||
expected, err := svg.Satitize(s.readTestFile("test1.svg"))
|
||||
expected, err := svg.Satitize(&imagedata.ImageData{Data: s.readTestFile("test1.svg")})
|
||||
|
||||
require.Nil(s.T(), err)
|
||||
|
||||
require.True(s.T(), bytes.Equal(expected, actual))
|
||||
require.True(s.T(), bytes.Equal(expected.Data, actual))
|
||||
}
|
||||
|
||||
func (s *ProcessingHandlerTestSuite) TestNotSkipProcessingSVGToJPG() {
|
||||
|
19
svg/svg.go
19
svg/svg.go
@ -7,14 +7,15 @@ import (
|
||||
|
||||
"github.com/tdewolff/parse/v2"
|
||||
"github.com/tdewolff/parse/v2/xml"
|
||||
|
||||
"github.com/imgproxy/imgproxy/v3/imagedata"
|
||||
)
|
||||
|
||||
func Satitize(data []byte) ([]byte, error) {
|
||||
r := bytes.NewReader(data)
|
||||
func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
|
||||
r := bytes.NewReader(data.Data)
|
||||
l := xml.NewLexer(parse.NewInput(r))
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.Grow(len(data))
|
||||
buf, cancel := imagedata.BorrowBuffer()
|
||||
|
||||
ignoreTag := 0
|
||||
|
||||
@ -35,9 +36,17 @@ func Satitize(data []byte) ([]byte, error) {
|
||||
switch tt {
|
||||
case xml.ErrorToken:
|
||||
if l.Err() != io.EOF {
|
||||
cancel()
|
||||
return nil, l.Err()
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
|
||||
newData := imagedata.ImageData{
|
||||
Data: buf.Bytes(),
|
||||
Type: data.Type,
|
||||
}
|
||||
newData.SetCancel(cancel)
|
||||
|
||||
return &newData, nil
|
||||
case xml.StartTagToken:
|
||||
if strings.ToLower(string(l.Text())) == "script" {
|
||||
ignoreTag++
|
||||
|
Loading…
x
Reference in New Issue
Block a user