OpenTofu same-basename precedence causes findings from shadowed .tf/.tf.json files #11181
Closed
lokesh0186
started this conversation in
False Detection
Replies: 1 comment
|
Hi @lokesh0186 ! Thanks for the report. Track #11192 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
IDs
AWS-0107, AWS-0099, AWS-0124
Description
Trivy 0.74.0 reports misconfigurations from Terraform files that OpenTofu does not load when same-basename OpenTofu files are present.
OpenTofu gives
main.tofuprecedence overmain.tf, andmain.tofu.jsonprecedence overmain.tf.json. Trivy recognizes all four formats but currently scans both files in each pair.In the HCL example below, Trivy emits three findings from the shadowed
main.tf, including HIGHAWS-0107. In the JSON example, it emitsAWS-0099from the shadowedmain.tf.json. The winning OpenTofu files contain onlyterraform_dataresources and do not contain equivalent AWS configuration.I confirmed the behavior with Trivy's default misconfiguration scan. I did not use
--file-patterns,--skip-files, a custom Trivy configuration, orTRIVY_*environment overrides. The JSON output confirms that the advisories are correct for the AWS resource text; the false detection comes from including source that is inactive under OpenTofu's file-selection rules.Reproduction steps
HCL case:
main.tofu:main.tf:main.tfand rerun the same Trivy command. Trivy reports no findings.JSON case:
main.tofu.json:{ "terraform": { "required_version": ">= 1.8.0" }, "resource": { "terraform_data": { "target": { "input": "effective-opentofu-json-source" }, "consumer": { "input": "${terraform_data.target.output}" } } } }main.tf.json:{ "resource": { "aws_security_group": { "shadowed_json": { "name": "shadowed-json-open-ssh", "ingress": [ { "from_port": 22, "to_port": 22, "protocol": "tcp", "cidr_blocks": ["0.0.0.0/0"] } ] } } } }Run
tofu validate -jsonandtrivy config --format json .. OpenTofu reports the configuration as valid, while Trivy reportsAWS-0099againstaws_security_group.shadowed_jsoninmain.tf.json.Remove only
main.tf.jsonand rerun the same Trivy command. Trivy reports no findings.A minimal Trivy-native regression also reproduces both cases on current main at
dcfb99218f072d1f54576af3c0b4f6fc8fe843f3.I also checked the source-set interpretation independently with public IaC-Guard-V 0.1.0b1; its OpenTofu source-mode evidence marks the same-basename Terraform files as
SHADOWED_BY_TOFU.Would Trivy want to model OpenTofu's effective-file precedence when scanning an OpenTofu project, and if so, how should Trivy distinguish Terraform mode from OpenTofu mode when both source forms are present?
Target
Filesystem
Scanner
Misconfiguration
Debug output
The JSON run has the equivalent parser entries for
main.tf.jsonandmain.tofu.json, reports two detected configuration files, and attributes the finding tomain.tf.json. Local absolute paths have been omitted from this excerpt.Version
Checklist
--format json, checked the reported data source, and confirmed that the AWS advisory is correct for the resource text.All reactions