Skip to content

Commit

Permalink
fix(codecs): coerce cid.Undef to null in dag{json,cbor} output
Browse files Browse the repository at this point in the history
Otherwise we end up with invalid output.

dag-cbor: 0xd82a4100

  d8 2a                                           #   tag(42)
    41                                            #     bytes(1)
      00                                          #       "\x00"

dag-json: {"/":"b"}

Fixes: #246
  • Loading branch information
rvagg committed Jun 1, 2022
1 parent 651b1cd commit eb767ed
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions codec/dagcbor/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func marshal(n datamodel.Node, tk *tok.Token, sink shared.TokenSink, options Enc
}
switch lnk := v.(type) {
case cidlink.Link:
if !lnk.Cid.Defined() {
return fmt.Errorf("encoding undefined CIDs are not supported by this codec")
}
tk.Type = tok.TBytes
tk.Bytes = append([]byte{0}, lnk.Bytes()...)
tk.Tagged = true
Expand Down
16 changes: 16 additions & 0 deletions codec/dagcbor/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"time"

qt "github.com/frankban/quicktest"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/testutil/garbage"
)
Expand Down Expand Up @@ -68,3 +72,15 @@ func TestEncodedLength(t *testing.T) {
}
})
}

func TestMarshalUndefCid(t *testing.T) {
link, err := cid.Decode("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
qt.Assert(t, err, qt.IsNil)
node, err := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "UndefCid", qp.Link(cidlink.Link{Cid: cid.Undef}))
qp.MapEntry(ma, "DefCid", qp.Link(cidlink.Link{Cid: link}))
})
qt.Assert(t, err, qt.IsNil)
_, err = ipld.Encode(node, Encode)
qt.Assert(t, err, qt.ErrorMatches, "encoding undefined CIDs are not supported by this codec")
}
3 changes: 3 additions & 0 deletions codec/dagjson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func Marshal(n datamodel.Node, sink shared.TokenSink, options EncodeOptions) err
}
switch lnk := v.(type) {
case cidlink.Link:
if !lnk.Cid.Defined() {
return fmt.Errorf("encoding undefined CIDs are not supported by this codec")
}
// Precisely four tokens to emit:
tk.Type = tok.TMapOpen
tk.Length = 1
Expand Down
25 changes: 25 additions & 0 deletions codec/dagjson/marshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dagjson

import (
"testing"

qt "github.com/frankban/quicktest"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
)

func TestMarshalUndefCid(t *testing.T) {
link, err := cid.Decode("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
qt.Assert(t, err, qt.IsNil)
node, err := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "UndefCid", qp.Link(cidlink.Link{Cid: cid.Undef}))
qp.MapEntry(ma, "DefCid", qp.Link(cidlink.Link{Cid: link}))
})
qt.Assert(t, err, qt.IsNil)
_, err = ipld.Encode(node, Encode)
qt.Assert(t, err, qt.ErrorMatches, "encoding undefined CIDs are not supported by this codec")
}

0 comments on commit eb767ed

Please sign in to comment.