Files
Reims-Go/internal/plugin/semver.go
Nwflower d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

277 lines
6.9 KiB
Go

package plugin
import (
"fmt"
"strconv"
"strings"
)
// SemVer is a parsed MAJOR.MINOR.PATCH semantic version.
type SemVer struct {
Major int
Minor int
Patch int
Pre string
}
// ParseSemVer parses "v1.2.3", "1.2", or "1.2.3-alpha.1".
func ParseSemVer(raw string) (SemVer, error) {
s := strings.TrimSpace(raw)
s = strings.TrimPrefix(s, "v")
if s == "" {
return SemVer{}, fmt.Errorf("empty version")
}
if idx := strings.IndexByte(s, '+'); idx >= 0 {
build := s[idx+1:]
if !validSemVerDotIdents(build) {
return SemVer{}, fmt.Errorf("invalid semver build metadata in %q", raw)
}
s = s[:idx]
}
var pre string
if idx := strings.IndexByte(s, '-'); idx >= 0 {
pre = s[idx+1:]
if !validSemVerDotIdents(pre) {
return SemVer{}, fmt.Errorf("invalid semver prerelease in %q", raw)
}
s = s[:idx]
}
parts := strings.Split(s, ".")
if len(parts) < 1 || len(parts) > 3 {
return SemVer{}, fmt.Errorf("invalid semver %q", raw)
}
nums := [3]int{}
for i, p := range parts {
n, err := strconv.Atoi(p)
if err != nil || n < 0 {
return SemVer{}, fmt.Errorf("invalid semver %q", raw)
}
nums[i] = n
}
return SemVer{Major: nums[0], Minor: nums[1], Patch: nums[2], Pre: pre}, nil
}
// validSemVerDotIdents checks RFC SemVer 2.0.0 dot-separated prerelease/build
// identifiers: non-empty, each segment non-empty and strictly [0-9A-Za-z-],
// which prevents path separators, "..", or whitespace from slipping into PkgDir.
func validSemVerDotIdents(s string) bool {
if s == "" {
return false
}
for _, seg := range strings.Split(s, ".") {
if seg == "" {
return false
}
for i := 0; i < len(seg); i++ {
c := seg[i]
ok := (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '-'
if !ok {
return false
}
}
}
return true
}
// Compare returns -1 if v < other, 0 if equal, +1 if v > other.
func (v SemVer) Compare(other SemVer) int {
if v.Major != other.Major {
if v.Major < other.Major {
return -1
}
return 1
}
if v.Minor != other.Minor {
if v.Minor < other.Minor {
return -1
}
return 1
}
if v.Patch != other.Patch {
if v.Patch < other.Patch {
return -1
}
return 1
}
if v.Pre == other.Pre {
return 0
}
// Release > prerelease.
if v.Pre == "" {
return 1
}
if other.Pre == "" {
return -1
}
if v.Pre < other.Pre {
return -1
}
return 1
}
// MatchesRange evaluates whether version satisfies a SemVer range expression.
// Supports empty/"*", "||" disjunctions, space/comma conjunctions, and
// operators: >=, <=, >, <, =, ^, ~.
func MatchesRange(version, expr string) bool {
expr = strings.TrimSpace(expr)
if expr == "" || expr == "*" {
return true
}
v, err := ParseSemVer(version)
if err != nil {
return false
}
for _, branch := range strings.Split(expr, "||") {
branch = strings.TrimSpace(branch)
if branch == "" {
continue
}
if matchesAndClauses(v, branch) {
return true
}
}
return false
}
// RangeRequiresAtLeast reports whether every version that satisfies expr is at
// least min. It is how a host tells that a plugin opted into a contract
// revision: "^1.3.0" and ">=1.3.0" do, "^1.1.0" and an empty range do not.
// An expression it cannot decide reports false.
func RangeRequiresAtLeast(expr, min string) bool {
want, err := ParseSemVer(min)
if err != nil {
return false
}
expr = strings.TrimSpace(expr)
if expr == "" || expr == "*" {
return false
}
for _, branch := range strings.Split(expr, "||") {
branch = strings.TrimSpace(branch)
if branch == "" {
continue
}
if !branchRequiresAtLeast(branch, want) {
return false
}
}
return true
}
// branchRequiresAtLeast reports whether one conjunction of constraints already
// forces a version of at least want.
func branchRequiresAtLeast(clause string, want SemVer) bool {
tokens := strings.Fields(strings.ReplaceAll(clause, ",", " "))
if len(tokens) == 0 {
return false
}
for _, tok := range tokens {
if lowerBoundAtLeast(tok, want) {
return true
}
}
return false
}
// lowerBoundAtLeast reports whether one constraint excludes every version
// below want. An upper bound constrains nothing below.
func lowerBoundAtLeast(tok string, want SemVer) bool {
tok = strings.TrimSpace(tok)
switch {
case tok == "" || tok == "*":
return false
case strings.HasPrefix(tok, "<="), strings.HasPrefix(tok, "<"):
return false
case strings.HasPrefix(tok, ">="):
tok = tok[2:]
case strings.HasPrefix(tok, ">"):
tok = tok[1:]
case strings.HasPrefix(tok, "="):
tok = tok[1:]
case strings.HasPrefix(tok, "^"), strings.HasPrefix(tok, "~"):
tok = tok[1:]
}
if strings.HasSuffix(tok, ".x") || strings.HasSuffix(tok, ".*") {
tok = strings.TrimSuffix(strings.TrimSuffix(tok, ".x"), ".*")
}
target, err := ParseSemVer(tok)
if err != nil {
return false
}
return target.Compare(want) >= 0
}
func matchesAndClauses(v SemVer, clause string) bool {
// Normalize comma separators into spaces.
norm := strings.ReplaceAll(clause, ",", " ")
tokens := strings.Fields(norm)
if len(tokens) == 0 {
return true
}
for _, tok := range tokens {
if !matchesSingleConstraint(v, tok) {
return false
}
}
return true
}
func matchesSingleConstraint(v SemVer, tok string) bool {
tok = strings.TrimSpace(tok)
if tok == "" || tok == "*" {
return true
}
switch {
case strings.HasPrefix(tok, ">="):
target, err := ParseSemVer(tok[2:])
return err == nil && v.Compare(target) >= 0
case strings.HasPrefix(tok, "<="):
target, err := ParseSemVer(tok[2:])
return err == nil && v.Compare(target) <= 0
case strings.HasPrefix(tok, ">"):
target, err := ParseSemVer(tok[1:])
return err == nil && v.Compare(target) > 0
case strings.HasPrefix(tok, "<"):
target, err := ParseSemVer(tok[1:])
return err == nil && v.Compare(target) < 0
case strings.HasPrefix(tok, "="):
target, err := ParseSemVer(tok[1:])
return err == nil && v.Compare(target) == 0
case strings.HasPrefix(tok, "^"):
target, err := ParseSemVer(tok[1:])
if err != nil || v.Compare(target) < 0 {
return false
}
if target.Major > 0 {
return v.Major == target.Major
}
if target.Minor > 0 {
return v.Major == 0 && v.Minor == target.Minor
}
return v.Major == 0 && v.Minor == 0 && v.Patch == target.Patch
case strings.HasPrefix(tok, "~"):
target, err := ParseSemVer(tok[1:])
if err != nil || v.Compare(target) < 0 {
return false
}
return v.Major == target.Major && v.Minor == target.Minor
default:
// Allow "1.x" or exact match.
if strings.HasSuffix(tok, ".x") || strings.HasSuffix(tok, ".*") {
prefix := strings.TrimSuffix(strings.TrimSuffix(tok, ".x"), ".*")
parts := strings.Split(strings.TrimPrefix(prefix, "v"), ".")
if len(parts) == 1 {
maj, err := strconv.Atoi(parts[0])
return err == nil && v.Major == maj
}
if len(parts) == 2 {
maj, e1 := strconv.Atoi(parts[0])
min, e2 := strconv.Atoi(parts[1])
return e1 == nil && e2 == nil && v.Major == maj && v.Minor == min
}
}
target, err := ParseSemVer(tok)
return err == nil && v.Compare(target) == 0
}
}