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 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-11-11 20:03:18 +01:00
1 parent 58fde59f34
commit 202b64cf1e
4 files changed
+63 -8

No files matched your search

+12
View File
@@ -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:
+44 -8
View File
@@ -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))
+3
View File
@@ -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()
+4
View File
@@ -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