Skip to content

Fix type assertion safety issues to prevent runtime panics #845

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 12 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 5, 2025

This PR addresses multiple type assertion safety issues across the codebase that could cause runtime panics when API responses have unexpected structure or types.

Issues Fixed

1. Connection Service (internal/services/connection/api_connection.go)

  • Issue: getPrincipalString function performed type assertion without checking if the key exists
  • Fix: Added existence check before type assertion to prevent panic when accessing non-existent map keys
// Before (unsafe)
value, ok := principal[key].(string)

// After (safe)
raw, exists := principal[key]
if !exists {
    return "", fmt.Errorf("principal key %s does not exist", key)
}
value, ok := raw.(string)

2. Data Record Service (internal/services/data_record/api_data_record.go)

  • Issue: Multiple unchecked type assertions on API response fields like @odata.context and value
  • Fix: Added safe extraction with proper error handling for malformed responses
// Before (unsafe)
pluralName := strings.Split(response["@odata.context"].(string), "#")[1]

// After (safe)
odataCtxRaw, exists := response["@odata.context"]
if !exists {
    return nil, errors.New("@odata.context field missing from response")
}
odataCtx, ok := odataCtxRaw.(string)
if !ok {
    return nil, errors.New("@odata.context field is not a string")
}

3. Tenant Settings Service (internal/services/tenant_settings/api_tenant_settings.go)

  • Issue: applyCorrections function logged error and returned nil on type assertion failure
  • Fix: Changed function signature to return error, updated all callers to handle errors properly
// Before
func applyCorrections(...) *tenantSettingsDto {
    // logs error and returns nil
}

// After
func applyCorrections(...) (*tenantSettingsDto, error) {
    // returns explicit error
}

4. Data Record DataSource (internal/services/data_record/datasource_data_record.go)

  • Issue: Type switch on value but then unsafe cast of columns[key]
  • Fix: Used type assignment in switch to avoid redundant unsafe casts
// Before (unsafe)
switch value.(type) {
case bool:
    caseBool(columns[key].(bool), ...)

// After (safe)
switch v := value.(type) {
case bool:
    caseBool(v, ...)

5. Solution Checker Rules (internal/services/solution_checker_rules/datasource_solution_checker_rules.go)

  • Issue: Missing nil check after type assertion
  • Fix: Added defensive nil check for client assignment

Testing

  • All existing unit tests pass
  • Linter reports no issues
  • Documentation regenerated
  • Changelog entry created

Impact

These changes make the provider more resilient to unexpected API responses and prevent potential runtime panics that could crash Terraform operations.

Fixes #839.


💡 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 13:54
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] API Response Type Assertion Safety Issues Fix type assertion safety issues to prevent runtime panics Jun 5, 2025
@Copilot Copilot AI requested a review from mawasile June 5, 2025 13:57
Copilot finished work on behalf of mawasile June 5, 2025 13:57
@mawasile mawasile marked this pull request as ready for review June 6, 2025 07:07
@Copilot Copilot AI review requested due to automatic review settings June 6, 2025 07:07
@mawasile mawasile requested a review from a team as a code owner June 6, 2025 07:07
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 adds safety checks for type assertions across multiple services to prevent runtime panics when API responses have unexpected structures.

  • Updated applyCorrections to return errors on assertion failures and handled those errors in resource CRUD methods.
  • Added existence and type checks for map-based API responses in connection, data record, and solution checker services.
  • Improved type switch usage by capturing values directly and handling unexpected cases.

Reviewed Changes

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

Show a summary per file
File Description
internal/services/tenant_settings/resource_tenant_settings.go Updated Create/Read/Update/Delete methods to handle errors from applyCorrections
internal/services/tenant_settings/api_tenant_settings.go Changed applyCorrections signature to return error and added error on failed assertion
internal/services/solution_checker_rules/datasource_solution_checker_rules.go Added nil-check for client before assigning SolutionCheckerRulesClient
internal/services/data_record/datasource_data_record.go Refactored type switches to bind values and added default case to skip unknown types
internal/services/data_record/api_data_record.go Added safe extraction of OData fields and improved type assertions for total record count
internal/services/connection/api_connection.go Added existence check before asserting principal map values
.changes/unreleased/fixed-20250605-135507.yaml Added changelog entry for fixed type assertion issues
Comments suppressed due to low confidence (5)

internal/services/data_record/api_data_record.go:151

  • Add unit tests to cover cases where @odata.context is missing or not a string to ensure the error paths are exercised.
odataCtxRaw, exists := response["@odata.context"]

internal/services/data_record/api_data_record.go:160

  • Add unit tests for malformed @odata.context strings (with no # separator) to validate this error case.
if len(splitParts) < 2 {

internal/services/data_record/api_data_record.go:297

  • Add tests for scenarios where value is not a []any or is empty to exercise alternate branches in GetTableSingularNameFromPlural.
if rawVal, exists := mapResponse["value"]; exists && rawVal != nil {

internal/services/connection/api_connection.go:177

  • Add unit tests for getPrincipalString covering missing keys and non-string values to verify error handling.
raw, exists := principal[key]

internal/services/tenant_settings/resource_tenant_settings.go:410

  • Add tests to cover the error branch when applyCorrections returns an error in the Create, Read, Update, and Delete methods.
stateDto, err := applyCorrections(ctx, plannedSettingsDto, *tenantSettingsDto)

@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
mawasile and others added 2 commits June 9, 2025 15:45
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…hecker_rules.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@mawasile
Copy link
Contributor

@copilot run linter and fix issues

Copy link
Contributor Author

Copilot AI commented Jun 12, 2025

@copilot run linter and fix issues

Linter ran successfully with 0 issues found. No fixes needed.

Co-authored-by: mattdot <266258+mattdot@users.noreply.github.com>
Copilot finished work on behalf of mattdot June 12, 2025 16:17
Co-authored-by: mattdot <266258+mattdot@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 13, 2025

@copilot run linter and fix issues

Linter ran successfully with 0 issues found. No fixes needed.

Copilot finished work on behalf of mattdot June 13, 2025 04:19
Copilot finished work on behalf of mawasile June 13, 2025 10:43
mattdot
mattdot previously approved these changes Jun 16, 2025
eduardodfmex
eduardodfmex previously approved these changes Jun 17, 2025
@mawasile mawasile dismissed stale reviews from eduardodfmex and mattdot via 09175a4 June 17, 2025 15:26
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] API Response Type Assertion Safety Issues
4 participants