From 57c3a5aca59b55253d4dbd792bb629990f271e03 Mon Sep 17 00:00:00 2001 From: "serein6174@163.com" Date: Fri, 31 Jul 2026 19:51:33 +0800 Subject: [PATCH] [fix] Prevent schema validation nil dereferences Return descriptive errors when scalar or map schemas contain missing type information instead of allowing nil pointer dereferences. Add regression tests for malformed scalar and map property schemas. --- opengin/core-api/pkg/schema/utils.go | 8 +++++ opengin/core-api/pkg/schema/utils_test.go | 40 +++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/opengin/core-api/pkg/schema/utils.go b/opengin/core-api/pkg/schema/utils.go index 155f757a..e28ef3ad 100644 --- a/opengin/core-api/pkg/schema/utils.go +++ b/opengin/core-api/pkg/schema/utils.go @@ -223,6 +223,10 @@ func ValidateSchema(value interface{}, schema *SchemaInfo) error { // Helper functions for schema validation func validateScalarValue(value interface{}, typeInfo *typeinference.TypeInfo) error { + if typeInfo == nil { + return fmt.Errorf("type info is nil") + } + if value == nil { if !typeInfo.IsNullable { return fmt.Errorf("value cannot be null") @@ -288,6 +292,10 @@ func validateMapValue(value interface{}, schema *SchemaInfo) error { if err := ValidateSchema(val, propSchema); err != nil { return fmt.Errorf("invalid value for key %s: %v", key, err) } + } else if propSchema == nil { + return fmt.Errorf("schema for key %s is nil", key) + } else if propSchema.TypeInfo == nil { + return fmt.Errorf("type info for key %s is nil", key) } else if !propSchema.TypeInfo.IsNullable { return fmt.Errorf("required key %s is missing", key) } diff --git a/opengin/core-api/pkg/schema/utils_test.go b/opengin/core-api/pkg/schema/utils_test.go index bdf86641..d9151bf3 100644 --- a/opengin/core-api/pkg/schema/utils_test.go +++ b/opengin/core-api/pkg/schema/utils_test.go @@ -427,6 +427,46 @@ func TestValidateSchema(t *testing.T) { } } +func TestValidateSchemaRejectsNilTypeInfo(t *testing.T) { + schema := &SchemaInfo{StorageType: ScalarData} + + err := ValidateSchema("value", schema) + + assert.EqualError(t, err, "type info is nil") +} + +func TestValidateSchemaRejectsInvalidMissingPropertySchema(t *testing.T) { + testCases := []struct { + name string + property *SchemaInfo + expectedErr string + }{ + { + name: "nil property schema", + property: nil, + expectedErr: "schema for key name is nil", + }, + { + name: "nil property type info", + property: &SchemaInfo{StorageType: ScalarData}, + expectedErr: "type info for key name is nil", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + schema := &SchemaInfo{ + StorageType: MapData, + Properties: map[string]*SchemaInfo{"name": tc.property}, + } + + err := ValidateSchema(map[string]interface{}{}, schema) + + assert.EqualError(t, err, tc.expectedErr) + }) + } +} + func TestValidateTabularData(t *testing.T) { // Create a schema for tabular data schema := &SchemaInfo{