diff --git a/vindex/client/client.go b/vindex/client/client.go index 44aa632..1934f10 100644 --- a/vindex/client/client.go +++ b/vindex/client/client.go @@ -125,6 +125,9 @@ func DomainToCanonicalKey(domain string) (string, error) { } labels := strings.Split(string(buf), ".") + if len(labels) < 2 { + return "", fmt.Errorf("%w: domain %q must have at least two labels", ErrBadRequest, domain) + } for _, lbl := range labels { if lbl == "" || len(lbl) > 63 { return "", fmt.Errorf("%w: empty or overlong DNS label in %q", ErrBadRequest, domain) @@ -138,6 +141,17 @@ func DomainToCanonicalKey(domain string) (string, error) { } } } + tld := labels[len(labels)-1] + allNumericTLD := true + for i := 0; i < len(tld); i++ { + if tld[i] < '0' || tld[i] > '9' { + allNumericTLD = false + break + } + } + if allNumericTLD { + return "", fmt.Errorf("%w: domain %q has all-numeric TLD", ErrBadRequest, domain) + } var b strings.Builder b.Grow(totalLen + 1) @@ -362,6 +376,9 @@ func verifyKeysBody(rawBody []byte, target string, isPrefix bool, cfg Config, cl if bindSec.InputSize == 0 && bindSec.InputRoot != wire.EmptyRoot() { return nil, fmt.Errorf("%w: non-empty input-root at input_size 0", ErrVerificationFailed) } + if bindSec.InputSize > 0 && bindSec.InputRoot == ([sha256.Size]byte{}) { + return nil, fmt.Errorf("%w: zero input-root at input_size %d", ErrVerificationFailed, bindSec.InputSize) + } if bindSec.InputSize == bindSec.From && (bindSec.MPTRoot != wire.EmptyRoot() || len(keysSec.Matches) > 0) { return nil, fmt.Errorf("%w: non-empty MPT state when input_size (%d) == from (%d)", ErrVerificationFailed, bindSec.InputSize, bindSec.From) } diff --git a/vindex/client/client_test.go b/vindex/client/client_test.go index d31a9a3..8eee2fa 100644 --- a/vindex/client/client_test.go +++ b/vindex/client/client_test.go @@ -199,6 +199,9 @@ func TestDomainToCanonicalKey(t *testing.T) { "-leading.example.com", "trailing-.example.com", "\u00a0example.com", // Non-ASCII NBSP must be rejected by IDNA2008, not stripped as whitespace + "localhost", + "*.com", + "192.0.2.1", overlongLabel, overlongDomain, } { diff --git a/vindex/internal/auditor/auditor.go b/vindex/internal/auditor/auditor.go index d2cb1c1..b383a9c 100644 --- a/vindex/internal/auditor/auditor.go +++ b/vindex/internal/auditor/auditor.go @@ -170,6 +170,7 @@ type Verifier struct { stateMu sync.RWMutex isHalted bool haltErr error + metricsAddr string verifiedOutputSize uint64 verifiedInputSize uint64 lastVerifiedRoot [sha256.Size]byte @@ -401,10 +402,12 @@ func New(cfg Config) (*Verifier, error) { } metrics.ResetAuditorRootMismatch() + metrics.ResetAuditorHalted() return v, nil } func (v *Verifier) halt(err error) error { + metrics.SetAuditorHalted(1) v.stateMu.Lock() v.isHalted = true v.haltErr = err @@ -1059,6 +1062,7 @@ func (v *Verifier) commitInMemoryWatermarks( func (v *Verifier) updateMetrics(nextOutputSize, targetInputSize uint64, latestInCP *log.Checkpoint) { metrics.ResetAuditorRootMismatch() + metrics.ResetAuditorHalted() metrics.OutputTreeSize.Set(float64(nextOutputSize)) metrics.ServingTreeSize.Set(float64(targetInputSize)) metrics.KVCommittedSize.Set(float64(targetInputSize)) @@ -1069,6 +1073,13 @@ func (v *Verifier) updateMetrics(nextOutputSize, targetInputSize uint64, latestI } } +// MetricsAddr returns the bound operational TCP address once Run has opened MetricsAddr. +func (v *Verifier) MetricsAddr() string { + v.stateMu.RLock() + defer v.stateMu.RUnlock() + return v.metricsAddr +} + // Run executes the periodic polling loop and optionally serves operational endpoints on MetricsAddr. func (v *Verifier) Run(ctx context.Context) error { g, ctx := errgroup.WithContext(ctx) @@ -1079,6 +1090,9 @@ func (v *Verifier) Run(ctx context.Context) error { return fmt.Errorf("auditor admin listener failed on %s: %w", v.cfg.MetricsAddr, err) } actualAddr := ln.Addr().String() + v.stateMu.Lock() + v.metricsAddr = actualAddr + v.stateMu.Unlock() mServer := server.NewHTTPServer(actualAddr, v.AdminHandler()) g.Go(func() error { klog.Infof("Serving auditor metrics at http://%s/metrics", actualAddr) diff --git a/vindex/internal/coordinator/coordinator.go b/vindex/internal/coordinator/coordinator.go index c0c7419..2bff43e 100644 --- a/vindex/internal/coordinator/coordinator.go +++ b/vindex/internal/coordinator/coordinator.go @@ -356,8 +356,14 @@ func (c *Coordinator) recoverGenesisMPT(ctx context.Context, treeSize uint64, tr // Recover restores the MPT and ServingState after startup or an unclean shutdown. func (c *Coordinator) Recover(ctx context.Context) error { + if fErr := c.FatalError(); fErr != nil { + return fErr + } c.syncMu.Lock() defer c.syncMu.Unlock() + if fErr := c.FatalError(); fErr != nil { + return fErr + } rawOutCP, outCP, err := c.readVerifiedOutputCheckpoint(ctx) if err != nil { @@ -569,8 +575,14 @@ func (c *Coordinator) reconcilePublisherMetadata(tipLeaf *wire.OutputLogLeaf) er // SyncOnce fetches the latest checkpoint from the Input Log, verifies append-only consistency, // indexes missing leaf batches, and publishes the resulting MPT state commitment to the Output Log. func (c *Coordinator) SyncOnce(ctx context.Context) error { + if fErr := c.FatalError(); fErr != nil { + return fErr + } c.syncMu.Lock() defer c.syncMu.Unlock() + if fErr := c.FatalError(); fErr != nil { + return fErr + } if c.fetcher == nil { return nil @@ -987,6 +999,14 @@ func (c *Coordinator) RecordFatalError(err error) { } } +// FatalError returns the recorded fatal error if the coordinator has permanently halted, or nil otherwise. +func (c *Coordinator) FatalError() error { + if fErr := c.fatalErr.Load(); fErr != nil && *fErr != nil { + return *fErr + } + return nil +} + // RunPollLoop enters the periodic ingestion polling loop until ctx is canceled or a fatal error occurs. func (c *Coordinator) RunPollLoop(ctx context.Context, pollInterval time.Duration) error { if pollInterval <= 0 { @@ -1018,7 +1038,9 @@ func (c *Coordinator) RunPollLoop(ctx context.Context, pollInterval time.Duratio func (c *Coordinator) Run(ctx context.Context, pollInterval time.Duration) error { if err := c.Recover(ctx); err != nil { fErr := fmt.Errorf("startup recovery failed: %w", err) - c.RecordFatalError(fErr) + if ctx.Err() == nil || IsFatalSyncError(err) { + c.RecordFatalError(fErr) + } return fErr } if err := c.SyncOnce(ctx); err != nil { diff --git a/vindex/internal/ingest/cache.go b/vindex/internal/ingest/cache.go index 0438822..93b9fac 100644 --- a/vindex/internal/ingest/cache.go +++ b/vindex/internal/ingest/cache.go @@ -22,6 +22,8 @@ import ( "path/filepath" "strings" "sync" + + "github.com/transparency-dev/incubator/vindex/internal/metrics" ) // maxInMemoryBundles bounds the in-memory fallback cache when cacheDir is empty. @@ -47,11 +49,17 @@ func NewManagedTileCache(cacheDir string, bundleSz uint64) (*ManagedTileCache, e return nil, fmt.Errorf("failed to create tile cache dir %q: %w", cacheDir, err) } } - return &ManagedTileCache{ + c := &ManagedTileCache{ cacheDir: cacheDir, memory: make(map[uint64]*LeafBundle), bundleSz: bundleSz, - }, nil + } + if cacheDir != "" { + if sz, err := c.DirSize(); err == nil { + metrics.TileCacheBytes.Set(float64(sz)) + } + } + return c, nil } // bundlePath returns the sharded filepath for a bundle index. diff --git a/vindex/internal/ingest/deref.go b/vindex/internal/ingest/deref.go index e32a314..4553f9d 100644 --- a/vindex/internal/ingest/deref.go +++ b/vindex/internal/ingest/deref.go @@ -19,6 +19,7 @@ import ( "context" "errors" "fmt" + "math" "net/http" "net/url" "os" @@ -40,18 +41,21 @@ type bundleCandidate struct { sumDB bool } -// FetchVerifiedEntryByIndex fetches the entry at idx from baseURL and, when treeSize > 0 and treeRoot is non-zero, -// cryptographically authenticates the entry against (treeSize, treeRoot) via an RFC 6962 inclusion proof. +// FetchVerifiedEntryByIndex fetches the entry at idx from baseURL and cryptographically +// authenticates the entry against (treeSize, treeRoot) via an RFC 6962 inclusion proof. func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, baseURL string, idx, treeSize uint64, treeRoot [32]byte) ([]byte, error) { base := strings.TrimSpace(baseURL) if base == "" { return nil, errors.New("empty baseURL") } - if treeSize > 0 && idx >= treeSize { + if treeSize == 0 { + return nil, errors.New("cannot verify entry inclusion against tree size 0") + } + if idx >= treeSize { return nil, fmt.Errorf("entry index %d out of bounds for tree size %d", idx, treeSize) } - if treeRoot != ([32]byte{}) && treeSize == 0 { - return nil, errors.New("cannot verify entry inclusion against tree size 0") + if treeRoot == ([32]byte{}) { + return nil, errors.New("cannot verify entry inclusion against zero treeRoot") } u, err := url.Parse(base) @@ -60,6 +64,9 @@ func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, bas } bIdx := idx / uint64(layout.EntryBundleWidth) + if bIdx > math.MaxInt64 { + return nil, fmt.Errorf("bundle index %d overflows int64", bIdx) + } offset := int(idx % uint64(layout.EntryBundleWidth)) p := layout.PartialTileSize(0, bIdx, treeSize) isSumDB := strings.Contains(base, "sum.golang.org") @@ -142,68 +149,69 @@ func FetchVerifiedEntryByIndex(ctx context.Context, httpClient *http.Client, bas return nil, fmt.Errorf("entry bundle for index %d not found at %s", idx, baseURL) } - if treeSize > 0 && treeRoot != ([32]byte{}) { - readTile := func(ctx context.Context, level, index uint64, p uint8) ([]byte, error) { - var tileCandidates []string - addSumDBTiles := func() { - w := int(p) - if w == 0 { - w = 256 - } - tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: w}.Path()) - if p != 0 { - tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: 256}.Path()) - } - } - if usedSumDB { - addSumDBTiles() + readTile := func(ctx context.Context, level, index uint64, p uint8) ([]byte, error) { + if level > 63 || index > math.MaxInt64 { + return nil, fmt.Errorf("tile coordinates (level=%d, index=%d) overflow signed bounds", level, index) + } + var tileCandidates []string + addSumDBTiles := func() { + w := int(p) + if w == 0 { + w = 256 } - tileCandidates = append(tileCandidates, layout.TilePath(level, index, p)) + tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: w}.Path()) if p != 0 { - tileCandidates = append(tileCandidates, layout.TilePath(level, index, 0)) + tileCandidates = append(tileCandidates, tlog.Tile{H: 8, L: int(level), N: int64(index), W: 256}.Path()) } - if !usedSumDB { - addSumDBTiles() + } + if usedSumDB { + addSumDBTiles() + } + tileCandidates = append(tileCandidates, layout.TilePath(level, index, p)) + if p != 0 { + tileCandidates = append(tileCandidates, layout.TilePath(level, index, 0)) + } + if !usedSumDB { + addSumDBTiles() + } + wantBytes := 256 * 32 + if p != 0 { + wantBytes = int(p) * 32 + } + var tileErr error + for _, tc := range tileCandidates { + data, ok, err := readBundleResource(ctx, httpClient, u, tc) + if err != nil { + tileErr = err + continue } - wantBytes := 256 * 32 - if p != 0 { - wantBytes = int(p) * 32 + if !ok { + continue } - var tileErr error - for _, tc := range tileCandidates { - data, ok, err := readBundleResource(ctx, httpClient, u, tc) - if err != nil { - tileErr = err - continue - } - if !ok { - continue - } - if len(data) < wantBytes { - tileErr = fmt.Errorf("tile %s too short: got %d bytes, want at least %d", tc, len(data), wantBytes) - continue - } - if len(data) > wantBytes { - data = data[:wantBytes] - } - return data, nil + if len(data) < wantBytes { + tileErr = fmt.Errorf("tile %s too short: got %d bytes, want at least %d", tc, len(data), wantBytes) + continue } - if tileErr != nil { - return nil, tileErr + if len(data) > wantBytes { + data = data[:wantBytes] } - return nil, fmt.Errorf("hash tile level=%d index=%d p=%d not found at %s", level, index, p, baseURL) - } - pb, err := tclient.NewProofBuilder(ctx, treeSize, readTile) - if err != nil { - return nil, fmt.Errorf("failed to initialize inclusion proof builder for entry %d: %w", idx, err) - } - incProof, err := pb.InclusionProof(ctx, idx) - if err != nil { - return nil, fmt.Errorf("failed to build inclusion proof for entry %d: %w", idx, err) + return data, nil } - if err := proof.VerifyInclusion(rfc6962.DefaultHasher, idx, treeSize, leafHash, incProof, treeRoot[:]); err != nil { - return nil, fmt.Errorf("entry %d failed Merkle inclusion verification against input root %x: %w", idx, treeRoot, err) + if tileErr != nil { + return nil, tileErr } + return nil, fmt.Errorf("hash tile level=%d index=%d p=%d not found at %s", level, index, p, baseURL) + } + pb, err := tclient.NewProofBuilder(ctx, treeSize, readTile) + if err != nil { + return nil, fmt.Errorf("failed to initialize inclusion proof builder for entry %d: %w", idx, err) + } + incProof, err := pb.InclusionProof(ctx, idx) + if err != nil { + return nil, fmt.Errorf("failed to build inclusion proof for entry %d: %w", idx, err) + } + if err := proof.VerifyInclusion(rfc6962.DefaultHasher, idx, treeSize, leafHash, incProof, treeRoot[:]); err != nil { + return nil, fmt.Errorf("entry %d failed Merkle inclusion verification against input root %x: %w", idx, treeRoot, err) } return entry, nil diff --git a/vindex/internal/ingest/ingest_test.go b/vindex/internal/ingest/ingest_test.go index 3cb3974..4354f8b 100644 --- a/vindex/internal/ingest/ingest_test.go +++ b/vindex/internal/ingest/ingest_test.go @@ -1320,7 +1320,15 @@ func TestFetchVerifiedEntryByIndex(t *testing.T) { t.Fatalf("ReadFile(level0Tile) failed: %v", err) } - sumDBTile := []byte("golang.org/x/mod v0.1.0 h1:aaa=\ngolang.org/x/mod v0.1.0/go.mod h1:mmm=\n\ngolang.org/x/net v0.2.0 h1:bbb=\n") + sumDBRec0 := []byte("golang.org/x/mod v0.1.0 h1:aaa=\ngolang.org/x/mod v0.1.0/go.mod h1:mmm=\n") + sumDBRec1 := []byte("golang.org/x/net v0.2.0 h1:bbb=\n") + sumDBTile := append(append([]byte(nil), sumDBRec0...), '\n') + sumDBTile = append(sumDBTile, sumDBRec1...) + sumDBH0 := rfc6962.DefaultHasher.HashLeaf(sumDBRec0) + sumDBH1 := rfc6962.DefaultHasher.HashLeaf(sumDBRec1) + sumDBHashTile := append(append([]byte(nil), sumDBH0...), sumDBH1...) + var sumDBRoot [32]byte + copy(sumDBRoot[:], rfc6962.DefaultHasher.HashChildren(sumDBH0, sumDBH1)) staticCTDataTile := []byte{ 0x00, 0x00, 0x01, 0x8b, 0xcf, 0xee, 0x58, 0x00, // timestamp (first two bytes are 0x00, 0x00) @@ -1373,6 +1381,9 @@ func TestFetchVerifiedEntryByIndex(t *testing.T) { mux.HandleFunc("/sum.golang.org/tile/8/data/000.p/2", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(sumDBTile) }) + mux.HandleFunc("/sum.golang.org/tile/8/0/000.p/2", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write(sumDBHashTile) + }) // Truncated bundle (claims 3 entries via treeSize=3, only contains 1 entry) mux.HandleFunc("/truncated/tile/entries/000.p/3", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(entriesTile[:2+len("entry-0")]) @@ -1408,7 +1419,7 @@ func TestFetchVerifiedEntryByIndex(t *testing.T) { t.Fatal("expected FetchVerifiedEntryByIndex to fail with wrong treeRoot") } - gotSumDB0, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/sum.golang.org", 0, 2, [32]byte{}) + gotSumDB0, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/sum.golang.org", 0, 2, sumDBRoot) if err != nil { t.Fatalf("FetchVerifiedEntryByIndex(sumdb, 0) failed: %v", err) } @@ -1417,7 +1428,7 @@ func TestFetchVerifiedEntryByIndex(t *testing.T) { t.Fatalf("FetchVerifiedEntryByIndex(sumdb, 0) = %q, want %q", gotSumDB0, wantSumDB0) } - gotSumDB1, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/sum.golang.org", 1, 2, [32]byte{}) + gotSumDB1, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/sum.golang.org", 1, 2, sumDBRoot) if err != nil { t.Fatalf("FetchVerifiedEntryByIndex(sumdb, 1) failed: %v", err) } @@ -1425,10 +1436,17 @@ func TestFetchVerifiedEntryByIndex(t *testing.T) { t.Fatalf("FetchVerifiedEntryByIndex(sumdb, 1) = %q", gotSumDB1) } - if _, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/truncated", 2, 3, [32]byte{}); err == nil { + if _, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/truncated", 2, 3, [32]byte(rootHash)); err == nil { t.Fatal("expected error when bundle has fewer entries than requested offset") } + if _, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/c2sp", 0, 0, [32]byte{}); err == nil { + t.Fatal("expected error for treeSize == 0") + } + if _, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/c2sp", 0, 3, [32]byte{}); err == nil { + t.Fatal("expected error for zero treeRoot") + } + if _, err := FetchVerifiedEntryByIndex(ctx, srv.Client(), srv.URL+"/c2sp", 3, 3, [32]byte(rootHash)); err == nil { t.Fatal("expected error for idx >= treeSize") } diff --git a/vindex/internal/mpt/mpt.go b/vindex/internal/mpt/mpt.go index edba641..40eff63 100644 --- a/vindex/internal/mpt/mpt.go +++ b/vindex/internal/mpt/mpt.go @@ -625,6 +625,7 @@ func (m *Manager) Close() error { m.lockTreeWrite() defer m.treeMu.Unlock() + m.exact.Store(false) if m.syncCtl != nil { m.syncCtl.deferSync = false } diff --git a/vindex/internal/mpt/mpt_test.go b/vindex/internal/mpt/mpt_test.go index 420a3b6..0fe6c45 100644 --- a/vindex/internal/mpt/mpt_test.go +++ b/vindex/internal/mpt/mpt_test.go @@ -803,3 +803,21 @@ func TestVerify_AdversarialWireBounds(t *testing.T) { } }) } + +func TestManager_ProveAfterCloseFailsCleanly(t *testing.T) { + mgr, err := Open(t.TempDir()) + if err != nil { + t.Fatalf("Open failed: %v", err) + } + if _, err := mgr.CommitKeysWithVersion(map[string][sha256.Size]byte{ + "com.example.": sha256.Sum256([]byte("v1")), + }, 1); err != nil { + t.Fatalf("CommitKeysWithVersion failed: %v", err) + } + if err := mgr.Close(); err != nil { + t.Fatalf("Close failed: %v", err) + } + if _, _, _, err := mgr.ProveKey([]byte("com.example.")); !errors.Is(err, torchmpt.ErrModifiedTree) { + t.Fatalf("ProveKey after Close err = %v, want torchmpt.ErrModifiedTree", err) + } +} diff --git a/vindex/internal/wasmhost/wasm.go b/vindex/internal/wasmhost/wasm.go index 6628713..9d54fe5 100644 --- a/vindex/internal/wasmhost/wasm.go +++ b/vindex/internal/wasmhost/wasm.go @@ -424,6 +424,9 @@ func cloneBundleResults(results [][]MappedEntry) [][]MappedEntry { // Close closes all pooled module instances. func (h *WASMHost) Close(ctx context.Context) error { + if h == nil || h.compiled == nil { + return nil + } h.mu.Lock() defer h.mu.Unlock() if h.closed { @@ -452,6 +455,9 @@ type WASMRunner struct { // NewRunner creates a dedicated WASMRunner from the host. // The runner has exclusive access to its WASM module instance, eliminating pool and mutex overhead. func (h *WASMHost) NewRunner(ctx context.Context) (*WASMRunner, error) { + if h == nil || h.compiled == nil { + return nil, ErrHostClosed + } h.mu.Lock() if h.closed { h.mu.Unlock() diff --git a/vindex/internal/wire/policy.go b/vindex/internal/wire/policy.go index ea8f8f8..ac89e9b 100644 --- a/vindex/internal/wire/policy.go +++ b/vindex/internal/wire/policy.go @@ -165,7 +165,13 @@ func ParseVerifier(keyOrPath string) (note.Verifier, error) { return NewMTCVerifier(name, ed25519.PublicKey(pubKeyBytes), cosignerID, logID) } - return f_note.NewVerifier(s) + if v, err := f_note.NewVerifier(s); err == nil { + return v, nil + } else if cv, cErr := f_note.NewVerifierForCosignatureV1(s); cErr == nil { + return cv, nil + } else { + return nil, err + } } // ParseWitnessVerifier parses a witness cosignature or note verifier key string or file path. diff --git a/vindex/mapfn/examples/ct/main.go b/vindex/mapfn/examples/ct/main.go index a14d6d2..07e6f28 100644 --- a/vindex/mapfn/examples/ct/main.go +++ b/vindex/mapfn/examples/ct/main.go @@ -165,7 +165,7 @@ func canonicalizeDomainBytes(raw []byte, outBuf *[256]byte) ([]byte, bool) { if hasWildcard { needed += 2 } - if len(raw) > 253 || needed > len(outBuf) { + if needed-1 > 253 || needed > len(outBuf) { return nil, false }