mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 04:02:29 +02:00
fix(svg/sanitize): preserve headers from origin data
This commit is contained in:
@@ -362,7 +362,7 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
|
|||||||
// Don't process SVG
|
// Don't process SVG
|
||||||
if originData.Type == imagetype.SVG {
|
if originData.Type == imagetype.SVG {
|
||||||
if config.SanitizeSvg {
|
if config.SanitizeSvg {
|
||||||
sanitized, svgErr := svg.Satitize(originData)
|
sanitized, svgErr := svg.Sanitize(originData)
|
||||||
checkErr(ctx, "svg_processing", svgErr)
|
checkErr(ctx, "svg_processing", svgErr)
|
||||||
|
|
||||||
// Since we'll replace origin data, it's better to close it to return
|
// Since we'll replace origin data, it's better to close it to return
|
||||||
|
@@ -317,13 +317,36 @@ func (s *ProcessingHandlerTestSuite) TestSkipProcessingSVG() {
|
|||||||
require.Equal(s.T(), 200, res.StatusCode)
|
require.Equal(s.T(), 200, res.StatusCode)
|
||||||
|
|
||||||
actual := s.readBody(res)
|
actual := s.readBody(res)
|
||||||
expected, err := svg.Satitize(&imagedata.ImageData{Data: s.readTestFile("test1.svg")})
|
expected, err := svg.Sanitize(&imagedata.ImageData{Data: s.readTestFile("test1.svg")})
|
||||||
|
|
||||||
require.Nil(s.T(), err)
|
require.Nil(s.T(), err)
|
||||||
|
|
||||||
require.True(s.T(), bytes.Equal(expected.Data, actual))
|
require.True(s.T(), bytes.Equal(expected.Data, actual))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ProcessingHandlerTestSuite) TestPreserveOriginSVGHeaders() {
|
||||||
|
rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.svg")
|
||||||
|
res := rw.Result()
|
||||||
|
|
||||||
|
require.Equal(s.T(), 200, res.StatusCode)
|
||||||
|
|
||||||
|
actual := s.readBody(res)
|
||||||
|
originHeaders := map[string]string{
|
||||||
|
"Content-Type": "image/svg+xml",
|
||||||
|
"Cache-Control": "public, max-age=12345",
|
||||||
|
}
|
||||||
|
|
||||||
|
expected, err := svg.Sanitize(&imagedata.ImageData{
|
||||||
|
Data: s.readTestFile("test1.svg"),
|
||||||
|
Headers: originHeaders,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.Nil(s.T(), err)
|
||||||
|
|
||||||
|
require.True(s.T(), bytes.Equal(expected.Data, actual))
|
||||||
|
require.Equal(s.T(), originHeaders, expected.Headers)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ProcessingHandlerTestSuite) TestNotSkipProcessingSVGToJPG() {
|
func (s *ProcessingHandlerTestSuite) TestNotSkipProcessingSVGToJPG() {
|
||||||
rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.svg@jpg")
|
rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.svg@jpg")
|
||||||
res := rw.Result()
|
res := rw.Result()
|
||||||
|
@@ -27,7 +27,7 @@ const feDropShadowTemplate = `
|
|||||||
</feMerge>
|
</feMerge>
|
||||||
`
|
`
|
||||||
|
|
||||||
func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
|
func Sanitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
|
||||||
r := bytes.NewReader(data.Data)
|
r := bytes.NewReader(data.Data)
|
||||||
l := xml.NewLexer(parse.NewInput(r))
|
l := xml.NewLexer(parse.NewInput(r))
|
||||||
|
|
||||||
@@ -62,8 +62,9 @@ func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
newData := imagedata.ImageData{
|
newData := imagedata.ImageData{
|
||||||
Data: buf.Bytes(),
|
Data: buf.Bytes(),
|
||||||
Type: data.Type,
|
Type: data.Type,
|
||||||
|
Headers: data.Headers,
|
||||||
}
|
}
|
||||||
newData.SetCancel(cancel)
|
newData.SetCancel(cancel)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user