From e18818489d4487bec13e0e8cfe6fc7e0e15d771a Mon Sep 17 00:00:00 2001 From: Zheng Xi Zhou Date: Sun, 18 Apr 2021 16:30:22 +0800 Subject: [PATCH] Fix panic when calling ParseHCL In function ParseHCL, not all fields of tfconfig.Module is checked, so panic will happen. Fix #https://github.com/hashicorp/terraform-config-inspect/issues/61 --- tfconfig/load_hcl.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tfconfig/load_hcl.go b/tfconfig/load_hcl.go index cb59ce6..a1a0c90 100644 --- a/tfconfig/load_hcl.go +++ b/tfconfig/load_hcl.go @@ -62,6 +62,10 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { switch block.Type { case "terraform": + + if mod.RequiredCore == nil || mod.RequiredProviders == nil { + break + } content, _, contentDiags := block.Body.PartialContent(terraformBlockSchema) diags = append(diags, contentDiags...) @@ -104,6 +108,10 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { } case "variable": + + if mod.Variables == nil { + break + } content, _, contentDiags := block.Body.PartialContent(variableSchema) diags = append(diags, contentDiags...) @@ -177,6 +185,9 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { case "output": + if mod.Outputs == nil { + break + } content, _, contentDiags := block.Body.PartialContent(outputSchema) diags = append(diags, contentDiags...) @@ -204,6 +215,9 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { case "provider": + if mod.RequiredProviders == nil || mod.ProviderConfigs == nil { + break + } content, _, contentDiags := block.Body.PartialContent(providerConfigSchema) diags = append(diags, contentDiags...) @@ -239,6 +253,10 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { case "resource", "data": + if mod.ManagedResources == nil || mod.DataResources == nil { + break + } + content, _, contentDiags := block.Body.PartialContent(resourceSchema) diags = append(diags, contentDiags...) @@ -318,6 +336,10 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { case "module": + if mod.ModuleCalls == nil { + break + } + content, _, contentDiags := block.Body.PartialContent(moduleCallSchema) diags = append(diags, contentDiags...)