The fetcher accepts image/svg+xml; internal/magic detects SVG. orig of an SVG at the orig size is the SVG unchanged, cached, with a Content-Security-Policy allowing only its styles and data: images; at another size, ErrSVGNotOriginalSize (400). Any other format renders it at the least size covering the output, at most 8192 on a side, so it stays sharp. Rendering refuses an SVG with a side over 8192 times the other (ErrSVGTooThin, 415), and the orig size of one above 8192 on a side (ErrSVGTooLargeForOrigSize, 400). The cache reads up to 1024 first bytes to find a file's format. Tests pinning the SVG refusal now expect SVG. Render time and memory are unbounded: P1 in TODO.md until the owner decides on #68. Model: opus-5-5
This commit is contained in:
1 parent
092176d917
commit
d47aea67b8
20 files changed
+945
-97
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -113,10 +114,27 @@ var ErrUnsupportedOutputFormat = errors.New("unsupported output format")
|
||||
// ErrUnsupportedInputFormat is returned, before the input is decoded, when
|
||||
// libvips reads the input's first bytes as another format than its source
|
||||
// format, or as none. An SVG whose first bytes pass the first-bytes check for
|
||||
// AVIF is one example. A source format other than the six pixa reads is also
|
||||
// refused with it.
|
||||
// AVIF is one example. A source format other than the seven pixa reads is
|
||||
// also refused with it.
|
||||
var ErrUnsupportedInputFormat = errors.New("unsupported input format")
|
||||
|
||||
// ErrSVGNotOriginalSize is returned when orig is asked of an SVG source at a
|
||||
// size other than orig. pixa returns an SVG only as it is; a raster format
|
||||
// gives it a size.
|
||||
var ErrSVGNotOriginalSize = errors.New(
|
||||
"an SVG is returned as SVG only at the orig size")
|
||||
|
||||
// ErrSVGTooThin is returned, before rendering, when an SVG source has one
|
||||
// side more than MaxInputDimension times the other. Rendered with its longer
|
||||
// side at most MaxInputDimension, its shorter side would be under 1 pixel.
|
||||
var ErrSVGTooThin = errors.New("SVG source too thin to render")
|
||||
|
||||
// ErrSVGTooLargeForOrigSize is returned, before rendering, when a format other
|
||||
// than orig asks for an SVG source at the orig size and the SVG has a side
|
||||
// above MaxOutputDimension.
|
||||
var ErrSVGTooLargeForOrigSize = errors.New(
|
||||
"SVG source too large to render at its own size")
|
||||
|
||||
// ErrTooManyImages is returned when MaxConcurrentProcessing images are being
|
||||
// processed and none finishes within ProcessingWaitTimeout.
|
||||
var ErrTooManyImages = errors.New("too many images being processed at once")
|
||||
@@ -164,7 +182,9 @@ func New(params Params) *ImageProcessor {
|
||||
// encodes, it returns ctx's error and gives the slot back. govips gives pixa
|
||||
// no way to stop libvips once an encode has begun, so an encode keeps its
|
||||
// slot until it finishes. An AVIF output of more than MaxAVIFOutputPixels is
|
||||
// refused with ErrAVIFOutputTooLarge before anything is decoded.
|
||||
// refused with ErrAVIFOutputTooLarge before anything is decoded. orig of an
|
||||
// SVG source is the SVG as it is, since libvips cannot save SVG; any other
|
||||
// format renders it at the output size.
|
||||
func (p *ImageProcessor) Process(
|
||||
ctx context.Context,
|
||||
input io.Reader,
|
||||
@@ -183,34 +203,22 @@ func (p *ImageProcessor) Process(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Input libvips would not load as sourceFormat is refused before it is
|
||||
// decoded
|
||||
err = checkInputFormat(data, sourceFormat)
|
||||
img, err := loadImage(data, sourceFormat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Decode image
|
||||
img, err := vips.NewImageFromBuffer(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
// Turn the image upright now: encode strips the EXIF orientation tag,
|
||||
// and sizes below must be worked out on the upright image.
|
||||
err = img.AutoRotate()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to auto-rotate: %w", err)
|
||||
origWidth, origHeight := img.Width(), img.Height()
|
||||
outputFormat := resolveOutputFormat(transform.Format, sourceFormat)
|
||||
|
||||
if outputFormat == imagerequest.FormatSVG && sourceFormat == imagerequest.FormatSVG {
|
||||
return svgAsItIs(data, transform.Size, origWidth, origHeight)
|
||||
}
|
||||
|
||||
// Get original dimensions
|
||||
origWidth := img.Width()
|
||||
origHeight := img.Height()
|
||||
|
||||
// Validate input dimensions to prevent DoS via decompression bombs
|
||||
if origWidth > MaxInputDimension || origHeight > MaxInputDimension {
|
||||
return nil, ErrInputTooLarge
|
||||
err = checkSVGOrigSize(sourceFormat, transform.Size, origWidth, origHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Determine target dimensions; too large ones are refused before resizing
|
||||
@@ -220,12 +228,6 @@ func (p *ImageProcessor) Process(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// orig is the source's own format; encode refuses an empty format
|
||||
outputFormat := transform.Format
|
||||
if outputFormat == imagerequest.FormatOriginal {
|
||||
outputFormat = sourceFormat
|
||||
}
|
||||
|
||||
// Determine output dimensions; too large ones are refused before
|
||||
// resizing, which with FitCover already decodes the input
|
||||
outputWidth, outputHeight, err := outputDimensions(origWidth, origHeight,
|
||||
@@ -234,6 +236,19 @@ func (p *ImageProcessor) Process(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// An SVG is rendered again at about the output size, to stay sharp
|
||||
if sourceFormat == imagerequest.FormatSVG {
|
||||
rendered, err := renderSVG(data, origWidth, origHeight,
|
||||
outputWidth, outputHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rendered.Close()
|
||||
|
||||
img = rendered
|
||||
}
|
||||
|
||||
// Resize if needed
|
||||
if targetWidth != origWidth || targetHeight != origHeight {
|
||||
err := p.resize(img, outputWidth, outputHeight, transform.FitMode)
|
||||
@@ -290,6 +305,132 @@ func targetDimensions(
|
||||
return width, height, nil
|
||||
}
|
||||
|
||||
// resolveOutputFormat returns the format of the output for the requested
|
||||
// format: sourceFormat for orig, and format itself for any other. encode
|
||||
// refuses an empty format.
|
||||
func resolveOutputFormat(
|
||||
format, sourceFormat imagerequest.Format,
|
||||
) imagerequest.Format {
|
||||
if format == imagerequest.FormatOriginal {
|
||||
return sourceFormat
|
||||
}
|
||||
|
||||
return format
|
||||
}
|
||||
|
||||
// loadImage refuses data that libvips would not load as sourceFormat, then
|
||||
// decodes it and turns the image upright: encode strips the EXIF orientation
|
||||
// tag, and sizes must be worked out on the upright image. An image with a
|
||||
// side above MaxInputDimension is ErrInputTooLarge, which guards against
|
||||
// decompression bombs. An SVG's size is not checked: only its header has
|
||||
// been read, and the output checks limit the size it is rendered at.
|
||||
func loadImage(
|
||||
data []byte, sourceFormat imagerequest.Format,
|
||||
) (*vips.ImageRef, error) {
|
||||
err := checkInputFormat(data, sourceFormat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
img, err := vips.NewImageFromBuffer(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
err = img.AutoRotate()
|
||||
if err != nil {
|
||||
img.Close()
|
||||
|
||||
return nil, fmt.Errorf("failed to auto-rotate: %w", err)
|
||||
}
|
||||
|
||||
if sourceFormat != imagerequest.FormatSVG &&
|
||||
(img.Width() > MaxInputDimension || img.Height() > MaxInputDimension) {
|
||||
img.Close()
|
||||
|
||||
return nil, ErrInputTooLarge
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// svgAsItIs returns the SVG data unchanged, which is orig of an SVG source
|
||||
// at the orig size. width x height is the SVG's own size. Any other size is
|
||||
// ErrSVGNotOriginalSize.
|
||||
func svgAsItIs(
|
||||
data []byte, size imagerequest.Size, width, height int,
|
||||
) (*Result, error) {
|
||||
if !size.OriginalSize() {
|
||||
return nil, fmt.Errorf("%w: asked for %dx%d", ErrSVGNotOriginalSize,
|
||||
size.Width, size.Height)
|
||||
}
|
||||
|
||||
return &Result{
|
||||
Content: io.NopCloser(bytes.NewReader(data)),
|
||||
ContentLength: int64(len(data)),
|
||||
ContentType: imagerequest.FormatSVG.MIMEType(),
|
||||
Width: width,
|
||||
Height: height,
|
||||
InputWidth: width,
|
||||
InputHeight: height,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// checkSVGOrigSize returns ErrSVGTooLargeForOrigSize when size is the orig
|
||||
// size and the source is an SVG of width x height with a side above
|
||||
// MaxOutputDimension. Process calls it after returning orig of an SVG, so the
|
||||
// SVG would be rendered at its own size.
|
||||
func checkSVGOrigSize(
|
||||
sourceFormat imagerequest.Format, size imagerequest.Size, width, height int,
|
||||
) error {
|
||||
if sourceFormat == imagerequest.FormatSVG && size.OriginalSize() &&
|
||||
max(width, height) > MaxOutputDimension {
|
||||
return fmt.Errorf("%w: %dx%d is above %d", ErrSVGTooLargeForOrigSize,
|
||||
width, height, MaxOutputDimension)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// svgRenderSize returns the least size in the proportions of width x height,
|
||||
// an SVG's own size, that covers outputWidth x outputHeight, so that resize
|
||||
// hardly changes it. Neither side is above MaxInputDimension.
|
||||
func svgRenderSize(width, height, outputWidth, outputHeight int) (int, int) {
|
||||
scale := min(
|
||||
max(float64(outputWidth)/float64(width),
|
||||
float64(outputHeight)/float64(height)),
|
||||
float64(MaxInputDimension)/float64(max(width, height)))
|
||||
|
||||
return int(math.Round(float64(width) * scale)),
|
||||
int(math.Round(float64(height) * scale))
|
||||
}
|
||||
|
||||
// renderSVG renders the SVG data, whose own size is width x height, at
|
||||
// svgRenderSize for outputWidth x outputHeight. An SVG with one side more
|
||||
// than MaxInputDimension times the other is ErrSVGTooThin. libvips'
|
||||
// thumbnail has librsvg render the SVG at that size directly, at any scale:
|
||||
// govips takes a density only in whole dots per inch, which cannot render
|
||||
// an SVG below 1/72 of its own size. Given the SVG as bytes with no file
|
||||
// name, librsvg loads no file or URL the SVG refers to.
|
||||
func renderSVG(
|
||||
data []byte, width, height, outputWidth, outputHeight int,
|
||||
) (*vips.ImageRef, error) {
|
||||
if max(width, height) > MaxInputDimension*min(width, height) {
|
||||
return nil, fmt.Errorf("%w: %dx%d", ErrSVGTooThin, width, height)
|
||||
}
|
||||
|
||||
renderWidth, renderHeight := svgRenderSize(width, height,
|
||||
outputWidth, outputHeight)
|
||||
|
||||
img, err := vips.NewThumbnailWithSizeFromBuffer(data,
|
||||
renderWidth, renderHeight, vips.InterestingNone, vips.SizeBoth)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to render SVG: %w", err)
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// WaitForProcessing waits until no image is being processed, or until ctx
|
||||
// ends, and returns how many images were still being processed then. It
|
||||
// waits by taking each slot in processingSemaphore as it frees up until it
|
||||
@@ -397,6 +538,8 @@ func vipsImageType(format imagerequest.Format) vips.ImageType {
|
||||
return vips.ImageTypeAVIF
|
||||
case imagerequest.FormatJXL:
|
||||
return vips.ImageTypeJXL
|
||||
case imagerequest.FormatSVG:
|
||||
return vips.ImageTypeSVG
|
||||
case imagerequest.FormatOriginal, imagerequest.FormatAuto:
|
||||
return vips.ImageTypeUnknown
|
||||
default:
|
||||
@@ -575,7 +718,9 @@ func (p *ImageProcessor) encode(
|
||||
case imagerequest.FormatJXL:
|
||||
return exportJXL(img, quality)
|
||||
|
||||
case imagerequest.FormatOriginal, imagerequest.FormatAuto:
|
||||
// libvips cannot save SVG; Process returns an SVG source as it is
|
||||
case imagerequest.FormatSVG, imagerequest.FormatOriginal,
|
||||
imagerequest.FormatAuto:
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnsupportedOutputFormat, format)
|
||||
|
||||
default:
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestImageProcessor_LoadsEachSourceFormat(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestImageProcessor_NotASourceFormatRefused verifies that a source format
|
||||
// other than the six, such as orig, auto or none, is refused with
|
||||
// other than the seven, such as orig, auto or none, is refused with
|
||||
// ErrUnsupportedInputFormat. The input is text, which libvips reads as no
|
||||
// format.
|
||||
func TestImageProcessor_NotASourceFormatRefused(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,344 @@
|
||||
package imageprocessor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/davidbyttow/govips/v2/vips"
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
)
|
||||
|
||||
// testSVG is a 16x12 SVG, black on its left half and white on its right.
|
||||
const testSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12">` +
|
||||
`<rect width="16" height="12" fill="white"/>` +
|
||||
`<rect width="8" height="12" fill="black"/></svg>`
|
||||
|
||||
// newSVGTestProcessor returns a processor for the SVG tests.
|
||||
func newSVGTestProcessor() *ImageProcessor {
|
||||
return New(Params{MaxInputBytes: 1 << 20, MaxConcurrentProcessing: 1})
|
||||
}
|
||||
|
||||
// processSVG processes svg, as an SVG source, with transform.
|
||||
func processSVG(
|
||||
t *testing.T, svg string, transform imagerequest.Transform,
|
||||
) (*Result, []byte, error) {
|
||||
t.Helper()
|
||||
|
||||
result, err := newSVGTestProcessor().Process(t.Context(),
|
||||
bytes.NewReader([]byte(svg)), imagerequest.FormatSVG, &transform)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
output, err := io.ReadAll(result.Content)
|
||||
if err != nil {
|
||||
t.Fatalf("reading the output: %v", err)
|
||||
}
|
||||
|
||||
return result, output, nil
|
||||
}
|
||||
|
||||
// TestImageProcessor_SVGOrigIsTheSVG checks that orig of an SVG at the orig
|
||||
// size is the SVG itself, never another format, and that orig at another
|
||||
// size is ErrSVGNotOriginalSize.
|
||||
func TestImageProcessor_SVGOrigIsTheSVG(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, output, err := processSVG(t, testSVG,
|
||||
imagerequest.Transform{Format: imagerequest.FormatOriginal})
|
||||
if err != nil {
|
||||
t.Fatalf("orig of the SVG: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("orig of the SVG: %s, %dx%d", result.ContentType, result.Width,
|
||||
result.Height)
|
||||
|
||||
if result.ContentType != "image/svg+xml" || string(output) != testSVG {
|
||||
t.Errorf("orig of the SVG = %s %q, want image/svg+xml %q",
|
||||
result.ContentType, output, testSVG)
|
||||
}
|
||||
|
||||
_, _, err = processSVG(t, testSVG, imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 32, Height: 24},
|
||||
Format: imagerequest.FormatOriginal,
|
||||
})
|
||||
if !errors.Is(err, ErrSVGNotOriginalSize) {
|
||||
t.Errorf("orig of the SVG at 32x24: error = %v, want %v", err,
|
||||
ErrSVGNotOriginalSize)
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageProcessor_RendersSVGToEachRasterFormat checks that an SVG is
|
||||
// rendered to each raster format at the requested size.
|
||||
func TestImageProcessor_RendersSVGToEachRasterFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, format := range []imagerequest.Format{
|
||||
imagerequest.FormatJPEG, imagerequest.FormatPNG, imagerequest.FormatGIF,
|
||||
imagerequest.FormatWebP, imagerequest.FormatAVIF, imagerequest.FormatJXL,
|
||||
} {
|
||||
result, output, err := processSVG(t, testSVG, imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 40, Height: 30},
|
||||
Format: format,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("SVG as %s: %v", format, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
t.Logf("SVG as %s: %s, %dx%d, %d bytes", format, result.ContentType,
|
||||
result.Width, result.Height, len(output))
|
||||
|
||||
if result.ContentType != format.MIMEType() ||
|
||||
vips.DetermineImageType(output) != vipsImageType(format) ||
|
||||
result.Width != 40 || result.Height != 30 {
|
||||
t.Errorf("SVG as %s = %s, %dx%d; want %s, 40x30", format,
|
||||
result.ContentType, result.Width, result.Height, format.MIMEType())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageProcessor_RendersSVGSharpWhenEnlarged checks that a 16x12 SVG
|
||||
// asked for at 1600x1200 is rendered at that size, not rendered at 16x12 and
|
||||
// enlarged: 10 pixels either side of the edge between its black and white
|
||||
// halves are black and white, where an enlarged image is grey there.
|
||||
func TestImageProcessor_RendersSVGSharpWhenEnlarged(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, output, err := processSVG(t, testSVG, imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 1600, Height: 1200},
|
||||
Format: imagerequest.FormatPNG,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SVG as PNG at 1600x1200: %v", err)
|
||||
}
|
||||
|
||||
img, err := vips.NewImageFromBuffer(output)
|
||||
if err != nil {
|
||||
t.Fatalf("decoding the PNG: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(img.Close)
|
||||
|
||||
for x, want := range map[int]float64{790: 0, 810: 255} {
|
||||
point, err := img.GetPoint(x, 600)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPoint(%d, 600): %v", x, err)
|
||||
}
|
||||
|
||||
t.Logf("pixel at %d,600: %v", x, point)
|
||||
|
||||
if point[0] != want {
|
||||
t.Errorf("pixel at %d,600 = %v, want %v", x, point[0], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageProcessor_SVGFetchesNothing checks that rendering an SVG draws no
|
||||
// image it refers to: one at a URL on a local server, by href or xlink:href,
|
||||
// or a local file that exists. Each SVG renders without error, white where
|
||||
// the image would be. An SVG that uses an external entity at that server is
|
||||
// a separate case. No SVG makes a request to the server.
|
||||
func TestImageProcessor_SVGFetchesNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var requests atomic.Int64
|
||||
|
||||
// Blue is 128 in every pixel of this PNG.
|
||||
png := createTestPNG(t, 4, 4)
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
requests.Add(1)
|
||||
t.Logf("request for %s", r.URL)
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
_, _ = w.Write(png)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
pngPath := filepath.Join(t.TempDir(), "image.png")
|
||||
|
||||
err := os.WriteFile(pngPath, png, 0o600)
|
||||
if err != nil {
|
||||
t.Fatalf("writing the PNG file: %v", err)
|
||||
}
|
||||
|
||||
for _, reference := range []string{
|
||||
`href="` + server.URL + `/image.png"`,
|
||||
`xlink:href="` + server.URL + `/xlink.png"`,
|
||||
`href="file://` + pngPath + `"`,
|
||||
} {
|
||||
svg := `<svg xmlns="http://www.w3.org/2000/svg" ` +
|
||||
`xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="12">` +
|
||||
`<rect width="16" height="12" fill="white"/>` +
|
||||
`<image ` + reference + ` width="16" height="12"/></svg>`
|
||||
|
||||
_, output, err := processSVG(t, svg, imagerequest.Transform{
|
||||
Format: imagerequest.FormatPNG,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("SVG with image %s as PNG: %v", reference, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
img, err := vips.NewImageFromBuffer(output)
|
||||
if err != nil {
|
||||
t.Fatalf("decoding the PNG: %v", err)
|
||||
}
|
||||
|
||||
point, err := img.GetPoint(8, 6)
|
||||
img.Close()
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("GetPoint(8, 6): %v", err)
|
||||
}
|
||||
|
||||
t.Logf("SVG with image %s: pixel at 8,6 = %v", reference, point)
|
||||
|
||||
if point[2] != 255 {
|
||||
t.Errorf("SVG with image %s: blue at 8,6 = %v, want 255: the "+
|
||||
"image is drawn", reference, point[2])
|
||||
}
|
||||
}
|
||||
|
||||
svg := `<?xml version="1.0"?>` +
|
||||
`<!DOCTYPE svg [<!ENTITY outside SYSTEM "` + server.URL + `/entity">]>` +
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="16" height="12">` +
|
||||
`<text y="10">&outside;</text></svg>`
|
||||
|
||||
// librsvg loads no external entity, so it refuses this SVG: outside is
|
||||
// not defined.
|
||||
_, _, err = processSVG(t, svg, imagerequest.Transform{
|
||||
Format: imagerequest.FormatPNG,
|
||||
})
|
||||
t.Logf("SVG with an external entity as PNG: error = %v", err)
|
||||
|
||||
if requests.Load() != 0 {
|
||||
t.Errorf("rendering the SVGs made %d requests, want none",
|
||||
requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageProcessor_RendersSVGLargerThanMaxInputDimension checks that an
|
||||
// SVG whose own size is above MaxInputDimension is limited only by the
|
||||
// output checks: it is rendered at a raster size, orig of it is the SVG
|
||||
// itself, and a raster format at its own size is ErrSVGTooLargeForOrigSize.
|
||||
// The 1000000x1000000 one is rendered at a ten-thousandth of its own size.
|
||||
func TestImageProcessor_RendersSVGLargerThanMaxInputDimension(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, svg := range []string{
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="10000" height="10">` +
|
||||
`<rect width="10000" height="10"/></svg>`,
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="1000000" ` +
|
||||
`height="1000000"><rect width="1000000" height="1000000"/></svg>`,
|
||||
} {
|
||||
result, _, err := processSVG(t, svg, imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 100, Height: 100},
|
||||
Format: imagerequest.FormatPNG,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("%s as PNG at 100x100: %v", svg, err)
|
||||
} else {
|
||||
t.Logf("%s as PNG at 100x100: %dx%d", svg, result.Width, result.Height)
|
||||
|
||||
if result.Width != 100 || result.Height != 100 {
|
||||
t.Errorf("%s as PNG at 100x100 = %dx%d, want 100x100", svg,
|
||||
result.Width, result.Height)
|
||||
}
|
||||
}
|
||||
|
||||
result, output, err := processSVG(t, svg,
|
||||
imagerequest.Transform{Format: imagerequest.FormatOriginal})
|
||||
if err != nil || result.ContentType != imagerequest.FormatSVG.MIMEType() ||
|
||||
string(output) != svg {
|
||||
t.Errorf("orig of %s: error = %v, want the SVG itself", svg, err)
|
||||
}
|
||||
|
||||
_, _, err = processSVG(t, svg,
|
||||
imagerequest.Transform{Format: imagerequest.FormatPNG})
|
||||
if !errors.Is(err, ErrSVGTooLargeForOrigSize) {
|
||||
t.Errorf("%s as PNG at its own size: error = %v, want %v", svg,
|
||||
err, ErrSVGTooLargeForOrigSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageProcessor_RefusesSVGTooThin checks that an SVG with one side more
|
||||
// than MaxInputDimension times the other is ErrSVGTooThin at a raster size,
|
||||
// that one at exactly that proportion is rendered, and that orig of either
|
||||
// is the SVG itself.
|
||||
func TestImageProcessor_RefusesSVGTooThin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tooThin := `<svg xmlns="http://www.w3.org/2000/svg" width="81930" ` +
|
||||
`height="10"><rect width="81930" height="10"/></svg>`
|
||||
atTheLimit := `<svg xmlns="http://www.w3.org/2000/svg" width="81920" ` +
|
||||
`height="10"><rect width="81920" height="10"/></svg>`
|
||||
size := imagerequest.Size{Width: 100, Height: 100}
|
||||
|
||||
_, _, err := processSVG(t, tooThin,
|
||||
imagerequest.Transform{Size: size, Format: imagerequest.FormatPNG})
|
||||
if !errors.Is(err, ErrSVGTooThin) {
|
||||
t.Errorf("81930x10 SVG as PNG at 100x100: error = %v, want %v", err,
|
||||
ErrSVGTooThin)
|
||||
}
|
||||
|
||||
result, _, err := processSVG(t, atTheLimit,
|
||||
imagerequest.Transform{Size: size, Format: imagerequest.FormatPNG})
|
||||
if err != nil {
|
||||
t.Errorf("81920x10 SVG as PNG at 100x100: %v", err)
|
||||
} else if result.Width != 100 || result.Height != 100 {
|
||||
t.Errorf("81920x10 SVG as PNG at 100x100 = %dx%d, want 100x100",
|
||||
result.Width, result.Height)
|
||||
}
|
||||
|
||||
for _, svg := range []string{tooThin, atTheLimit} {
|
||||
result, output, err := processSVG(t, svg,
|
||||
imagerequest.Transform{Format: imagerequest.FormatOriginal})
|
||||
if err != nil || result.ContentType != imagerequest.FormatSVG.MIMEType() ||
|
||||
string(output) != svg {
|
||||
t.Errorf("orig of %s: error = %v, want the SVG itself", svg, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSVGRenderSize checks the size at which an SVG is rendered to cover an
|
||||
// output size, and that it is held to MaxInputDimension.
|
||||
func TestSVGRenderSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
width, height int
|
||||
outputWidth, outputHeight int
|
||||
wantWidth, wantHeight int
|
||||
}{
|
||||
{"own size", 16, 12, 16, 12, 16, 12},
|
||||
{"100 times larger", 16, 12, 1600, 1200, 1600, 1200},
|
||||
{"wider than the SVG's proportions", 16, 12, 160, 12, 160, 120},
|
||||
{"10 times smaller", 100, 100, 10, 10, 10, 10},
|
||||
{"held to MaxInputDimension", 8192, 1, 8192, 8192, 8192, 1},
|
||||
{"held to MaxInputDimension when enlarged", 1024, 1, 8192, 8192, 8192, 8},
|
||||
{"far above MaxInputDimension", 1000000, 1000000, 100, 100, 100, 100},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
gotWidth, gotHeight := svgRenderSize(tt.width, tt.height,
|
||||
tt.outputWidth, tt.outputHeight)
|
||||
if gotWidth != tt.wantWidth || gotHeight != tt.wantHeight {
|
||||
t.Errorf("%s: svgRenderSize(%d, %d, %d, %d) = %dx%d, want %dx%d",
|
||||
tt.name, tt.width, tt.height, tt.outputWidth, tt.outputHeight,
|
||||
gotWidth, gotHeight, tt.wantWidth, tt.wantHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user