Files
Pulse/internal/api/unified_agent_validation.go
courtmanr@gmail.com 1d21be06b5 fix(agent): validate executable identity before activation
Substring version checks admitted stale agents and dependency literals, while
ARMv6 executables could select ARMv7 updates. Share compiler target and exact
release identity admission across producers, downloads and activation, and
keep validated local downloads bound to their open file descriptor.

Cover bounded runtime probes, failure atomicity, canonical executable formats,
ARM emulation and saved-state installer repair boundaries. Retain the installed
ARM fleet and physical-device gap, and leave release qualification open.
2026-11-05 18:22:58 +00:00

86 lines
2.8 KiB
Go

package api
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"github.com/rcourtman/pulse-go-rewrite/internal/agentidentity"
)
type agentArtifactChangeStamp struct{ seconds, nanoseconds int64 }
type unifiedAgentValidationEntry struct {
change agentArtifactChangeStamp
info os.FileInfo
version, target, checksum string
}
// Cache only artifact facts, including inode and filesystem change time. Expected release and
// target are part of the admission key. Authentication decisions are not cached.
func (r *Router) openValidatedUnifiedAgent(path, version, target string) (*os.File, os.FileInfo, string, error) {
file, err := os.Open(path)
if err != nil {
return nil, nil, "", err
}
keep := false
defer func() {
if !keep {
_ = file.Close()
}
}()
info, err := file.Stat()
if err != nil {
return nil, nil, "", err
}
if !info.Mode().IsRegular() {
return nil, nil, "", errors.New("agent artifact is not a regular file")
}
change, changeKnown := agentArtifactChangeTime(info)
if value, ok := r.unifiedAgentValidationCache.Load(path); ok && changeKnown {
entry := value.(unifiedAgentValidationEntry)
if entry.change == change && entry.version == version && entry.target == target && entry.info.Size() == info.Size() && entry.info.ModTime().Equal(info.ModTime()) && os.SameFile(entry.info, info) {
keep = true
return file, info, entry.checksum, nil
}
}
checksum, err := inspectUnifiedAgentBinary(file, info.Size(), version, target)
if err != nil {
return nil, nil, "", err
}
after, err := file.Stat()
if err != nil {
return nil, nil, "", err
}
afterChange, afterKnown := agentArtifactChangeTime(after)
if after.Size() != info.Size() || !after.ModTime().Equal(info.ModTime()) || (changeKnown && (!afterKnown || afterChange != change)) {
return nil, nil, "", errors.New("agent artifact changed during validation")
}
if changeKnown {
r.unifiedAgentValidationCache.Store(path, unifiedAgentValidationEntry{info: info, change: change, version: version, target: target, checksum: checksum})
}
keep = true
return file, info, checksum, nil
}
func inspectUnifiedAgentBinary(reader io.ReaderAt, size int64, version, target string) (string, error) {
if err := agentidentity.ValidateReader(reader, version, target); err != nil {
return "", err
}
hash := sha256.New()
canonical, legacy, err := scanUnifiedAgentBinaryContract(io.TeeReader(io.NewSectionReader(reader, 0, size), hash))
if err != nil {
return "", fmt.Errorf("scan binary contract: %w", err)
}
if legacy {
return "", fmt.Errorf("references deprecated host endpoint %s", legacyUnifiedAgentReportPath)
}
if !canonical {
return "", fmt.Errorf("missing canonical host endpoint %s", canonicalUnifiedAgentReportPath)
}
return hex.EncodeToString(hash.Sum(nil)), nil
}