Skip to content

Commit

Permalink
Enforce a length limit on identity hashes.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Apr 5, 2018
1 parent f7b6e67 commit 6a6fd90
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions thirdparty/idstore/idstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package idstore

import (
"context"
"fmt"

mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
bls "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
)

var MaxIdHashLen = 64

// idstore wraps a BlockStore to add support for identity hashes
type idstore struct {
bs bls.Blockstore
Expand All @@ -18,43 +21,59 @@ func IdStore(bs bls.Blockstore) bls.Blockstore {
return &idstore{bs}
}

func extractContents(k *cid.Cid) (bool, []byte) {
func extractContents(k *cid.Cid) (bool, []byte, error) {
dmh, err := mh.Decode(k.Hash())
if err != nil || dmh.Code != mh.ID {
return false, nil
return false, nil, nil
}
if len(dmh.Digest) > MaxIdHashLen {
return true, nil, fmt.Errorf("identity hash of %d bytes is longer than maximum size of %d bytes",
len(dmh.Digest), MaxIdHashLen)
}
return true, dmh.Digest
return true, dmh.Digest, nil
}

func (b *idstore) DeleteBlock(k *cid.Cid) error {
isId, _ := extractContents(k)
isId, _, err := extractContents(k)
if err != nil {
return err
}
// always try to delete the block in case it was added to the
// blockstore without this wrapper
err := b.bs.DeleteBlock(k)
err = b.bs.DeleteBlock(k)
if isId && err == bls.ErrNotFound {
return nil
}
return err
}

func (b *idstore) Has(k *cid.Cid) (bool, error) {
isId, _ := extractContents(k)
isId, _, err := extractContents(k)
if err != nil {
return false, err
}
if isId {
return true, nil
}
return b.bs.Has(k)
}

func (b *idstore) Get(k *cid.Cid) (blocks.Block, error) {
isId, bdata := extractContents(k)
isId, bdata, err := extractContents(k)
if err != nil {
return nil, err
}
if isId {
return blocks.NewBlockWithCid(bdata, k)
}
return b.bs.Get(k)
}

func (b *idstore) Put(bl blocks.Block) error {
isId, _ := extractContents(bl.Cid())
isId, _, err := extractContents(bl.Cid())
if err != nil {
return err
}
if isId {
return nil
}
Expand All @@ -64,10 +83,14 @@ func (b *idstore) Put(bl blocks.Block) error {
func (b *idstore) PutMany(bs []blocks.Block) error {
toPut := make([]blocks.Block, 0, len(bs))
for _, bl := range bs {
isId, _ := extractContents(bl.Cid())
if !isId {
toPut = append(toPut, bl)
isId, _, err := extractContents(bl.Cid())
if err != nil {
return err
}
if isId {
continue
}
toPut = append(toPut, bl)
}
err := b.bs.PutMany(toPut)
if err != nil {
Expand Down

0 comments on commit 6a6fd90

Please sign in to comment.