/* Copyright 2019 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package yaml implements fuzzers for yaml deserialization routines in // Kubernetes. These targets are compatible with the github.com/dvyukov/go-fuzz // fuzzing framework. package yaml import ( "fmt" "strings" "gopkg.in/yaml.v2" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" sigyaml "sigs.k8s.io/yaml" ) // FuzzDurationStrict is a fuzz target for strict-unmarshaling Duration defined // in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the // unmarshaled result can be marshaled back to the input. func FuzzDurationStrict(b []byte) int { var durationHolder struct { D metav1.Duration `json:"d"` } if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil { return 0 } result, err := sigyaml.Marshal(&durationHolder) if err != nil { panic(err) } // Result is in the format "d: \n", so strip off the trailing // newline and convert durationHolder.D to the expected format. resultStr := strings.TrimSpace(string(result[:])) inputStr := fmt.Sprintf("d: %s", durationHolder.D.Duration) if resultStr != inputStr { panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr)) } return 1 } // FuzzMicroTimeStrict is a fuzz target for strict-unmarshaling MicroTime // defined in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks // that the unmarshaled result can be marshaled back to the input. func FuzzMicroTimeStrict(b []byte) int { var microTimeHolder struct { T metav1.MicroTime `json:"t"` } if err := sigyaml.UnmarshalStrict(b, µTimeHolder); err != nil { return 0 } result, err := sigyaml.Marshal(µTimeHolder) if err != nil { panic(err) } // Result is in the format "t: