/
json.go
153 lines (137 loc) · 4.84 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
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 json implements fuzzers for json deserialization routines in
// Kubernetes. These targets are compatible with the github.com/dvyukov/go-fuzz
// fuzzing framework.
package json
import (
"bytes"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
)
var (
gvk = &schema.GroupVersionKind{Version: "v1"}
strictOpt = json.SerializerOptions{Yaml: false, Pretty: false, Strict: true}
strictYamlOpt = json.SerializerOptions{Yaml: true, Pretty: false, Strict: true}
strictPrettyOpt = json.SerializerOptions{Yaml: false, Pretty: true, Strict: true}
nonstrictOpt = json.SerializerOptions{Yaml: false, Pretty: false, Strict: false}
nonstrictYamlOpt = json.SerializerOptions{Yaml: true, Pretty: false, Strict: false}
nonstrictPrettyOpt = json.SerializerOptions{Yaml: false, Pretty: true, Strict: false}
scheme = runtime.NewScheme()
strictSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictOpt)
ysSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictYamlOpt)
psSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, strictPrettyOpt)
nonstrictSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictOpt)
ynsSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictYamlOpt)
pnsSer = json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, nonstrictPrettyOpt)
)
// FuzzStrictDecode is a fuzz target for "k8s.io/apimachinery/pkg/runtime/serializer/json" strict decoding.
func FuzzStrictDecode(data []byte) int {
obj0, _, err0 := strictSer.Decode(data, gvk, nil)
obj1, _, err1 := nonstrictSer.Decode(data, gvk, nil)
obj2, _, err2 := ysSer.Decode(data, gvk, nil)
obj3, _, err3 := psSer.Decode(data, gvk, nil)
if obj0 == nil {
if obj1 != nil {
panic("NonStrict is stricter than Strict")
}
if obj2 != nil {
panic("Yaml strict different from plain strict")
}
if obj3 != nil {
panic("Pretty strict different from plain strict")
}
if err0 == nil || err1 == nil || err2 == nil || err3 == nil {
panic("no error")
}
return 0
}
if err0 != nil {
panic("got object and error for strict")
}
if err2 != nil {
panic("got object and error for yaml strict")
}
if err3 != nil {
panic("got object and error pretty strict")
}
var b0 bytes.Buffer
err4 := strictSer.Encode(obj0, &b0)
if err4 != nil {
panic("Can't encode decoded data")
}
if !bytes.Equal(b0.Bytes(), data) {
panic("Encoded data doesn't match original")
}
b0.Reset()
err5 := ysSer.Encode(obj1, &b0)
if err5 != nil {
panic("Can't encode yaml strict decoded data")
}
if !bytes.Equal(b0.Bytes(), data) {
panic("Encoded yaml strict data doesn't match original")
}
b0.Reset()
err6 := psSer.Encode(obj2, &b0)
if err6 != nil {
panic("Can't encode pretty strict decoded data")
}
if !bytes.Equal(b0.Bytes(), data) {
panic("Encoded pretty strict data doesn't match original")
}
b0.Reset()
err7 := nonstrictSer.Encode(obj3, &b0)
if err7 != nil {
panic("Can't encode nonstrict decoded data")
}
if !bytes.Equal(b0.Bytes(), data) {
panic("Encoded nonstrict data doesn't match original")
}
return 1
}
// FuzzNonStrictDecode is a fuzz target for "k8s.io/apimachinery/pkg/runtime/serializer/json" non-strict decoding.
func FuzzNonStrictDecode(data []byte) int {
obj0, _, err0 := nonstrictSer.Decode(data, gvk, nil)
if err0 != nil {
return 0
}
var b0 bytes.Buffer
err1 := nonstrictSer.Encode(obj0, &b0)
if err1 != nil {
panic("Can't nonstrict encode decoded data")
}
_, _, err2 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
if err2 != nil {
panic("Can't nonstrict decode encoded data")
}
b0.Reset()
err3 := ynsSer.Encode(obj0, &b0)
if err3 != nil {
panic("Can't yaml strict encode decoded data")
}
_, _, err4 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
if err4 != nil {
panic("Can't nonstrict decode encoded data")
}
b0.Reset()
err5 := pnsSer.Encode(obj0, &b0)
if err5 != nil {
panic("Can't pretty strict encode decoded data")
}
_, _, err6 := nonstrictSer.Decode(b0.Bytes(), gvk, nil)
if err6 != nil {
panic("Can't nonstrict decode encoded data")
}
return 1
}