Skip to content

fix(queries): fix policy evaluation when scanning Terraform plan vs HCL files#7926

Merged
cx-artur-ribeiro merged 6 commits into
masterfrom
AST-127589-fix-get-resource-policy
Feb 4, 2026
Merged

fix(queries): fix policy evaluation when scanning Terraform plan vs HCL files#7926
cx-artur-ribeiro merged 6 commits into
masterfrom
AST-127589-fix-get-resource-policy

Conversation

@cx-artur-ribeiro

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

Copy link
Copy Markdown
Contributor

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.
  • A refactor to the query IAM Access Analyzer Not Enabled was carried to improve maintenance and the overall behaviour of the query. However, since the query's objective is to verify the absence of a specific resource, the search_line and search_key are still having problems detecting the same resource each time (depends on the other of the payload structure). When KICS engine is able to point to the root of the project, this query will have the expected behaviour.

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
Copy Markdown
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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@cx-ricardo-jesus cx-ricardo-jesus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@cx-artur-ribeiro cx-artur-ribeiro merged commit 0d393c1 into master Feb 4, 2026
28 checks passed
@cx-artur-ribeiro cx-artur-ribeiro deleted the AST-127589-fix-get-resource-policy branch February 4, 2026 14:55
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.

4 participants