diff --git a/backend/protondrive/nodes.go b/backend/protondrive/nodes.go index 0c58757d1..f345e2561 100644 --- a/backend/protondrive/nodes.go +++ b/backend/protondrive/nodes.go @@ -439,13 +439,17 @@ func annotate(what string, err error) error { // and parent's key is unverified: whoever made that key could have // made this one too. // -// It returns unreadable, and no node, if the node's name or key can't -// be decrypted, its name isn't valid or its type is unknown. err is +// It returns unreadable, and no node, if the node has no ID, its name +// or key can't be decrypted, its name isn't valid, its type is unknown +// or its encrypted size is negative. err is // set only if the verification keys can't be had. Failed signature // checks, and extended attributes or hash key which can't be // decrypted, are logged once per node. func (c *client) decryptNode(ctx context.Context, d *api.LinkDetails, parent *nodeKeys) (n *node, unreadable, err error) { l := &d.Link + if l.LinkID == "" { + return nil, errors.New("node has no ID"), nil + } parentKR := parent.keyRing now := c.s.serverTime() @@ -527,6 +531,9 @@ func (c *client) decryptNode(ctx context.Context, d *api.LinkDetails, parent *no case l.Type == api.LinkTypeFile && d.File != nil: n.MIMEType = d.File.MediaType if rev := d.File.ActiveRevision; rev != nil { + if rev.EncryptedSize < 0 { + return nil, fmt.Errorf("%q: negative encrypted size %d", truncateName(name), rev.EncryptedSize), nil + } n.RevisionID = rev.RevisionID n.EncryptedSize = rev.EncryptedSize revVerifyKR, err := c.verifyKeys(ctx, rev.SignatureEmail, keys.keyRing) diff --git a/backend/protondrive/nodes_internal_test.go b/backend/protondrive/nodes_internal_test.go index 0964585f4..a848cb3ff 100644 --- a/backend/protondrive/nodes_internal_test.go +++ b/backend/protondrive/nodes_internal_test.go @@ -1110,3 +1110,36 @@ func TestNewNodesKeepListing(t *testing.T) { assert.Contains(t, listed, name) } } + +// TestDecryptNodeRefusesHostileValues checks a node which decrypts but +// which the server describes with values no node can have is +// unreadable, not handed on. +func TestDecryptNodeRefusesHostileValues(t *testing.T) { + t.Parallel() + c, _, u := newTestClient(t) + ctx := context.Background() + fileID := u.SeedFile(u.RootID(), "file.txt", []byte("content"), time.Now()) + rootKeys, err := c.folderKeysOf(ctx, c.rootID()) + require.NoError(t, err) + + for name, change := range map[string]func(d *api.LinkDetails){ + "no ID": func(d *api.LinkDetails) { d.Link.LinkID = "" }, + "negative encrypted size": func(d *api.LinkDetails) { d.File.ActiveRevision.EncryptedSize = -5 }, + } { + t.Run(name, func(t *testing.T) { + links, err := c.fetchLinks(ctx, []string{fileID}) + require.NoError(t, err) + require.Len(t, links, 1) + n, unreadable, err := c.decryptNode(ctx, &links[0], rootKeys) + require.NoError(t, err) + require.NoError(t, unreadable, "unchanged") + require.NotNil(t, n) + + change(&links[0]) + n, unreadable, err = c.decryptNode(ctx, &links[0], rootKeys) + require.NoError(t, err) + assert.Error(t, unreadable) + assert.Nil(t, n) + }) + } +}