Skip to content

Fix unsafe type assertions in provider data handling #847

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

Merged
merged 4 commits into from
Jun 12, 2025
Merged

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 5, 2025

This PR addresses critical type assertion safety issues in the Terraform provider that could cause runtime panics and misleading error messages.

Issues Fixed

Issue 1: Unsafe Direct Type Assertion

Several Configure functions were performing direct type assertions without safety checks:

// BEFORE - Unsafe: Could panic if req.ProviderData is not *api.ProviderClient
client := req.ProviderData.(*api.ProviderClient).Api

Issue 2: Misleading Error Messages

When type assertions failed or Api was nil, error messages incorrectly referenced *http.Client instead of the actual expected type:

// BEFORE - Misleading error message
resp.Diagnostics.AddError(
    "Unexpected Resource Configure Type",
    fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

Solution

Replaced all unsafe type assertions with the safe pattern used elsewhere in the codebase:

// AFTER - Safe type assertion with proper error handling
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
    resp.Diagnostics.AddError(
        "Unexpected Resource Configure Type",
        fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
    )
    return
}

For cases where additional nil checks were needed, provided clear error messages:

if client.Api == nil {
    resp.Diagnostics.AddError(
        "Nil Api client",
        "ProviderData contained a *api.ProviderClient but with nil Api. Please check provider initialization and credentials.",
    )
    return
}

Files Changed

  • internal/services/environment_group_rule_set/resource_environment_group_rule_set.go
  • internal/services/managed_environment/resource_managed_environment.go
  • internal/services/environment_settings/resources_environment_settings.go
  • internal/services/languages/datasource_languages.go
  • internal/services/data_record/datasource_data_record.go
  • internal/services/data_record/resource_data_record.go
  • internal/services/tenant_settings/resource_tenant_settings.go

Testing

  • ✅ All existing unit tests pass
  • ✅ Build and lint checks pass
  • ✅ Documentation generated successfully

These changes improve provider reliability by preventing potential runtime panics and providing clearer error messages for debugging configuration issues.

Fixes #841.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Copilot] Provider Data Type Assertion Safety Issues Fix unsafe type assertions in provider data handling Jun 5, 2025
@Copilot Copilot AI requested a review from mawasile June 5, 2025 15:14
Copilot finished work on behalf of mawasile June 5, 2025 15:15
@mawasile mawasile marked this pull request as ready for review June 9, 2025 08:46
@Copilot Copilot AI review requested due to automatic review settings June 9, 2025 08:46
@mawasile mawasile requested a review from a team as a code owner June 9, 2025 08:46
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves provider reliability by replacing unsafe direct type assertions with safe, conditional checks that prevent runtime panics and clarify error messages. Key changes include:

  • Replacing unsafe type assertions with safe checks for provider data in resource configuration.
  • Updating error messages to correctly indicate the expected type (*api.ProviderClient) instead of *http.Client.
  • Applying these changes across multiple resource and datasource files.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
internal/services/tenant_settings/resource_tenant_settings.go Updated safe type assertion for tenant settings with revised error messaging.
internal/services/managed_environment/resource_managed_environment.go Introduced a nil check for the Api field before client initialization.
internal/services/languages/datasource_languages.go Applied safe type assertion and updated error messaging in the Languages datasource.
internal/services/environment_settings/resources_environment_settings.go Modified safe type assertion for environment settings resources.
internal/services/environment_group_rule_set/resource_environment_group_rule_set.go Revised safe type assertion and error messaging for environment group rule sets.
internal/services/data_record/resource_data_record.go Implemented safe type assertion in the Data Record resource configuration.
internal/services/data_record/datasource_data_record.go Updated safe type assertion for the Data Record datasource.
.changes/unreleased/fixed-20250605-151206.yaml Documented the fix in the unreleased changes log.

@mawasile mawasile added ai assisted Pull requests that have be fully or partially implemented using Copilot or AI ai found Issues and Bugs that were found using AI copilot fixed using GitHub copilot autonomous agent labels Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
@mattdot mattdot merged commit f90dd96 into main Jun 12, 2025
11 checks passed
@mattdot mattdot deleted the copilot/fix-841 branch June 12, 2025 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai assisted Pull requests that have be fully or partially implemented using Copilot or AI ai found Issues and Bugs that were found using AI copilot fixed using GitHub copilot autonomous agent
Projects
None yet
Development

Error loading sessions

Retrying...

Successfully merging this pull request may close these issues.

[Copilot] Provider Data Type Assertion Safety Issues
4 participants