Skip to content

Commit 727fe16

Browse files
authored
Merge pull request kubernetes#125421 from benluddy/cbor-simple-values
KEP-4222: Reject CBOR simple values other than true, false, and null.
2 parents d0579b6 + 326e0a4 commit 727fe16

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/appendixa_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,14 @@ func TestAppendixA(t *testing.T) {
249249
{
250250
example: hex("f7"),
251251
reject: "only simple values false, true, and null have a clear analog",
252-
fixme: "the undefined simple value should not successfully decode as nil",
253252
},
254253
{
255254
example: hex("f0"),
256255
reject: "only simple values false, true, and null have a clear analog",
257-
fixme: "simple values other than false, true, and null should be rejected",
258256
},
259257
{
260258
example: hex("f8ff"),
261259
reject: "only simple values false, true, and null have a clear analog",
262-
fixme: "simple values other than false, true, and null should be rejected",
263260
},
264261
{
265262
example: hex("c074323031332d30332d32315432303a30343a30305a"),

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ import (
2222
"github.com/fxamacker/cbor/v2"
2323
)
2424

25+
var simpleValues *cbor.SimpleValueRegistry = func() *cbor.SimpleValueRegistry {
26+
var opts []func(*cbor.SimpleValueRegistry) error
27+
for sv := 0; sv <= 255; sv++ {
28+
// Reject simple values 0-19, 23, and 32-255. The simple values 24-31 are reserved
29+
// and considered ill-formed by the CBOR specification. We only accept false (20),
30+
// true (21), and null (22).
31+
switch sv {
32+
case 20: // false
33+
case 21: // true
34+
case 22: // null
35+
case 24, 25, 26, 27, 28, 29, 30, 31: // reserved
36+
default:
37+
opts = append(opts, cbor.WithRejectedSimpleValue(cbor.SimpleValue(sv)))
38+
}
39+
}
40+
simpleValues, err := cbor.NewSimpleValueRegistryFromDefaults(opts...)
41+
if err != nil {
42+
panic(err)
43+
}
44+
return simpleValues
45+
}()
46+
2547
var Decode cbor.DecMode = func() cbor.DecMode {
2648
decode, err := cbor.DecOptions{
2749
// Maps with duplicate keys are well-formed but invalid according to the CBOR spec
@@ -100,6 +122,9 @@ var Decode cbor.DecMode = func() cbor.DecMode {
100122
// Reject the arbitrary-precision integer tags because they can't be faithfully
101123
// roundtripped through the allowable Unstructured types.
102124
BignumTag: cbor.BignumTagForbidden,
125+
126+
// Reject anything other than the simple values true, false, and null.
127+
SimpleValues: simpleValues,
103128
}.DecMode()
104129
if err != nil {
105130
panic(err)

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode_test.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,11 @@ func TestDecode(t *testing.T) {
585585
{
586586
name: "simple value 23",
587587
in: hex("f7"), // undefined
588-
assertOnError: func(t *testing.T, e error) {
589-
// TODO: Once this can pass, make the assertion stronger.
590-
if e == nil {
591-
t.Error("expected non-nil error")
588+
assertOnError: assertOnConcreteError(func(t *testing.T, e *cbor.UnacceptableDataItemError) {
589+
if diff := cmp.Diff(&cbor.UnacceptableDataItemError{CBORType: "primitives", Message: "simple value 23 is not recognized"}, e); diff != "" {
590+
t.Errorf("unexpected error diff:\n%s", diff)
592591
}
593-
},
594-
fixme: "cbor simple value 23 (\"undefined\") should not be accepted",
592+
}),
595593
},
596594
}, func() (generated []test) {
597595
// Generate test cases for all simple values (0 to 255) because the number of possible simple values is fixed and small.
@@ -619,13 +617,11 @@ func TestDecode(t *testing.T) {
619617
})
620618
default:
621619
// reject all unrecognized simple values
622-
each.assertOnError = func(t *testing.T, e error) {
623-
// TODO: Once this can pass, make the assertion stronger.
624-
if e == nil {
625-
t.Error("expected non-nil error")
620+
each.assertOnError = assertOnConcreteError(func(t *testing.T, e *cbor.UnacceptableDataItemError) {
621+
if diff := cmp.Diff(&cbor.UnacceptableDataItemError{CBORType: "primitives", Message: fmt.Sprintf("simple value %d is not recognized", i)}, e); diff != "" {
622+
t.Errorf("unexpected error diff:\n%s", diff)
626623
}
627-
}
628-
each.fixme = "unrecognized simple values should be rejected"
624+
})
629625
}
630626
generated = append(generated, each)
631627
}

0 commit comments

Comments
 (0)