diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 01ef01e..30e21e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: run: go run github.com/rhysd/actionlint/cmd/actionlint@v1.7.12 - name: Rebuild packaged binaries env: - VERSION: v0.3.1 + VERSION: v0.3.2 run: scripts/build-dist.sh - name: Verify packaged binaries are reproducible run: git diff --exit-code -- dist diff --git a/README.md b/README.md index fb78ab6..51a1f30 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ go vet ./... go run honnef.co/go/tools/cmd/staticcheck@v0.7.0 ./... go run golang.org/x/vuln/cmd/govulncheck@v1.6.0 ./... node --test action/*.test.js -VERSION=v0.3.1 scripts/build-dist.sh +VERSION=v0.3.2 scripts/build-dist.sh git diff --exit-code -- dist (cd dist && sha256sum --check SHA256SUMS) ``` diff --git a/dist/SHA256SUMS b/dist/SHA256SUMS index 43c5d05..187e7e5 100644 --- a/dist/SHA256SUMS +++ b/dist/SHA256SUMS @@ -1,2 +1,2 @@ -9b7e9b76bfa605420a0c5498ff6b40864ecaffef3d2ef16203502a1786cf7e93 cache-server-linux-amd64 -08d858dfb6c528c79667c06d14d35575b9f32d05720656f4c13b6f1f39de4f91 cache-server-linux-arm64 +f78d1caf02f9211eaf6e680e9dd5692b1a03355365505f617a5ad3fd254af0f0 cache-server-linux-amd64 +f6881c1ecefc1aa070bfe62ee567c45508e187da5af09fb1529bb00709a7ba30 cache-server-linux-arm64 diff --git a/dist/cache-server-linux-amd64 b/dist/cache-server-linux-amd64 index ed43d0f..bbbdec4 100755 Binary files a/dist/cache-server-linux-amd64 and b/dist/cache-server-linux-amd64 differ diff --git a/dist/cache-server-linux-arm64 b/dist/cache-server-linux-arm64 index d862d7c..943adc1 100755 Binary files a/dist/cache-server-linux-arm64 and b/dist/cache-server-linux-arm64 differ diff --git a/internal/server/pack_store.go b/internal/server/pack_store.go index 17f365b..f82c68a 100644 --- a/internal/server/pack_store.go +++ b/internal/server/pack_store.go @@ -15,6 +15,7 @@ import ( "github.com/cre4ture/bazel-github-actions-cache-v2/internal/cache" blocks "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" + carv2 "github.com/ipld/go-car/v2" "github.com/ipld/go-car/v2/blockstore" ) @@ -221,7 +222,7 @@ func (p *packStore) resolve(ctx context.Context, key, kind, digest string) (obje if err != nil { return object{}, false, err } - data, err := readCARBlock(ctx, path, contentCID) + data, err := readCARBlock(ctx, path, contentCID, p.server.cfg.MaxBlobSize) if err != nil { return object{}, false, err } @@ -725,8 +726,17 @@ func parseManifestKey(prefix, key string) (string, bool) { return id, validCID(id) } -func readCARBlock(ctx context.Context, path, contentCID string) ([]byte, error) { - store, err := blockstore.OpenReadOnly(path, blockstore.UseWholeCIDs(true)) +const rawCIDSectionOverheadBytes = 64 + +func readCARBlock(ctx context.Context, path, contentCID string, maxBlobSize int64) ([]byte, error) { + // CAR sections include the block CID as well as the payload. Packs use + // CIDv1/raw/SHA-256 CIDs, so a small fixed allowance above the configured + // payload limit safely admits every block this cache is allowed to store. + store, err := blockstore.OpenReadOnly( + path, + blockstore.UseWholeCIDs(true), + carv2.MaxAllowedSectionSize(uint64(maxBlobSize+rawCIDSectionOverheadBytes)), + ) if err != nil { return nil, fmt.Errorf("open CARv2 pack: %w", err) } diff --git a/internal/server/pack_store_test.go b/internal/server/pack_store_test.go index 7724746..c84fab2 100644 --- a/internal/server/pack_store_test.go +++ b/internal/server/pack_store_test.go @@ -1,6 +1,7 @@ package server import ( + "bytes" "context" "net/http" "sort" @@ -34,10 +35,15 @@ func (c memoryCatalog) List(_ context.Context, prefix string, limit int) ([]stri var _ cache.Catalog = memoryCatalog{} func testPackedServer(t *testing.T, backend *memoryBackend) *Server { + return testPackedServerWithMaxBlob(t, backend, 1024) +} + +func testPackedServerWithMaxBlob(t *testing.T, backend *memoryBackend, maxBlobSize int64) *Server { t.Helper() return testServer(t, backend, func(cfg *Config) { cfg.StorageMode = "packs" cfg.Catalog = memoryCatalog{backend: backend} + cfg.MaxBlobSize = maxBlobSize cfg.PackSize = 1024 cfg.PackFlushInterval = time.Hour cfg.MaxManifests = 100 @@ -86,6 +92,29 @@ func TestPackedStoreRoundTripCommitsPackBeforeManifest(t *testing.T) { } } +func TestPackedStoreRestoresCASLargerThanCARDefaultSectionLimit(t *testing.T) { + const payloadSize = (8 << 20) + 1 + const maxBlobSize = payloadSize + 1024 + backend := newMemoryBackend() + seed := testPackedServerWithMaxBlob(t, backend, maxBlobSize) + payload := bytes.Repeat([]byte{0xa5}, payloadSize) + digest := digest(payload) + if response := putCacheObject(seed, "/cas/"+digest, payload); response.Code != http.StatusNoContent { + t.Fatalf("CAS PUT = %d, body = %s", response.Code, response.Body.String()) + } + closePackedServer(t, seed) + + restore := testPackedServerWithMaxBlob(t, backend, maxBlobSize) + defer closePackedServer(t, restore) + response := readCacheObject(restore, "/cas/"+digest) + if response.Code != http.StatusOK { + t.Fatalf("CAS GET = %d, body = %s", response.Code, response.Body.String()) + } + if !bytes.Equal(response.Body.Bytes(), payload) { + t.Fatal("restored CAS payload differs from the published payload") + } +} + func TestPackedStoreMergesConcurrentManifestHeads(t *testing.T) { backend := newMemoryBackend() writerA := testPackedServer(t, backend)