mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
fix(pdf): bound extracted attachments by the stream budget
This commit is contained in:
2
go.mod
2
go.mod
@@ -11,7 +11,7 @@ require (
|
||||
github.com/dlclark/regexp2/v2 v2.13.1
|
||||
github.com/gomarkdown/markdown v0.0.0-20261117205700-e8c6d33a8b6c
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gotenberg/litho v0.0.16
|
||||
github.com/gotenberg/litho v0.0.17
|
||||
github.com/hashicorp/go-retryablehttp v0.7.8
|
||||
github.com/labstack/echo/v5 v5.4.6
|
||||
github.com/labstack/gommon v0.5.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -118,8 +118,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
|
||||
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
|
||||
github.com/gotenberg/litho v0.0.16 h1:BSbYecWu3kP69IILHd/NPY5cyka9sbu5UTAHkapvWVs=
|
||||
github.com/gotenberg/litho v0.0.16/go.mod h1:BYtOYpLnI02JzPt4FbeozPgTz1OrCPyyao5kAoEpn4Y=
|
||||
github.com/gotenberg/litho v0.0.17 h1:ns3VFYd8Y0Pc6HTlK2X5uLQfndjVJOSYOuLg2xox1h0=
|
||||
github.com/gotenberg/litho v0.0.17/go.mod h1:BYtOYpLnI02JzPt4FbeozPgTz1OrCPyyao5kAoEpn4Y=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.34.0 h1:vzlTzzi4tHLa/JQdIE5LeXw6SEX5EVVhq/VPqKHnKOk=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.34.0/go.mod h1:FHXQZnW5pA0txiMr3jApv9uNJzZ+7mYZd+u+PPenCFU=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
|
||||
|
||||
@@ -160,6 +160,8 @@ func uniqueName(name string, taken map[string]bool) string {
|
||||
}
|
||||
|
||||
// writeAttachment streams the decoded content of f into a new file at path.
|
||||
// litho bounds the decoded bytes by the stream budget, --pdf-max-stream-bytes:
|
||||
// an attachment past it fails the read, and leaves no file behind.
|
||||
func writeAttachment(ctx context.Context, path string, f models.EmbeddedFile) error {
|
||||
if f.Open == nil {
|
||||
return fmt.Errorf("attachment %q has no content", f.Name)
|
||||
@@ -167,8 +169,7 @@ func writeAttachment(ctx context.Context, path string, f models.EmbeddedFile) er
|
||||
|
||||
in, err := f.Open(ctx)
|
||||
if litho.IsInvalidInput(err) {
|
||||
// The client knows the attachment by its name, not by the step.
|
||||
return &stageInputError{message: fmt.Sprintf("attachment %q: %s", f.Name, err), err: err}
|
||||
return attachmentInputError(f.Name, err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -184,13 +185,25 @@ func writeAttachment(ctx context.Context, path string, f models.EmbeddedFile) er
|
||||
_, err = io.Copy(out, in)
|
||||
closeErr := out.Close()
|
||||
|
||||
if err == nil && closeErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
_ = os.Remove(path)
|
||||
|
||||
if litho.IsInvalidInput(err) {
|
||||
return attachmentInputError(f.Name, err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("copy: %w", err)
|
||||
}
|
||||
|
||||
if closeErr != nil {
|
||||
return fmt.Errorf("close file: %w", closeErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
return fmt.Errorf("close file: %w", closeErr)
|
||||
}
|
||||
|
||||
// attachmentInputError words a failure the attachment's data caused. The
|
||||
// client knows the attachment by its name, not by the step.
|
||||
func attachmentInputError(name string, err error) error {
|
||||
return &stageInputError{message: fmt.Sprintf("attachment %q: %s", name, err), err: err}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ package pdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
litho "github.com/gotenberg/litho/pkg/pdf"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v9/pkg/modules/api"
|
||||
)
|
||||
|
||||
@@ -143,3 +147,79 @@ func TestUniqueName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestExtractAttachmentsStreamBudget pins that an attachment under a single
|
||||
// FlateDecode filter, which litho streams, decodes no further than
|
||||
// --pdf-max-stream-bytes: past it, the stage fails as the client's mistake and
|
||||
// leaves no partial attachment on disk.
|
||||
func TestExtractAttachmentsStreamBudget(t *testing.T) {
|
||||
const budget = 1 << 20
|
||||
|
||||
var z bytes.Buffer
|
||||
w := zlib.NewWriter(&z)
|
||||
if _, err := w.Write(make([]byte, 3*budget)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, _ := testContext(t)
|
||||
path := filepath.Join(ctx.DirPath(), "bomb.pdf")
|
||||
err := os.WriteFile(path, attachmentPDF(z.Bytes()), 0o600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
processor := testProcessor()
|
||||
processor.limits = litho.Limits{MaxStreamBytes: budget}
|
||||
|
||||
_, err = processor.Run(ctx, mustStages(t, `[{"op":"extract-attachments"}]`), []string{path}, nil)
|
||||
|
||||
status, message := api.ParseError(err)
|
||||
want := `Stage 'extract-attachments' failed on 'bomb.pdf': attachment "a.bin": FlateDecode output exceeds 1048576 bytes: decoded stream exceeds the stream size limit`
|
||||
if status != http.StatusBadRequest || message != want {
|
||||
t.Fatalf("got %d %q, want 400 %q", status, message, want)
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(ctx.DirPath())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Errorf("%d files in the request directory, want only bomb.pdf", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
// attachmentPDF returns a one-page PDF embedding a.bin, whose stream is data
|
||||
// under a single FlateDecode filter.
|
||||
func attachmentPDF(data []byte) []byte {
|
||||
var b bytes.Buffer
|
||||
var offsets []int
|
||||
object := func(body string, stream []byte) {
|
||||
offsets = append(offsets, b.Len())
|
||||
fmt.Fprintf(&b, "%d 0 obj\n%s\n", len(offsets), body)
|
||||
if stream != nil {
|
||||
b.WriteString("stream\n")
|
||||
b.Write(stream)
|
||||
b.WriteString("\nendstream\n")
|
||||
}
|
||||
b.WriteString("endobj\n")
|
||||
}
|
||||
|
||||
b.WriteString("%PDF-1.7\n")
|
||||
object("<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles << /Names [(a.bin) 4 0 R] >> >> >>", nil)
|
||||
object("<< /Type /Pages /Kids [3 0 R] /Count 1 >>", nil)
|
||||
object("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] >>", nil)
|
||||
object("<< /Type /Filespec /F (a.bin) /EF << /F 5 0 R >> >>", nil)
|
||||
object(fmt.Sprintf("<< /Type /EmbeddedFile /Filter /FlateDecode /Length %d >>", len(data)), data)
|
||||
|
||||
xref := b.Len()
|
||||
fmt.Fprintf(&b, "xref\n0 %d\n0000000000 65535 f \n", len(offsets)+1)
|
||||
for _, offset := range offsets {
|
||||
fmt.Fprintf(&b, "%010d 00000 n \n", offset)
|
||||
}
|
||||
fmt.Fprintf(&b, "trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n", len(offsets)+1, xref)
|
||||
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user