json.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package json implements fuzzers for json deserialization routines in
  14. // Kubernetes. These targets are compatible with the github.com/dvyukov/go-fuzz
  15. // fuzzing framework.
  16. package json
  17. import (
  18. "bytes"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/apimachinery/pkg/runtime/serializer/json"
  22. )
  23. var (
  24. gvk = &schema.GroupVersionKind{Version: "v1"}
  25. strictOpt = json.SerializerOptions{Yaml: false, Pretty: false, Strict: true}
  26. strictYamlOpt = json.SerializerOptions{Yaml: true, Pretty: false, Strict: true}
  27. strictPrettyOpt = json.SerializerOptions{Yaml: false, Pretty: true, Strict: true}
  28. nonstrictOpt = json.SerializerOptions{Yaml: false, Pretty: false, Strict: false}
  29. nonstrictYamlOpt = json.SerializerOptions{Yaml: true, Pretty: false, Strict: false}
  30. nonstrictPrettyOpt = json.SerializerOptions{Yaml: false, Pretty: true, Strict: false}
  31. scheme = runtime.NewScheme()
  32. strictSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictOpt)
  33. ysSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictYamlOpt)
  34. psSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictPrettyOpt)
  35. nonstrictSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictOpt)
  36. ynsSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictYamlOpt)
  37. pnsSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictPrettyOpt)
  38. )
  39. // FuzzStrictDecode is a fuzz target for "k8s.io/apimachinery/pkg/runtime/serializer/json" strict decoding.
  40. func FuzzStrictDecode(data []byte) int {
  41. obj0, _, err0 := strictSer.Decode(data, gvk, nil)
  42. obj1, _, err1 := nonstrictSer.Decode(data, gvk, nil)
  43. obj2, _, err2 := ysSer.Decode(data, gvk, nil)
  44. obj3, _, err3 := psSer.Decode(data, gvk, nil)
  45. if obj0 == nil {
  46. if obj1 != nil {
  47. panic("NonStrict is stricter than Strict")
  48. }
  49. if obj2 != nil {
  50. panic("Yaml strict different from plain strict")
  51. }
  52. if obj3 != nil {
  53. panic("Pretty strict different from plain strict")
  54. }
  55. if err0 == nil || err1 == nil || err2 == nil || err3 == nil {
  56. panic("no error")
  57. }
  58. return 0
  59. }
  60. if err0 != nil {
  61. panic("got object and error for strict")
  62. }
  63. if err2 != nil {
  64. panic("got object and error for yaml strict")
  65. }
  66. if err3 != nil {
  67. panic("got object and error pretty strict")
  68. }
  69. var b0 bytes.Buffer
  70. err4 := strictSer.Encode(obj0, &b0)
  71. if err4 != nil {
  72. panic("Can't encode decoded data")
  73. }
  74. if !bytes.Equal(b0.Bytes(), data) {
  75. panic("Encoded data doesn't match original")
  76. }
  77. b0.Reset()
  78. err5 := ysSer.Encode(obj1, &b0)
  79. if err5 != nil {
  80. panic("Can't encode yaml strict decoded data")
  81. }
  82. if !bytes.Equal(b0.Bytes(), data) {
  83. panic("Encoded yaml strict data doesn't match original")
  84. }
  85. b0.Reset()
  86. err6 := psSer.Encode(obj2, &b0)
  87. if err6 != nil {
  88. panic("Can't encode pretty strict decoded data")
  89. }
  90. if !bytes.Equal(b0.Bytes(), data) {
  91. panic("Encoded pretty strict data doesn't match original")
  92. }
  93. b0.Reset()
  94. err7 := nonstrictSer.Encode(obj3, &b0)
  95. if err7 != nil {
  96. panic("Can't encode nonstrict decoded data")
  97. }
  98. if !bytes.Equal(b0.Bytes(), data) {
  99. panic("Encoded nonstrict data doesn't match original")
  100. }
  101. return 1
  102. }
  103. // FuzzNonStrictDecode is a fuzz target for "k8s.io/apimachinery/pkg/runtime/serializer/json" non-strict decoding.
  104. func FuzzNonStrictDecode(data []byte) int {
  105. obj0, _, err0 := nonstrictSer.Decode(data, gvk, nil)
  106. if err0 != nil {
  107. return 0
  108. }
  109. var b0 bytes.Buffer
  110. err1 := nonstrictSer.Encode(obj0, &b0)
  111. if err1 != nil {
  112. panic("Can't nonstrict encode decoded data")
  113. }
  114. _, _, err2 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
  115. if err2 != nil {
  116. panic("Can't nonstrict decode encoded data")
  117. }
  118. b0.Reset()
  119. err3 := ynsSer.Encode(obj0, &b0)
  120. if err3 != nil {
  121. panic("Can't yaml strict encode decoded data")
  122. }
  123. _, _, err4 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
  124. if err4 != nil {
  125. panic("Can't nonstrict decode encoded data")
  126. }
  127. b0.Reset()
  128. err5 := pnsSer.Encode(obj0, &b0)
  129. if err5 != nil {
  130. panic("Can't pretty strict encode decoded data")
  131. }
  132. _, _, err6 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
  133. if err6 != nil {
  134. panic("Can't nonstrict decode encoded data")
  135. }
  136. return 1
  137. }