From 908c643941e49425cea30207b0744b7eb433f3bf Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 11 Nov 2024 17:32:18 +0100 Subject: [PATCH] blueprint: add map conversion check to fs customization unmarshaller If an element in the `customizations.filesystem` array is not an object, the existing code would fail the conversion without an error, but would fail on the `d["mountpoint"].(type)` conversion, which produces an error message that isn't very useful: mountpoint must be string, got of type The commit catches the conversion error and instead prints a more useful error message. --- pkg/blueprint/filesystem_customizations.go | 5 +++- .../filesystem_customizations_test.go | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/blueprint/filesystem_customizations.go b/pkg/blueprint/filesystem_customizations.go index 172f3b3783..68c7126f06 100644 --- a/pkg/blueprint/filesystem_customizations.go +++ b/pkg/blueprint/filesystem_customizations.go @@ -15,7 +15,10 @@ type FilesystemCustomization struct { } func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error { - d, _ := data.(map[string]interface{}) + d, ok := data.(map[string]interface{}) + if !ok { + return fmt.Errorf("customizations.filesystem is not an object") + } switch d["mountpoint"].(type) { case string: diff --git a/pkg/blueprint/filesystem_customizations_test.go b/pkg/blueprint/filesystem_customizations_test.go index 7bdc195185..4ea1dcf8f9 100644 --- a/pkg/blueprint/filesystem_customizations_test.go +++ b/pkg/blueprint/filesystem_customizations_test.go @@ -104,6 +104,30 @@ func TestFilesystemCustomizationUnmarshalJSONUnhappy(t *testing.T) { } } +func TestFilesystemCustomizationUnmarshalTOMLNotAnObject(t *testing.T) { + cases := []struct { + name string + input string + err string + }{ + { + name: "filesystem is not an object", + input: ` +[customizations] +filesystem = ["hello"]`, + err: "toml: line 3 (last key \"customizations.filesystem\"): customizations.filesystem is not an object", + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + var bp blueprint.Blueprint + err := toml.Unmarshal([]byte(c.input), &bp) + assert.EqualError(t, err, c.err) + }) + } +} + func TestCheckMountpointsPolicy(t *testing.T) { policy := pathpolicy.NewPathPolicies(map[string]pathpolicy.PathPolicy{ "/": {Exact: true},