Skip to content

Fix type assertion safety issues in DTO conversions and data transformations #846

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 5, 2025

This PR fixes critical type safety issues in Data Transfer Object (DTO) conversions and data transformation operations that could cause runtime panics in the Terraform provider.

Issues Fixed

1. Unchecked Type Assertions in tenant_settings/dto.go

Multiple conversion functions performed unsafe type assertions like object.(basetypes.ObjectValue) without checking for assertion failures:

// Before - unsafe
teamIntegrationObject.(basetypes.ObjectValue).As(ctx, &teamsIntegrationSettings, ...)

// After - safe
objectValue, ok := teamIntegrationObject.(basetypes.ObjectValue)
if !ok {
    return // Skip conversion if type assertion fails
}
objectValue.As(ctx, &teamsIntegrationSettings, ...)

2. Unchecked Type Assertions in environment_group_rule_set/dto.go

Similar unsafe patterns in AI generative settings, backup retention, and other conversion functions:

// Before - unsafe
aiGenerativeSettingsObj.(basetypes.ObjectValue).As(ctx, &aiGenerativeSettings, ...)

// After - safe with error handling
objectValue, ok := aiGenerativeSettingsObj.(basetypes.ObjectValue)
if !ok {
    return fmt.Errorf("expected ai_generative_settings to be of type ObjectValue, got %T", aiGenerativeSettingsObj)
}

3. Ignored Error Returns in dlp_policy/helpers.go

The convertToDlpEnvironment function ignored error returns from ElementsAs:

// Before - ignored errors
func convertToDlpEnvironment(ctx context.Context, environmentsInPolicy basetypes.SetValue) []dlpEnvironmentDto {
    envs := []string{}
    environmentsInPolicy.ElementsAs(ctx, &envs, true) // Error ignored
    // ...
}

// After - proper error handling
func convertToDlpEnvironment(ctx context.Context, environmentsInPolicy basetypes.SetValue) ([]dlpEnvironmentDto, error) {
    envs := []string{}
    if err := environmentsInPolicy.ElementsAs(ctx, &envs, true); err != nil {
        return nil, fmt.Errorf("failed to convert environments: %v", err)
    }
    // ...
}

4. Silent Type Assertion Failures in data_record/resource_data_record.go

Case functions silently ignored type assertion failures, leading to missing data:

// Before - silent failures
func caseString(columnValue any, attrValue map[string]attr.Value, attrType map[string]attr.Type, key string) {
    value, ok := columnValue.(string)
    if ok {
        // Only set value if assertion succeeded, silently ignore failures
    }
}

// After - debug logging for failures
func caseString(columnValue any, attrValue map[string]attr.Value, attrType map[string]attr.Type, key string) {
    value, ok := columnValue.(string)
    if !ok {
        tflog.Debug(context.TODO(), "caseString: failed to cast value to string", 
            map[string]any{"key": key, "value_type": fmt.Sprintf("%T", columnValue)})
        return
    }
    // Set value...
}

5. Incomplete Type Support in customtypes/uuid_type.go

The UUID type equality method only supported value types, not pointer types:

// Before - only value type support
func (t UUIDType) Equal(o attr.Type) bool {
    other, ok := o.(UUIDType)
    if !ok {
        return false
    }
    return t.StringType.Equal(other.StringType)
}

// After - supports both value and pointer types
func (t UUIDType) Equal(o attr.Type) bool {
    switch v := o.(type) {
    case UUIDType:
        return t.StringType.Equal(v.StringType)
    case *UUIDType:
        return t.StringType.Equal(v.StringType)
    default:
        return false
    }
}

Impact

These fixes prevent:

  • Runtime panics from failed type assertions that could crash the provider
  • Silent data loss from ignored conversion failures
  • Inconsistent behavior when working with different type representations
  • Difficult debugging by adding proper error logging and handling

Testing

  • ✅ All existing linter checks pass
  • ✅ Build and compilation successful
  • ✅ Documentation generation works correctly
  • ✅ Minimal code changes with no breaking functionality

Fixes #840.


💡 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.

Copilot AI and others added 2 commits June 5, 2025 14:52
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Copilot] DTO and Data Conversion Type Safety Issues Fix type assertion safety issues in DTO conversions and data transformations Jun 5, 2025
@Copilot Copilot AI requested a review from mawasile June 5, 2025 14:54
Copilot finished work on behalf of mawasile June 5, 2025 14:55
@mawasile mawasile marked this pull request as ready for review June 6, 2025 07:16
@Copilot Copilot AI review requested due to automatic review settings June 6, 2025 07:16
@mawasile mawasile requested a review from a team as a code owner June 6, 2025 07:16
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 enhances type safety and error handling across DTO conversions to prevent panics and silent failures.

  • Replace unchecked type assertions with safe checks and skips or error returns in tenant_settings and environment_group_rule_set.
  • Handle and propagate errors from ElementsAs in DLP helper functions and update resource callers.
  • Add debug logging for cast failures in data_record converters and extend UUID type equality to support pointer types.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/services/tenant_settings/dto.go Wrap unsafe assertions with ok checks and skip on failure
internal/services/environment_group_rule_set/dto.go Use safe assertions with error returns or skips for conversions
internal/services/dlp_policy/helpers.go Change convertToDlpEnvironment to return an error on failure
internal/services/dlp_policy/resource_dlp_policy.go Update callers to handle convertToDlpEnvironment errors
internal/services/data_record/resource_data_record.go Add debug logs for failed type casts in case* functions
internal/customtypes/uuid_type.go Extend Equal method to handle both value and pointer types
Comments suppressed due to low confidence (4)

internal/services/tenant_settings/dto.go:222

  • Add unit tests for the failure path when type assertions return !ok to ensure correct skip behavior is exercised.
objectValue, ok := teamIntegrationObject.(basetypes.ObjectValue)

internal/services/dlp_policy/resource_dlp_policy.go:335

  • [nitpick] Enhance the diagnostic detail by including contextual information (e.g. which field or index failed) rather than only err.Error().
if err != nil {

internal/customtypes/uuid_type.go:22

  • [nitpick] Consider adding a doc comment for Equal to explain why pointer types are now supported and any implications for consumers.
func (t UUIDType) Equal(o attr.Type) bool {

internal/services/dlp_policy/helpers.go:221

  • Update or add a function comment to reflect the new error return so callers understand that failures will now bubble up.
func convertToDlpEnvironment(ctx context.Context, environmentsInPolicy basetypes.SetValue) ([]dlpEnvironmentDto, error) {

@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
…ings DTO conversions

Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
Copilot finished work on behalf of mawasile June 9, 2025 14:07
@microsoft microsoft deleted a comment from Copilot AI Jun 9, 2025
mawasile and others added 2 commits June 13, 2025 12:39
Copilot finished work on behalf of mawasile June 13, 2025 10:52
Co-authored-by: mattdot <266258+mattdot@users.noreply.github.com>
Copilot finished work on behalf of mattdot June 16, 2025 18:49
@mawasile mawasile requested a review from mattdot June 17, 2025 09:33
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

Successfully merging this pull request may close these issues.

[Copilot] DTO and Data Conversion Type Safety Issues
3 participants