From 202b64cf1e081982d8e1f10ce63c1ea34f1e7e1c Mon Sep 17 00:00:00 2001 From: Lucas Winther Date: Wed, 11 Nov 2026 20:03:18 +0100 Subject: [PATCH] protondrive: cap the number of blocks a file may list A full read holds the list of a file's blocks until it has checked the signed manifest, and how long that list could grow was bounded only by a size the server sends unsigned. A file listing more than 2^19 blocks (2 TiB in 4 MiB blocks) is now an error. Co-Authored-By: Claude Opus 5.5 --- backend/protondrive/download.go | 12 +++++ backend/protondrive/download_internal_test.go | 52 ++++++++++++++++--- backend/protondrive/nodes.go | 3 ++ docs/specs/001-protondrive-tier1.md | 4 ++ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/backend/protondrive/download.go b/backend/protondrive/download.go index 207eb85a7..940ce6410 100644 --- a/backend/protondrive/download.go +++ b/backend/protondrive/download.go @@ -62,6 +62,13 @@ const ( // this, plus one. minUnclaimedBlockSize = 1 << 20 + // maxFileBlocks is the most blocks a file may list: 2 TiB in + // 4 MiB blocks, beyond any one file the largest Proton plan, of a + // few TB in all, could hold. A full read holds the metadata of + // every block, a few hundred bytes each, until the manifest is + // checked, so this bounds that whatever the server claims. + maxFileBlocks = 1 << 19 + // contentKeyFallbackUntil is the Unix time until which a content // key packet may be signed by any of the account's address keys, // as in the SDK. @@ -94,6 +101,9 @@ func (c *client) open(ctx context.Context, n *node, offset, limit int64) (io.Rea sizes = n.X.BlockSizes } full := offset == 0 && limit < 0 + if len(sizes) > c.maxFileBlocks { + return nil, fmt.Errorf("open %q: %w: %d blocks recorded, more than %d blocks", n.Name, errIntegrity, len(sizes), c.maxFileBlocks) + } if sizes != nil { var total int64 for i, size := range sizes { @@ -367,6 +377,8 @@ func (r *blockReader) listBlocks(first, last, maxBlocks int, verify bool) error } for _, b := range rev.Blocks { switch { + case index > r.c.maxFileBlocks: + return fmt.Errorf("%w: more than %d blocks listed, more than any file can have", errIntegrity, r.c.maxFileBlocks) case b.Index != index: return fmt.Errorf("%w: block %d listed where block %d belongs", errIntegrity, b.Index, index) case index > maxBlocks && r.sizes != nil: diff --git a/backend/protondrive/download_internal_test.go b/backend/protondrive/download_internal_test.go index 862c68057..86f4a0fee 100644 --- a/backend/protondrive/download_internal_test.go +++ b/backend/protondrive/download_internal_test.go @@ -520,17 +520,15 @@ func (zeroReader) Read(p []byte) (int, error) { return len(p), nil } -func TestDownloadEndlessBlockList(t *testing.T) { - t.Parallel() - srv := protontest.New(t) - u := srv.AddUser("user", "pass") - var pages atomic.Int32 - // Every page is full of copies of the first block, renumbered. +// endlessBlockList returns a transport wrapper whose revision pages are +// all full of copies of the file's first block, renumbered, counting +// the pages in pages. +func endlessBlockList(pages *atomic.Int32) func(next http.RoundTripper) http.RoundTripper { var ( mu sync.Mutex first map[string]any ) - c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper { + return func(next http.RoundTripper) http.RoundTripper { return roundTripFunc(func(r *http.Request) (*http.Response, error) { resp, err := next.RoundTrip(r) if err != nil || !strings.Contains(r.URL.Path, "/revisions/") { @@ -570,7 +568,15 @@ func TestDownloadEndlessBlockList(t *testing.T) { resp.Header.Del("Content-Length") return resp, nil }) - }) + } +} + +func TestDownloadEndlessBlockList(t *testing.T) { + t.Parallel() + srv := protontest.New(t) + u := srv.AddUser("user", "pass") + var pages atomic.Int32 + c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, endlessBlockList(&pages)) n := seedFile(t, c, u, "f", testContent(4<<20+1)) for name, n := range map[string]*node{"claimed": n, "assumed": withoutBlockSizes(n)} { t.Run(name, func(t *testing.T) { @@ -583,6 +589,36 @@ func TestDownloadEndlessBlockList(t *testing.T) { } } +func TestDownloadBlockCap(t *testing.T) { + t.Parallel() + srv := protontest.New(t) + u := srv.AddUser("user", "pass") + var pages atomic.Int32 + c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, endlessBlockList(&pages)) + c.maxFileBlocks = 45 + n := seedFile(t, c, u, "f", testContent(4<<20+1)) + + // The encrypted size, which nothing signs, claims room for many + // more blocks than the cap. + huge := withoutBlockSizes(n) + huge.EncryptedSize = 1 << 50 + data, err := readRange(t, c, huge, 0, -1) + require.ErrorIs(t, err, errIntegrity) + assert.Contains(t, err.Error(), "more than 45 blocks") + assert.Empty(t, data) + assert.LessOrEqual(t, pages.Load(), int32(3), "listed past the cap") + + // Recorded block sizes beyond the cap fail before any request. + claims := *n + x := *n.X + x.BlockSizes = make([]int64, 46) + claims.X = &x + pages.Store(0) + _, err = readRange(t, c, &claims, 0, -1) + require.ErrorIs(t, err, errIntegrity) + assert.Zero(t, pages.Load()) +} + func TestDownloadCloseEarly(t *testing.T) { c, srv, u := newTestClient(t) n := seedFile(t, c, u, "f", testContent(8<<20)) diff --git a/backend/protondrive/nodes.go b/backend/protondrive/nodes.go index 7675c5c96..e2ae24689 100644 --- a/backend/protondrive/nodes.go +++ b/backend/protondrive/nodes.go @@ -91,6 +91,8 @@ type client struct { // before it is sent; for tests. verifierHook func(token []byte) + maxFileBlocks int // the most blocks a file may list: maxFileBlocks, unless a test lowers it + mu sync.Mutex folderKeys map[string]*nodeKeys // by folder ID; kept whatever the cache, as node keys never change reported map[string]bool // problems already logged, by kind and node ID @@ -180,6 +182,7 @@ func newClient(ctx context.Context, s *session, opt clientOptions) (*client, err cache: newNodeCache(opt.EnableCaching), folderKeys: make(map[string]*nodeKeys), reported: make(map[string]bool), + maxFileBlocks: maxFileBlocks, } if err := c.bootstrap(ctx); err != nil { c.close() diff --git a/docs/specs/001-protondrive-tier1.md b/docs/specs/001-protondrive-tier1.md index 163024ba4..be907f1e1 100644 --- a/docs/specs/001-protondrive-tier1.md +++ b/docs/specs/001-protondrive-tier1.md @@ -303,6 +303,10 @@ AES-GCM blocks; names, passphrases, hash keys and attributes without AEAD). covering the whole file from offset 0 (as `bytes=0-`) is a full read. Other ranged reads check each block against its listed hash but cannot verify the manifest; this is documented, as it is in the SDK. +- A file may list at most 2^19 blocks (2 TiB in 4 MiB blocks, more than + any one file a Proton plan can hold), whatever its unsigned size fields + claim, which bounds the block metadata a full read holds before the + manifest check (a few hundred bytes per block). - A server serving an earlier revision whole (blocks, manifest and extended attributes, all validly signed) under the current revision ID is undetectable: nothing signed binds them to the revision. The SDK cannot