Skip to content

fix(terraform): skip label validation for unresolved Terraform references#7978

Open
cx-ori-bendet wants to merge 1 commit intomasterfrom
fix/metadata-label-terraform-reference
Open

fix(terraform): skip label validation for unresolved Terraform references#7978
cx-ori-bendet wants to merge 1 commit intomasterfrom
fix/metadata-label-terraform-reference

Conversation

@cx-ori-bendet
Copy link
Contributor

Summary

  • KICS's HCL converter wraps bare Terraform references (local.*, var.*, data.*, etc.) in ${...} notation since it cannot resolve them at parse time
  • These wrapped strings (e.g. "${local.resource_name}") fail the Kubernetes label value regex because $, {, } are not valid K8s label characters, causing false positives
  • Added two guards before the regex check:
    1. is_string(labels[key]) — skips non-string values (e.g. nested objects that appear from HCL keys containing dots or slashes — also fixes bug(terraform): Detecting valid label as invalid #7938)
    2. not contains(labels[key], "${") — skips any value containing an unresolved Terraform reference wrapper
  • Fixed a pre-existing typo in result messages: "metada""metadata"
  • Added negative test cases: a local.* reference and a prefixed label key (gateway.istio.io/defaults-for-class)

Fixes #7944
Also fixes #7938

Root cause trace

# HCL input
labels = { app = local.resource_name }

# KICS converter output (wrapExpr in default.go)
"labels": { "app": "${local.resource_name}" }

# Regex check (before fix)
regex.match("^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$", "${local.resource_name}")
# => false  ($ is not in the allowed charset)
# => false == false => true  => FALSE POSITIVE

Test plan

  • Scan a Terraform resource with app = local.resource_name — should produce no finding
  • Scan a resource with label key "gateway.istio.io/defaults-for-class" — should produce no finding
  • Scan a resource with app = "g**dy.l+bel" — should still produce a finding
  • Run go test ./test/... -run TestQueries to verify all query tests pass

I submit this contribution under the Apache-2.0 license.

🤖 Generated with Claude Code

…nces

KICS's HCL converter wraps bare Terraform references (local.*, var.*, etc.)
in \${...} notation since it cannot resolve them at parse time. These wrapped
strings (e.g. \"\${local.resource_name}\") fail the Kubernetes label value
regex because of the \${} characters, causing false positives.

Add two guards before the regex check:
- is_string(): skip non-string values (e.g. nested objects from dotted keys)
- not contains(labels[key], \"\${\"):  skip unresolved Terraform references

Also fix a pre-existing typo in result messages: \"metada\" -> \"metadata\".
Add negative test cases for a Terraform local reference and a prefixed label
key (gateway.istio.io/defaults-for-class).

Fixes #7944

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cx-ori-bendet cx-ori-bendet requested a review from a team as a code owner February 28, 2026 23:31
@github-actions github-actions bot added community Community contribution query New query feature terraform Terraform query kubernetes Kubernetes query labels Feb 28, 2026
@github-actions
Copy link
Contributor

kics-logo

KICS version: v2.1.18

Category Results
CRITICAL CRITICAL 0
HIGH HIGH 0
MEDIUM MEDIUM 0
LOW LOW 0
INFO INFO 0
TRACE TRACE 0
TOTAL TOTAL 0
Metric Values
Files scanned placeholder 1
Files parsed placeholder 1
Files failed to scan placeholder 0
Total executed queries placeholder 47
Queries failed to execute placeholder 0
Execution time placeholder 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Community contribution kubernetes Kubernetes query query New query feature terraform Terraform query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False Positive: Metadata Label Is Invalid (bc3dabb6) for Terraform local variable interpolation bug(terraform): Detecting valid label as invalid

1 participant