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

panic if JSON variable file has extraneous characters #24895

Closed
felixplajer opened this issue May 7, 2020 · 2 comments
Closed

panic if JSON variable file has extraneous characters #24895

felixplajer opened this issue May 7, 2020 · 2 comments

Comments

@felixplajer
Copy link

Terraform Version

Terraform v0.12.24
+ provider.null v2.1.2

Terraform Configuration Files

main.tf:

variable "foo" {
  type        = map
}

resource "null_resource" "blah" {
}

Note, the same crash occurs if you have no configuration files at all. It's only related to the variable file.

vars.tfvars.json:

{
    "foo" : {
        "a" : {
            "b": "c"
        }
    / }
}

Several other characters also cause a panic: \, #, etc.

Debug Output

see crash log

Crash Output

crash log

Expected Behavior

Should get an error that the JSON is invalid, similar to what happens in other cases of invalid JSON.

Actual Behavior

panic!

Steps to Reproduce

terraform init
terraform plan --var-file=vars.tfvars.json

Additional Context

Arguably it's on the user to not provide invalid JSON, but, well, the crash output says This is always indicative of a bug within Terraform so here I am. I noticed this issue when absentmindedly trying to "comment out" part of the variable file 😛

References

@apparentlymart
Copy link
Contributor

Hi @felixplajer! Sorry for this crash and thanks for reporting it.

I think you may have encountered the same bug that hashicorp/hcl#358 was aiming to fix. That fix has now been merged, and we have the new HCL version containing the fix in Terraform's master branch, so this issue ought to be resolved in the forthcoming Terraform v0.13 release.

I tried it on a build from the master branch to make sure, and indeed it didn't crash, though the errors it detected for your input were a bit "noisy" because the parser wasn't able to figure out what was going on enough to give a precise error in this case:

Error: Unclosed object

  on terraform.tfvars.json line 1:
   1: {

No closing brace was found for this JSON object.


Error: Root value must be object

  on terraform.tfvars.json line 1:
   1: {

The root value in a JSON-based configuration must be either a JSON object or a
JSON array of objects.


Error: Missing attribute seperator comma

  on terraform.tfvars.json line 6:
   6:     / }

A comma must appear between each property definition in an object.

The third error does at least point to the problematic character though, which is better than the old behavior. HCL could potentially do a better job here by simply reporting that / is not a valid character in that context at all, but since that's relatively minor I'm going to consider this bug fixed -- it's no longer crashing -- and let the decision about whether to improve the JSON syntax error handling in HCL happen in the context of the HCL repository, rather than here.

Thanks again for reporting this!

@ghost
Copy link

ghost commented Jun 12, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jun 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants