fix(client): harden label pinning and close review findings

- Resolve the Windows admin config via the ProgramData known folder
  instead of the user-controlled ProgramData variable, so a user cannot
  redirect the admin lookup and drop pinned labels
- vendor rename rewrites matching agent-lib.labels.json entries, so a
  renamed item keeps its labels instead of going to every client
- validate checks the labels file in repositories without a lockfile
- A kept modified copy of a deselected item is no longer drift in status,
  and the toast explains it as "no longer assigned to you"
- Labels parser rejects trailing data such as an extra closing brace
- Tests: admin pin wiring (config chain, --repo, admin as chosen config),
  reselect redeploy with byte-level no-op, status labels JSON, validate
  without lockfile, rename keeps labels
- README: document unreadable admin config, drift semantics, rename

Refs: agent_lib-fh4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
m3ta-chironandClaude Opus 5.5 committed 2026-11-14 11:00:41 +01:00
1 parent 59735c3b24
commit ae316860e5
17 files changed
+296 -17

No files matched your search

+3 -5
View File
@@ -38,13 +38,11 @@ const (
)
// DefaultConfigPath returns the admin-protected config location per platform:
// ProgramData on Windows, /etc on Linux/macOS.
// the ProgramData known folder on Windows (resolved via the shell API, not
// the user-controlled environment variable), /etc on Linux/macOS.
func DefaultConfigPath() string {
if runtime.GOOS == "windows" {
if pd := os.Getenv("ProgramData"); pd != "" {
return filepath.Join(pd, "agent-lib", "config.json")
}
return `C:\ProgramData\agent-lib\config.json`
return filepath.Join(programDataDir(), "agent-lib", "config.json")
}
return "/etc/agent-lib/config.json"
}
+35
View File
@@ -263,3 +263,38 @@ func TestAdminConfigBrokenIsFatal(t *testing.T) {
t.Fatal("an unreadable admin config must abort: its label intent is unknown")
}
}
func TestResolveRunConfigAppliesAdminPin(t *testing.T) {
dir := t.TempDir()
admin := filepath.Join(dir, "admin.json")
writeRaw(t, admin, `{"repo_url":"r","labels":["finance"]}`)
orig := adminConfigPath
t.Cleanup(func() { adminConfigPath = orig })
adminConfigPath = func() string { return admin }
user := filepath.Join(dir, "user.json")
writeRaw(t, user, `{"repo_url":"r","labels":["sales","it"]}`)
cfg, err := resolveRunConfig(SyncOptions{ConfigPath: user})
if err != nil {
t.Fatal(err)
}
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
t.Errorf("config-chain run: admin pin not applied, got %v (%s)", *cfg.Labels, cfg.LabelSource)
}
cfg, err = resolveRunConfig(SyncOptions{ConfigPath: user, RepoURL: "other"})
if err != nil {
t.Fatal(err)
}
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
t.Errorf("--repo run: admin pin not applied, got %v (%s)", *cfg.Labels, cfg.LabelSource)
}
cfg, err = resolveRunConfig(SyncOptions{ConfigPath: admin})
if err != nil {
t.Fatal(err)
}
if cfg.LabelSource != "admin" {
t.Errorf("admin config as chosen config: source = %q", cfg.LabelSource)
}
}
+7
View File
@@ -35,6 +35,7 @@ type SyncReport struct {
func ExecutePlan(tree *gitsource.GitTree, plan []PlanEntry, manifest *Manifest, paths StatePaths, rev string, stdout io.Writer) (*SyncReport, error) {
report := &SyncReport{Revision: rev, Manifest: paths.Manifest}
var warnings []string
labelSkips := 0
for _, e := range plan {
key := manifestKey(e.Type, e.Name)
@@ -62,6 +63,9 @@ func ExecutePlan(tree *gitsource.GitTree, plan []PlanEntry, manifest *Manifest,
case ActSkip:
warnings = append(warnings, fmt.Sprintf("SKIP %s/%s: %s", e.Type, e.Name, e.Reason))
report.Skipped++
if IsLabelSkip(e) {
labelSkips++
}
case ActLeave:
warnings = append(warnings, fmt.Sprintf("LEAVE %s/%s: %s", e.Type, e.Name, e.Reason))
report.Left++
@@ -79,6 +83,9 @@ func ExecutePlan(tree *gitsource.GitTree, plan []PlanEntry, manifest *Manifest,
}
title := "agent-lib sync"
body := fmt.Sprintf("%d item(s) kept local: modifications blocked company updates. See the sync log for details.", report.Skipped+report.Left)
if labelSkips > 0 && labelSkips == report.Skipped && report.Left == 0 {
body = fmt.Sprintf("%d item(s) kept local: no longer assigned to you, but you modified them. See the sync log for details.", labelSkips)
}
if err := Notifier.Send(title, body); err != nil {
fmt.Fprintf(stdout, "warning: notification failed: %v\n", err)
}
+11 -1
View File
@@ -1,6 +1,9 @@
package client
import "sort"
import (
"sort"
"strings"
)
// Action is one step of a sync plan.
type Action string
@@ -20,6 +23,13 @@ const (
// ReasonNotSelected explains removals caused by the client's labels.
const ReasonNotSelected = "not selected for this client's labels"
// IsLabelSkip reports whether a skip keeps a modified copy of an item that
// is no longer selected for this client's labels. Such a copy blocks no
// repository update, so it is not drift.
func IsLabelSkip(e PlanEntry) bool {
return e.Action == ActSkip && strings.HasPrefix(e.Reason, ReasonNotSelected)
}
// PlanOptions tune planning. Force overwrites/removes locally modified
// managed items; Deselected holds manifest keys filtered out by labels.
type PlanOptions struct {
+6
View File
@@ -0,0 +1,6 @@
//go:build !windows
package client
// programDataDir is only meaningful on Windows.
func programDataDir() string { return "" }
+16
View File
@@ -0,0 +1,16 @@
//go:build windows
package client
import "golang.org/x/sys/windows"
// programDataDir resolves the ProgramData known folder through the shell
// API instead of the ProgramData environment variable: the variable is
// user-controlled, and the admin config pins labels — a user must not be
// able to point the admin lookup at a folder of their own.
func programDataDir() string {
if p, err := windows.KnownFolderPath(windows.FOLDERID_ProgramData, 0); err == nil && p != "" {
return p
}
return `C:\ProgramData`
}
+3
View File
@@ -51,6 +51,9 @@ func Status(opts SyncOptions) (*StatusReport, error) {
if rec := p.manifest.Items[manifestKey(e.Type, e.Name)]; rec != nil && rec.Hash == e.NewHash {
blocked = false
}
if IsLabelSkip(e) {
blocked = false
}
if blocked {
report.Drift = true
}
+6 -2
View File
@@ -37,6 +37,10 @@ type prepared struct {
labels *LabelSelection
}
// adminConfigPath locates the admin default config for label pinning;
// tests swap it to exercise the pin end to end.
var adminConfigPath = DefaultConfigPath
// LabelSelection describes an active label filter for one run: the client
// labels, where they came from, which items were left out and which names
// in the labels file match no item. nil when the client carries no labels.
@@ -171,7 +175,7 @@ func resolveRunConfig(opts SyncOptions) (*ClientConfig, error) {
return nil, err
}
cfg.FillTokenAuth()
if err := ApplyAdminLabels(cfg, DefaultConfigPath()); err != nil {
if err := ApplyAdminLabels(cfg, adminConfigPath()); err != nil {
return nil, err
}
return cfg, nil
@@ -188,7 +192,7 @@ func resolveRunConfig(opts SyncOptions) (*ClientConfig, error) {
cfg.Ref = opts.Ref
}
cfg.FillTokenAuth()
if err := ApplyAdminLabels(cfg, DefaultConfigPath()); err != nil {
if err := ApplyAdminLabels(cfg, adminConfigPath()); err != nil {
return nil, err
}
return cfg, nil