Skip to content

fix: fix nil pointer dereference in ReportTask #19045

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions coderd/mcp/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ func (s *Server) RegisterTools(client *codersdk.Client) error {
return xerrors.Errorf("failed to initialize tool dependencies: %w", err)
}

// Register all available tools
// Register all available tools, but exclude tools that require dependencies not available in the
// remote MCP context
for _, tool := range toolsdk.All {
// Skip ReportTask tool in MCP context since we don't have a task reporter configured
// This prevents the nil pointer dereference panic
if tool.Name == toolsdk.ToolNameReportTask {
continue
}

s.mcpServer.AddTools(mcpFromSDK(tool, toolDeps))
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions codersdk/toolsdk/toolsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ ONLY report an "idle" or "failure" state if you have FULLY completed the task.
if len(args.Summary) > 160 {
return codersdk.Response{}, xerrors.New("summary must be less than 160 characters")
}
// Check if task reporting is available to prevent nil pointer dereference
if deps.report == nil {
return codersdk.Response{}, xerrors.New("task reporting not available. Please ensure a task reporter is configured.")
}
err := deps.report(args)
if err != nil {
return codersdk.Response{}, err
Expand Down
61 changes: 61 additions & 0 deletions codersdk/toolsdk/toolsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,64 @@ func TestMain(m *testing.M) {

os.Exit(code)
}

func TestReportTaskNilPointerDeref(t *testing.T) {
t.Parallel()

// Create deps without a task reporter (simulating MCP server scenario)
client, _ := coderdtest.NewWithDatabase(t, nil) // Use a proper test client
deps, err := toolsdk.NewDeps(client)
require.NoError(t, err)

// Prepare test arguments
args := toolsdk.ReportTaskArgs{
Summary: "Test task",
Link: "https://example.com",
State: string(codersdk.WorkspaceAppStatusStateWorking),
}

// This should panic with nil pointer dereference if the bug is present
ctx := t.Context()

// Attempt to call the handler - this should cause the panic described in the issue
_, err = toolsdk.ReportTask.Handler(ctx, deps, args)

// We expect an error, not a panic
require.Error(t, err)
require.Contains(t, err.Error(), "task reporting not available")
}

func TestReportTaskWithReporter(t *testing.T) {
t.Parallel()

// Create deps with a task reporter
client, _ := coderdtest.NewWithDatabase(t, nil) // Use a proper test client

called := false
reporter := func(args toolsdk.ReportTaskArgs) error {
called = true
require.Equal(t, "Test task", args.Summary)
require.Equal(t, "https://example.com", args.Link)
require.Equal(t, string(codersdk.WorkspaceAppStatusStateWorking), args.State)
return nil
}

deps, err := toolsdk.NewDeps(client, toolsdk.WithTaskReporter(reporter))
require.NoError(t, err)

// Prepare test arguments
args := toolsdk.ReportTaskArgs{
Summary: "Test task",
Link: "https://example.com",
State: string(codersdk.WorkspaceAppStatusStateWorking),
}

// This should work correctly
ctx := t.Context()
result, err := toolsdk.ReportTask.Handler(ctx, deps, args)
require.NoError(t, err)
require.True(t, called)

// Verify response
require.Equal(t, "Thanks for reporting!", result.Message)
}
Loading