...

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

Documentation: k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes

     1  /*
     2  Copyright 2024 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package modes
    18  
    19  import (
    20  	"reflect"
    21  
    22  	"github.com/fxamacker/cbor/v2"
    23  )
    24  
    25  var Decode cbor.DecMode = func() cbor.DecMode {
    26  	decode, err := cbor.DecOptions{
    27  		// Maps with duplicate keys are well-formed but invalid according to the CBOR spec
    28  		// and never acceptable. Unlike the JSON serializer, inputs containing duplicate map
    29  		// keys are rejected outright and not surfaced as a strict decoding error.
    30  		DupMapKey: cbor.DupMapKeyEnforcedAPF,
    31  
    32  		// For JSON parity, decoding an RFC3339 string into time.Time needs to be accepted
    33  		// with or without tagging. If a tag number is present, it must be valid.
    34  		TimeTag: cbor.DecTagOptional,
    35  
    36  		// Observed depth up to 16 in fuzzed batch/v1 CronJobList. JSON implementation limit
    37  		// is 10000.
    38  		MaxNestedLevels: 64,
    39  
    40  		MaxArrayElements: 1024,
    41  		MaxMapPairs:      1024,
    42  
    43  		// Indefinite-length sequences aren't produced by this serializer, but other
    44  		// implementations can.
    45  		IndefLength: cbor.IndefLengthAllowed,
    46  
    47  		// Accept inputs that contain CBOR tags.
    48  		TagsMd: cbor.TagsAllowed,
    49  
    50  		// Decode type 0 (unsigned integer) as int64.
    51  		// TODO: IntDecConvertSignedOrFail errors on overflow, JSON will try to fall back to float64.
    52  		IntDec: cbor.IntDecConvertSignedOrFail,
    53  
    54  		// Disable producing map[cbor.ByteString]interface{}, which is not acceptable for
    55  		// decodes into interface{}.
    56  		MapKeyByteString: cbor.MapKeyByteStringForbidden,
    57  
    58  		// Error on map keys that don't map to a field in the destination struct.
    59  		ExtraReturnErrors: cbor.ExtraDecErrorUnknownField,
    60  
    61  		// Decode maps into concrete type map[string]interface{} when the destination is an
    62  		// interface{}.
    63  		DefaultMapType: reflect.TypeOf(map[string]interface{}(nil)),
    64  
    65  		// A CBOR text string whose content is not a valid UTF-8 sequence is well-formed but
    66  		// invalid according to the CBOR spec. Reject invalid inputs. Encoders are
    67  		// responsible for ensuring that all text strings they produce contain valid UTF-8
    68  		// sequences and may use the byte string major type to encode strings that have not
    69  		// been validated.
    70  		UTF8: cbor.UTF8RejectInvalid,
    71  
    72  		// Never make a case-insensitive match between a map key and a struct field.
    73  		FieldNameMatching: cbor.FieldNameMatchingCaseSensitive,
    74  
    75  		// Produce string concrete values when decoding a CBOR byte string into interface{}.
    76  		DefaultByteStringType: reflect.TypeOf(""),
    77  
    78  		// Allow CBOR byte strings to be decoded into string destination values.
    79  		ByteStringToString: cbor.ByteStringToStringAllowed,
    80  
    81  		// Allow CBOR byte strings to match struct fields when appearing as a map key.
    82  		FieldNameByteString: cbor.FieldNameByteStringAllowed,
    83  
    84  		// When decoding an unrecognized tag to interface{}, return the decoded tag content
    85  		// instead of the default, a cbor.Tag representing a (number, content) pair.
    86  		UnrecognizedTagToAny: cbor.UnrecognizedTagContentToAny,
    87  	}.DecMode()
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  	return decode
    92  }()
    93  
    94  // DecodeLax is derived from Decode, but does not complain about unknown fields in the input.
    95  var DecodeLax cbor.DecMode = func() cbor.DecMode {
    96  	opts := Decode.DecOptions()
    97  	opts.ExtraReturnErrors &^= cbor.ExtraDecErrorUnknownField // clear bit
    98  	dm, err := opts.DecMode()
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	return dm
   103  }()
   104  

View as plain text