| new file mode 100644 |
| index 000000000..a905d02d0 |
| --- /dev/null |
| +++ b/internal/release/hunt_H3-M1_signedhead_test.go |
| @@ -0,0 +1,116 @@ |
| +package release |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// huntSignedHeadBox is the fresh box named-unit recovery runs on |
| +// (internal/host/namedrecovery.go, namedrecovery_active.go): an empty opt |
| +// dir, empty ratchet dirs, the require flag off, and a route that names the |
| +// account the way the host's unit binding does. It ships and promotes v1 |
| +// with the account's digest marker off (the default), so the signed head |
| +// carries no digest and a promotion record with the shipped manifest's |
| +// digest is the only binding. |
| +func huntSignedHeadBox(t *testing.T) (ctx context.Context, s Store, pub []byte, rat *DigestRatchet, opt string, rt registry.Route) { |
| + t.Helper() |
| + ctx = context.Background() |
| + s = NewDirStore(t.TempDir()) |
| + priv, pub := testKeys(t) |
| + opt = t.TempDir() |
| + shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("the bytes the publisher shipped")) |
| + if err := Promote(ctx, s, priv, "bab", "km", "canary/x", "v1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + p, err := Channel(ctx, s, pub, "bab", "km", "canary/x") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if p.Digest != "" || p.CarriesDigests() { |
| + t.Fatalf("setup: marker off still produced a digest %q", p.Digest) |
| + } |
| + rec, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1") |
| + if err != nil || rec.ManifestDigest != manifestDigestOf(t, s, "bab", "km", "v1") { |
| + t.Fatalf("setup: promotion record %+v, %v", rec, err) |
| + } |
| + rat = NewDigestRatchet(filepath.Join(t.TempDir(), "root-marks"), []string{filepath.Join(t.TempDir(), "edge-marks")}, false) |
| + rt = registry.Route{Host: "a.example.com", Kind: "instance", App: "km", Channel: "canary/x"} |
| + rt.Account = "bab" |
| + return ctx, s, pub, rat, opt, rt |
| +} |
| + |
| +// H3-M1 sibling path: the adopt pass binds a digestless head to its |
| +// promotion record (stagePointerHead), but StageSignedHead, which named-unit |
| +// recovery calls to restage the head on a box, is a second way to stage the |
| +// same pointer. A bucket writer swaps manifest+artifact after promotion; the |
| +// signed pointer is untouched; a fresh box has no retained copy. SECURE |
| +// expectation: StageSignedHead refuses, and the swapped bytes are never |
| +// written under the version. |
| +func TestHunt_H3_M1_SignedHeadRefusesManifestSwap(t *testing.T) { |
| + ctx, s, pub, rat, opt, rt := huntSignedHeadBox(t) |
| + evil := []byte("bytes nobody promoted") |
| + huntReplaceRelease(t, s, "bab", "km", "v1", evil) |
| + |
| + var out bytes.Buffer |
| + var p *ChannelPointer |
| + var m *Manifest |
| + var err error |
| + withCapturedStdout(t, &out, func() { |
| + p, m, err = StageSignedHead(ctx, s, pub, rat, opt, rt) |
| + }) |
| + staged, rerr := os.ReadFile(VersionPath(opt, "bab", "v1", "km")) |
| + if err == nil || rerr == nil || bytes.Equal(staged, evil) { |
| + t.Fatalf("H3-M1 (StageSignedHead): digestless signed head admitted substituted bytes: err=%v ptr=%+v manifest=%+v staged=%q (a signed promotion record with the real manifest digest exists but was not consulted)\n%s", |
| + err, p, m, staged, out.String()) |
| + } |
| +} |
| + |
| +// A head promoted before records existed, or whose record a writer removed, |
| +// has nothing to bind its bytes: a fresh box refuses it rather than staging |
| +// whatever the bucket holds. (`carlos releases authorize` writes the record.) |
| +func TestHunt_H3_M1_SignedHeadRefusesMissingRecord(t *testing.T) { |
| + ctx, s, pub, rat, opt, rt := huntSignedHeadBox(t) |
| + key := PromotionRecordKey("bab", "km", "v1") |
| + _, etag, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.(*DirStore).DeleteIfMatch(ctx, key, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + var out bytes.Buffer |
| + withCapturedStdout(t, &out, func() { |
| + _, _, err = StageSignedHead(ctx, s, pub, rat, opt, rt) |
| + }) |
| + if _, rerr := os.ReadFile(VersionPath(opt, "bab", "v1", "km")); err == nil || rerr == nil { |
| + t.Fatalf("H3-M1 (StageSignedHead): digestless head with no promotion record was staged on a fresh box: err=%v\n%s", err, out.String()) |
| + } |
| +} |
| + |
| +// Control: the untouched head still stages through StageSignedHead on a |
| +// fresh box, bound to the record, and comes back with its binary manifest. |
| +func TestHunt_H3_M1_SignedHeadStagesUnswappedHead(t *testing.T) { |
| + ctx, s, pub, rat, opt, rt := huntSignedHeadBox(t) |
| + var out bytes.Buffer |
| + var p *ChannelPointer |
| + var m *Manifest |
| + var err error |
| + withCapturedStdout(t, &out, func() { |
| + p, m, err = StageSignedHead(ctx, s, pub, rat, opt, rt) |
| + }) |
| + if err != nil { |
| + t.Fatalf("control: StageSignedHead refused the untouched head: %v\n%s", err, out.String()) |
| + } |
| + if p.Version != "v1" || m == nil || m.Kind != "binary" { |
| + t.Fatalf("control: pointer %+v manifest %+v", p, m) |
| + } |
| + staged, rerr := os.ReadFile(VersionPath(opt, "bab", "v1", "km")) |
| + if rerr != nil || string(staged) != "the bytes the publisher shipped" { |
| + t.Fatalf("control: staged %q, %v", staged, rerr) |
| + } |
| +} |