13
0
Fork 0
mirror of https://github.com/yannh/kubeconform.git synced 2026-06-28 16:00:44 +00:00

fix: avoid panic when a schema document decodes to null (#356)
Some checks are pending
ci / kubeconform-test (push) Waiting to run
ci / openapi2jsonschema-test (push) Waiting to run
ci / goreleaser (push) Blocked by required conditions

This commit is contained in:
somaz 2026-06-04 16:40:54 +09:00 committed by GitHub
parent d412494432
commit 8e634e18c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View file

@ -372,6 +372,13 @@ func downloadSchema(registries []registry.Registry, l jsonschema.SchemeURLLoader
for _, reg := range registries {
path, s, err = reg.DownloadSchema(kind, version, k8sVersion)
if err == nil {
// A schema document that decodes to nil (for example a file whose
// entire content is the literal "null") is accepted by AddResource
// but makes Compile dereference a nil root and panic. Treat it as a
// non-parseable response and try the next registry instead.
if s == nil {
continue
}
c := jsonschema.NewCompiler()
c.RegisterFormat(&jsonschema.Format{"duration", validateDuration})
c.UseLoader(l)

View file

@ -460,6 +460,40 @@ interval: test
Invalid,
[]ValidationError{{Path: "/interval", Msg: "'test' is not valid duration: must start with P"}},
},
{
// A schema file whose entire content is the literal "null" decodes to
// a nil document. It used to reach jsonschema's Compile and panic with
// a nil pointer dereference (issue #337); it must now be treated as a
// missing schema instead.
"schema document is null",
[]byte(`
kind: name
apiVersion: v1
firstName: foo
`),
[]byte(`null`),
nil,
true,
false,
Skipped,
[]ValidationError{},
},
{
// Same as above but with IgnoreMissingSchemas disabled: a null schema
// document must surface as a graceful Error, not a panic (issue #337).
"schema document is null, do not ignore missing",
[]byte(`
kind: name
apiVersion: v1
firstName: foo
`),
[]byte(`null`),
nil,
false,
false,
Error,
[]ValidationError{},
},
} {
val := v{
opts: Opts{