Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe json construction for scale.go and codec_check.go #88695

Merged
merged 1 commit into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion staging/src/k8s.io/apimachinery/pkg/runtime/codec_check.go
Expand Up @@ -21,6 +21,7 @@ import (
"reflect"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
)

// CheckCodec makes sure that the codec can encode objects like internalType,
Expand All @@ -32,7 +33,14 @@ func CheckCodec(c Codec, internalType Object, externalTypes ...schema.GroupVersi
return fmt.Errorf("Internal type not encodable: %v", err)
}
for _, et := range externalTypes {
exBytes := []byte(fmt.Sprintf(`{"kind":"%v","apiVersion":"%v"}`, et.Kind, et.GroupVersion().String()))
typeMeta := TypeMeta{
Kind: et.Kind,
APIVersion: et.GroupVersion().String(),
}
exBytes, err := json.Marshal(&typeMeta)
if err != nil {
return err
}
obj, err := Decode(c, exBytes)
if err != nil {
return fmt.Errorf("external type %s not interpretable: %v", et, err)
Expand Down
1 change: 1 addition & 0 deletions staging/src/k8s.io/kubectl/pkg/scale/BUILD
Expand Up @@ -12,6 +12,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/client-go/scale:go_default_library",
],
Expand Down
17 changes: 16 additions & 1 deletion staging/src/k8s.io/kubectl/pkg/scale/scale.go
Expand Up @@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/wait"
scaleclient "k8s.io/client-go/scale"
)
Expand Down Expand Up @@ -136,7 +137,21 @@ func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *Scale
return updatedScale.ResourceVersion, nil
}

patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, newSize))
// objectForReplicas is used for encoding scale patch
type objectForReplicas struct {
Replicas uint `json:"replicas"`
}
// objectForSpec is used for encoding scale patch
type objectForSpec struct {
Spec objectForReplicas `json:"spec"`
}
spec := objectForSpec{
Spec: objectForReplicas{Replicas: newSize},
}
patch, err := json.Marshal(&spec)
if err != nil {
return "", err
}
patchOptions := metav1.PatchOptions{}
if dryRun {
patchOptions.DryRun = []string{metav1.DryRunAll}
Expand Down