Skip to content

Conversation

@cx-artur-ribeiro
Copy link
Contributor

@cx-artur-ribeiro cx-artur-ribeiro commented Dec 29, 2025

Reason for Proposed Changes

  • KICS fails to detect policy-related security issues when scanning Terraform plan JSON files, while correctly identifying the same issues in HCL source files (.tf).
  • This inconsistency occurs because policies are represented differently in the parsed payloads:
    • In HCL source files: Policies using jsonencode() are parsed as escaped JSON strings (e.g., "policy": "{"Statement":[...]}")
    • In Terraform plan files: The same policies are already resolved as JSON objects (e.g., "policy": {"Statement": [...]})
  • The current implementation in terraform.rego uses json_unmarshal() which only handles string inputs, causing queries to silently fail on plan files where policies are already objects. This results in various different vulnerabilities being missed when scanning Terraform plans;

Proposed Changes

  • Update terraform.rego to use common_lib.get_policy() instead of common_lib.json_unmarshal() for policy parsing. The get_policy() function handles both string and object formats consistently;
  • This ensures policy-based queries produce consistent results regardless of whether they scan HCL source files or Terraform plan JSON files.

Note

  • There are additional queries that need the same update. That update won't be done in the context of this pull request.
  • I will open a new pull request to tackle the rest of the queries that need the same update.

I submit this contribution under the Apache-2.0 license.

@cx-artur-ribeiro cx-artur-ribeiro self-assigned this Dec 29, 2025
@cx-artur-ribeiro cx-artur-ribeiro requested a review from a team as a code owner December 29, 2025 14:20
@github-actions github-actions bot added query New query feature terraform Terraform query labels Dec 29, 2025
@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

Copy link
Contributor

@cx-eduardo-semanas cx-eduardo-semanas left a comment

Choose a reason for hiding this comment

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

LGTM

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

Labels

query New query feature terraform Terraform query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants