Skip to content

[Security] Session.cancelBackgroundTask has race conditions and missing authorization #55

@randomm

Description

@randomm

Summary

Adversarial review of cancel_task tool revealed issues in the underlying Session.cancelBackgroundTask() function.

Issues Found

CRITICAL: Missing Authorization Control

  • tryCancel() does NOT validate caller has permission to cancel a specific task
  • Any session can cancel ANY task in the system

CRITICAL: TOCTOU Race Condition

  • Time-of-check-time-of-use between status check (line 562) and actual cancellation (line 569)
  • Task can complete between check and cancel, leading to incorrect state

CRITICAL: Double-Modify State Corruption

  • cancelBackgroundTask() mutates backgroundTaskResults after task completion
  • Completed tasks can be incorrectly marked as "failed"
  • Result data lost, audit trail corrupted

MEDIUM: Slot Release Race

  • release_slot() called before deleting from maps
  • Brief window for double-reservation

Location

/packages/opencode/src/session/index.ts

  • Lines 499-548: cancelBackgroundTask()
  • Lines 550-581: tryCancel()

Impact

For current use case (PM cancelling own tasks), risk is low. But these should be fixed before:

  • Multi-user scenarios
  • Cross-session task management
  • Production deployment

Recommended Fixes

  1. Add session ownership check before cancel
  2. Make status check + cancel atomic
  3. Never mutate already-completed task results
  4. Reorder slot release to after map deletion

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions