* feat(plugin): custom SVG icons, market stats, submit link, and native locale scaffold * chore(ci): allow spaces in PR model names and rename agent declaration to tool * fix(plugin): validate icon.svg size and markup during static scan
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
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 <svg> 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), "<svg")
|
|
}
|
|
|
|
// ValidatePluginIcon checks tone token and SVG path/markup constraints.
|
|
func ValidatePluginIcon(icon *PluginIcon, isBuiltin bool) error {
|
|
if icon == nil {
|
|
return nil
|
|
}
|
|
tone := strings.TrimSpace(icon.Tone)
|
|
if tone != "" && !IsValidPluginIconTone(tone) {
|
|
return fmt.Errorf("invalid icon.tone %q (allowed: %s)", icon.Tone, strings.Join(PluginIconTones, ", "))
|
|
}
|
|
svg := strings.TrimSpace(icon.SVG)
|
|
if svg == "" {
|
|
return nil
|
|
}
|
|
if isBuiltin {
|
|
if _, err := SanitizePluginIconSVG([]byte(svg)); err != nil {
|
|
return fmt.Errorf("invalid builtin icon.svg: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
if IsInlineSVG(svg) {
|
|
return errors.New("external plugin icon.svg must be a relative .svg file path inside the package, not inline markup")
|
|
}
|
|
if err := validateSafeManifestAsset(svg, ""); err != nil {
|
|
return fmt.Errorf("invalid icon.svg %q: %w", icon.SVG, err)
|
|
}
|
|
if !strings.EqualFold(path.Ext(svg), ".svg") {
|
|
return fmt.Errorf("invalid icon.svg %q: must be a .svg file", icon.SVG)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SanitizePluginIconSVG validates size and active content, then returns sanitized inline <svg> 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, "<svg")
|
|
end := strings.LastIndex(low, "</svg>")
|
|
if start < 0 || end < start {
|
|
return "", errors.New("missing <svg>...</svg> root element")
|
|
}
|
|
return strings.TrimSpace(cleaned[start : end+len("</svg>")]), 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
|
|
}
|