Fix NPE in CEl accessors of additionalProperties=true objects

This commit is contained in:
Joe Betz
2025-11-05 15:08:15 -05:00
committed by Your Name
parent e95f580e5a
commit de4e04baf6
3 changed files with 83 additions and 0 deletions

View File

@@ -297,6 +297,26 @@ func TestTypeCheck(t *testing.T) {
},
}},
}}
reproducerPolicy := &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{
Validations: []v1.Validation{
{
Expression: "has(object.spec.text)",
},
},
MatchConstraints: &v1.MatchResources{ResourceRules: []v1.NamedRuleWithOperations{
{
RuleWithOperations: v1.RuleWithOperations{
Rule: v1.Rule{
APIGroups: []string{"example.com"},
APIVersions: []string{"v1alpha1"},
Resources: []string{"reproducers"},
},
},
},
}},
}}
for _, tc := range []struct {
name string
schemaToReturn *spec.Schema
@@ -437,6 +457,39 @@ func TestTypeCheck(t *testing.T) {
toContain("found no matching overload for 'allowed' applied to 'kubernetes.authorization.Authorizer"),
},
},
{
name: "additionalProperties: true",
policy: reproducerPolicy,
schemaToReturn: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"spec": {
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"text": *spec.StringProperty(),
},
},
},
"status": {
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"problematicProperty": {
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
},
},
},
},
},
},
},
},
assertions: []assertionFunc{toBeEmpty},
},
{
name: "variables valid",
policy: &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{

View File

@@ -36,6 +36,9 @@ type SchemaOrBool struct {
}
func (sb *SchemaOrBool) Schema() common.Schema {
if sb.SchemaOrBool.Schema == nil {
return nil
}
return &Schema{Schema: sb.SchemaOrBool.Schema}
}

View File

@@ -29,6 +29,33 @@ import (
)
func TestSchemaDeclType(t *testing.T) {
t.Run("a schema with additionalProperties: true should be an valid object type", func(t *testing.T) {
schema := &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"foo": {
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
},
},
},
},
}
declType := SchemaDeclType(schema, false)
if declType == nil {
t.Fatal("SchemaDeclType returned nil")
}
fooField, found := declType.FindField("foo")
if !found {
t.Fatal("field 'foo' not found")
}
fooType := fooField.Type
if fooType.TypeName() != "object" {
t.Fatalf("expected foo to be a object, but got %s", fooType.TypeName())
}
})
ts := testSchema()
cust := SchemaDeclType(ts, false)
if cust.TypeName() != "object" {