Skip to content

Enforce ownership-based subscription scoping #268

Open

Description

@vybe

Summary

Subscription credentials have an owner_id column but ownership is not enforced at any boundary. Users can assign any subscription to their agents, auto-assignment picks globally, and only admins can register/list subscriptions. This prevents the target UX where users connect via CLI, register their own auth token, and have it scoped exclusively to their agents.

Problem

  1. No ownership check on assignment — PUT /api/subscriptions/agents/{name} checks agent ownership but not subscription ownership. User A can assign User B's subscription to their own agents.
  2. Admin-only registration — Only admins can call POST /api/subscriptions. Users can't self-register their Claude subscription tokens.
  3. Admin-only listing — GET /api/subscriptions requires admin. Users can't see their own subscriptions.
  4. Global auto-assignment — On agent creation, get_least_used_subscription() picks globally, not scoped to agent owner. User A's new agent could get User B's subscription.
  5. Global rate-limit auto-switch — subscription_auto_switch.py selects alternatives globally, not scoped to agent owner.

Target UX

# User connects to Trinity MCP via CLI
claude mcp add trinity http://your-domain:8080/mcp

# User registers their Claude subscription token
> register_subscription name="my-max" token="sk-ant-oat01-..."

# All agents this user creates auto-get their subscription
# Other users' agents never touch this subscription

Changes Required

Backend — routers/subscriptions.py

  • Register: Remove require_admin(), allow any authenticated user. owner_id = current_user.id
  • Assign: Add check that subscription owner_id == current_user.id (admin bypasses)
  • List: Non-admins see only own subscriptions; admins see all
  • Delete: Owner can delete their own subscriptions (not just admin)

Backend — services/agent_service/crud.py

  • Auto-assign on creation: get_least_used_subscription() must filter by agent owner's subscriptions only

Backend — services/subscription_auto_switch.py

  • Rate-limit fallback: Alternative subscription selection must filter by agent owner

DB Layer — db/subscriptions.py

  • Add get_subscriptions_by_owner(owner_id) query
  • Add get_least_used_subscription_for_owner(owner_id) query
  • Add ownership verification helper

MCP Server — src/mcp-server/src/tools/subscriptions.ts

  • list_subscriptions returns only user's own (unless admin scope)
  • assign_subscription passes through to backend which enforces ownership

What Doesn't Change

  • Encryption (AES-256-GCM) — already solid
  • Usage tracking (subscription_id snapshots) — already per-subscription
  • Container injection flow — works, just needs scoped selection
  • DB schema — owner_id column already exists, just underutilized

Acceptance Criteria

  • Non-admin users can register their own subscriptions via API/MCP
  • Users can only assign their own subscriptions to their own agents
  • Auto-assignment on agent creation scopes to owner's subscriptions
  • Rate-limit auto-switch scopes to owner's subscriptions
  • Admin retains full access across all subscriptions
  • Existing subscriptions continue working (backward compatible)

Labels

enhancement, security, subscriptions

Activity

  1. added
    enhancement
    on Apr 5, 2026
  2. added
    complexity-low
    theme-monetization
    on Jun 3, 2026
  3. added
    type-feature
    on Jun 3, 2026
  4. added
    status-incubating
    on Jun 14, 2026
  5. removed
    enhancement
    on Aug 4, 2026
  6. added
    complexity-medium
    and removed
    complexity-low
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p2status-incubatingtheme-monetizationtype-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions