Skip to content

Commit a9bb1e3

Browse files
committed
add extra defense
1 parent 50dedf6 commit a9bb1e3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

extract/variable.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@ import (
1414

1515
// VariableFromBlock extracts a terraform variable, but not it's final resolved value.
1616
// code taken mostly from https://github.com/aquasecurity/trivy/blob/main/pkg/iac/scanners/terraform/parser/evaluator.go#L479
17-
func VariableFromBlock(block *terraform.Block) types.Variable {
17+
func VariableFromBlock(block *terraform.Block) (tfVar types.Variable) {
18+
defer func() {
19+
// Extra safety mechanism to ensure that if a panic occurs, we do not break
20+
// everything else.
21+
if r := recover(); r != nil {
22+
tfVar = types.Variable{
23+
Name: block.Label(),
24+
Diagnostics: types.Diagnostics{
25+
{
26+
Severity: hcl.DiagError,
27+
Summary: "Panic occurred in extracting variable. This should not happen, please report this to Coder.",
28+
Detail: fmt.Sprintf("panic in variable extract: %+v", r),
29+
},
30+
},
31+
}
32+
}
33+
}()
34+
1835
attributes := block.Attributes()
1936

2037
var valType cty.Type

0 commit comments

Comments
 (0)