Skip to content

Commit 86ffaff

Browse files
authored
Merge pull request #529 from mengqiy/decodercheck
✨ decoder now errors out when encountering an empty runtime.RawExtension
2 parents 8f633b1 + ad914c1 commit 86ffaff

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/webhook/admission/decode.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package admission
1818

1919
import (
20+
"fmt"
21+
2022
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2123
"k8s.io/apimachinery/pkg/runtime"
2224
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -36,11 +38,17 @@ func NewDecoder(scheme *runtime.Scheme) (*Decoder, error) {
3638

3739
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
3840
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
41+
// It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
3942
func (d *Decoder) Decode(req Request, into runtime.Object) error {
43+
// we error out if rawObj is an empty object.
44+
if len(req.Object.Raw) == 0 {
45+
return fmt.Errorf("there is no content to decode")
46+
}
4047
return d.DecodeRaw(req.Object, into)
4148
}
4249

4350
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
51+
// It errors out if rawObj is empty i.e. containing 0 raw bytes.
4452
func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
4553
// NB(directxman12): there's a bug/weird interaction between decoders and
4654
// the API server where the API server doesn't send a GVK on the embedded
@@ -49,6 +57,11 @@ func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) er
4957
// and call unstructured's special Unmarshal implementation, which calls
5058
// back into that same decoder :-/
5159
// See kubernetes/kubernetes#74373.
60+
61+
// we error out if rawObj is an empty object.
62+
if len(rawObj.Raw) == 0 {
63+
return fmt.Errorf("there is no content to decode")
64+
}
5265
if unstructuredInto, isUnstructured := into.(*unstructured.Unstructured); isUnstructured {
5366
// unmarshal into unstructured's underlying object to avoid calling the decoder
5467
if err := json.Unmarshal(rawObj.Raw, &unstructuredInto.Object); err != nil {

0 commit comments

Comments
 (0)