2026-04-06 09:00:47 +00:00
|
|
|
// Phase C Step 9 — On-disk plugin discovery.
|
|
|
|
|
//
|
|
|
|
|
// Each plugin lives in its own subdirectory under PluginsConfig.Directory:
|
|
|
|
|
//
|
|
|
|
|
// plugins/
|
|
|
|
|
// hello/
|
|
|
|
|
// plugin.json
|
|
|
|
|
// hello.wasm
|
|
|
|
|
// game-detection/
|
|
|
|
|
// plugin.json
|
|
|
|
|
// detector.wasm
|
|
|
|
|
// assets/...
|
|
|
|
|
//
|
|
|
|
|
// Loader walks the directory, parses every plugin.json, and returns a slice
|
|
|
|
|
// of foundPlugin records. The Registry then persists each into the store.
|
2026-07-18 12:55:22 +02:00
|
|
|
|
2026-04-06 09:00:47 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type foundPlugin struct {
|
|
|
|
|
Manifest *Manifest
|
|
|
|
|
Dir string
|
|
|
|
|
WASMPath string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// scanPluginDirectory walks dir non-recursively and parses plugin.json from
|
2026-04-06 22:03:35 +00:00
|
|
|
// every immediate subdirectory. Returns on the first error encountered;
|
|
|
|
|
// partial results are not returned alongside errors.
|
2026-04-06 09:00:47 +00:00
|
|
|
func scanPluginDirectory(dir string) ([]foundPlugin, error) {
|
|
|
|
|
if dir == "" {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
// Directory absent is fine — operators may not have created it yet.
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var found []foundPlugin
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
if !e.IsDir() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
pluginDir := filepath.Join(dir, e.Name())
|
2026-04-06 23:25:16 +02:00
|
|
|
|
|
|
|
|
// Prefer plugin.toml (wazero build) over plugin.json.
|
|
|
|
|
manifest, ok, tomlErr := tryLoadPluginTOML(pluginDir)
|
|
|
|
|
if tomlErr != nil {
|
|
|
|
|
return nil, fmt.Errorf("plugin %q: %w", e.Name(), tomlErr)
|
2026-04-06 09:00:47 +00:00
|
|
|
}
|
2026-04-06 23:25:16 +02:00
|
|
|
if !ok {
|
|
|
|
|
// Fall back to plugin.json.
|
|
|
|
|
manifestPath := filepath.Join(pluginDir, "plugin.json")
|
|
|
|
|
raw, rdErr := os.ReadFile(manifestPath)
|
|
|
|
|
if rdErr != nil {
|
|
|
|
|
if os.IsNotExist(rdErr) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("plugin %q: read plugin.json: %w", e.Name(), rdErr)
|
|
|
|
|
}
|
|
|
|
|
var parseErr error
|
|
|
|
|
manifest, parseErr = ParseManifest(raw)
|
|
|
|
|
if parseErr != nil {
|
|
|
|
|
return nil, fmt.Errorf("plugin %q: %w", e.Name(), parseErr)
|
|
|
|
|
}
|
2026-04-06 09:00:47 +00:00
|
|
|
}
|
2026-04-06 09:49:03 +00:00
|
|
|
// Reject any symlinks anywhere in the plugin directory tree. The asset
|
|
|
|
|
// handler enforces that resolved paths stay rooted at pluginDir, but
|
|
|
|
|
// http.ServeFile / os.Open follow symlinks transparently — a malicious
|
|
|
|
|
// plugin .zip containing `assets/index.html -> /etc/passwd` would
|
2026-04-06 22:03:35 +00:00
|
|
|
// otherwise serve host files. os.Lstat is used for the entrypoint
|
|
|
|
|
// check below so a symlink is detected instead of followed, even
|
|
|
|
|
// when its target is a valid .wasm file.
|
2026-04-06 09:49:03 +00:00
|
|
|
if err := rejectSymlinksUnder(pluginDir); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("plugin %q: %w", e.Name(), err)
|
|
|
|
|
}
|
2026-04-06 09:00:47 +00:00
|
|
|
wasmPath := filepath.Join(pluginDir, manifest.Entrypoint)
|
2026-04-06 09:49:03 +00:00
|
|
|
if info, statErr := os.Lstat(wasmPath); statErr != nil {
|
2026-04-06 09:00:47 +00:00
|
|
|
return nil, fmt.Errorf("plugin %q: missing entrypoint %s: %w", e.Name(), manifest.Entrypoint, statErr)
|
2026-04-06 09:49:03 +00:00
|
|
|
} else if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
|
return nil, fmt.Errorf("plugin %q: entrypoint %s is a symlink", e.Name(), manifest.Entrypoint)
|
2026-04-06 09:00:47 +00:00
|
|
|
}
|
|
|
|
|
found = append(found, foundPlugin{
|
|
|
|
|
Manifest: manifest,
|
|
|
|
|
Dir: pluginDir,
|
|
|
|
|
WASMPath: wasmPath,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return found, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:49:03 +00:00
|
|
|
// rejectSymlinksUnder walks root and returns an error if any entry is a
|
|
|
|
|
// symlink. Defends against malicious plugin packages that ship symlinks to
|
|
|
|
|
// host filesystem paths.
|
|
|
|
|
func rejectSymlinksUnder(root string) error {
|
|
|
|
|
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
|
return fmt.Errorf("symlink not allowed: %s", path)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:00:47 +00:00
|
|
|
// serialize returns a canonical JSON encoding of the manifest, used as the
|
|
|
|
|
// manifest_json column value in the plugins table.
|
|
|
|
|
func (m *Manifest) serialize() (string, error) {
|
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("manifest serialize: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return string(b), nil
|
|
|
|
|
}
|