From 69c2ed55d5c1c6b1574600210579c0dbfca62777 Mon Sep 17 00:00:00 2001 From: Juana De La Cuesta Date: Thu, 6 Mar 2025 10:38:33 +0100 Subject: [PATCH] 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 --- .changelog/25294.txt | 3 +++ jobspec2/hcl_conversions.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .changelog/25294.txt diff --git a/.changelog/25294.txt b/.changelog/25294.txt new file mode 100644 index 000000000..33ab42288 --- /dev/null +++ b/.changelog/25294.txt @@ -0,0 +1,3 @@ +```release-note:bug +hcl: Avoid panics by checking null values on durations +``` diff --git a/jobspec2/hcl_conversions.go b/jobspec2/hcl_conversions.go index ecf5e4714..443afe288 100644 --- a/jobspec2/hcl_conversions.go +++ b/jobspec2/hcl_conversions.go @@ -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 }