Skip to content

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Aug 29, 2025

Any call to AsString is unsafe. All places where the error is ignored is used in errors, so a placeholder or empty string is ok.

A panic was observed by a customer. I am not sure where it occured. To be safe, I am using the function we have to protect these AsString calls. I added a custom linter to prevent these calls, this PR just removes all calls that were not fixed already.

Comment on lines 301 to 318
tyValStr, ok := hclext.AsString(tyVal)
if !ok {
// Either the val is unknown or null
diag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid %q attribute for block %s", key, block.Label()),
Detail: "Expected a string, got an unknown or null value",
EvalContext: block.Context().Inner(),
}

if tyAttr.IsNotNil() {
diag.Subject = &(tyAttr.HCLAttribute().Range)
// diag.Context = &(block.HCLBlock().DefRange)
diag.Expression = tyAttr.HCLAttribute().Expr
}
return "", diag
}
return tyValStr, nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns an error now instead of a panic.

Comment on lines -14 to +19
// TODO: Should this error be captured?
if key.Type() != cty.String {
return true
}
//nolint:gocritic // string type asserted above
k := key.AsString()
k, ok := AsString(key)
if !ok {
// TODO: Should this error be captured?
return stop
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid values were skipped before, so behavior is unchanged. Just no potential for a panic.

Comment on lines -71 to +76
refParts = append(refParts, fmt.Sprintf("[%s]", part.Key.AsString()))
stringName, ok := AsString(part.Key)
if !ok {
// Nothing we can do, just put a placeholder
stringName = "??"
}
refParts = append(refParts, fmt.Sprintf("[%s]", stringName))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateDotReferenceFromTraversal converts a reference to a string for error messages and logs. So the ?? is ok if that value is really unknown.

Comment on lines +32 to +35
path, ok := hclext.AsString(args[0])
if !ok {
return cty.NilVal, xerrors.Errorf("invalid path argument")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being safe

Comment on lines +42 to +45
name, ok := hclext.AsString(nameVal)
if !ok {
continue
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code above does the same thing as this.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo some suggestions below.

k := key.AsString()
k, ok := AsString(key)
if !ok {
// TODO: Should this error be captured?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no harm to drop a warn diag here, if possible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I cannot really. This is all for merging context values. Which happens in a hook that cannot raise errors.

The context is supposed to store variable (reference) values. So this failing just means a reference/var is skipped. Which leaves it's value as unknown when the context tries to load the value.

Which is the correct behavior at a high level. In reality though, I do not think this should happen. Maybe with some complex structs/types? I need to test more.

@Emyrk Emyrk merged commit 442072e into main Aug 29, 2025
3 checks passed
@Emyrk Emyrk deleted the stevenmasley/unknown_val_deref branch August 29, 2025 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants