Check for nil values when parsing HCL strings (#25294)

* fix: when parsing hcl durations, check for nil values and fail validation if present

* docs: add changelog

* style: remove unnecesary function
This commit is contained in:
Juana De La Cuesta
2025-03-06 10:38:33 +01:00
committed by GitHub
parent 6ffe441983
commit 69c2ed55d5
2 changed files with 14 additions and 2 deletions

3
.changelog/25294.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
hcl: Avoid panics by checking null values on durations
```

View File

@@ -42,9 +42,20 @@ func newHCLDecoder() *gohcl.Decoder {
func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics {
srcVal, diags := expr.Value(ctx)
if srcVal.IsNull() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value",
Detail: "Unsuitable duration value: nil",
Subject: expr.StartRange().Ptr(),
Context: expr.Range().Ptr(),
})
return diags
}
if srcVal.Type() == cty.String {
dur, err := time.ParseDuration(srcVal.AsString())
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
@@ -68,7 +79,6 @@ func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{})
Context: expr.Range().Ptr(),
})
return diags
}
err := gocty.FromCtyValue(srcVal, val)
@@ -81,7 +91,6 @@ func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{})
Context: expr.Range().Ptr(),
})
}
return diags
}