From 83316fc98647efd278bbb633c6a737936fcb3bf0 Mon Sep 17 00:00:00 2001 From: kisfenyo Date: Sat, 14 Nov 2026 10:11:31 +0100 Subject: [PATCH] hub v0.181.0 (not deployed): root files are 'behind' only when the files differ (R-1392, decision 304); Go 1.27.4 (R-1391) --- REUSE.md | 1 + documentation/architecture/11-os-updates.md | 9 ++ hub/CHANGELOG.md | 24 ++++ hub/Dockerfile | 2 +- hub/cmd/hub/main.go | 13 ++ hub/go.mod | 2 +- hub/internal/bundlefiles/bundlefiles.go | 142 +++++++++++++++++++ hub/internal/bundlefiles/bundlefiles_test.go | 116 +++++++++++++++ hub/internal/gitea/gitea.go | 29 ++++ hub/internal/osupdates/bundle_alarm_test.go | 55 +++++++ hub/internal/osupdates/service.go | 7 + hub/internal/web/server.go | 6 + hub/internal/web/system.go | 17 ++- hub/internal/web/system_bundle_test.go | 27 +++- hub/internal/web/system_view.go | 3 +- 15 files changed, 442 insertions(+), 11 deletions(-) create mode 100644 hub/internal/bundlefiles/bundlefiles.go create mode 100644 hub/internal/bundlefiles/bundlefiles_test.go diff --git a/REUSE.md b/REUSE.md index 63f1aa51..bafac99a 100644 --- a/REUSE.md +++ b/REUSE.md @@ -112,6 +112,7 @@ | `(*Store).GetOperatorPasswordHash` / `SetOperatorPasswordHash` | hub/internal/store/store.go (~L1350) | `() string` / `(hash) error` | The DB-backed (hub_settings) operator login password override | Read via `Server.effectivePasswordHash()`, not directly. "" = no override (config seed authoritative). Store the bcrypt hash, never the plaintext. | | `(*Server).handleSetArtifacts` + `resolveArtifactSHA` | hub/internal/web/configs.go (~L644 / ~L680) | `POST /configuration/artifacts` | Operator UI to vouch artifact versions | With a Gitea client the sha is fetched AUTHORITATIVELY (submitted sha ignored); fetch failure refuses the save. Manual sha only in the no-creds fallback. | | `(*gitea.Client).ListVersions` / `FileSHA256` | hub/internal/gitea/gitea.go (~L47 / ~L72) | `(ctx, pkg) ([]string, error)` / `(ctx, pkg, ver, file)` | Read-only Gitea generic-package metadata | sha comes from package metadata — artifact bytes are never downloaded. Newest-semver-first sort. | +| `(*gitea.Client).FetchFile` + `(*bundlefiles.Resolver).Same` / `bundlefiles.FilesDigest` | hub/internal/gitea/gitea.go, hub/internal/bundlefiles/bundlefiles.go | `(ctx, pkg, ver, file, maxBytes) ([]byte, error)` / `(boxVer, boxSHA, vouchedVer, vouchedSHA) bool` | „Do two config bundles hold the same files under another label?” (R-1392) | The one place the hub downloads artifact bytes. Bytes count only when they hash to a sha256 the caller already trusts; anything unread answers false (the old „behind” verdict). Cached per sha256; a failed read is retried after 10 min. | | `(*Server).artifactChoices` | hub/internal/web/server.go (~L155) | `(ctx, pkg, file) []artifactChoice` | Version+sha dropdown data | nil Gitea client / unreachable → nil → UI degrades to manual entry. One bad version drops itself, not the list. | | `(*Handler).handleArtifactManifest` | hub/internal/api/handler.go (~L1550) | `GET /api/v1/artifacts/{id}` | Serving the vouched set to the bootstrap script | Auth mirrors handleConfigRetrieve exactly. Unset manifest = empty fields, not an error. | | `normalizeFloorInput` / `normalizeSHA256` | hub/internal/web/configs.go (~L27 / ~L627) | `(raw string) (string, bool)` | Validating operator-typed versions / shas | Empty string is VALID (means "clear"). Reuse for any new version/sha form field. | diff --git a/documentation/architecture/11-os-updates.md b/documentation/architecture/11-os-updates.md index 6efb6cc6..b4134ed4 100644 --- a/documentation/architecture/11-os-updates.md +++ b/documentation/architecture/11-os-updates.md @@ -354,6 +354,15 @@ an installed box by reinstall or by hand. operator (`runbooks/config-bundle.md`). - **Visibility.** System page "Root files" column; alarm `os_config_bundle_behind` after 7 days (`OS_ALARM_BUNDLE_BEHIND_AFTER`; decided by CC unattended — operator may reverse). +- **„Behind” is judged on the FILES, not on the bundle's label (hub v0.181.0, R-1392, `09` §3 decision 304).** The + bundle carries its `agent_version`, so its sha256 moves with every agent release — also when `configs/` did not + change and the release sends no bundle job (measured: 0.172.1 → 0.173.0, 32 files equal, two sha256s). When the box's + bundle sha256 differs from the vouched one, the hub fetches both bundles from the package registry, accepts each + only if its bytes hash to the sha256 already trusted (the vouched one; the one the box reports), and compares + everything but the label. Equal → not behind: no amber, no alarm, the „behind since” mark is cleared. Anything it + cannot read, verify or parse → the old verdict (behind). `hub/internal/bundlefiles`; pinned by `TestSame*`, + `TestBundleAlarm_R1392_*`, `TestBundleCell_R1392`. **Not covered:** a box whose bundle version was pruned from the + registry reads „behind” (true enough: it is older than the kept versions). - **Measured live** (`audits/r840-config-bundle-2026-10-04/partB/`): both demo boxes had every file equal to the release except `felhom-os-apply`; after the bootstrap the signed bundle wrote 0 of 22 (21 same, 1 setting kept), self-check ok, capability probe 71/71. demo-hp: a wrong-sha job refused (nothing changed); a bundle with one deliberate change diff --git a/hub/CHANGELOG.md b/hub/CHANGELOG.md index d6d9fa71..62883d70 100644 --- a/hub/CHANGELOG.md +++ b/hub/CHANGELOG.md @@ -1,3 +1,27 @@ +## v0.181.0 (2026-11-14) — R-1392: root files are „behind” only when the FILES differ; Go 1.27.4 (R-1391) — NOT DEPLOYED YET + +- **The defect (R-1392, found 2026-11-14).** The agent's config bundle carries its `agent_version`, so its sha256 + moves with every agent release. The hub compared that one sha256. Agent 0.173.0 changed no root file and sent no + bundle job, so all three test boxes read „root files behind the vouched agent's” from the vouch (2026-11-10), and + `os_config_bundle_behind` was due for each on 2026-11-17 — about files that were the vouched ones. +- **The ruling (`09` §3 decision 304, D81 = yes).** The hub must not call vouched files „behind”. +- **The fix (`internal/bundlefiles`, new).** When the two sha256s differ, the hub fetches both bundles from the package + registry (`gitea.Client.FetchFile`, new, 4 MB cap), accepts each only if its bytes hash to the sha256 already + trusted, and compares everything but `agent_version`. Equal → not behind, for the alarm + (`osupdates.Service.SameBundleFiles`) and for the System page's cell (`bundleCell`, whose title then says „the same + files … only the label differs”). A verified answer is cached for the process's life; a failed read is asked again + after 10 minutes. A version or sha256 that does not look like one never reaches the registry. +- **Fail direction.** No registry, a read error, a sha256 mismatch, no `files` list → „not the same” → the old verdict. +- **Tests, each seen red** (the fix line switched off): `TestBundleAlarm_R1392_SameFilesNewLabel` (no alarm after + 8 days, the mark cleared; different files still alarm), `TestBundleAlarm_R1392_NeverGotABundleStillAlarms` (through + the real resolver), `TestBundleCell_R1392`; `bundlefiles`: `TestSame`, `TestSame_UnverifiedBytesNeverCount`, + `TestSame_RegistryAway`, `TestFilesDigest_NoFilesIsAnError`. The two real bundles (0.172.1, 0.173.0) were compared by + hand: 32 files, equal but for the label. +- **Go 1.27.4 (R-1391, GO-2026-6859).** `go.mod` and the builder image + (`golang:1.27.4-alpine3.24@sha256:0a5f7fe7…`). Reach, read 2026-11-14: the hub has no `MaxBytesReader`, + `MaxBytesHandler` or `TimeoutHandler` at all, so no hub handler matches the advisory's crash pattern. +- Design: `documentation/architecture/11-os-updates.md` §5.4.2. + ## v0.180.0 (2026-11-10) — D68: a full host disk is the operator's alarm (image `sha256:768e73ab1ab502a8c3fe23a9d42cc038443a6456a1f40dcec853a2c588d04a8f` built from `97b7e471`, CI run 2616; the operator's yes 2026-11-10 15:05 UTC; the manifest bump is the commit that carries this line) - **The ruling (`09` §3 decision 287, R-1371).** A household is not mailed about a disk it cannot free. diff --git a/hub/Dockerfile b/hub/Dockerfile index 67aa5212..18187513 100644 --- a/hub/Dockerfile +++ b/hub/Dockerfile @@ -1,6 +1,6 @@ # R-972: a supported, patched Go and the current Alpine, pinned by version and digest (no moving tag). # upstream: osv:Go/stdlib -FROM golang:1.27.3-alpine3.24@sha256:b8c9b82affbbb82a020bbcf998dad2ba9516944ac7e883524642a481a9505e9c AS builder +FROM golang:1.27.4-alpine3.24@sha256:0a5f7fe796599ff487605987432e7619ae45beeb2fba4c2ce0b907879932aebf AS builder WORKDIR /app diff --git a/hub/cmd/hub/main.go b/hub/cmd/hub/main.go index c6a9feba..c99f1e31 100644 --- a/hub/cmd/hub/main.go +++ b/hub/cmd/hub/main.go @@ -17,6 +17,7 @@ import ( "gitea.dooplex.hu/admin/felhom-hub/internal/accesslog" "gitea.dooplex.hu/admin/felhom-hub/internal/api" "gitea.dooplex.hu/admin/felhom-hub/internal/assets" + "gitea.dooplex.hu/admin/felhom-hub/internal/bundlefiles" "gitea.dooplex.hu/admin/felhom-hub/internal/claim" "gitea.dooplex.hu/admin/felhom-hub/internal/dbsnap" "gitea.dooplex.hu/admin/felhom-hub/internal/gitea" @@ -497,6 +498,18 @@ func main() { logger.Printf("[ERROR] OS_APPROVE_NIGHTS=%q invalid — keeping 1", v) } } + // R-1392 (`09` §3 decision 304): a box whose config bundle holds the vouched FILES under an older label is not + // "behind". Needs the package registry; without it the old verdict stays (compare the bundle's sha256 only). + if giteaClient != nil { + bundleSame := &bundlefiles.Resolver{Logger: logger, Fetch: func(ctx context.Context, version string) ([]byte, error) { + return giteaClient.FetchFile(ctx, "felhom-agent", version, "felhom-config-bundle.json", 4<<20) + }} + osSvc.SameBundleFiles = bundleSame.Same + webServer.SetSameBundleFiles(bundleSame.Same) + logger.Printf("[INFO] Root files: bundles are compared by their files (R-1392)") + } else { + logger.Printf("[WARN] Root files: no package registry — bundles are compared by sha256 only (R-1392 not active)") + } osSvc.DockerNights = 2 if v := os.Getenv("OS_DOCKER_APPROVE_NIGHTS"); v != "" { if n, nerr := strconv.Atoi(v); nerr == nil && n >= 0 { diff --git a/hub/go.mod b/hub/go.mod index d01a6243..cfca096d 100644 --- a/hub/go.mod +++ b/hub/go.mod @@ -1,6 +1,6 @@ module gitea.dooplex.hu/admin/felhom-hub -go 1.27.3 +go 1.27.4 require ( golang.org/x/crypto v0.58.0 diff --git a/hub/internal/bundlefiles/bundlefiles.go b/hub/internal/bundlefiles/bundlefiles.go new file mode 100644 index 00000000..22cd4b5c --- /dev/null +++ b/hub/internal/bundlefiles/bundlefiles.go @@ -0,0 +1,142 @@ +// Package bundlefiles answers one question for the "root files" verdict (R-1392, `09` §3 decision 304): do two config +// bundles hold THE SAME FILES under a different label? +// +// WHY. The agent's config bundle (`felhom-config-bundle.json`, `11` §5.4.2) carries its own `agent_version`, so its +// sha256 moves with every agent release — also when `configs/` did not change and the release (rightly) sent no bundle +// job. The hub compared that one sha256, so every such release turned every box amber and, 7 days later, mailed +// `os_config_bundle_behind` about files that were the vouched ones (measured 2026-11-14: 0.172.1 vs 0.173.0). +// +// HOW. A bundle is fetched from the package registry by its version, and it counts ONLY when the bytes hash to the +// sha256 the caller already trusts (the vouched one, or the one the box reports). Then everything but the label +// (`agent_version`) is hashed: the format and the whole `files` list (path, mode, check, policy, sha256, content). +// +// FAIL DIRECTION. Anything that cannot be read, verified or parsed answers "not the same" — the old verdict (behind, +// amber, the alarm after its wait). A registry that is away never makes a box look current. +package bundlefiles + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "log" + "regexp" + "sync" + "time" +) + +// Fetcher returns the published bundle file of one agent version. +type Fetcher func(ctx context.Context, version string) ([]byte, error) + +var ( + versionRE = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$`) // the builder's own rule + shaRE = regexp.MustCompile(`^[0-9a-f]{64}$`) +) + +const ( + retryAfter = 10 * time.Minute // a failed read is not asked again sooner (the System page calls this too) + fetchTimeout = 5 * time.Second +) + +type entry struct { + digest string // "" = failed + at time.Time +} + +// Resolver caches the files digest per bundle sha256. Safe for concurrent use. +type Resolver struct { + Fetch Fetcher + Logger *log.Logger + Now func() time.Time + + mu sync.Mutex + cache map[string]entry +} + +func (r *Resolver) now() time.Time { + if r.Now != nil { + return r.Now() + } + return time.Now() +} + +func (r *Resolver) logf(format string, a ...any) { + if r.Logger != nil { + r.Logger.Printf(format, a...) + } +} + +// FilesDigest hashes a bundle's content without its label. It errors on a bundle with no `files` list. +func FilesDigest(bundle []byte) (string, error) { + var body map[string]any + if err := json.Unmarshal(bundle, &body); err != nil { + return "", fmt.Errorf("not JSON: %w", err) + } + files, ok := body["files"].([]any) + if !ok || len(files) == 0 { + return "", errors.New("no files list") + } + delete(body, "agent_version") + canon, err := json.Marshal(body) // map keys are sorted by encoding/json + if err != nil { + return "", err + } + sum := sha256.Sum256(canon) + return hex.EncodeToString(sum[:]), nil +} + +func (r *Resolver) digest(version, sha string) string { + if r == nil || r.Fetch == nil || !versionRE.MatchString(version) || !shaRE.MatchString(sha) { + return "" + } + r.mu.Lock() + if e, ok := r.cache[sha]; ok && (e.digest != "" || r.now().Sub(e.at) < retryAfter) { + r.mu.Unlock() + return e.digest + } + r.mu.Unlock() + + ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout) + defer cancel() + d := "" + data, err := r.Fetch(ctx, version) + switch { + case err != nil: + r.logf("[WARN] [bundlefiles] bundle %s: cannot be read (%v) — its files are not compared", version, err) + default: + sum := sha256.Sum256(data) + if got := hex.EncodeToString(sum[:]); got != sha { + r.logf("[WARN] [bundlefiles] bundle %s: the published file is sha256 %s, expected %s — its files are not compared", version, got, sha) + } else if d, err = FilesDigest(data); err != nil { + r.logf("[WARN] [bundlefiles] bundle %s: %v — its files are not compared", version, err) + d = "" + } + } + r.mu.Lock() + if r.cache == nil { + r.cache = map[string]entry{} + } + r.cache[sha] = entry{digest: d, at: r.now()} + r.mu.Unlock() + return d +} + +// Same reports whether the box's bundle and the vouched bundle hold the same files. Equal sha256s are the caller's +// business (nothing is fetched for them); false on anything unread. +func (r *Resolver) Same(boxVersion, boxSHA, vouchedVersion, vouchedSHA string) bool { + if boxSHA == vouchedSHA { + return boxSHA != "" + } + a := r.digest(vouchedVersion, vouchedSHA) + if a == "" { + return false + } + b := r.digest(boxVersion, boxSHA) + same := b != "" && a == b + if same { + r.logf("[DEBUG] [bundlefiles] bundle %s holds the same files as the vouched %s (only the label differs)", boxVersion, vouchedVersion) + } + return same +} diff --git a/hub/internal/bundlefiles/bundlefiles_test.go b/hub/internal/bundlefiles/bundlefiles_test.go new file mode 100644 index 00000000..6e828147 --- /dev/null +++ b/hub/internal/bundlefiles/bundlefiles_test.go @@ -0,0 +1,116 @@ +package bundlefiles + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "testing" + "time" +) + +func bundle(version, wrapperSHA string) []byte { + return []byte(`{ + "agent_version": "` + version + `", + "files": [ + {"check": "", "content_b64": "QQ==", "mode": "0o755", "path": "/usr/local/sbin/felhom-os-apply", "policy": "replace", "sha256": "` + wrapperSHA + `"} + ], + "format": 1 +} +`) +} + +func sha(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) } + +type reg struct { + files map[string][]byte + calls int + err error +} + +func (g *reg) fetch(_ context.Context, v string) ([]byte, error) { + g.calls++ + if g.err != nil { + return nil, g.err + } + b, ok := g.files[v] + if !ok { + return nil, errors.New("404") + } + return b, nil +} + +// R-1392: the three verdicts, and the fail direction. +func TestSame(t *testing.T) { + old, relabel, changed := bundle("0.172.1", "aa"), bundle("0.173.0", "aa"), bundle("0.174.0", "bb") + g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel, "0.174.0": changed}} + r := &Resolver{Fetch: g.fetch} + + if sha(old) == sha(relabel) { + t.Fatal("fixture: the label must move the bundle's sha256") + } + if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) { + t.Fatal("same files under a new label must be the same") + } + if r.Same("0.172.1", sha(old), "0.174.0", sha(changed)) { + t.Fatal("one file differs: must NOT be the same") + } + if r.Same("none", "", "0.173.0", sha(relabel)) { + t.Fatal("a box that never got a bundle must NOT be the same") + } + if r.Same("", "", "", "") { + t.Fatal("nothing vouched and nothing on the box is not 'the same'") + } + // The cache: every bundle was read once. + n := g.calls + r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) + if g.calls != n { + t.Fatalf("a verified bundle was read again (%d -> %d)", n, g.calls) + } +} + +// A bundle counts only when its bytes hash to the sha256 the caller trusts. +func TestSame_UnverifiedBytesNeverCount(t *testing.T) { + old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa") + g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}} + r := &Resolver{Fetch: g.fetch} + wrong := sha([]byte("something else")) + if r.Same("0.172.1", wrong, "0.173.0", sha(relabel)) { + t.Fatal("the box's sha256 does not match the published file: must NOT be the same") + } + if r.Same("0.172.1", sha(old), "0.173.0", wrong) { + t.Fatal("the vouched sha256 does not match the published file: must NOT be the same") + } + if r.Same("../../x", sha(old), "0.173.0", sha(relabel)) { + t.Fatal("a version that is not a version must never reach the registry") + } +} + +// A registry that is away answers "not the same", is not hammered, and is asked again later. +func TestSame_RegistryAway(t *testing.T) { + old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa") + g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}, err: errors.New("dial tcp: refused")} + now := time.Date(2026, 11, 14, 10, 0, 0, 0, time.UTC) + r := &Resolver{Fetch: g.fetch, Now: func() time.Time { return now }} + if r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) { + t.Fatal("unreadable must NOT be the same") + } + n := g.calls + r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) + if g.calls != n { + t.Fatal("a failed read was retried at once") + } + g.err = nil + now = now.Add(11 * time.Minute) + if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) { + t.Fatal("the registry is back after the retry wait: must be the same") + } +} + +func TestFilesDigest_NoFilesIsAnError(t *testing.T) { + for _, b := range []string{`{}`, `{"files":[]}`, `{"files":"x"}`, `not json`} { + if _, err := FilesDigest([]byte(b)); err == nil { + t.Fatalf("%q: no error", b) + } + } +} diff --git a/hub/internal/gitea/gitea.go b/hub/internal/gitea/gitea.go index ea8ec8db..eb939a89 100644 --- a/hub/internal/gitea/gitea.go +++ b/hub/internal/gitea/gitea.go @@ -177,6 +177,35 @@ func (c *Client) getJSON(ctx context.Context, url string, out interface{}) error return json.NewDecoder(resp.Body).Decode(out) } +// FetchFile downloads one file of a generic package version, refusing a body above maxBytes (R-1392: the agent's config +// bundle, so the hub can compare the FILES of two bundles). The caller verifies the bytes against a sha256 it trusts. +func (c *Client) FetchFile(ctx context.Context, pkg, version, file string, maxBytes int64) ([]byte, error) { + url := fmt.Sprintf("%s/api/packages/%s/generic/%s/%s/%s", c.baseURL, c.owner, pkg, version, file) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + if c.token != "" { + req.SetBasicAuth(c.user, c.token) + } + resp, err := c.http.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, &StatusError{URL: url, Code: resp.StatusCode} + } + data, err := io.ReadAll(io.LimitReader(resp.Body, maxBytes+1)) + if err != nil { + return nil, err + } + if int64(len(data)) > maxBytes { + return nil, fmt.Errorf("gitea GET %s: larger than %d bytes", url, maxBytes) + } + return data, nil +} + // compareSemver returns >0 if a>b, 0 if equal, <0 if a