// Package storepkg defines and validates declarative Archie store packages. package storepkg import ( "bytes" "errors" "fmt" "io" "path" "regexp" "strings" "gopkg.in/yaml.v3" ) const APIVersion = "dev.archie.package.v1" var digestPattern = regexp.MustCompile(`^sha256:[a-f0-9]{64}$`) // Descriptor is the dev.archie.package.v1 manifest annotation payload. type Descriptor struct { APIVersion string `yaml:"apiVersion"` DisplayName string `yaml:"displayName"` Description string `yaml:"description"` Version string `yaml:"version"` Contributes Contributions `yaml:"contributes"` Requires []PackageRef `yaml:"requires"` Authority Authority `yaml:"authority"` Files []File `yaml:"files"` // Settings are what an operator enters for the package's extensions to // run, which the dashboard renders as a form. Settings []Setting `yaml:"settings"` } // Setting is one value an extension reads from its operator-entered settings. // A secret setting holds an engine:key reference, never the secret, and its // key ends in "_ref" so the host resolves it before the extension starts. type Setting struct { Key string `yaml:"key" json:"key"` Label string `yaml:"label" json:"label"` Description string `yaml:"description" json:"description,omitempty"` Secret bool `yaml:"secret" json:"secret,omitempty"` Required bool `yaml:"required" json:"required,omitempty"` // Type is how the value is entered and checked; empty means a string. Type string `yaml:"type" json:"type,omitempty"` // Options are the allowed values of a select setting. Options []string `yaml:"options" json:"options,omitempty"` } // SettingType names how a setting's value is entered and validated. type SettingType string // Setting types. The empty type is a string. const ( SettingString SettingType = "string" SettingNumber SettingType = "number" SettingBoolean SettingType = "boolean" SettingDuration SettingType = "duration" SettingList SettingType = "list" SettingSelect SettingType = "select" // SettingText is a string entered over several lines. SettingText SettingType = "text" ) // Kind returns the setting's type with the default applied. func (s Setting) Kind() SettingType { if s.Type == "" { return SettingString } return SettingType(s.Type) } func (s Setting) validateType() error { switch s.Kind() { case SettingString, SettingNumber, SettingBoolean, SettingDuration, SettingList, SettingSelect, SettingText: default: return fmt.Errorf("setting %q: unknown type %q", s.Key, s.Type) } if s.Secret && s.Kind() != SettingString { return fmt.Errorf("setting %q: a secret setting is a string", s.Key) } if s.Required && s.Kind() == SettingBoolean { return fmt.Errorf("setting %q: a boolean setting cannot be required, false counts as filled", s.Key) } if s.Kind() != SettingSelect { if len(s.Options) > 0 { return fmt.Errorf("setting %q: options belong to a select setting", s.Key) } return nil } if len(s.Options) == 0 { return fmt.Errorf("setting %q: a select setting needs options", s.Key) } seen := make(map[string]bool, len(s.Options)) for _, option := range s.Options { if option == "" || seen[option] { return fmt.Errorf("setting %q: options must be non-empty and unique", s.Key) } seen[option] = true } return nil } var settingKeyPattern = regexp.MustCompile(`^[a-z][a-z0-9_]*$`) // MissingSettings returns the labels of the required settings values lacks. func (d Descriptor) MissingSettings(values map[string]string) []string { var missing []string for _, setting := range d.Settings { if setting.Required && strings.TrimSpace(values[setting.Key]) == "" { missing = append(missing, setting.Label) } } return missing } func (d Descriptor) validateSettings() error { seen := make(map[string]bool, len(d.Settings)) for i, setting := range d.Settings { switch { case !settingKeyPattern.MatchString(setting.Key): return fmt.Errorf("setting %d: key %q must be lower-case letters, digits and underscores", i, setting.Key) case seen[setting.Key]: return fmt.Errorf("setting %q is declared twice", setting.Key) case strings.TrimSpace(setting.Label) == "": return fmt.Errorf("setting %q: label is required", setting.Key) case setting.Secret != strings.HasSuffix(setting.Key, "_ref"): return fmt.Errorf("setting %q: a secret setting's key ends in _ref, and only a secret's does", setting.Key) } if err := setting.validateType(); err != nil { return err } seen[setting.Key] = true } return nil } // Contributions names the declarative families supplied by a package. type Contributions struct { Workflows []string `yaml:"workflows"` Prompts []string `yaml:"prompts"` Skills []string `yaml:"skills"` MCPServers []string `yaml:"mcpServers"` Defaults []string `yaml:"defaults"` Playbooks []string `yaml:"playbooks"` Profiles []string `yaml:"profiles"` // Extensions are executable plugins the host launches as processes. They // are the only package files allowed to carry an executable mode. Extensions []Extension `yaml:"extensions"` } // Extension is one plugin binary in the package layer and the gRPC surface it // serves. type Extension struct { Surface string `yaml:"surface"` Path string `yaml:"path"` } // Surfaces are the extension surfaces the host defines a contract for. const ( SurfaceSecretEngine = "secretengine" SurfaceForge = "forge" SurfaceChannel = "channel" ) func validSurface(surface string) bool { return surface == SurfaceSecretEngine || surface == SurfaceForge || surface == SurfaceChannel } // PackageRef identifies a required package. References are pinned to // immutable content digests so dependency resolution cannot drift by tag. type PackageRef struct { Name string `yaml:"name"` Digest string `yaml:"digest"` } // Authority lists the grants the package needs to operate. type Authority struct { CredentialServices []string `yaml:"credentialServices"` EgressHosts []string `yaml:"egressHosts"` ForgePermissions []string `yaml:"forgePermissions"` Triggers []string `yaml:"triggers"` Tools []string `yaml:"tools"` // Env names the host environment variables an extension process may read. Env []string `yaml:"env"` } // File describes one path carried in the package layer and its Unix mode. type File struct { Path string `yaml:"path"` Mode uint32 `yaml:"mode"` } // Decode strictly decodes one YAML document and validates the descriptor. // Unknown fields and additional YAML documents are errors. func Decode(r io.Reader) (Descriptor, error) { data, err := io.ReadAll(r) if err != nil { return Descriptor{}, fmt.Errorf("read package descriptor: %w", err) } decoder := yaml.NewDecoder(bytes.NewReader(data)) decoder.KnownFields(true) var descriptor Descriptor if err := decoder.Decode(&descriptor); err != nil { return Descriptor{}, fmt.Errorf("decode package descriptor: %w", err) } var extra yaml.Node if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) { if err != nil { return Descriptor{}, fmt.Errorf("decode trailing package descriptor: %w", err) } return Descriptor{}, fmt.Errorf("decode package descriptor: multiple YAML documents are not allowed") } if err := descriptor.Validate(); err != nil { return Descriptor{}, err } return descriptor, nil } // Validate checks descriptor identity, references, authority values, and // that every listed package file remains declarative and non-executable, except // the binaries a package declares as extensions. func (d Descriptor) Validate() error { if d.APIVersion != APIVersion { return fmt.Errorf("apiVersion must be %q", APIVersion) } if strings.TrimSpace(d.DisplayName) == "" { return fmt.Errorf("display name is required") } if strings.TrimSpace(d.Version) == "" { return fmt.Errorf("version is required") } if !d.hasContribution() { return fmt.Errorf("at least one package contribution is required") } for i, ref := range d.Requires { if strings.TrimSpace(ref.Name) == "" { return fmt.Errorf("required package %d: name is required", i) } if !digestPattern.MatchString(ref.Digest) { return fmt.Errorf("required package %q digest must be a sha256 digest", ref.Name) } } if err := d.Authority.Validate(); err != nil { return err } if err := d.validateSettings(); err != nil { return err } executable, err := d.validateExtensions() if err != nil { return err } for i, file := range d.Files { if err := validateFile(file, executable[file.Path]); err != nil { return fmt.Errorf("package file %d: %w", i, err) } } return nil } // validateExtensions checks each extension names a known surface and a // declared, owner-executable file, and returns the set of extension paths. func (d Descriptor) validateExtensions() (map[string]bool, error) { modes := make(map[string]uint32, len(d.Files)) for _, file := range d.Files { modes[file.Path] = file.Mode } paths := make(map[string]bool, len(d.Contributes.Extensions)) for i, extension := range d.Contributes.Extensions { if !validSurface(extension.Surface) { return nil, fmt.Errorf("extension %d: unknown surface %q", i, extension.Surface) } mode, ok := modes[extension.Path] if !ok { return nil, fmt.Errorf("extension %d: %q is not a declared package file", i, extension.Path) } if mode&0o100 == 0 { return nil, fmt.Errorf("extension %d: %q must be owner-executable", i, extension.Path) } if paths[extension.Path] { return nil, fmt.Errorf("extension %d: %q is listed twice", i, extension.Path) } paths[extension.Path] = true } return paths, nil } func (d Descriptor) hasContribution() bool { return len(d.Contributes.Workflows)+len(d.Contributes.Prompts)+len(d.Contributes.Skills)+ len(d.Contributes.MCPServers)+len(d.Contributes.Defaults)+len(d.Contributes.Playbooks)+ len(d.Contributes.Profiles)+len(d.Contributes.Extensions) > 0 } func validForgePermission(permission string) bool { switch permission { case "read", "comment", "review", "push", "open_pr": return true default: return false } } func validateFile(file File, extension bool) error { clean := path.Clean(file.Path) if file.Path == "" || clean == "." || clean == ".." || strings.HasPrefix(clean, "../") || path.IsAbs(file.Path) { return fmt.Errorf("path must be a relative package path") } if extension { return nil } if file.Mode&0o111 != 0 { return fmt.Errorf("executable file modes are not allowed") } if isHostCodePath(clean) { return fmt.Errorf("host code is not declarative package content") } return nil } func isHostCodePath(file string) bool { ext := strings.ToLower(path.Ext(file)) switch ext { case ".go", ".sh", ".bash", ".zsh", ".fish", ".ksh", ".csh", ".py", ".pyw", ".rb", ".pl", ".pm", ".php", ".lua", ".tcl", ".r", ".js", ".mjs", ".cjs", ".ts", ".bat", ".cmd", ".ps1": return true default: return false } }