Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
241 lines
8.3 KiB
Go
241 lines
8.3 KiB
Go
package plugin
|
|
|
|
import (
|
|
"archive/zip"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSemVerRejectsPathTraversal(t *testing.T) {
|
|
bad := []string{
|
|
"1.0.0-a/../../../x",
|
|
"1.0.0+a/../b",
|
|
"1.0.0-a\\b",
|
|
"1.0.0-a..b",
|
|
"1.0.0-",
|
|
"1.0.0+",
|
|
"1.0.0-alpha beta",
|
|
"1.0.0-../evil",
|
|
}
|
|
for _, v := range bad {
|
|
if _, err := ParseSemVer(v); err == nil {
|
|
t.Errorf("ParseSemVer(%q) expected error, got nil", v)
|
|
}
|
|
}
|
|
good := []string{"1.0.0", "v0.2.6", "1.2.3-alpha.1", "1.2.3-rc-1+build.42"}
|
|
for _, v := range good {
|
|
if _, err := ParseSemVer(v); err != nil {
|
|
t.Errorf("ParseSemVer(%q) unexpected error: %v", v, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInstallArchiveRejectsTraversalVersion(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
zipPath := filepath.Join(tmp, "evil.zip")
|
|
manifest := `{"schema":1,"id":"evil.pkg","version":"1.0.0-a/../../../escaped","name":{"en-US":"Evil"}}`
|
|
sum := sha256.Sum256([]byte(manifest))
|
|
sums := hex.EncodeToString(sum[:]) + " plugin.json\n"
|
|
|
|
f, err := os.Create(zipPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zw := zip.NewWriter(f)
|
|
for name, body := range map[string]string{"plugin.json": manifest, "SHA256SUMS": sums} {
|
|
w, err := zw.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write([]byte(body)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = f.Close()
|
|
|
|
layout := NewPluginsLayout(tmp)
|
|
_, err = InstallArchive(layout, zipPath, PackageInstallOptions{HostVersion: "0.3.0", AllowUnsigned: true})
|
|
if err == nil {
|
|
t.Fatal("expected InstallArchive to reject traversal version")
|
|
}
|
|
}
|
|
|
|
func TestValidateArgFragmentBlocksInjection(t *testing.T) {
|
|
bad := [][]string{
|
|
{"-device", "x -smbios type=1,serial=SECRET"},
|
|
{"-device", `"x" -smbios type=1`},
|
|
{"-device", "x\n-smbios"},
|
|
{"-drive", "file=a.img -smbios type=1"},
|
|
{"positional", "-no-reboot"},
|
|
{"-no-reboot", "extra"},
|
|
{"-smbios", "type=1"},
|
|
{"-device", `"unterminated`},
|
|
}
|
|
for _, toks := range bad {
|
|
if err := ValidateArgFragment(ArgFragment{Tokens: toks}); err == nil {
|
|
t.Errorf("ValidateArgFragment(%q) expected error, got nil", toks)
|
|
}
|
|
}
|
|
good := []ArgFragment{
|
|
{Tokens: []string{"-machine", "kernel-irqchip=off"}},
|
|
{Tokens: []string{"-no-reboot"}},
|
|
{Builtin: true, Tokens: []string{"-no-reboot", "-qmp", "unix:D:/Data/reims-go-reboot.sock,server=on,wait=off"}},
|
|
{Builtin: true, Tokens: []string{"-drive", `"id=reimshelp,if=none,format=raw,readonly=on,file=D:/My VMs/reimshelp.img"`, "-device", "usb-storage,id=reimshelp-dev,drive=reimshelp,bus=xhci.0"}},
|
|
}
|
|
for _, f := range good {
|
|
if err := ValidateArgFragment(f); err != nil {
|
|
t.Errorf("ValidateArgFragment(%+v) unexpected error: %v", f, err)
|
|
}
|
|
}
|
|
// External (non-builtin) plugin must be rejected from file/socket options.
|
|
if err := ValidateArgFragment(ArgFragment{Tokens: []string{"-drive", "file=secret.img"}}); err == nil {
|
|
t.Error("expected external -drive to be rejected")
|
|
}
|
|
// External plugin must ALWAYS be rejected from -smbios, even type=17.
|
|
if err := ValidateArgFragment(ArgFragment{Tokens: []string{"-smbios", "type=17,speed=6400,memory_type=34"}}); err == nil {
|
|
t.Error("expected external -smbios type=17 to be rejected")
|
|
}
|
|
// Builtin -smbios must allow ONLY type=17 with speed and optional memory_type.
|
|
for _, goodBuiltin := range [][]string{
|
|
{"-smbios", "type=17,speed=6400,memory_type=34"},
|
|
{"-smbios", "type=17,speed=3200"},
|
|
} {
|
|
if err := ValidateArgFragment(ArgFragment{Builtin: true, Tokens: goodBuiltin}); err != nil {
|
|
t.Errorf("expected builtin %q to be allowed, got %v", goodBuiltin, err)
|
|
}
|
|
}
|
|
for _, badBuiltin := range [][]string{
|
|
{"-smbios", "type=1,serial=SECRET"},
|
|
{"-smbios", "type=17,speed=6400,serial=SECRET"},
|
|
{"-smbios", "type=17"},
|
|
{"-smbios", "type=17,speed=0"},
|
|
{"-smbios", "type=17,speed=70000"},
|
|
{"-smbios", "type=17,speed=6400,memory_type=300"},
|
|
} {
|
|
if err := ValidateArgFragment(ArgFragment{Builtin: true, Tokens: badBuiltin}); err == nil {
|
|
t.Errorf("expected builtin %q to be rejected, got nil", badBuiltin)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuiltinBeforeStartExemptFromExternalTimeout(t *testing.T) {
|
|
reg := NewRegistry(nil)
|
|
var completed bool
|
|
if err := reg.Register(PluginDef{
|
|
Manifest: Manifest{
|
|
Schema: 1, ID: "builtin.slow.prep", Version: "1.0.0",
|
|
Name: LocalizedText{EnUS: "Slow Builtin"},
|
|
},
|
|
Source: SourceBuiltin, Trust: TrustOfficial, Desired: true,
|
|
Activate: func(_ context.Context, sc *Scope) error {
|
|
sc.Registry().Hooks().Subscribe(sc, HookRegistration{
|
|
Event: EventVMBeforeStart,
|
|
Builtin: true,
|
|
Handler: func(ctx context.Context, _ any) (any, error) {
|
|
// Longer than the 2s external plugin timeout.
|
|
select {
|
|
case <-time.After(2200 * time.Millisecond):
|
|
completed = true
|
|
return StartIntent{
|
|
PluginID: "builtin.slow.prep",
|
|
Args: []ArgFragment{{Key: "k", Tokens: []string{"-machine", "kernel-irqchip=off"}, Builtin: true}},
|
|
}, nil
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
},
|
|
})
|
|
return nil
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
|
|
intents, err := reg.Hooks().DispatchBeforeStart(context.Background(), BeforeStartPayload{VMName: "test"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !completed || len(intents) != 1 {
|
|
t.Fatalf("builtin prep hook did not finish synchronously: completed=%v intents=%d", completed, len(intents))
|
|
}
|
|
if st := reg.StateOf("builtin.slow.prep"); st != StateActive {
|
|
t.Fatalf("builtin state = %s, want active", st)
|
|
}
|
|
}
|
|
|
|
func TestValidateThemeCSSAndContributionBlocksBypasses(t *testing.T) {
|
|
badCSS := []struct {
|
|
name string
|
|
css string
|
|
}{
|
|
{"css-escaped-url", `.rg-p-shell { background: u\72l("https://evil.test/x.png"); }`},
|
|
{"escaped-scheme", `.rg-p-shell { background: url("\68ttps://evil.test/x.png"); }`},
|
|
{"image-set-remote", `.rg-p-shell { background-image: image-set("https://evil.test/a.png" 1x); }`},
|
|
{"data-svg", `.rg-p-shell { background: url("data:image/svg+xml,<svg onload=alert(1)>"); }`},
|
|
{"data-html", `.rg-p-shell { background: url("data:text/html,<script>alert(1)</script>"); }`},
|
|
{"escaped-import", `@\69mport "https://evil.test/x.css";`},
|
|
{"unbalanced-close", `.rg-p-shell { color: red; } } .evil { display: none; }`},
|
|
{"unbalanced-open", `.rg-p-shell { color: red;`},
|
|
{"expression", `.rg-p-shell { width: expression(alert(1)); }`},
|
|
{"style-tag-breakout", `.rg-p-shell { content: "</style><script>alert(1)</script>"; }`},
|
|
{"traversal-url", `.rg-p-shell { background: url("../../secret.png"); }`},
|
|
}
|
|
for _, tc := range badCSS {
|
|
if err := ValidateThemeCSSSafety(tc.css, ""); err == nil {
|
|
t.Errorf("ValidateThemeCSSSafety(%s) expected error, got nil", tc.name)
|
|
}
|
|
}
|
|
|
|
// Verify asset containment when rootDir is provided.
|
|
dir := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(dir, "assets"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "assets", "bg.png"), []byte("png"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ValidateThemeCSSSafety(`.rg-p-shell { background: url("assets/bg.png"); }`, dir); err != nil {
|
|
t.Errorf("valid local asset rejected: %v", err)
|
|
}
|
|
if err := ValidateThemeCSSSafety(`.rg-p-shell { background: url("assets/missing.png"); }`, dir); err == nil {
|
|
t.Error("missing local asset should be rejected")
|
|
}
|
|
|
|
// Verify token & font injection checks.
|
|
badTokens := []ThemeContribution{
|
|
{ID: "t1", Tokens: map[string]string{"--accent": "red; } .evil { color: blue"}},
|
|
{ID: "t2", Tokens: map[string]string{"--accent": `u\72l(https://evil.test/x)`}},
|
|
{ID: "t3", Tokens: map[string]string{"not-a-var": "red"}},
|
|
{ID: "t4", Background: "https://evil.test/bg.png"},
|
|
{ID: "t5", Fonts: []ThemeFont{{Family: `Evil"; } body { display:none`, File: "assets/bg.png"}}},
|
|
}
|
|
for _, th := range badTokens {
|
|
if err := ValidateThemeContribution(th, dir); err == nil {
|
|
t.Errorf("ValidateThemeContribution(%s) expected error, got nil", th.ID)
|
|
}
|
|
}
|
|
|
|
goodTheme := ThemeContribution{
|
|
ID: "ok",
|
|
Tokens: map[string]string{"--accent": "#d97757"},
|
|
Background: "assets/bg.png",
|
|
Fonts: []ThemeFont{{Family: "CustomSans", File: "assets/bg.png", Weight: "400 700", Style: "normal"}},
|
|
}
|
|
if err := ValidateThemeContribution(goodTheme, dir); err != nil {
|
|
t.Errorf("valid theme rejected: %v", err)
|
|
}
|
|
if !strings.Contains(goodTheme.Background, "assets/") {
|
|
t.Fatal("sanity")
|
|
}
|
|
}
|