Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

feat(plugins): support shared runtimes for Javascript and Python

Gabe Kangasd5a90f132fd8
diff --git a/services/plugins/engine_cache.go b/services/plugins/engine_cache.go
new file mode 100644
index 0000000000..e1e318ab3f
--- /dev/null
+++ b/services/plugins/engine_cache.go
@@ -0,0 +1,82 @@
+package plugins
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ extism "github.com/extism/go-sdk"
+ "github.com/owncast/owncast/services/plugins/engines"
+ "github.com/tetratelabs/wazero"
+)
+
+// compiledEngines holds the per-language interpreter engines, each compiled
+// once (extism.NewCompiledPlugin) from the embedded engine wasm and then
+// instantiated per plugin. This is what collapses per-plugin memory: instead of
+// every plugin compiling its own copy of QuickJS/CPython, one compiled engine
+// is shared across all plugins of that language.
+//
+// It is package-level (not Manager-owned) for the same reason the plugin
+// registry and the compilation cache are: the load path (loadFromBytes,
+// LoadPlugin) runs without a Manager in the test runner and package preflight.
+// In production there is exactly one *HostEnv; the engine's host functions are
+// built once against the env captured on first compile.
+// engineKey identifies a compiled engine. It includes the *HostEnv because the
+// engine's host functions close over it: in production there is exactly one
+// HostEnv (one Manager), so all plugins of a language share one engine and the
+// per-plugin memory win holds. Tests construct a fresh HostEnv per case, so
+// keying by env keeps each case's host calls routed to its own env rather than
+// whichever env happened to compile the engine first.
+type engineKey struct {
+ lang string
+ env *HostEnv
+}
+
+type engineCache struct {
+ mu sync.Mutex
+ byKey map[engineKey]*extism.CompiledPlugin
+}
+
+var compiledEngines = &engineCache{byKey: map[engineKey]*extism.CompiledPlugin{}}
+
+// get returns the compiled engine for a (language, env) pair, compiling and
+// memoizing it on first use. Host functions are built once per engine and
+// shared across every instance it produces; they resolve the calling plugin's
+// identity at call time (see registry.go).
+func (c *engineCache) get(ctx context.Context, env *HostEnv, lang string) (*extism.CompiledPlugin, error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ key := engineKey{lang: lang, env: env}
+ if cp, ok := c.byKey[key]; ok {
+ return cp, nil
+ }
+ wasmBytes, ok := engines.Bytes(lang)
+ if !ok {
+ return nil, fmt.Errorf("no embedded engine for runtime %q", lang)
+ }
+
+ em := extism.Manifest{
+ Wasm: []extism.Wasm{extism.WasmData{Data: wasmBytes, Name: "engine-" + lang}},
+ Timeout: 10_000,
+ Memory: &extism.ManifestMemory{
+ MaxPages: MaxWasmPages,
+ MaxHttpResponseBytes: MaxExtismHTTPResponseBytes,
+ MaxVarBytes: MaxExtismVarBytes,
+ },
+ // AllowedHosts is intentionally omitted: network scope is per plugin,
+ // not per engine, so it's set on each instance (inst.AllowedHosts)
+ // from the plugin's manifest at load time.
+ }
+ // Share the compilation cache and build the full host-function set once.
+ // ModuleConfig is set per-instance (Instance()), not here.
+ pc := extism.PluginConfig{
+ EnableWasi: true,
+ RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(sharedCompilationCache()),
+ }
+ cp, err := extism.NewCompiledPlugin(ctx, em, pc, BuildHostFunctions(env))
+ if err != nil {
+ return nil, fmt.Errorf("compile %s engine: %w", lang, err)
+ }
+ c.byKey[key] = cp
+ return cp, nil
+}
diff --git a/services/plugins/engines/engines.go b/services/plugins/engines/engines.go
new file mode 100644
index 0000000000..6c94a8e0da
--- /dev/null
+++ b/services/plugins/engines/engines.go
@@ -0,0 +1,33 @@
+// Package engines holds the shared language interpreter engines (QuickJS for
+// JS, CPython for Python) compiled to WebAssembly. One engine per language is
+// embedded into the Owncast binary and compiled once at runtime, then
+// instantiated per plugin with the plugin's script injected via Extism config.
+// This replaces the old model where every plugin embedded its own copy of the
+// interpreter (which made memory scale linearly per plugin).
+//
+// The .wasm files are build artifacts produced in the owncast-plugin-sdk repo
+// (engines/build.mjs, engines/build_py.py) from a fixed bootstrap entry, and
+// committed here so Owncast's Go build stays toolchain-free. Rebuild and
+// re-commit them when the SDK runtime or the bootstrap changes.
+package engines
+
+import _ "embed"
+
+//go:embed javascript/engine.wasm
+var jsEngine []byte
+
+//go:embed python/engine.wasm
+var pyEngine []byte
+
+// Bytes returns the embedded engine wasm for a manifest Type ("javascript" or
+// "python"), and whether that language has an engine.
+func Bytes(lang string) ([]byte, bool) {
+ switch lang {
+ case "javascript":
+ return jsEngine, true
+ case "python":
+ return pyEngine, true
+ default:
+ return nil, false
+ }
+}
diff --git a/services/plugins/engines/javascript/engine.wasm b/services/plugins/engines/javascript/engine.wasm
new file mode 100644
index 0000000000..087697ad94
Binary files /dev/null and b/services/plugins/engines/javascript/engine.wasm differ
diff --git a/services/plugins/engines/python/engine.wasm b/services/plugins/engines/python/engine.wasm
new file mode 100644
index 0000000000..cbb108fdf9
Binary files /dev/null and b/services/plugins/engines/python/engine.wasm differ
diff --git a/services/plugins/hostfns.go b/services/plugins/hostfns.go
index 78db16d8ae..ff16c98595 100644
--- a/services/plugins/hostfns.go
+++ b/services/plugins/hostfns.go
@@ -346,133 +346,97 @@ type HostEnv struct {
Timer *TimerHub
}
-// BuildHostFunctions returns the list of extism host functions a single
-// plugin should be granted, based on its declared permissions. A plugin
-// only sees imports for permissions it declared; importing anything else
-// will fail to link at instantiation time.
-func BuildHostFunctions(env *HostEnv, manifest *Manifest, assetsFS fs.FS) []extism.HostFunction {
- var fns []extism.HostFunction
- granted := stringSet(manifest.Permissions)
-
- if granted[PermStorageKV] {
- ns := env.KV.Namespace(manifest.Slug)
- fns = append(fns, hostKVGet(ns), hostKVSet(ns))
- }
- if granted[PermChatSend] {
- // Chat send fns capture both slug and chat display name in
- // their closure: slug routes to the right bot user, display
- // name is what chat viewers see.
- chatDisplay := manifest.ChatDisplayName()
- fns = append(fns,
- hostSendChat(env.OnChat, manifest.Slug, chatDisplay),
- hostSendChatAction(env.OnChat, manifest.Slug, chatDisplay),
- hostSendChatSystem(env.OnChat, manifest.Slug, chatDisplay),
- hostSendChatTo(env, manifest.Slug),
- )
- }
- if granted[PermEmitEvent] {
- fns = append(fns, hostEmitEvent(env, manifest.Slug))
- }
- if granted[PermServerRead] {
- fns = append(fns,
- hostStreamCurrent(env),
- hostServerInfo(env),
- hostServerSocials(env),
- hostServerEmotes(env),
- hostServerFederation(env),
- hostStreamBroadcaster(env),
- hostServerTags(env),
- )
- }
- if granted[PermChatHistory] {
- fns = append(fns, hostChatHistory(env))
- }
- if granted[PermChatModerate] {
- fns = append(fns,
- hostDeleteMessage(env, manifest.Slug),
- hostKickClient(env, manifest.Slug),
- )
- }
- if granted[PermNotificationsSend] {
- fns = append(fns,
- hostSendDiscord(env, manifest.Slug),
- hostSendBrowserPush(env, manifest.Slug),
- hostSendFediverse(env, manifest.Slug),
- )
- }
- if granted[PermChatHistory] {
- fns = append(fns, hostChatClients(env))
- }
- if granted[PermUsersRead] {
- fns = append(fns, hostUsersList(env), hostUserGet(env))
- }
- if granted[PermUsersModerate] {
- fns = append(fns,
- hostUserSetEnabled(env, manifest.Slug),
- hostBanIP(env, manifest.Slug),
- )
- }
- fns = append(fns, storageHostFunctions(env, manifest, granted)...)
- if granted[PermFediversePost] {
- fns = append(fns, hostFediversePost(env, manifest.Slug))
- }
- if granted[PermHttpSSE] {
- fns = append(fns, hostSSESend(env, manifest.Slug))
- }
+// BuildHostFunctions returns the full set of extism host functions, built
+// once and shared across all plugins. Every function is wired
+// unconditionally; the calling plugin's identity and permission grants are
+// resolved at call time via resolveCaller, which enforces the per-function
+// permission (and logs denials) before the body runs.
+func BuildHostFunctions(env *HostEnv) []extism.HostFunction {
+ // Capacity hint for the full host-function set built below; an off-by-a-few
+ // estimate just trades one reallocation, so it needn't track exactly.
+ fns := make([]extism.HostFunction, 0, 48)
+
+ fns = append(fns, hostKVGet(), hostKVSet())
+ // Chat send fns resolve both slug and chat display name at call time:
+ // slug routes to the right bot user, display name is what chat viewers
+ // see.
+ fns = append(fns,
+ hostSendChat(env),
+ hostSendChatAction(env),
+ hostSendChatSystem(env),
+ hostSendChatTo(env),
+ )
+ fns = append(fns, hostEmitEvent(env))
+ fns = append(fns,
+ hostStreamCurrent(env),
+ hostServerInfo(env),
+ hostServerSocials(env),
+ hostServerEmotes(env),
+ hostServerFederation(env),
+ hostStreamBroadcaster(env),
+ hostServerTags(env),
+ )
+ fns = append(fns, hostChatHistory(env))
+ fns = append(fns,
+ hostDeleteMessage(env),
+ hostKickClient(env),
+ )
+ fns = append(fns,
+ hostSendDiscord(env),
+ hostSendBrowserPush(env),
+ hostSendFediverse(env),
+ )
+ fns = append(fns, hostChatClients(env))
+ fns = append(fns, hostUsersList(env), hostUserGet(env))
+ fns = append(fns,
+ hostUserSetEnabled(env),
+ hostBanIP(env),
+ )
+ fns = append(fns, storageHostFunctions(env)...)
+ fns = append(fns, hostFediversePost(env))
+ fns = append(fns, hostSSESend(env))
// Timers are ambient (no permission): a plugin can't setTimeout in the
// sandbox, so scheduling is a baseline capability. The act of scheduling
// is benign — whatever the callback does still needs its own permissions —
// and TimerHub's per-plugin caps bound abuse.
- fns = append(fns, hostTimerSet(env, manifest.Slug), hostTimerClear(env, manifest.Slug))
+ fns = append(fns, hostTimerSet(env), hostTimerClear(env))
// Config is ambient too: reading the plugin's own manifest-declared config
// (admin override, else declared default) is benign and needs no grant.
- fns = append(fns, hostConfigGet(env, manifest))
+ fns = append(fns, hostConfigGet(env))
// Asset reading is ambient: a plugin reads only files it shipped itself.
- fns = append(fns, hostAssetRead(assetsFS))
- if granted[PermUIModify] {
- fns = append(fns,
- hostAddActions(env, manifest),
- hostClearActions(env, manifest.Slug),
- )
- }
- fns = append(fns, videoConfigHostFunctions(env, manifest, granted)...)
+ fns = append(fns, hostAssetRead())
+ fns = append(fns,
+ hostAddActions(env),
+ hostClearActions(env),
+ )
+ fns = append(fns, videoConfigHostFunctions(env)...)
return fns
}
// storageHostFunctions returns the storage.upload / storage.fs host functions
// a plugin is granted. Split out of BuildHostFunctions to keep that function's
// cyclomatic complexity in check.
-func storageHostFunctions(env *HostEnv, manifest *Manifest, granted map[string]bool) []extism.HostFunction {
- var fns []extism.HostFunction
- if granted[PermStorageUpload] {
- fns = append(fns, hostStorageUpload(env, manifest.Slug))
+func storageHostFunctions(env *HostEnv) []extism.HostFunction {
+ return []extism.HostFunction{
+ hostStorageUpload(env),
+ hostFSRead(env),
+ hostFSWrite(env),
+ hostFSList(env),
+ hostFSDelete(env),
+ hostFSExists(env),
}
- if granted[PermStorageFS] {
- fns = append(fns,
- hostFSRead(env, manifest.Slug),
- hostFSWrite(env, manifest.Slug),
- hostFSList(env, manifest.Slug),
- hostFSDelete(env, manifest.Slug),
- hostFSExists(env, manifest.Slug),
- )
- }
- return fns
}
// videoConfigHostFunctions returns the videoconfig.read / videoconfig.write
// host functions a plugin is granted. Split out of BuildHostFunctions to keep
// that function's cyclomatic complexity in check.
-func videoConfigHostFunctions(env *HostEnv, manifest *Manifest, granted map[string]bool) []extism.HostFunction {
- var fns []extism.HostFunction
- if granted[PermVideoConfigRead] {
- fns = append(fns, hostVideoConfigRead(env))
+func videoConfigHostFunctions(env *HostEnv) []extism.HostFunction {
+ return []extism.HostFunction{
+ hostVideoConfigRead(env),
+ hostVideoConfigWrite(env),
}
- if granted[PermVideoConfigWrite] {
- fns = append(fns, hostVideoConfigWrite(env, manifest.Slug))
- }
- return fns
}
// hostSSESend backs owncast.sse.send(channel, event, data). It publishes a
@@ -480,10 +444,15 @@ func videoConfigHostFunctions(env *HostEnv, manifest *Manifest, granted map[stri
// plugin's <channel> stream. Fire-and-forget: the call returns as soon as
// the frame is queued to each client, so it never blocks the plugin on a
// slow browser. Requires the http.sse permission.
-func hostSSESend(env *HostEnv, pluginName string) extism.HostFunction {
+func hostSSESend(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_sse_send",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_sse_send", PermHttpSSE)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
channel, err := p.ReadString(stack[0])
if err != nil {
return
@@ -517,10 +486,16 @@ func hostSSESend(env *HostEnv, pluginName string) extism.HostFunction {
// timer.fire event carrying the id. Returns 1 on success, 0 if the plugin is
// at its pending-timer cap. A nil Timer (test harness) reports success so the
// guest keeps its callback; fires are then simulated via events.
-func hostTimerSet(env *HostEnv, pluginName string) extism.HostFunction {
+func hostTimerSet(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_timer_set",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ ident, ok := resolveCaller(ctx, "owncast_timer_set", "")
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := ident.slug
id := stack[0]
// Clamp the requested delay to a sane ceiling before narrowing to
// int64 — bounds the duration math and keeps the conversion in range.
@@ -548,10 +523,15 @@ func hostTimerSet(env *HostEnv, pluginName string) extism.HostFunction {
}
// hostTimerClear backs owncast.timer.clear(id), cancelling a pending timer.
-func hostTimerClear(env *HostEnv, pluginName string) extism.HostFunction {
+func hostTimerClear(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_timer_clear",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ ident, ok := resolveCaller(ctx, "owncast_timer_clear", "")
+ if !ok {
+ return
+ }
+ pluginName := ident.slug
id := stack[0]
if env.Timer != nil {
env.Timer.Clear(pluginName, id)
@@ -564,10 +544,16 @@ func hostTimerClear(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostFediversePost(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFediversePost(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fediverse_post",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fediverse_post", PermFediversePost)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
text, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -606,6 +592,10 @@ func hostChatClients(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_chat_clients",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_chat_clients", PermChatHistory); !ok {
+ stack[0] = 0
+ return
+ }
var clients []HostChatClient
if env.ChatClients != nil {
clients = env.ChatClients()
@@ -636,6 +626,10 @@ func hostUsersList(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_users_list",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_users_list", PermUsersRead); !ok {
+ stack[0] = 0
+ return
+ }
var users []HostUser
if env.Users != nil {
users = env.Users()
@@ -666,6 +660,10 @@ func hostUserGet(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_user_get",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_user_get", PermUsersRead); !ok {
+ stack[0] = 0
+ return
+ }
id, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -699,10 +697,15 @@ func hostUserGet(env *HostEnv) extism.HostFunction {
return fn
}
-func hostUserSetEnabled(env *HostEnv, pluginName string) extism.HostFunction {
+func hostUserSetEnabled(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_user_set_enabled",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ ident, ok := resolveCaller(ctx, "owncast_user_set_enabled", PermUsersModerate)
+ if !ok {
+ return
+ }
+ pluginName := ident.slug
id, err := p.ReadString(stack[0])
if err != nil {
return
@@ -720,10 +723,15 @@ func hostUserSetEnabled(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostBanIP(env *HostEnv, pluginName string) extism.HostFunction {
+func hostBanIP(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_ban_ip",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_ban_ip", PermUsersModerate)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
ip, err := p.ReadString(stack[0])
if err != nil {
return
@@ -743,6 +751,10 @@ func hostServerSocials(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_server_socials",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_server_socials", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var socials []SocialHandle
if env.Socials != nil {
socials = env.Socials()
@@ -773,10 +785,16 @@ func hostServerSocials(env *HostEnv) extism.HostFunction {
// as JSON: the admin-set override when present, otherwise the manifest's
// declared default. Returns 0 (→ undefined in the guest) for an unknown key or
// a declared key with no override and no default.
-func hostConfigGet(env *HostEnv, manifest *Manifest) extism.HostFunction {
+func hostConfigGet(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_config_get",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_config_get", "")
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ manifest := id.manifest
key, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -820,10 +838,16 @@ func hostConfigGet(env *HostEnv, manifest *Manifest) extism.HostFunction {
// plugin's bundled assets/ directory. The path must be relative — no ".."
// segments, no leading "/". Returns 0 when assetsFS is nil or the file
// doesn't exist. Ambient — no permission required.
-func hostAssetRead(assetsFS fs.FS) extism.HostFunction {
+func hostAssetRead() extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_asset_read",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_asset_read", "")
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ assetsFS := id.assetsFS
path, err := p.ReadString(stack[0])
if err != nil || assetsFS == nil {
stack[0] = 0
@@ -856,6 +880,10 @@ func hostServerEmotes(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_server_emotes",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_server_emotes", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var emotes []Emote
if env.Emotes != nil {
emotes = env.Emotes()
@@ -886,6 +914,10 @@ func hostServerFederation(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_server_federation",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_server_federation", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var info FederationInfo
if env.Federation != nil {
info = env.Federation()
@@ -909,10 +941,15 @@ func hostServerFederation(env *HostEnv) extism.HostFunction {
return fn
}
-func hostSendFediverse(env *HostEnv, pluginName string) extism.HostFunction {
+func hostSendFediverse(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_notify_fediverse",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_notify_fediverse", PermNotificationsSend)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
payloadBytes, err := p.ReadBytes(stack[0])
if err != nil {
return
@@ -933,10 +970,15 @@ func hostSendFediverse(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostSendChatTo(env *HostEnv, pluginName string) extism.HostFunction {
+func hostSendChatTo(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_send_chat_to",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_send_chat_to", PermChatSend)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
clientID := stack[0]
text, err := p.ReadString(stack[1])
if err != nil {
@@ -953,10 +995,16 @@ func hostSendChatTo(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostStorageUpload(env *HostEnv, pluginName string) extism.HostFunction {
+func hostStorageUpload(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_storage_upload",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_storage_upload", PermStorageUpload)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
name, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -999,10 +1047,16 @@ func hostStorageUpload(env *HostEnv, pluginName string) extism.HostFunction {
// hostFSRead backs owncast.fs.read(path). Returns the file's raw bytes,
// or 0 (null to the plugin) when the path is missing, escapes the
// sandbox, or can't be read. Requires the storage.fs permission.
-func hostFSRead(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFSRead(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fs_read",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fs_read", PermStorageFS)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
path, err := p.ReadString(stack[0])
if err != nil || env.FSRead == nil {
stack[0] = 0
@@ -1032,10 +1086,16 @@ func hostFSRead(env *HostEnv, pluginName string) extism.HostFunction {
// directories as needed and writes the bytes, returning a JSON
// {ok, error?} result so the plugin can react to a rejected write
// (sandbox escape, oversized payload, disk error). Requires storage.fs.
-func hostFSWrite(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFSWrite(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fs_write",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fs_write", PermStorageFS)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
path, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -1075,10 +1135,16 @@ func hostFSWrite(env *HostEnv, pluginName string) extism.HostFunction {
// hostFSList backs owncast.fs.list(dir). Returns a JSON array of the
// entry names (files and subdirectories) directly inside dir. A missing
// directory lists as empty rather than erroring. Requires storage.fs.
-func hostFSList(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFSList(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fs_list",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fs_list", PermStorageFS)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
dir, err := p.ReadString(stack[0])
if err != nil || env.FSList == nil {
stack[0] = 0
@@ -1115,10 +1181,16 @@ func hostFSList(env *HostEnv, pluginName string) extism.HostFunction {
// hostFSDelete backs owncast.fs.delete(path). Removes a single file or
// empty directory, returning a JSON {ok, error?} result. Requires
// storage.fs.
-func hostFSDelete(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFSDelete(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fs_delete",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fs_delete", PermStorageFS)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
path, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -1153,10 +1225,16 @@ func hostFSDelete(env *HostEnv, pluginName string) extism.HostFunction {
// hostFSExists backs owncast.fs.exists(path). Returns 1 if the path
// exists inside the sandbox, 0 otherwise (including on a sandbox-escape
// attempt or stat error). Requires storage.fs.
-func hostFSExists(env *HostEnv, pluginName string) extism.HostFunction {
+func hostFSExists(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_fs_exists",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_fs_exists", PermStorageFS)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
path, err := p.ReadString(stack[0])
if err != nil || env.FSExists == nil {
stack[0] = 0
@@ -1181,10 +1259,15 @@ func hostFSExists(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostDeleteMessage(env *HostEnv, pluginName string) extism.HostFunction {
+func hostDeleteMessage(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_delete_message",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ ident, ok := resolveCaller(ctx, "owncast_delete_message", PermChatModerate)
+ if !ok {
+ return
+ }
+ pluginName := ident.slug
id, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1200,10 +1283,15 @@ func hostDeleteMessage(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostKickClient(env *HostEnv, pluginName string) extism.HostFunction {
+func hostKickClient(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_kick_client",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_kick_client", PermChatModerate)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
clientID := stack[0]
if env.KickClient != nil {
env.KickClient(pluginName, clientID)
@@ -1216,10 +1304,15 @@ func hostKickClient(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostSendDiscord(env *HostEnv, pluginName string) extism.HostFunction {
+func hostSendDiscord(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_notify_discord",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_notify_discord", PermNotificationsSend)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
text, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1235,10 +1328,15 @@ func hostSendDiscord(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostSendBrowserPush(env *HostEnv, pluginName string) extism.HostFunction {
+func hostSendBrowserPush(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_notify_browser_push",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_notify_browser_push", PermNotificationsSend)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
payloadBytes, err := p.ReadBytes(stack[0])
if err != nil {
return
@@ -1264,6 +1362,10 @@ func hostChatHistory(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_chat_history",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_chat_history", PermChatHistory); !ok {
+ stack[0] = 0
+ return
+ }
limit := int(int32(stack[0])) //nolint:gosec // G115: truncation is intentional; non-positive results fall back to defaultLimit
if limit <= 0 {
limit = defaultLimit
@@ -1294,10 +1396,15 @@ func hostChatHistory(env *HostEnv) extism.HostFunction {
return fn
}
-func hostEmitEvent(env *HostEnv, pluginName string) extism.HostFunction {
+func hostEmitEvent(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_emit_event",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_emit_event", PermEmitEvent)
+ if !ok {
+ return
+ }
+ pluginName := id.slug
eventType, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1325,10 +1432,17 @@ func hostEmitEvent(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostSendChat(sink func(ChatSendRequest), pluginSlug, botDisplayName string) extism.HostFunction {
+func hostSendChat(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_send_chat",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_send_chat", PermChatSend)
+ if !ok {
+ return
+ }
+ sink := env.OnChat
+ pluginSlug := id.slug
+ botDisplayName := id.chatDisplay
text, err := p.ReadString(stack[0])
if err != nil {
fmt.Fprintf(os.Stderr, "owncast_send_chat: read string: %v\n", err)
@@ -1350,10 +1464,17 @@ func hostSendChat(sink func(ChatSendRequest), pluginSlug, botDisplayName string)
return fn
}
-func hostSendChatSystem(sink func(ChatSendRequest), pluginSlug, botDisplayName string) extism.HostFunction {
+func hostSendChatSystem(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_send_chat_system",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_send_chat_system", PermChatSend)
+ if !ok {
+ return
+ }
+ sink := env.OnChat
+ pluginSlug := id.slug
+ botDisplayName := id.chatDisplay
body, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1374,10 +1495,17 @@ func hostSendChatSystem(sink func(ChatSendRequest), pluginSlug, botDisplayName s
return fn
}
-func hostSendChatAction(sink func(ChatSendRequest), pluginSlug, botDisplayName string) extism.HostFunction {
+func hostSendChatAction(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_send_chat_action",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_send_chat_action", PermChatSend)
+ if !ok {
+ return
+ }
+ sink := env.OnChat
+ pluginSlug := id.slug
+ botDisplayName := id.chatDisplay
text, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1402,6 +1530,10 @@ func hostStreamCurrent(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_stream_current",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_stream_current", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var info StreamInfo
if env.StreamCurrent != nil {
info = env.StreamCurrent()
@@ -1429,6 +1561,10 @@ func hostServerInfo(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_server_info",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_server_info", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var info ServerInfo
if env.ServerInfo != nil {
info = env.ServerInfo()
@@ -1456,6 +1592,10 @@ func hostStreamBroadcaster(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_stream_broadcaster",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_stream_broadcaster", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var info StreamBroadcaster
if env.Broadcaster != nil {
info = env.Broadcaster()
@@ -1483,6 +1623,10 @@ func hostServerTags(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_server_tags",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_server_tags", PermServerRead); !ok {
+ stack[0] = 0
+ return
+ }
var tags []string
if env.Tags != nil {
tags = env.Tags()
@@ -1513,6 +1657,10 @@ func hostVideoConfigRead(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_video_config_read",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ if _, ok := resolveCaller(ctx, "owncast_video_config_read", PermVideoConfigRead); !ok {
+ stack[0] = 0
+ return
+ }
var cfg VideoConfig
if env.VideoConfig != nil {
cfg = env.VideoConfig()
@@ -1543,10 +1691,16 @@ func hostVideoConfigRead(env *HostEnv) extism.HostFunction {
// partial video/transcoding configuration change via the host. Returns a
// JSON {ok, error?} result so the plugin can react to a rejected config.
// Requires the videoconfig.write permission.
-func hostVideoConfigWrite(env *HostEnv, pluginName string) extism.HostFunction {
+func hostVideoConfigWrite(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_video_config_write",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_video_config_write", PermVideoConfigWrite)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ pluginName := id.slug
payloadBytes, err := p.ReadBytes(stack[0])
if err != nil {
stack[0] = 0
@@ -1583,10 +1737,16 @@ func hostVideoConfigWrite(env *HostEnv, pluginName string) extism.HostFunction {
return fn
}
-func hostKVGet(ns kv.Namespace) extism.HostFunction {
+func hostKVGet() extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_kv_get",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_kv_get", PermStorageKV)
+ if !ok {
+ stack[0] = 0
+ return
+ }
+ ns := id.kvNamespace
key, err := p.ReadString(stack[0])
if err != nil {
stack[0] = 0
@@ -1611,10 +1771,15 @@ func hostKVGet(ns kv.Namespace) extism.HostFunction {
return fn
}
-func hostKVSet(ns kv.Namespace) extism.HostFunction {
+func hostKVSet() extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_kv_set",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_kv_set", PermStorageKV)
+ if !ok {
+ return
+ }
+ ns := id.kvNamespace
key, err := p.ReadString(stack[0])
if err != nil {
return
@@ -1649,18 +1814,23 @@ const RuntimeActionsConfigKey = "owncast.actions"
//
// Requires the ui.modify permission; invalid input is logged but not
// surfaced back to the plugin.
-func hostAddActions(env *HostEnv, manifest *Manifest) extism.HostFunction {
- pluginSlug := manifest.Slug
- hasHTTPServe := false
- for _, perm := range manifest.Permissions {
- if perm == PermHttpServe {
- hasHTTPServe = true
- break
- }
- }
+func hostAddActions(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_add_actions",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_add_actions", PermUIModify)
+ if !ok {
+ return
+ }
+ manifest := id.manifest
+ pluginSlug := manifest.Slug
+ hasHTTPServe := false
+ for _, perm := range manifest.Permissions {
+ if perm == PermHttpServe {
+ hasHTTPServe = true
+ break
+ }
+ }
payloadBytes, err := p.ReadBytes(stack[0])
if err != nil {
return
@@ -1705,10 +1875,15 @@ func hostAddActions(env *HostEnv, manifest *Manifest) extism.HostFunction {
// hostClearActions backs owncast.actions.clear(). Removes the runtime
// list from the plugin's config so only manifest.actions remain in the
// effective set returned by /api/config.
-func hostClearActions(env *HostEnv, pluginSlug string) extism.HostFunction {
+func hostClearActions(env *HostEnv) extism.HostFunction {
fn := extism.NewHostFunctionWithStack(
"owncast_clear_actions",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
+ id, ok := resolveCaller(ctx, "owncast_clear_actions", PermUIModify)
+ if !ok {
+ return
+ }
+ pluginSlug := id.slug
if env.KV == nil {
return
}
diff --git a/services/plugins/manager.go b/services/plugins/manager.go
index 33c74401e4..583e250b87 100644
--- a/services/plugins/manager.go
+++ b/services/plugins/manager.go
@@ -117,6 +117,30 @@ const (
HTTPHandlerTimeout = 5 * time.Second
)
+// wasmCompilationCache is shared across every plugin instance so wazero
+// compiles a given wasm module to native code at most once for the lifetime
+// of the process. The cache is keyed on module content, so it dedupes the
+// repeated work of reloading the same plugin (dev churn, reload-on-change,
+// or running multiple instances of one plugin). It is safe for concurrent
+// use across runtimes.
+//
+// NOTE: this does NOT dedupe the interpreter engine across *different*
+// plugins — each plugin's wasm bundles the QuickJS/CPython engine together
+// with the author's code, so every plugin's bytes differ and hash to a
+// distinct cache key. Sharing the engine across distinct plugins requires
+// splitting it out of the per-plugin module; tracked separately.
+var (
+ wasmCompilationCache wazero.CompilationCache
+ wasmCompilationCacheOnce sync.Once
+)
+
+func sharedCompilationCache() wazero.CompilationCache {
+ wasmCompilationCacheOnce.Do(func() {
+ wasmCompilationCache = wazero.NewCompilationCache()
+ })
+ return wasmCompilationCache
+}
+
// IsDisabled reports whether the plugin has been auto-disabled by the
// strike system. Disabled plugins are skipped by both the filter chain
// and the notification dispatcher.
@@ -207,9 +231,18 @@ func (p *Loaded) callContentExport(ctx context.Context, export, slug string, use
// (the .ocpkg zip reader for packaged plugins). Safe to call multiple times.
func (l *Loaded) Close(ctx context.Context) {
if l.plugin != nil {
+ // Closes this instance only. For shared-engine plugins the underlying
+ // CompiledPlugin (the engine) is process-global and is never closed
+ // here; for legacy wasm plugins this closes their own runtime.
_ = l.plugin.Close(ctx)
l.plugin = nil
}
+ // Drop the call-time identity so any in-flight or stale host call from this
+ // plugin resolves to "not found" and fails cleanly rather than acting on a
+ // torn-down plugin.
+ if l.Manifest != nil {
+ globalPluginRegistry.remove(l.Manifest.Slug)
+ }
if l.pkgCloser != nil {
_ = l.pkgCloser.Close()
l.pkgCloser = nil
@@ -512,14 +545,18 @@ func validateUploadedPackage(ctx context.Context, env *HostEnv, packageBytes []b
if err != nil {
return nil, fmt.Errorf("missing manifest: %w", err)
}
- wasmBytes, err := readZipFile(zr, pkgWasmFilename)
- if err != nil {
- return nil, fmt.Errorf("missing compiled plugin: %w", err)
- }
manifest, err := ParseManifest(manifestBytes)
if err != nil {
return nil, fmt.Errorf("invalid manifest: %w", err)
}
+ codeName, runtimeType, ok := detectPackageCode(zr)
+ if !ok {
+ return nil, fmt.Errorf("missing plugin code (expected one of plugin.js, plugin.py, plugin.wasm)")
+ }
+ codeBytes, err := readZipFile(zr, codeName)
+ if err != nil {
+ return nil, fmt.Errorf("missing plugin code (%s): %w", codeName, err)
+ }
// Preflight the package through the real load path before we write it into
// the plugins directory. Without this, an .ocpkg whose manifest parses but
// whose wasm fails register() or disagrees with the manifest appears to
@@ -534,7 +571,7 @@ func validateUploadedPackage(ctx context.Context, env *HostEnv, packageBytes []b
assetsFS = sub
}
}
- loaded, err := loadFromBytes(ctx, env, manifestBytes, wasmBytes, manifest.Slug, assetsFS)
+ loaded, err := loadFromBytes(ctx, env, manifestBytes, codeBytes, runtimeType, manifest.Slug, assetsFS)
if err != nil {
return nil, err
}
@@ -1140,69 +1177,75 @@ func pendingPermissions(manifestPerms, approved []string) []string {
// Assets are discovered from an `assets/` sibling directory when present and
// wired into the returned Loaded so the same asset-backed host APIs work for
// both loose-file and packaged plugins.
-func LoadPlugin(ctx context.Context, env *HostEnv, wasmPath, manifestPath string) (*Loaded, error) {
+func LoadPlugin(ctx context.Context, env *HostEnv, artifactPath, manifestPath string) (*Loaded, error) {
manifestBytes, err := os.ReadFile(manifestPath) //nolint:gosec // G304: plugin paths are admin-controlled, not user input
if err != nil {
return nil, fmt.Errorf("read manifest %s: %w", manifestPath, err)
}
- wasmBytes, err := os.ReadFile(wasmPath) //nolint:gosec // G304: plugin paths are admin-controlled, not user input
+ artifactBytes, err := os.ReadFile(artifactPath) //nolint:gosec // G304: plugin paths are admin-controlled, not user input
if err != nil {
- return nil, fmt.Errorf("read wasm %s: %w", wasmPath, err)
+ return nil, fmt.Errorf("read plugin code %s: %w", artifactPath, err)
}
- displayName := strings.TrimSuffix(filepath.Base(wasmPath), ".wasm")
+ // Infer the runtime from the artifact's extension (.js / .py / .wasm); the
+ // author doesn't declare it in the manifest.
+ runtimeType := runtimeForExt(artifactPath)
+ base := filepath.Base(artifactPath)
+ displayName := strings.TrimSuffix(base, filepath.Ext(base))
var assetsFS fs.FS
- if as := filepath.Join(filepath.Dir(wasmPath), "assets"); dirExists(as) {
+ if as := filepath.Join(filepath.Dir(artifactPath), "assets"); dirExists(as) {
assetsFS = os.DirFS(as)
}
- loaded, err := loadFromBytes(ctx, env, manifestBytes, wasmBytes, displayName, assetsFS)
+ loaded, err := loadFromBytes(ctx, env, manifestBytes, artifactBytes, runtimeType, displayName, assetsFS)
if err != nil {
return nil, err
}
- loaded.WasmPath = wasmPath
+ loaded.WasmPath = artifactPath
return loaded, nil
}
-// loadFromBytes is the shared core of LoadPlugin and LoadPackage.
-func loadFromBytes(ctx context.Context, env *HostEnv, manifestBytes, wasmBytes []byte, displayName string, assetsFS fs.FS) (*Loaded, error) {
+// runtimeForExt maps a code artifact's file extension to its runtime:
+// .js → javascript, .py → python, anything else (.wasm) → wasm.
+func runtimeForExt(path string) string {
+ switch filepath.Ext(path) {
+ case ".js":
+ return RuntimeJavaScript
+ case ".py":
+ return RuntimePython
+ default:
+ return RuntimeWasm
+ }
+}
+
+// loadFromBytes is the shared core of LoadPlugin and LoadPackage. artifactBytes
+// is the plugin's wasm (for "wasm"/legacy) or its source (for the shared-engine
+// runtimes "javascript"/"python"); runtime — inferred by the caller from the
+// code artifact (its filename/extension), not authored in the manifest —
+// selects which. It is authoritative over any manifest-declared type.
+func loadFromBytes(ctx context.Context, env *HostEnv, manifestBytes, artifactBytes []byte, runtimeType, displayName string, assetsFS fs.FS) (*Loaded, error) {
manifest, err := ParseManifest(manifestBytes)
if err != nil {
return nil, err
}
+ manifest.Type = runtimeType
- hostFns := BuildHostFunctions(env, manifest, assetsFS)
-
- extismManifest := extism.Manifest{
- Wasm: []extism.Wasm{extism.WasmData{Data: wasmBytes, Name: displayName}},
- Timeout: 10_000, // milliseconds; enables Wazero's WithCloseOnContextDone
- // Sandbox caps. A plugin that exceeds these gets an error from the
- // next Call; the host stays up. Defaults are generous enough for
- // realistic plugins; per-plugin manifest overrides are a future TODO.
- Memory: &extism.ManifestMemory{
- MaxPages: MaxWasmPages, // wasm linear memory cap
- MaxHttpResponseBytes: MaxExtismHTTPResponseBytes, // outbound http body cap
- MaxVarBytes: MaxExtismVarBytes, // extism's internal Var KV
- },
- }
- for _, p := range manifest.Permissions {
- if p == PermNetworkFetch {
- // Manifest validation already required AllowedHosts to be
- // non-empty when network.fetch is granted, so passing the
- // list through is safe — admins explicitly authorized this
- // scope by approving the manifest at install time.
- extismManifest.AllowedHosts = append([]string(nil), manifest.Network.AllowedHosts...)
- break
- }
- }
- // Give the guest the real host wall clock and monotonic clock so Date and
- // performance.now() reflect actual time. Wazero's default ModuleConfig
- // uses a frozen deterministic clock (Date.now() would otherwise return a
- // fixed 2022 epoch). Nanosleep is deliberately NOT wired: a plugin must
- // not be able to block inside a call and burn its call-timeout budget.
- moduleConfig := wazero.NewModuleConfig().WithSysWalltime().WithSysNanotime()
- p, err := extism.NewPlugin(ctx, extismManifest, extism.PluginConfig{EnableWasi: true, ModuleConfig: moduleConfig}, hostFns)
+ p, err := instantiate(ctx, env, manifest, manifestBytes, artifactBytes, displayName)
if err != nil {
- return nil, fmt.Errorf("instantiate wasm: %w", err)
+ return nil, err
}
+
+ // Register the plugin's identity so shared host functions can resolve it at
+ // call time (by the slug stashed in the instance's Extism config). Done
+ // before register() because the engine bootstrap evals author top-level
+ // code during register(), which may call host functions. On any failure
+ // below, drop the identity again.
+ globalPluginRegistry.put(buildIdentity(env, manifest, assetsFS))
+ loaded := false
+ defer func() {
+ if !loaded {
+ globalPluginRegistry.remove(manifest.Slug)
+ }
+ }()
+
p.SetLogger(func(level extism.LogLevel, message string) {
fmt.Fprintf(os.Stderr, "[%s] %s\n", displayName, message)
})
@@ -1247,9 +1290,104 @@ func loadFromBytes(ctx context.Context, env *HostEnv, manifestBytes, wasmBytes [
adminPaths = append(adminPaths, page.Path)
}
+ loaded = true
return &Loaded{Manifest: manifest, plugin: p, adminGlobs: adminGlobs, adminPaths: adminPaths, AssetsFS: assetsFS}, nil
}
+// instantiate creates the extism plugin instance for a manifest: a per-plugin
+// instance of the shared engine for "js"/"python", or a self-contained module
+// for "wasm"/legacy. Either way the result is a *extism.Plugin with its
+// identity slug stashed in Config so shared host functions can resolve the
+// caller, and its per-plugin network scope applied.
+func instantiate(ctx context.Context, env *HostEnv, manifest *Manifest, manifestBytes, artifactBytes []byte, displayName string) (*extism.Plugin, error) {
+ // Give the guest the real host wall and monotonic clocks (wazero's default
+ // is a frozen 2022 clock). Nanosleep is deliberately NOT wired so a plugin
+ // can't block inside a call and burn its timeout budget.
+ moduleConfig := wazero.NewModuleConfig().WithSysWalltime().WithSysNanotime()
+
+ if manifest.usesSharedEngine() {
+ engine, err := compiledEngines.get(ctx, env, manifest.Type)
+ if err != nil {
+ return nil, err
+ }
+ inst, err := engine.Instance(ctx, extism.PluginInstanceConfig{ModuleConfig: moduleConfig})
+ if err != nil {
+ return nil, fmt.Errorf("instantiate %s engine: %w", manifest.Type, err)
+ }
+ // Inject the per-plugin script + manifest the engine bootstrap reads at
+ // runtime, plus the slug shared host functions resolve identity by.
+ inst.Config = map[string]string{
+ configKeySlug: manifest.Slug,
+ configKeyScript: string(artifactBytes),
+ configKeyManifest: string(manifestBytes),
+ }
+ applyNetworkScope(inst, manifest)
+ return inst, nil
+ }
+
+ // Legacy self-contained wasm: each plugin compiles into its own runtime.
+ // Share the compilation cache so reloads / repeated loads of the same
+ // module don't recompile its embedded engine.
+ extismManifest := extism.Manifest{
+ Wasm: []extism.Wasm{extism.WasmData{Data: artifactBytes, Name: displayName}},
+ Timeout: 10_000, // milliseconds; enables Wazero's WithCloseOnContextDone
+ Memory: &extism.ManifestMemory{
+ MaxPages: MaxWasmPages,
+ MaxHttpResponseBytes: MaxExtismHTTPResponseBytes,
+ MaxVarBytes: MaxExtismVarBytes,
+ },
+ }
+ for _, perm := range manifest.Permissions {
+ if perm == PermNetworkFetch {
+ extismManifest.AllowedHosts = append([]string(nil), manifest.Network.AllowedHosts...)
+ break
+ }
+ }
+ pc := extism.PluginConfig{
+ EnableWasi: true,
+ ModuleConfig: moduleConfig,
+ RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(sharedCompilationCache()),
+ }
+ p, err := extism.NewPlugin(ctx, extismManifest, pc, BuildHostFunctions(env))
+ if err != nil {
+ return nil, fmt.Errorf("instantiate wasm: %w", err)
+ }
+ // Even self-contained plugins now use the shared, identity-resolving host
+ // functions, so stash the slug for the registry lookup.
+ p.Config = map[string]string{configKeySlug: manifest.Slug}
+ return p, nil
+}
+
+// applyNetworkScope sets a shared-engine instance's per-plugin AllowedHosts.
+// The compiled engine carries no host allowlist (it's one engine for many
+// plugins); each instance's network scope comes from its own manifest. Manifest
+// validation already requires AllowedHosts to be non-empty when network.fetch
+// is granted.
+func applyNetworkScope(inst *extism.Plugin, manifest *Manifest) {
+ for _, perm := range manifest.Permissions {
+ if perm == PermNetworkFetch {
+ inst.AllowedHosts = append([]string(nil), manifest.Network.AllowedHosts...)
+ return
+ }
+ }
+}
+
+// buildIdentity assembles the call-time identity shared host functions resolve
+// for a loaded plugin (see registry.go).
+func buildIdentity(env *HostEnv, manifest *Manifest, assetsFS fs.FS) *pluginIdentity {
+ id := &pluginIdentity{
+ slug: manifest.Slug,
+ granted: stringSet(manifest.Permissions),
+ chatDisplay: manifest.ChatDisplayName(),
+ assetsFS: assetsFS,
+ manifest: manifest,
+ }
+ if env != nil && env.KV != nil {
+ id.kvNamespace = env.KV.Namespace(manifest.Slug)
+ }
+ return id
+}
+
// requireChatFilterPermission rejects a runtime registration that
// subscribes to filterChatMessage without declaring the chat.filter
// permission. Modifying or dropping every chat message is a meaningful
diff --git a/services/plugins/manifest.go b/services/plugins/manifest.go
index 754e4c3c0a..3cdd3ab84b 100644
--- a/services/plugins/manifest.go
+++ b/services/plugins/manifest.go
@@ -33,8 +33,23 @@ type ConfigField struct {
Description string `json:"description,omitempty"`
}
+// Plugin runtimes, inferred at load time from the plugin's code file (not
+// authored in the manifest): plugin.js → "javascript" and plugin.py → "python"
+// run the author's source on the shared embedded interpreter engine, while
+// plugin.wasm → "wasm" is a self-contained module the host loads directly.
+const (
+ RuntimeWasm = "wasm"
+ RuntimeJavaScript = "javascript"
+ RuntimePython = "python"
+)
+
type Manifest struct {
API string `json:"api"`
+ // Type is the plugin's runtime, set by the host at load time from the code
+ // artifact's filename (plugin.js/plugin.py/plugin.wasm) — NOT authored in
+ // the manifest. The json tag is retained only so any stray legacy value
+ // round-trips harmlessly; the loader always overrides it.
+ Type string `json:"type,omitempty"`
// DisplayName is the user-facing plugin name shown in admin lists,
// the Browse cards on the registry, and (by default) as the
// in-chat bot identity. Mapped from the JSON `name` field for
@@ -246,6 +261,9 @@ func (m *Manifest) Validate() error {
if err := m.resolveSlug(); err != nil {
return err
}
+ if err := m.validateConfigKeys(); err != nil {
+ return err
+ }
if err := m.validateAdminPages(); err != nil {
return err
}
@@ -267,6 +285,27 @@ func (m *Manifest) Validate() error {
return m.validateTabs()
}
+// usesSharedEngine reports whether this plugin runs on the shared embedded
+// interpreter engine (its code ships as source — plugin.js / plugin.py) rather
+// than as a self-contained wasm module. Type is set at load time from the code
+// artifact's filename, not authored in the manifest.
+func (m *Manifest) usesSharedEngine() bool {
+ return m.Type == RuntimeJavaScript || m.Type == RuntimePython
+}
+
+// validateConfigKeys rejects author config keys reserved by the host. Keys
+// starting with "__" are used to inject per-instance state (e.g. "__slug") into
+// shared-engine plugins via Extism config, and must not collide with declared
+// plugin config.
+func (m *Manifest) validateConfigKeys() error {
+ for key := range m.Config {
+ if strings.HasPrefix(key, "__") {
+ return fmt.Errorf("manifest.config key %q is reserved (keys starting with \"__\" are used internally)", key)
+ }
+ }
+ return nil
+}
+
// resolveSlug fills in m.Slug when the author didn't pin one
// explicitly, and validates the resulting slug against slugPattern.
// Called from Validate, so every Manifest returned by ParseManifest
diff --git a/services/plugins/package.go b/services/plugins/package.go
index 3ce209e275..0bdd96ff23 100644
--- a/services/plugins/package.go
+++ b/services/plugins/package.go
@@ -27,11 +27,44 @@ import (
const (
packageSuffix = ".ocpkg"
pkgManifestFilename = "plugin.manifest.json"
- pkgWasmFilename = "plugin.wasm"
- pkgPublicPrefix = "public/"
- pkgAssetsPrefix = "assets/"
+ // Code entries. The plugin's runtime is inferred from which one is present
+ // — the author doesn't declare it in the manifest. plugin.js / plugin.py
+ // are author source run on the shared embedded engine; plugin.wasm is a
+ // self-contained module the host loads directly.
+ pkgWasmFilename = "plugin.wasm"
+ pkgJSFilename = "plugin.js"
+ pkgPyFilename = "plugin.py"
+ pkgPublicPrefix = "public/"
+ pkgAssetsPrefix = "assets/"
)
+// codeEntries maps each canonical code-entry name to the runtime it implies,
+// in detection-precedence order.
+var codeEntries = []struct{ name, runtime string }{
+ {pkgJSFilename, RuntimeJavaScript},
+ {pkgPyFilename, RuntimePython},
+ {pkgWasmFilename, RuntimeWasm},
+}
+
+// detectPackageCode finds a package's code entry and the runtime it implies.
+func detectPackageCode(zr *zip.Reader) (name, runtime string, ok bool) {
+ for _, e := range codeEntries {
+ if zipHasFile(zr, e.name) {
+ return e.name, e.runtime, true
+ }
+ }
+ return "", "", false
+}
+
+func zipHasFile(zr *zip.Reader, name string) bool {
+ for _, f := range zr.File {
+ if f.Name == name {
+ return true
+ }
+ }
+ return false
+}
+
// LoadPackage loads a plugin from a .ocpkg file. The archive is opened with
// a file-backed reader so only the central directory and the manifest/wasm
// entries actually enter memory; per-asset reads happen on demand when the
@@ -56,7 +89,13 @@ func LoadPackage(ctx context.Context, env *HostEnv, path string) (*Loaded, error
if err != nil {
return nil, fmt.Errorf("%s: %w", filepath.Base(path), err)
}
- wasmBytes, err := readZipFile(&zr.Reader, pkgWasmFilename)
+ // The code entry's name implies the runtime (plugin.js / plugin.py / plugin.wasm),
+ // so the author never declares it in the manifest.
+ codeName, runtimeType, ok := detectPackageCode(&zr.Reader)
+ if !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)
if err != nil {
return nil, fmt.Errorf("%s: %w", filepath.Base(path), err)
}
@@ -72,7 +111,7 @@ func LoadPackage(ctx context.Context, env *HostEnv, path string) (*Loaded, error
}
}
- loaded, err := loadFromBytes(ctx, env, manifestBytes, wasmBytes, displayName, assetsFS)
+ loaded, err := loadFromBytes(ctx, env, manifestBytes, codeBytes, runtimeType, displayName, assetsFS)
if err != nil {
return nil, err
}
diff --git a/services/plugins/registry.go b/services/plugins/registry.go
new file mode 100644
index 0000000000..87312ee24f
--- /dev/null
+++ b/services/plugins/registry.go
@@ -0,0 +1,111 @@
+package plugins
+
+import (
+ "context"
+ "fmt"
+ "io/fs"
+ "os"
+ "sync"
+
+ extism "github.com/extism/go-sdk"
+ "github.com/owncast/owncast/services/plugins/kv"
+)
+
+// pluginIdentity is everything a host function needs to serve a call on behalf
+// of a specific plugin. In the legacy self-contained-wasm model these values
+// were captured in each host function's closure at BuildHostFunctions time. In
+// the shared-engine model host functions are built once and shared across all
+// plugins, so the calling plugin's identity is resolved at call time from this
+// registry instead (keyed by the slug the host stashed in the instance's
+// Extism config under "__slug").
+type pluginIdentity struct {
+ slug string
+ granted map[string]bool // permission set, gated at call time
+ chatDisplay string // manifest.ChatDisplayName(), for chat sends
+ assetsFS fs.FS // per-plugin bundled assets/ (may be nil)
+ manifest *Manifest // for config_get (declared config) + action buttons
+ kvNamespace kv.Namespace // env.KV.Namespace(slug), resolved once at load
+}
+
+// can reports whether the plugin was granted perm. Ambient capabilities (timer,
+// config, asset read) don't call this.
+func (id *pluginIdentity) can(perm string) bool {
+ return id != nil && id.granted[perm]
+}
+
+// pluginRegistry maps slug -> identity for every currently-loaded shared-engine
+// plugin. It has its own lock, independent of Manager.mu, because host
+// functions read it on the hot call path with no Manager involved (the test
+// runner and package preflight also load plugins without a Manager). Slug
+// uniqueness is guaranteed upstream by the Manager's discovered/loaded maps.
+type pluginRegistry struct {
+ mu sync.RWMutex
+ byslug map[string]*pluginIdentity
+}
+
+// Reserved Extism config keys the host sets per shared-engine instance. They
+// are prefixed with "__" so they can't collide with author-declared config
+// (manifest validation rejects author keys starting with "__"). The guest
+// engine reads "script"/"manifest"; the host reads "__slug" back to resolve
+// identity inside shared host functions.
+const (
+ configKeySlug = "__slug"
+ configKeyScript = "script"
+ configKeyManifest = "manifest"
+)
+
+var globalPluginRegistry = &pluginRegistry{byslug: map[string]*pluginIdentity{}}
+
+func (r *pluginRegistry) put(id *pluginIdentity) {
+ r.mu.Lock()
+ r.byslug[id.slug] = id
+ r.mu.Unlock()
+}
+
+func (r *pluginRegistry) get(slug string) (*pluginIdentity, bool) {
+ r.mu.RLock()
+ id, ok := r.byslug[slug]
+ r.mu.RUnlock()
+ return id, ok
+}
+
+func (r *pluginRegistry) remove(slug string) {
+ r.mu.Lock()
+ delete(r.byslug, slug)
+ r.mu.Unlock()
+}
+
+// resolveCaller resolves the calling plugin's identity and enforces its
+// permission grant. It is the single permission-gating boundary for shared host
+// functions (in the legacy per-plugin model, an ungranted capability simply
+// wasn't linked; now every plugin links every host import, so the gate must
+// live here). Pass perm == "" for ambient capabilities (timer, config, asset
+// read) that need no grant. On a false return the host function must abort
+// (returning zero/empty to the guest); permission denials are logged.
+func resolveCaller(ctx context.Context, fnName, perm string) (*pluginIdentity, bool) {
+ id, ok := callerIdentity(ctx)
+ if !ok {
+ return nil, false
+ }
+ if perm != "" && !id.can(perm) {
+ fmt.Fprintf(os.Stderr, "%s: plugin %q lacks permission %q\n", fnName, id.slug, perm)
+ return nil, false
+ }
+ return id, true
+}
+
+// callerIdentity resolves the plugin making the current host-function call. The
+// extism go-sdk injects the calling *extism.Plugin into the context at every
+// Call (see PluginCtxKey), and the host sets inst.Config["__slug"] before the
+// first call, so we read the slug back here and look up its identity.
+func callerIdentity(ctx context.Context) (*pluginIdentity, bool) {
+ p, ok := ctx.Value(extism.PluginCtxKey("plugin")).(*extism.Plugin)
+ if !ok || p == nil {
+ return nil, false
+ }
+ slug := p.Config[configKeySlug]
+ if slug == "" {
+ return nil, false
+ }
+ return globalPluginRegistry.get(slug)
+}
diff --git a/services/plugins/shared_engine_test.go b/services/plugins/shared_engine_test.go
new file mode 100644
index 0000000000..c289fc8ae8
--- /dev/null
+++ b/services/plugins/shared_engine_test.go
@@ -0,0 +1,280 @@
+package plugins
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "strings"
+ "sync"
+ "testing"
+)
+
+// captureEnv returns a HostEnv whose OnChat records every send, plus the slice
+// it records into. Enough to exercise the shared-engine dispatch path.
+func captureEnv() (*HostEnv, *[]ChatSendRequest, *sync.Mutex) {
+ var mu sync.Mutex
+ var sends []ChatSendRequest
+ env := &HostEnv{
+ OnChat: func(r ChatSendRequest) {
+ mu.Lock()
+ sends = append(sends, r)
+ mu.Unlock()
+ },
+ }
+ return env, &sends, &mu
+}
+
+func chatPayload(name, body string) any {
+ return map[string]any{
+ "id": "1",
+ "user": map[string]any{"id": "u1", "displayName": name},
+ "clientId": 7,
+ "body": body,
+ "timestamp": "2024-01-01T00:00:00Z",
+ }
+}
+
+// loadShared loads an inline script as a shared-engine plugin through the real
+// production load path (loadFromBytes → engine.Instance → register).
+func loadShared(t *testing.T, ctx context.Context, env *HostEnv, runtime, slug, script string, perms []string) *Loaded {
+ t.Helper()
+ // No "type" in the manifest — the runtime is passed explicitly, mirroring
+ // how the host infers it from the code artifact's filename at load.
+ m := map[string]any{
+ "api": "1",
+ "name": slug,
+ "slug": slug,
+ "version": "0.1.0",
+ "permissions": perms,
+ }
+ manifestBytes, _ := json.Marshal(m)
+ loaded, err := loadFromBytes(ctx, env, manifestBytes, []byte(script), runtime, slug, nil)
+ if err != nil {
+ t.Fatalf("loadFromBytes(%s/%s): %v", runtime, slug, err)
+ }
+ return loaded
+}
+
+func TestSharedEngineJSRegisterAndDispatch(t *testing.T) {
+ ctx := context.Background()
+ compiledEngines.resetForTest(ctx)
+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })
+
+ env, sends, mu := captureEnv()
+ script := `
+const { definePlugin, owncast } = require("@owncast/plugin-sdk");
+module.exports = definePlugin({
+ onChatMessage(msg) { owncast.chat.send(msg.user.displayName + " said: " + msg.body); }
+});`
+ loaded := loadShared(t, ctx, env, RuntimeJavaScript, "echo-bot", script, []string{PermChatSend})
+ defer loaded.Close(ctx)
+
+ // register() must have derived the notify subscription for onChatMessage.
+ if !subscribed(loaded.Manifest.Subscriptions.Notify, EventChatMessageReceived) {
+ t.Fatalf("expected notify subscription for %s, got %+v", EventChatMessageReceived, loaded.Manifest.Subscriptions)
+ }
+
+ d := NewLiveDispatcher(func() []*Loaded { return []*Loaded{loaded} })
+ d.Dispatch(ctx, EventChatMessageReceived, chatPayload("alice", "hi"))
+
+ mu.Lock()
+ defer mu.Unlock()
+ if len(*sends) != 1 {
+ t.Fatalf("expected 1 chat send, got %d: %+v", len(*sends), *sends)
+ }
+ if got := (*sends)[0].Text; got != "alice said: hi" {
+ t.Errorf("chat send text = %q, want %q", got, "alice said: hi")
+ }
+ if got := (*sends)[0].PluginSlug; got != "echo-bot" {
+ t.Errorf("chat send routed to slug %q, want echo-bot", got)
+ }
+}
+
+// TestSharedEnginePermissionGate is the R1 check: a plugin that calls a host
+// function for a permission it was not granted must be denied at call time
+// (the shared engine links every host import, so the gate is the only
+// enforcement). The plugin still loads; the call is simply a no-op.
+func TestSharedEnginePermissionGate(t *testing.T) {
+ ctx := context.Background()
+ compiledEngines.resetForTest(ctx)
+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })
+
+ env, sends, mu := captureEnv()
+ script := `
+const { definePlugin, owncast } = require("@owncast/plugin-sdk");
+module.exports = definePlugin({
+ onChatMessage(msg) { owncast.chat.send("should be blocked"); }
+});`
+ // No chat.send permission granted.
+ loaded := loadShared(t, ctx, env, RuntimeJavaScript, "sneaky", script, []string{})
+ defer loaded.Close(ctx)
+
+ d := NewLiveDispatcher(func() []*Loaded { return []*Loaded{loaded} })
+ d.Dispatch(ctx, EventChatMessageReceived, chatPayload("alice", "hi"))
+
+ mu.Lock()
+ defer mu.Unlock()
+ if len(*sends) != 0 {
+ t.Fatalf("ungranted chat.send must be blocked; got %d sends: %+v", len(*sends), *sends)
+ }
+}
+
+// TestManifestParse covers the R4 reserved-config-key guard and confirms the
+// manifest no longer requires (or validates) a "type" field — the runtime is
+// inferred from the code artifact at load time.
+func TestManifestParse(t *testing.T) {
+ base := `{"api":"1","name":"X","slug":"x","version":"0.1.0"%s}`
+ cases := []struct {
+ name string
+ extra string
+ wantErr bool
+ }{
+ {"no type field is fine", "", false},
+ {"stray type ignored, not validated", `,"type":"anything"`, false},
+ {"reserved config key rejected", `,"config":{"__slug":{"type":"string"}}`, true},
+ {"normal config key ok", `,"config":{"greeting":{"type":"string"}}`, false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ m, err := ParseManifest([]byte(fmt.Sprintf(base, tc.extra)))
+ if tc.wantErr {
+ if err == nil {
+ t.Fatalf("expected error, got nil (manifest %+v)", m)
+ }
+ return
+ }
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ })
+ }
+}
+
+// TestSharedEngineNetworkIsolation is the R3 check: a shared engine carries no
+// host allowlist, so each instance must get its own network scope from its own
+// manifest. Two plugins on the same engine must not inherit each other's
+// allowed hosts.
+func TestSharedEngineNetworkIsolation(t *testing.T) {
+ ctx := context.Background()
+ compiledEngines.resetForTest(ctx)
+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })
+
+ env, _, _ := captureEnv()
+ script := `const { definePlugin } = require("@owncast/plugin-sdk"); module.exports = definePlugin({});`
+
+ load := func(slug, host string) *Loaded {
+ m := map[string]any{
+ "api": "1", "name": slug, "slug": slug, "version": "0.1.0",
+ "permissions": []string{PermNetworkFetch},
+ "network": map[string]any{"allowedHosts": []string{host}},
+ }
+ mb, _ := json.Marshal(m)
+ l, err := loadFromBytes(ctx, env, mb, []byte(script), RuntimeJavaScript, slug, nil)
+ if err != nil {
+ t.Fatalf("load %s: %v", slug, err)
+ }
+ return l
+ }
+ a := load("net-a", "a.example.com")
+ defer a.Close(ctx)
+ b := load("net-b", "b.example.com")
+ defer b.Close(ctx)
+
+ if got := a.plugin.AllowedHosts; len(got) != 1 || got[0] != "a.example.com" {
+ t.Errorf("plugin A AllowedHosts = %v, want [a.example.com]", got)
+ }
+ if got := b.plugin.AllowedHosts; len(got) != 1 || got[0] != "b.example.com" {
+ t.Errorf("plugin B AllowedHosts = %v, want [b.example.com]", got)
+ }
+}
+
+// TestSharedEngineConcurrency is the R2 check: many instances of one shared
+// compiled engine, dispatched concurrently, must route correctly and not race.
+// Run the package with -race to exercise the shared wazero runtime.
+func TestSharedEngineConcurrency(t *testing.T) {
+ ctx := context.Background()
+ compiledEngines.resetForTest(ctx)
+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })
+
+ env, sends, mu := captureEnv()
+ script := `
+const { definePlugin, owncast } = require("@owncast/plugin-sdk");
+module.exports = definePlugin({
+ onChatMessage(msg) { owncast.chat.send(msg.body); }
+});`
+ var loadeds []*Loaded
+ for _, slug := range []string{"c1", "c2", "c3"} {
+ l := loadShared(t, ctx, env, RuntimeJavaScript, slug, script, []string{PermChatSend})
+ defer l.Close(ctx)
+ loadeds = append(loadeds, l)
+ }
+ d := NewLiveDispatcher(func() []*Loaded { return loadeds })
+
+ const perPlugin = 20
+ var wg sync.WaitGroup
+ for i := 0; i < perPlugin; i++ {
+ wg.Add(1)
+ go func(n int) {
+ defer wg.Done()
+ d.Dispatch(ctx, EventChatMessageReceived, chatPayload("u", "m"))
+ }(i)
+ }
+ wg.Wait()
+
+ mu.Lock()
+ defer mu.Unlock()
+ // Each of the perPlugin dispatches notifies all 3 plugins.
+ if want := perPlugin * len(loadeds); len(*sends) != want {
+ t.Fatalf("expected %d sends, got %d", want, len(*sends))
+ }
+}
+
+func TestSharedEnginePythonRegister(t *testing.T) {
+ ctx := context.Background()
+ compiledEngines.resetForTest(ctx)
+ t.Cleanup(func() { compiledEngines.resetForTest(ctx) })
+
+ env, sends, mu := captureEnv()
+ // The SDK build strips the `from owncast_plugin import ...` line; the SDK
+ // names (plugin, owncast) are already globals in the engine. Injecting the
+ // import would make the frozen CPython try to load a nonexistent module.
+ script := `
[email protected]_chat_message
+def greet(msg):
+ owncast.chat.send(msg.user.display_name + " said: " + msg.body)
+`
+ loaded := loadShared(t, ctx, env, RuntimePython, "py-echo", script, []string{PermChatSend})
+ defer loaded.Close(ctx)
+
+ if !subscribed(loaded.Manifest.Subscriptions.Notify, EventChatMessageReceived) {
+ t.Fatalf("python: expected notify subscription for %s, got %+v", EventChatMessageReceived, loaded.Manifest.Subscriptions)
+ }
+
+ d := NewLiveDispatcher(func() []*Loaded { return []*Loaded{loaded} })
+ d.Dispatch(ctx, EventChatMessageReceived, chatPayload("bob", "yo"))
+
+ mu.Lock()
+ defer mu.Unlock()
+ if len(*sends) != 1 {
+ t.Fatalf("python: expected 1 chat send, got %d: %+v", len(*sends), *sends)
+ }
+ if got := (*sends)[0].Text; !strings.Contains(got, "bob said: yo") {
+ t.Errorf("python chat send text = %q, want to contain %q", got, "bob said: yo")
+ }
+}
+
+// resetForTest closes and clears the compiled engines and the plugin registry.
+// Tests construct fresh HostEnvs per case; without this an engine compiled
+// against an earlier test's env would leak into the next. It lives in a test
+// file so it isn't flagged as unused production code.
+func (c *engineCache) resetForTest(ctx context.Context) {
+ c.mu.Lock()
+ for k, cp := range c.byKey {
+ _ = cp.Close(ctx)
+ delete(c.byKey, k)
+ }
+ c.mu.Unlock()
+ globalPluginRegistry.mu.Lock()
+ globalPluginRegistry.byslug = map[string]*pluginIdentity{}
+ globalPluginRegistry.mu.Unlock()
+}