From e619e1877889d0d58a3ba00673940f377b3d0559 Mon Sep 17 00:00:00 2001 From: ioterw Date: Thu, 21 Aug 2025 02:27:27 +0700 Subject: [PATCH 1/2] Warn about unknown fields in OCI config --- runsc/specutils/specutils.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index c33dc86ee0..d816938541 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -26,6 +26,7 @@ import ( "strconv" "strings" "time" + "bytes" "github.com/cenkalti/backoff" "github.com/mohae/deepcopy" @@ -215,8 +216,14 @@ func ReadSpecFromFile(bundleDir string, specFile *os.File, conf *config.Config) return nil, fmt.Errorf("error reading spec from file %q: %v", specFile.Name(), err) } var spec specs.Spec - if err := json.Unmarshal(specBytes, &spec); err != nil { - return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err, string(specBytes)) + decoder := json.NewDecoder(bytes.NewReader(specBytes)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(&spec); err != nil { + if err2 := json.Unmarshal(specBytes, &spec); err2 != nil { + return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err2, string(specBytes)) + } else { + log.Warningf("Problem with spec file %q: %v. Consider removing unnecessary fields.", specFile.Name(), err) + } } if err := ValidateSpec(&spec); err != nil { return nil, err From 0662917defa6dbe397cde5b3c967e27876d2bec6 Mon Sep 17 00:00:00 2001 From: ioterw Date: Thu, 21 Aug 2025 10:13:14 +0700 Subject: [PATCH 2/2] Made code more clear --- runsc/specutils/specutils.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index d816938541..43e65ab766 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -218,11 +218,11 @@ func ReadSpecFromFile(bundleDir string, specFile *os.File, conf *config.Config) var spec specs.Spec decoder := json.NewDecoder(bytes.NewReader(specBytes)) decoder.DisallowUnknownFields() - if err := decoder.Decode(&spec); err != nil { - if err2 := json.Unmarshal(specBytes, &spec); err2 != nil { - return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err2, string(specBytes)) + if errLooseDecode := decoder.Decode(&spec); errLooseDecode != nil { + if errStrictDecode := json.Unmarshal(specBytes, &spec); errStrictDecode != nil { + return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), errStrictDecode, string(specBytes)) } else { - log.Warningf("Problem with spec file %q: %v. Consider removing unnecessary fields.", specFile.Name(), err) + log.Warningf("OCI spec file %q contains fields unknown to `runsc`: %v. Ignoring these fields and continuing anyway.", specFile.Name(), errLooseDecode) } } if err := ValidateSpec(&spec); err != nil {