Cap forge archive download and extracted size
This commit is contained in:
@@ -14,7 +14,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultForge = "https://forgeapi.puppet.com"
|
const (
|
||||||
|
defaultForge = "https://forgeapi.puppet.com"
|
||||||
|
maxForgeDownload = 100 << 20
|
||||||
|
maxForgeExtracted = 512 << 20
|
||||||
|
)
|
||||||
|
|
||||||
var httpClient = &http.Client{Timeout: 5 * time.Minute}
|
var httpClient = &http.Client{Timeout: 5 * time.Minute}
|
||||||
|
|
||||||
@@ -79,24 +83,42 @@ func forgeInstall(ctx context.Context, base, release, dir string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := extract(body, tmp); err != nil {
|
if err := extract(&capReader{r: body, n: maxForgeDownload}, tmp, maxForgeExtracted); err != nil {
|
||||||
_ = os.RemoveAll(tmp)
|
_ = os.RemoveAll(tmp)
|
||||||
return fmt.Errorf("%s: %w", release, err)
|
return fmt.Errorf("%s: %w", release, err)
|
||||||
}
|
}
|
||||||
return os.Rename(tmp, dir)
|
return os.Rename(tmp, dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extract(r io.Reader, dst string) error {
|
// capReader fails once more than n bytes are read, so an oversized stream errors instead of being truncated silently.
|
||||||
|
type capReader struct {
|
||||||
|
r io.Reader
|
||||||
|
n int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *capReader) Read(p []byte) (int, error) {
|
||||||
|
if int64(len(p)) > c.n+1 {
|
||||||
|
p = p[:c.n+1]
|
||||||
|
}
|
||||||
|
n, err := c.r.Read(p)
|
||||||
|
if c.n -= int64(n); c.n < 0 {
|
||||||
|
return 0, fmt.Errorf("archive exceeds size cap")
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func extract(r io.Reader, dst string, limit int64) error {
|
||||||
gz, err := gzip.NewReader(r)
|
gz, err := gzip.NewReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cr := &capReader{r: gz, n: limit}
|
||||||
root, err := os.OpenRoot(dst)
|
root, err := os.OpenRoot(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() { _ = root.Close() }()
|
defer func() { _ = root.Close() }()
|
||||||
tr := tar.NewReader(gz)
|
tr := tar.NewReader(cr)
|
||||||
for {
|
for {
|
||||||
hdr, err := tr.Next()
|
hdr, err := tr.Next()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import (
|
|||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ func TestExtractStripsTopDir(t *testing.T) {
|
|||||||
entry{name: "./puppetlabs-stdlib-9.7.0/lib/x.rb", body: "x", mode: 0o644},
|
entry{name: "./puppetlabs-stdlib-9.7.0/lib/x.rb", body: "x", mode: 0o644},
|
||||||
entry{name: "puppetlabs-stdlib-9.7.0/bin/run", body: "#!", mode: 0o700},
|
entry{name: "puppetlabs-stdlib-9.7.0/bin/run", body: "#!", mode: 0o700},
|
||||||
entry{name: "puppetlabs-stdlib-9.7.0/link", body: "/etc/passwd", typ: tar.TypeSymlink},
|
entry{name: "puppetlabs-stdlib-9.7.0/link", body: "/etc/passwd", typ: tar.TypeSymlink},
|
||||||
)), dst)
|
)), dst, maxForgeExtracted)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -75,7 +77,7 @@ func TestExtractStripsTopDir(t *testing.T) {
|
|||||||
|
|
||||||
func TestExtractRejectsTraversal(t *testing.T) {
|
func TestExtractRejectsTraversal(t *testing.T) {
|
||||||
dst := t.TempDir()
|
dst := t.TempDir()
|
||||||
err := extract(bytes.NewReader(tarball(t, entry{name: "m-1/../../evil", body: "x", mode: 0o644})), dst)
|
err := extract(bytes.NewReader(tarball(t, entry{name: "m-1/../../evil", body: "x", mode: 0o644})), dst, maxForgeExtracted)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected traversal error")
|
t.Fatal("expected traversal error")
|
||||||
}
|
}
|
||||||
@@ -95,3 +97,22 @@ func TestForgeURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractRejectsOversize(t *testing.T) {
|
||||||
|
big := entry{name: "m/a", body: strings.Repeat("x", 4096), mode: 0o644}
|
||||||
|
if err := extract(bytes.NewReader(tarball(t, big)), t.TempDir(), 1024); err == nil {
|
||||||
|
t.Fatal("want extracted size cap error")
|
||||||
|
}
|
||||||
|
if err := extract(bytes.NewReader(tarball(t, big)), t.TempDir(), 1<<20); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCapReaderErrorsAtCap(t *testing.T) {
|
||||||
|
if _, err := io.ReadAll(&capReader{r: strings.NewReader("abcdef"), n: 4}); err == nil {
|
||||||
|
t.Fatal("want cap error")
|
||||||
|
}
|
||||||
|
if b, err := io.ReadAll(&capReader{r: strings.NewReader("abc"), n: 4}); err != nil || string(b) != "abc" {
|
||||||
|
t.Fatalf("under cap: %q %v", b, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user