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 <[email protected]>
  • Loading branch information
kevina committed Apr 5, 2018
1 parent f7b6e67 commit 771f31b
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions thirdparty/idstore/idstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,97 @@ 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
}

func IdStore(bs bls.Blockstore) bls.Blockstore {
func IdStore(bs bls.Blockstore) *idstore {
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())
println("put")
isId, _, err := extractContents(bl.Cid())
if err != nil {
return err
}
if isId {
return nil
}
return b.bs.Put(bl)
}

func (b *idstore) PutMany(bs []blocks.Block) error {
println("putmany")
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 771f31b

Please sign in to comment.