Skip to content

Commit

Permalink
Merge pull request kubernetes#125422 from benluddy/cbor-disable-binar…
Browse files Browse the repository at this point in the history
…ymarshaler

KEP-4222: Disable recognition of Binary(Unm|M)arshaler in CBOR serializer.
  • Loading branch information
k8s-ci-robot committed Jun 26, 2024
2 parents 25a4307 + cc5a186 commit 8637867
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ var Decode cbor.DecMode = func() cbor.DecMode {

// Reject anything other than the simple values true, false, and null.
SimpleValues: simpleValues,

// Disable default recognition of types implementing encoding.BinaryUnmarshaler,
// which is not recognized for JSON decoding.
BinaryUnmarshaler: cbor.BinaryUnmarshalerNone,
}.DecMode()
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import (
"github.com/google/go-cmp/cmp"
)

type int64BinaryUnmarshaler int64

func (i *int64BinaryUnmarshaler) UnmarshalBinary(_ []byte) error {
return nil
}

func TestDecode(t *testing.T) {
hex := func(h string) []byte {
b, err := hex.DecodeString(h)
Expand Down Expand Up @@ -203,6 +209,20 @@ func TestDecode(t *testing.T) {
want: "AQIDBA==",
assertOnError: assertNilError,
},
{
name: "into non-string type implementing BinaryUnmarshaler",
in: hex("40"), // ''
into: int64BinaryUnmarshaler(7),
assertOnError: assertOnConcreteError(func(t *testing.T, e *cbor.UnmarshalTypeError) {
want := &cbor.UnmarshalTypeError{
CBORType: "byte string",
GoType: reflect.TypeFor[int64BinaryUnmarshaler]().String(),
}
if e.CBORType != want.CBORType || e.GoType != want.GoType {
t.Errorf("expected %q, got %q", want, e)
}
}),
},
})

group(t, "text string", []test{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ var Encode cbor.EncMode = func() cbor.EncMode {
// base64 encoding of the original bytes. No base64 encoding or decoding needs to be
// performed for []byte-to-CBOR-to-[]byte roundtrips.
ByteSliceLaterFormat: cbor.ByteSliceLaterFormatBase64,

// Disable default recognition of types implementing encoding.BinaryMarshaler, which
// is not recognized for JSON encoding.
BinaryMarshaler: cbor.BinaryMarshalerNone,
}.EncMode()
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
"github.com/google/go-cmp/cmp"
)

type int64BinaryMarshaler int64

func (i int64BinaryMarshaler) MarshalBinary() ([]byte, error) {
return []byte{}, nil
}

func TestEncode(t *testing.T) {
for _, tc := range []struct {
name string
Expand All @@ -34,6 +40,12 @@ func TestEncode(t *testing.T) {
want []byte
assertOnError func(t *testing.T, e error)
}{
{
name: "implementations of BinaryMarshaler are ignored",
in: int64BinaryMarshaler(7),
want: []byte{0x07},
assertOnError: assertNilError,
},
{
name: "all duplicate fields are ignored", // Matches behavior of JSON serializer.
in: struct {
Expand Down

0 comments on commit 8637867

Please sign in to comment.