Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): bound decompressed .ocpkg entry reads against zip bombs
Gabe Kangasd26ab32f9c61
diff --git a/services/plugins/manager.go b/services/plugins/manager.goindex 996c536ce5..437aa29c04 100644--- a/services/plugins/manager.go+++ b/services/plugins/manager.go@@ -653,7 +653,7 @@ func validateUploadedPackage(ctx context.Context, env *HostEnv, packageBytes []bif err != nil {return nil, fmt.Errorf("not a valid .ocpkg: %w", err)}- manifestBytes, err := readZipFile(zr, pkgManifestFilename)+ manifestBytes, err := readZipFile(zr, pkgManifestFilename, maxManifestEntryBytes)if err != nil {return nil, fmt.Errorf("missing manifest: %w", err)}@@ -665,7 +665,7 @@ func validateUploadedPackage(ctx context.Context, env *HostEnv, packageBytes []bif !ok {return nil, fmt.Errorf("missing plugin code (expected one of plugin.js, plugin.py, plugin.wasm)")}- codeBytes, err := readZipFile(zr, codeName)+ codeBytes, err := readZipFile(zr, codeName, maxCodeEntryBytes)if err != nil {return nil, fmt.Errorf("missing plugin code (%s): %w", codeName, err)}@@ -1615,7 +1615,8 @@ func (m *Manager) IconBytes(name string) ([]byte, error) {return nil, fmt.Errorf("open icon entry: %w", err)}defer rc.Close()- return io.ReadAll(rc)+ // Bound the decompressed read so a zip-bomb icon entry can't OOM the host.+ return io.ReadAll(io.LimitReader(rc, maxIconEntryBytes))}return nil, fmt.Errorf("plugin %q icon entry vanished between scan and read", name)case strings.HasSuffix(path, ".wasm"):@@ -1694,7 +1695,8 @@ func (m *Manager) InstructionsBytes(name string) ([]byte, error) {return nil, fmt.Errorf("open instructions entry: %w", err)}defer rc.Close()- return io.ReadAll(rc)+ // Bound the decompressed read so a zip-bomb entry can't OOM the host.+ return io.ReadAll(io.LimitReader(rc, maxInstructionsEntryBytes))}return nil, fmt.Errorf("plugin %q instructions entry vanished between scan and read", name)case strings.HasSuffix(path, ".wasm"):diff --git a/services/plugins/package.go b/services/plugins/package.goindex 0bdd96ff23..13699ee15f 100644--- a/services/plugins/package.go+++ b/services/plugins/package.go@@ -5,11 +5,24 @@ import ("bytes""context""fmt"+ "io""io/fs""path/filepath""strings")+// Decompressed-size caps for the entries the host reads out of an .ocpkg. The+// archive itself is capped (MaxUploadBytes), but a small compressed entry can+// inflate to gigabytes (a zip bomb) and OOM the host, so every read of a+// decompressed entry is bounded. The manifest cap is the tightest because the+// manifest is re-read on every plugins-directory scan.+const (+ maxManifestEntryBytes = 4 << 20 // 4 MiB+ maxCodeEntryBytes = 128 << 20 // 128 MiB (self-contained wasm can be large)+ maxIconEntryBytes = 8 << 20 // 8 MiB+ maxInstructionsEntryBytes = 2 << 20 // 2 MiB+)+// The .ocpkg ("Owncast plugin package") format is a zip archive with four// well-known entries://@@ -85,7 +98,7 @@ func LoadPackage(ctx context.Context, env *HostEnv, path string) (*Loaded, error}}()- manifestBytes, err := readZipFile(&zr.Reader, pkgManifestFilename)+ manifestBytes, err := readZipFile(&zr.Reader, pkgManifestFilename, maxManifestEntryBytes)if err != nil {return nil, fmt.Errorf("%s: %w", filepath.Base(path), err)}@@ -95,7 +108,7 @@ func LoadPackage(ctx context.Context, env *HostEnv, path string) (*Loaded, errorif !ok {return nil, fmt.Errorf("%s: missing plugin code (expected one of plugin.js, plugin.py, plugin.wasm)", filepath.Base(path))}- codeBytes, err := readZipFile(&zr.Reader, codeName)+ codeBytes, err := readZipFile(&zr.Reader, codeName, maxCodeEntryBytes)if err != nil {return nil, fmt.Errorf("%s: %w", filepath.Base(path), err)}@@ -143,14 +156,14 @@ func readManifestFromPackage(path string) (*Manifest, error) {return nil, fmt.Errorf("open %s as zip: %w", path, err)}defer zr.Close()- manifestBytes, err := readZipFile(&zr.Reader, pkgManifestFilename)+ manifestBytes, err := readZipFile(&zr.Reader, pkgManifestFilename, maxManifestEntryBytes)if err != nil {return nil, fmt.Errorf("%s: %w", filepath.Base(path), err)}return ParseManifest(manifestBytes)}-func readZipFile(zr *zip.Reader, name string) ([]byte, error) {+func readZipFile(zr *zip.Reader, name string, maxBytes int64) ([]byte, error) {for _, f := range zr.File {if f.Name == name {rc, err := f.Open()@@ -158,10 +171,16 @@ func readZipFile(zr *zip.Reader, name string) ([]byte, error) {return nil, fmt.Errorf("open %s: %w", name, err)}defer rc.Close()+ // Bound the DECOMPRESSED size: read one byte past the cap so we can+ // tell "exactly at the cap" from "over it" without trusting the+ // zip header's claimed size.buf := &bytes.Buffer{}- if _, err := buf.ReadFrom(rc); err != nil {+ if _, err := buf.ReadFrom(io.LimitReader(rc, maxBytes+1)); err != nil {return nil, fmt.Errorf("read %s: %w", name, err)}+ if int64(buf.Len()) > maxBytes {+ return nil, fmt.Errorf("%s exceeds the %d-byte limit", name, maxBytes)+ }return buf.Bytes(), nil}}diff --git a/services/plugins/package_test.go b/services/plugins/package_test.goindex cb039fc5be..349975b327 100644--- a/services/plugins/package_test.go+++ b/services/plugins/package_test.go@@ -266,11 +266,37 @@ func TestReadZipFile_Missing(t *testing.T) {if err != nil {t.Fatalf("zip reader: %v", err)}- if _, err := readZipFile(zr, "missing.txt"); err == nil {+ if _, err := readZipFile(zr, "present.txt", maxManifestEntryBytes); err == nil {+ _ = err // present entry reads fine+ }+ if _, err := readZipFile(zr, "missing.txt", maxManifestEntryBytes); err == nil {t.Error("expected error for missing entry, got nil")}}+// TestReadZipFile_RejectsOversizedEntry is the C6 (zip-bomb) regression: a+// highly compressible entry that inflates past the cap must be rejected rather+// than read into memory in full.+func TestReadZipFile_RejectsOversizedEntry(t *testing.T) {+ const cap = 1 << 20 // 1 MiB cap for the test+ var buf bytes.Buffer+ zw := zip.NewWriter(&buf)+ w, _ := zw.Create("big.bin")+ // 8 MiB of zeros compresses to a few KB but decompresses past the 1 MiB cap.+ if _, err := w.Write(make([]byte, 8<<20)); err != nil {+ t.Fatalf("write: %v", err)+ }+ zw.Close()++ zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))+ if err != nil {+ t.Fatalf("zip reader: %v", err)+ }+ if _, err := readZipFile(zr, "big.bin", cap); err == nil {+ t.Fatal("expected oversized decompressed entry to be rejected")+ }+}+func TestHasPluginInstructions_Package(t *testing.T) {with := buildPkg(t, map[string][]byte{pkgManifestFilename: validManifestBytes(),