Skip to content

fix: prevent nil pointer panic when importing social providers#140

Merged
KT-Doan merged 3 commits intomainfrom
fix/social-provider-import-panic
Mar 20, 2026
Merged

fix: prevent nil pointer panic when importing social providers#140
KT-Doan merged 3 commits intomainfrom
fix/social-provider-import-panic

Conversation

@KT-Doan
Copy link
Copy Markdown
Collaborator

@KT-Doan KT-Doan commented Mar 19, 2026

Description

Fixes a nil pointer dereference panic that crashes the provider when importing social providers (or calling any console API operation) without a workspace API key configured.

The root cause was that OryClient.consoleClient is only initialized when workspace_api_key is set, but several console API methods (GetProject, PatchProject, CreateProject, DeleteProject, and workspace/event stream/API key operations) accessed it without checking for nil first.

Related Issues

Fixes #137

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING guide
  • My code follows the existing code style
  • I have added tests that prove my fix/feature works
  • I have updated documentation as needed
  • All new and existing tests pass (make test)
  • I have run the linter (make format)

Testing

  • Unit tests
  • Acceptance tests
  • Manual testing

Unit tests added

  • TestOryClient_RequireConsoleClient_NilPanics - Verifies that all console API methods (GetProject, PatchProject, CreateProject, DeleteProject, CreateWorkspace, GetWorkspace, ListWorkspaces, ListOrganizations) return a descriptive error instead of panicking when consoleClient is nil.

What changed

  • Added requireConsoleClient() helper method on OryClient that returns a clear error if the console API client is not initialized
  • Added nil checks to 17 console API methods that previously could panic:
    • Project operations: GetProject, CreateProject, DeleteProject, PatchProject
    • Workspace operations: CreateWorkspace, GetWorkspace, UpdateWorkspace, ListWorkspaces
    • API key operations: CreateProjectAPIKey, ListProjectAPIKeys, DeleteProjectAPIKey
    • Event stream operations: CreateEventStream, GetEventStream, SetEventStream, DeleteEventStream, ListEventStreams
    • Organization operations: ListOrganizations

Screenshots/Output

Before (panic):

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0xa0 pc=0x104cb6f7c]

After (descriptive error):

Error: getting project: console API client not configured. Set workspace_api_key (ORY_WORKSPACE_API_KEY) in the provider configuration

Add nil checks to all console API client methods (GetProject, PatchProject,
CreateProject, DeleteProject, workspace/event stream/API key operations)
that could panic when called without a workspace API key configured.

Previously, calling these methods with only a project API key would cause
a nil pointer dereference on the uninitialized consoleClient field. Now
they return a descriptive error message guiding the user to set
ORY_WORKSPACE_API_KEY.

Fixes #137
Copilot AI review requested due to automatic review settings March 19, 2026 09:09
Copy link
Copy Markdown
Contributor

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

Fixes a nil pointer dereference when console API methods are invoked without a configured workspace API key by adding a shared guard (requireConsoleClient) and validating behavior via unit tests.

Changes:

  • Added requireConsoleClient() to centralize the “console client not configured” check and error message.
  • Added the guard to multiple console API methods (projects, workspaces, API keys, event streams, orgs) to prevent panics.
  • Added a unit test ensuring several console API methods return a descriptive error when consoleClient is nil.

Reviewed changes

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

File Description
internal/client/client.go Introduces requireConsoleClient and applies it across console API methods to prevent nil dereferences.
internal/client/client_test.go Adds a regression test to ensure missing workspace API key yields a descriptive error instead of panicking.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

- Rename test to TestOryClient_RequireConsoleClient_NilReturnsError
- Add sentinel error ErrConsoleClientNotConfigured for errors.Is checks
- Add subtests for all guarded methods: UpdateWorkspace, CreateProjectAPIKey,
  ListProjectAPIKeys, DeleteProjectAPIKey, CreateEventStream, GetEventStream,
  SetEventStream, DeleteEventStream, ListEventStreams
Copilot AI review requested due to automatic review settings March 20, 2026 09:47
Copy link
Copy Markdown
Contributor

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

Fixes a nil pointer dereference when console API methods are called without a configured workspace API key by introducing a shared guard and adding unit coverage to ensure methods return a descriptive error instead of panicking.

Changes:

  • Added ErrConsoleClientNotConfigured and requireConsoleClient() to guard console API calls when consoleClient is nil
  • Added requireConsoleClient() checks to multiple console API methods (projects, workspaces, API keys, event streams, organizations)
  • Added a unit test that exercises the affected console API methods when WorkspaceAPIKey is absent

Reviewed changes

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

File Description
internal/client/client.go Adds a sentinel error + helper guard and uses it to prevent nil consoleClient panics across console API methods
internal/client/client_test.go Adds a regression test ensuring console API methods return the sentinel error when consoleClient is uninitialized

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@ryderdain ryderdain left a comment

Choose a reason for hiding this comment

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

lgtm

@KT-Doan KT-Doan merged commit 8ff1fdd into main Mar 20, 2026
10 checks passed
@KT-Doan KT-Doan deleted the fix/social-provider-import-panic branch March 20, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: importing social providers

3 participants