Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): cap plugin storage usage (kv entry size, fs aggregate quota)
Gabe Kangas58e0063ed68f
diff --git a/pluginhost/filesystem_test.go b/pluginhost/filesystem_test.goindex fd031680ab..e812e4d08e 100644--- a/pluginhost/filesystem_test.go+++ b/pluginhost/filesystem_test.go@@ -3,6 +3,7 @@ package pluginhostimport ("os""path/filepath"+ "strconv""strings""testing"@@ -142,3 +143,38 @@ func TestFilesystemHostFns_RejectsOversizedWrite(t *testing.T) {t.Fatalf("expected oversized write to be rejected")}}++// C8 regression: the per-file cap alone lets a plugin fill the disk with many+// files, so a per-plugin aggregate quota must reject a write once the sandbox+// total would exceed it — and overwriting an existing file in place must not+// (it doesn't grow the total).+func TestFilesystemHostFns_EnforcesAggregateQuota(t *testing.T) {+ root := t.TempDir()+ env := &plugins.HostEnv{}+ wireFilesystemHostFnsWithRoot(env, root)++ // A ~40 MiB chunk; under the 50 MiB per-file cap, so each write is allowed+ // individually until the 256 MiB plugin quota is reached.+ chunk := make([]byte, 40<<20)+ writes := 0+ var lastErr error+ for i := 0; i < 12; i++ { // 12 * 40 MiB = 480 MiB, well past the 256 MiB quota+ if err := env.FSWrite("hog", strconv.Itoa(i)+".bin", chunk); err != nil {+ lastErr = err+ break+ }+ writes+++ }+ if lastErr == nil {+ t.Fatal("expected the aggregate quota to reject a write before 480 MiB")+ }+ if writes == 0 || writes > 7 { // 256 MiB / 40 MiB ~= 6 files fit+ t.Fatalf("quota kicked in at the wrong point: %d writes succeeded", writes)+ }++ // Overwriting an existing file in place doesn't grow the total, so it's+ // allowed even at the quota.+ if err := env.FSWrite("hog", "0.bin", chunk); err != nil {+ t.Fatalf("in-place overwrite at quota should be allowed: %v", err)+ }+}diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.goindex 643ef3b092..512a3d75e6 100644--- a/pluginhost/pluginhost.go+++ b/pluginhost/pluginhost.go@@ -1125,10 +1125,42 @@ func wirePluginHostEnv(env *plugins.HostEnv, deps Deps) {const pluginDataRootDirName = "plugin-data"// maxPluginFileBytes caps a single storage.fs write. It bounds how much a-// misbehaving plugin can write in one call; the host's overall disk use is-// still the admin's responsibility.+// misbehaving plugin can write in one call.const maxPluginFileBytes = 50 << 20 // 50 MiB+// maxPluginDataBytes caps a plugin's total storage.fs footprint, so it can't+// fill the host disk with many files that each fit under maxPluginFileBytes.+const maxPluginDataBytes = 256 << 20 // 256 MiB per plugin++// dirSize returns the total size of regular files under dir, or 0 if dir does+// not exist yet.+// ponytail: O(files) walk on every write; if a plugin writes heavily into a+// large tree, switch to a cached running total per sandbox.+func dirSize(dir string) (int64, error) {+ var total int64+ err := filepath.WalkDir(dir, func(_ string, d os.DirEntry, err error) error {+ if err != nil {+ if os.IsNotExist(err) {+ return nil // sandbox not created yet, or an entry vanished mid-walk+ }+ return err+ }+ if d.IsDir() {+ return nil+ }+ info, err := d.Info()+ if err != nil {+ if os.IsNotExist(err) {+ return nil+ }+ return err+ }+ total += info.Size()+ return nil+ })+ return total, err+}+// resolvePluginSandboxPath maps a plugin-supplied relative path to an// absolute path inside that plugin's sandbox (root/<pluginName>), and// guarantees the result cannot escape it. rel is treated as rooted before@@ -1152,6 +1184,25 @@ func resolvePluginSandboxPath(root, pluginName, rel string) (string, error) {return full, nil}+// fsQuotaCheck rejects a write that would push the plugin's sandbox over the+// aggregate quota. full is the absolute path being written; when it already+// exists it's excluded from the current total, since the write overwrites it+// in place rather than growing the footprint.+func fsQuotaCheck(root, pluginName, full string, addBytes int) error {+ used, err := dirSize(filepath.Join(root, pluginName))+ if err != nil {+ return err+ }+ var existing int64+ if info, statErr := os.Stat(full); statErr == nil && !info.IsDir() {+ existing = info.Size()+ }+ if used-existing+int64(addBytes) > maxPluginDataBytes {+ return fmt.Errorf("write would exceed the plugin's %d-byte storage quota", maxPluginDataBytes)+ }+ return nil+}+// wireFilesystemHostFns implements the storage.fs host functions against a// per-plugin sandbox directory under config.DataDirectory/plugin-data.func wireFilesystemHostFns(env *plugins.HostEnv) {@@ -1180,6 +1231,11 @@ func wireFilesystemHostFnsWithRoot(env *plugins.HostEnv, root string) {if err != nil {return err}+ // Enforce the per-plugin aggregate quota: the single-file cap alone+ // still lets a plugin fill the disk with many files.+ if err := fsQuotaCheck(root, pluginName, full, len(data)); err != nil {+ return err+ }if err := os.MkdirAll(filepath.Dir(full), 0o700); err != nil {return err}diff --git a/services/plugins/hostfns.go b/services/plugins/hostfns.goindex 8ba7cf24be..6e40f28ce0 100644--- a/services/plugins/hostfns.go+++ b/services/plugins/hostfns.go@@ -1970,6 +1970,14 @@ func hostKVSet() extism.HostFunction {if err != nil {return}+ if len(key) > MaxKVKeyBytes {+ fmt.Fprintf(os.Stderr, "owncast_kv_set from %s: key is %d bytes, limit is %d — dropped\n", id.slug, len(key), MaxKVKeyBytes)+ return+ }+ if len(val) > MaxKVValueBytes {+ fmt.Fprintf(os.Stderr, "owncast_kv_set from %s: value is %d bytes, limit is %d — dropped\n", id.slug, len(val), MaxKVValueBytes)+ return+ }if err := ns.Set(key, val); err != nil {fmt.Fprintf(os.Stderr, "owncast_kv_set: %v\n", err)}diff --git a/services/plugins/manager.go b/services/plugins/manager.goindex 7c487d8076..d4ea40d4e0 100644--- a/services/plugins/manager.go+++ b/services/plugins/manager.go@@ -100,6 +100,15 @@ const (// chat messages, etc. — which are small in any realistic case.MaxFilterOutputBytes = 1 << 20 // 1 MiB+ // MaxKVKeyBytes / MaxKVValueBytes bound a single storage.kv entry. The+ // backing store is the shared server datastore, so an unbounded value would+ // let one plugin grow the host DB without limit.+ // ponytail: this bounds per-entry size, not the total number of keys — the+ // kv.Namespace interface can't enumerate keys. Add a per-namespace+ // key-count/byte cap (needs backend support) if that's ever abused.+ MaxKVKeyBytes = 1 << 10 // 1 KiB+ MaxKVValueBytes = 256 << 10 // 256 KiB+// MaxHTTPHandlerOutputBytes caps the JSON envelope a plugin returns// from on_http_request (status + headers + body). Sized to leave// headroom over MaxHTTPResponseBodyBytes (server.go); the inner body