Skip to content

Commit 5b3fe05

Browse files
authored
Merge pull request kubernetes#88695 from gavinfish/unsafe-json
Fix unsafe json construction for scale.go and codec_check.go
2 parents 3fc7831 + ef469a7 commit 5b3fe05

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

staging/src/k8s.io/apimachinery/pkg/runtime/codec_check.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"reflect"
2222

2323
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"k8s.io/apimachinery/pkg/util/json"
2425
)
2526

2627
// CheckCodec makes sure that the codec can encode objects like internalType,
@@ -32,7 +33,14 @@ func CheckCodec(c Codec, internalType Object, externalTypes ...schema.GroupVersi
3233
return fmt.Errorf("Internal type not encodable: %v", err)
3334
}
3435
for _, et := range externalTypes {
35-
exBytes := []byte(fmt.Sprintf(`{"kind":"%v","apiVersion":"%v"}`, et.Kind, et.GroupVersion().String()))
36+
typeMeta := TypeMeta{
37+
Kind: et.Kind,
38+
APIVersion: et.GroupVersion().String(),
39+
}
40+
exBytes, err := json.Marshal(&typeMeta)
41+
if err != nil {
42+
return err
43+
}
3644
obj, err := Decode(c, exBytes)
3745
if err != nil {
3846
return fmt.Errorf("external type %s not interpretable: %v", et, err)

staging/src/k8s.io/kubectl/pkg/scale/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
1313
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
1414
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
15+
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
1516
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
1617
"//staging/src/k8s.io/client-go/scale:go_default_library",
1718
],

staging/src/k8s.io/kubectl/pkg/scale/scale.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime/schema"
2929
"k8s.io/apimachinery/pkg/types"
30+
"k8s.io/apimachinery/pkg/util/json"
3031
"k8s.io/apimachinery/pkg/util/wait"
3132
scaleclient "k8s.io/client-go/scale"
3233
)
@@ -136,7 +137,21 @@ func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *Scale
136137
return updatedScale.ResourceVersion, nil
137138
}
138139

139-
patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, newSize))
140+
// objectForReplicas is used for encoding scale patch
141+
type objectForReplicas struct {
142+
Replicas uint `json:"replicas"`
143+
}
144+
// objectForSpec is used for encoding scale patch
145+
type objectForSpec struct {
146+
Spec objectForReplicas `json:"spec"`
147+
}
148+
spec := objectForSpec{
149+
Spec: objectForReplicas{Replicas: newSize},
150+
}
151+
patch, err := json.Marshal(&spec)
152+
if err != nil {
153+
return "", err
154+
}
140155
patchOptions := metav1.PatchOptions{}
141156
if dryRun {
142157
patchOptions.DryRun = []string{metav1.DryRunAll}

0 commit comments

Comments
 (0)