Skip to content

Commit 301fcf8

Browse files
committed
coreapi: block: don't allow creation of invalid cidv0s
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 955f4f2 commit 301fcf8

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

core/commands/block.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
185185

186186
format, formatSet := req.Options["format"].(string)
187187
if !formatSet {
188-
if mhtval == mh.SHA2_256 {
189-
format = "v0"
190-
} else {
188+
if mhtval != mh.SHA2_256 || (mhlen != -1 && mhlen != 32) {
191189
format = "protobuf"
190+
} else {
191+
format = "v0"
192192
}
193193
}
194194

@@ -259,16 +259,18 @@ It takes a list of base58 encoded multihashes to remove.
259259
}
260260

261261
err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
262-
if err != nil && !quiet {
262+
if err != nil {
263263
res.Emit(&util.RemovedBlock{
264264
Hash: rp.Cid().String(),
265265
Error: err.Error(),
266266
})
267267
}
268268

269-
res.Emit(&util.RemovedBlock{
270-
Hash: rp.Cid().String(),
271-
})
269+
if !quiet {
270+
res.Emit(&util.RemovedBlock{
271+
Hash: rp.Cid().String(),
272+
})
273+
}
272274
}
273275
},
274276
PostRun: cmds.PostRunMap{

core/coreapi/block.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,29 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
3838
var pref cid.Prefix
3939
pref.Version = 1
4040

41+
if settings.Codec == "" {
42+
if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
43+
settings.Codec = "protobuf"
44+
} else {
45+
settings.Codec = "v0"
46+
}
47+
}
48+
4149
formatval, ok := cid.Codecs[settings.Codec]
4250
if !ok {
4351
return nil, fmt.Errorf("unrecognized format: %s", settings.Codec)
4452
}
53+
4554
if settings.Codec == "v0" && settings.MhType == mh.SHA2_256 {
4655
pref.Version = 0
4756
}
57+
58+
if settings.Codec == "v0" {
59+
if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
60+
return nil, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
61+
}
62+
}
63+
4864
pref.Codec = formatval
4965

5066
pref.MhType = settings.MhType

core/coreapi/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestBlockPutHash(t *testing.T) {
5555

5656
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
5757
if err != nil {
58-
t.Error(err)
58+
t.Fatal(err)
5959
}
6060

6161
if res.Path().Cid().String() != "zBurKB9YZkcDf6xa53WBE8CFX4ydVqAyf9KPXBFZt5stJzEstaS8Hukkhu4gwpMtc1xHNDbzP7sPtQKyWsP3C8fbhkmrZ" {

core/coreapi/interface/options/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type BlockRmOption func(*BlockRmSettings) error
1919

2020
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
2121
options := &BlockPutSettings{
22-
Codec: "v0",
22+
Codec: "",
2323
MhType: multihash.SHA2_256,
2424
MhLength: -1,
2525
}

0 commit comments

Comments
 (0)