From 3ba9720b3e4883fec2708fc1085684f9945b40dc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 10 Dec 2016 19:27:01 -0500 Subject: [PATCH] config: validate invalid variable keys Fixes #9416 A simple change to verify that only valid keys for `variable` blocks are used. --- config/loader_hcl.go | 14 ++++++-------- config/loader_test.go | 7 +++++++ config/test-fixtures/var-invalid-key.tf | 3 +++ 3 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 config/test-fixtures/var-invalid-key.tf diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 881a98312..1b9348857 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -410,14 +410,12 @@ func loadVariablesHcl(list *ast.ObjectList) ([]*Variable, error) { item.Pos(), NameRegexp) } - /* - // TODO: catch extra fields - // Decode into raw map[string]interface{} so we know ALL fields - var config map[string]interface{} - if err := hcl.DecodeObject(&config, item.Val); err != nil { - return nil, err - } - */ + // Check for invalid keys + valid := []string{"type", "default", "description"} + if err := checkHCLKeys(item.Val, valid); err != nil { + return nil, multierror.Prefix(err, fmt.Sprintf( + "variable[%s]:", n)) + } // Decode into hclVariable to get typed values var hclVar hclVariable diff --git a/config/loader_test.go b/config/loader_test.go index f7fad33c0..caf25da2c 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -67,6 +67,13 @@ func TestLoadFile_lifecycleKeyCheck(t *testing.T) { t.Logf("err: %s", err) } +func TestLoadFile_varInvalidKey(t *testing.T) { + _, err := LoadFile(filepath.Join(fixtureDir, "var-invalid-key.tf")) + if err == nil { + t.Fatal("should have error") + } +} + func TestLoadFile_resourceArityMistake(t *testing.T) { _, err := LoadFile(filepath.Join(fixtureDir, "resource-arity-mistake.tf")) if err == nil { diff --git a/config/test-fixtures/var-invalid-key.tf b/config/test-fixtures/var-invalid-key.tf new file mode 100644 index 000000000..ad967b53d --- /dev/null +++ b/config/test-fixtures/var-invalid-key.tf @@ -0,0 +1,3 @@ +variable "a" { + a = "b" +}