Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HCL2 panics when parsing undefined variables #10326

Merged
merged 3 commits into from
Apr 8, 2021

Conversation

notnoop
Copy link
Contributor

@notnoop notnoop commented Apr 7, 2021

This PR fixes a panic related to parsing undefined "variables" appearing in HCL2 - HCL2 parser is meant to leave unknown variable references intact (e.g. ${VAR}). However, some attributes can be sophisticated e.g. ${attr.something-with-dots["and_indexing"].3} and the code didn't handle them resulting into a panic.

This PR makes two core changes:

First it updates hcl2 to be base on v2.9.1 with some nomad tweaks: nomad-v2.9.1+tweaks1. This branch is the same as before, but picks up the feedback we got in hashicorp/hcl#413

Secondly, with the updated undefined variable code, we attempt to pick the text of ${....} verbatim from the hcl body. Previously, we'd attempt to regenerate the string from the AST and pray it matches input; the generation is lossy, as the same AST can represent multiple variations (e.g. ${v.0} and ${v[0]} have the same HCLv2 AST). In this change, we attempt to go back to the hcl2 source and find the string snippet corresponding to the variable reference.

Fixes #10316
Fixes #9956

Mahmood Ali added 3 commits April 7, 2021 16:15
With the updated undefined variable code, we attempt to pick the text of
`${....}` verbatim from the hcl body. Previously, we'd attempt to
regenerate the string from the AST and pray it matches input; the
generation is lossy, as the same AST can represent multiple variations
(e.g. `${v.0}` and `${v[0]}` have the same HCLv2 AST). In this change,
we attempt to go back to the hcl2 source and find the string snippet
corresponding to the variable reference.
Copy link
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

LGTM!

v := "${" + expr + "}"
UndefinedVariable: func(t hcl.Traversal) (cty.Value, hcl.Diagnostics) {
body := c.ParseConfig.Body
start := t.SourceRange().Start.Byte
Copy link
Member

Choose a reason for hiding this comment

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

Just for my own clarity here, because the the docs on hcl.Traversal aren't very easy to drop into and come away with a solid understanding... the SourceRange starts right after the ${ we're evaluating and stops either at the } or the end of the body? Just want to make sure that's guaranteed so that the slicing below can't panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imperially, Start.Byte is the offset first non-white space character after ${, and End.Byte is the offset of the first whitespace or }.

It's an error if ${ is present without a corresponding }. I was being a bit more defensive though.

@notnoop notnoop merged commit eb834fe into main Apr 8, 2021
@notnoop notnoop deleted the b-hcl2-undefined-variables branch April 8, 2021 16:10
notnoop pushed a commit that referenced this pull request Apr 21, 2021
This fixes a regression in #10326, to handle unquoted unknown variables.

The HCL job may contain unquoted undefined variable references without ${...} wrapping, e.g. value = meta.node_class. In 1.0.4, this got parsed as value = "${meta.node_class}".

This code performs a scan to find the relevant ${ and }, and only tries to find the closest ones with whitespace as the only separator.
schmichael pushed a commit that referenced this pull request May 14, 2021
Fix HCL2 panics when parsing undefined variables
schmichael pushed a commit that referenced this pull request May 14, 2021
This fixes a regression in #10326, to handle unquoted unknown variables.

The HCL job may contain unquoted undefined variable references without ${...} wrapping, e.g. value = meta.node_class. In 1.0.4, this got parsed as value = "${meta.node_class}".

This code performs a scan to find the relevant ${ and }, and only tries to find the closest ones with whitespace as the only separator.
@tgross tgross added this to the 1.1.0 milestone May 17, 2021
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants