package objstore import ( "context" "errors" "os" "path/filepath" "testing" "time" "github.com/carlosframework/platform/internal/release" ) func resultFixture(key, verdict, version string) *ScanResult { return &ScanResult{ Key: key, Verdict: verdict, Version: version, Engine: "clamav", Signature: "ClamAV 1.4.1/27500/x", At: time.Now().UTC(), } } // THE test for this file, and the third appearance of this bug class in the // design's reviews. // // Without the version binding: v1 scanned, the tag not written because the // ten-tag cap fired, the result says clean; the tenant overwrites with v2; the // tag is still absent, so the door falls through to the result object and // answers clean for bytes nobody has read. That is the laundering the verdict // tag's own binding exists to stop, reintroduced by the mechanism meant to // close a gap in it. func TestResultObjectIsVersionBound(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil { t.Fatal(err) } got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v1") if err != nil || got == nil || got.Verdict != "clean" { t.Fatalf("read at v1 = %+v, %v", got, err) } // The object is overwritten. The result must not answer for the new bytes. got, err = ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v2") if err != nil { t.Fatal(err) } if got != nil { t.Fatalf("a result written for v1 answered for v2: %+v", got) } } // The second write for a key is a REPLACE. release.Store.Put has only // create-only and replace, so a create-only second write returns // ErrCASConflict and silently drops the newer verdict — leaving the door on // "pending" until some sweep happens to tag the object. func TestResultObjectReplacesOnSecondWrite(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil { t.Fatal(err) } if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "infected", "v2")); err != nil { t.Fatalf("second write: %v", err) } got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v2") if err != nil || got == nil { t.Fatalf("read at v2 = %+v, %v", got, err) } if got.Verdict != "infected" { t.Fatalf("verdict = %q, want the v2 verdict — the newer one was dropped", got.Verdict) } } // Absent is a state, not an error. func TestReadScanResultAbsent(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) got, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/never", "v1") if err != nil || got != nil { t.Fatalf("= %+v, %v", got, err) } } // An unknown schema refuses rather than being read as a verdict. Same rule as // the record and the sidecar, and for the same reason: this one can be the // only thing standing between an app and a payload. func TestScanResultSchemaRefusesUnknown(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) if _, err := s.Put(ctx, ScanResultKey("bab", "titogo", "uploads/x"), []byte(`{"schema":99,"key":"uploads/x","verdict":"clean","version":"v1"}`), ""); err != nil { t.Fatal(err) } if _, err := ReadScanResult(ctx, s, "bab", "titogo", "uploads/x", "v1"); err == nil { t.Fatal("an unknown schema was read as a verdict") } } // The result lives under the APP's prefix, so PurgeApp removes it and the // name-claim gate can see it. A top-level results prefix would survive app // deletion invisibly — and, because materialize counts any key under an app // prefix and fails toward keeping the row, would resurrect the deleted app. func TestScanResultKeyIsUnderTheAppPrefix(t *testing.T) { got := ScanResultKey("bab", "titogo", "uploads/x") if want := release.AppPrefix("bab", "titogo"); len(got) <= len(want) || got[:len(want)] != want { t.Fatalf("key %q is not under %q", got, want) } // Distinct object keys must not collide. if ScanResultKey("bab", "titogo", "a") == ScanResultKey("bab", "titogo", "b") { t.Fatal("two object keys share a result object") } // PINNED, not merely "deterministic". A key that is stable within one // build is stable enough to pass a self-comparison and still be a // different key from the one the last build wrote — and every result // written before the change becomes an object nothing will ever read or // delete, under the prefix that decides whether the app's name can be // claimed again. if want := release.AppPrefix("bab", "titogo") + "scan-results/c681b33175d57d52dc74238a2e327f0982614eeab6142d8e893dd83cb617488b.json"; got != want { t.Fatalf("key = %q, want %q — the key format moved and the old results are now unreachable", got, want) } } // The fallback set is deleted with the app's consent, and swept by age // otherwise. Nothing else removes one: the drain drops a result only when a // LATER scan records a real verdict on the same key, which for a permanently // untaggable object never happens. func TestResultObjectCollectedByAge(t *testing.T) { ctx := context.Background() dir := t.TempDir() s := release.NewDirStore(dir) for _, k := range []string{"uploads/old", "uploads/new"} { if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture(k, "clean", "v1")); err != nil { t.Fatal(err) } } old := time.Now().Add(-48 * time.Hour) aged := filepath.Join(dir, filepath.FromSlash(ScanResultKey("bab", "titogo", "uploads/old"))) if err := os.Chtimes(aged, old, old); err != nil { t.Fatal(err) } n, err := CollectScanResults(ctx, s, "bab", "titogo", time.Now().Add(-QueueTTL)) if err != nil { t.Fatal(err) } if n != 1 { t.Fatalf("collected %d, want the one past its TTL", n) } if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/old")); !errors.Is(err, os.ErrNotExist) { t.Fatalf("the aged result survived: %v", err) } if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/new")); err != nil { t.Fatalf("the age sweep took a result still inside its TTL: %v", err) } } // A store that cannot rank by modification time is SKIPPED, not an error — // the same degradation storagePass accepts for SizeLister. A tick that failed // over a capability a fake lacks would be a tick that stopped draining. func TestCollectScanResultsWithoutModLister(t *testing.T) { if _, err := CollectScanResults(context.Background(), plainStore{}, "bab", "titogo", time.Now()); !errors.Is(err, ErrNoModLister) { t.Fatalf("err = %v, want ErrNoModLister", err) } } // plainStore implements release.Store and nothing optional. type plainStore struct{ release.Store } // A whole app's results go when the app's consent does. func TestDeleteScanResults(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) for _, k := range []string{"uploads/a", "uploads/b"} { if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture(k, "clean", "v1")); err != nil { t.Fatal(err) } } if err := DeleteScanResults(ctx, s, "bab", "titogo"); err != nil { t.Fatal(err) } left, err := s.List(ctx, ScanResultsPrefix("bab", "titogo")) if err != nil || len(left) != 0 { t.Fatalf("results survived: %v, %v", left, err) } // Deleting an empty set is not an error. if err := DeleteScanResults(ctx, s, "bab", "titogo"); err != nil { t.Fatal(err) } } // Deleted when a later scan DOES persist a verdict, so the fallback cannot // outlive the real answer. Deleting an absent one is not an error. func TestDeleteScanResult(t *testing.T) { ctx := context.Background() s := release.NewDirStore(t.TempDir()) if err := WriteScanResult(ctx, s, "bab", "titogo", resultFixture("uploads/x", "clean", "v1")); err != nil { t.Fatal(err) } if err := DeleteScanResult(ctx, s, "bab", "titogo", "uploads/x"); err != nil { t.Fatal(err) } if _, _, err := s.Get(ctx, ScanResultKey("bab", "titogo", "uploads/x")); !errors.Is(err, os.ErrNotExist) { t.Fatalf("result survived deletion: %v", err) } if err := DeleteScanResult(ctx, s, "bab", "titogo", "uploads/x"); err != nil { t.Fatalf("deleting an absent result: %v", err) } }