package plugin import ( "errors" "fmt" "os" "path" "strings" ) const maxPluginIconSVGBytes = 32 << 10 // 32 KiB ceiling for monochrome foreground icon SVG // PluginIconTones lists the allowed background color tokens for plugin icons. var PluginIconTones = []string{ "indigo", "purple", "sky", "rose", "emerald", "amber", "teal", "slate", } // PluginIcon declares a plugin's custom squircle color token and monochrome SVG foreground icon. // In external plugin.json manifests, SVG is a relative path to an .svg file inside the package. // In resolved PluginInfo, MarketEntry, and built-in plugins, SVG holds the sanitized inline markup. type PluginIcon struct { Tone string `json:"tone,omitempty"` SVG string `json:"svg,omitempty"` } // IsValidPluginIconTone reports whether tone is one of the allowed color tokens. func IsValidPluginIconTone(tone string) bool { t := strings.ToLower(strings.TrimSpace(tone)) for _, allowed := range PluginIconTones { if t == allowed { return true } } return false } // IsInlineSVG reports whether raw looks like inline SVG markup rather than a file path. func IsInlineSVG(raw string) bool { s := strings.TrimSpace(raw) return strings.HasPrefix(strings.ToLower(s), " markup. func SanitizePluginIconSVG(raw []byte) (string, error) { if len(raw) == 0 { return "", errors.New("empty svg") } if len(raw) > maxPluginIconSVGBytes { return "", fmt.Errorf("svg exceeds %d bytes", maxPluginIconSVGBytes) } text := string(raw) if reason := scanSVGActiveContent(text); reason != "" { return "", fmt.Errorf("svg contains forbidden active content (%s)", reason) } cleaned := strings.TrimSpace(string(SanitizeSVG(raw))) low := strings.ToLower(cleaned) start := strings.Index(low, "") if start < 0 || end < start { return "", errors.New("missing ... root element") } return strings.TrimSpace(cleaned[start : end+len("")]), nil } // ResolvePluginIcon produces a sanitized PluginIcon ready for UI rendering. func ResolvePluginIcon(icon *PluginIcon, installDir string, isBuiltin bool) *PluginIcon { if icon == nil { return nil } out := &PluginIcon{} if tone := strings.ToLower(strings.TrimSpace(icon.Tone)); IsValidPluginIconTone(tone) { out.Tone = tone } svg := strings.TrimSpace(icon.SVG) if svg != "" { if isBuiltin || IsInlineSVG(svg) { if cleaned, err := SanitizePluginIconSVG([]byte(svg)); err == nil { out.SVG = cleaned } } else if installDir != "" { if full, err := safeJoin(installDir, svg); err == nil { if info, err := os.Stat(full); err == nil && !info.IsDir() && info.Size() <= maxPluginIconSVGBytes { if data, err := os.ReadFile(full); err == nil { if cleaned, err := SanitizePluginIconSVG(data); err == nil { out.SVG = cleaned } } } } } } if out.Tone == "" && out.SVG == "" { return nil } return out }