Skip to content

bug(access): Public-channel users auto-promoted to creator role on Trinity login #314

Closed

Description

@vybe

Summary

When a user gains access to an agent via Telegram, Slack, public links, or an access-request approval, their email is added to the global email_whitelist. If they later log into the Trinity web UI with that email, get_or_create_email_user() hardcodes their role to creator — giving them the ability to spin up their own agents on our platform, even though the owner only intended to grant chat access to a single agent.

The user role exists in the hierarchy for exactly this case (chat-only, no creation rights) but is never assigned by any code path today.

Impact

  • Anyone approved for one agent becomes a creator on the whole Trinity instance.
  • Privilege escalation for what the owner perceives as a narrow, agent-scoped grant.
  • Mismatch with owner intent: approving "can this person chat my bot?" silently grants "can this person create agents here?"

Root cause

Two pieces conspire:

1. Hardcoded role in get_or_create_email_user() — src/backend/db/email_auth.py:226-254:

cursor.execute("""
    INSERT INTO users (username, email, role, created_at, updated_at)
    VALUES (?, ?, ?, ?, ?)
""", (username, email.lower(), "creator", now, now))  # <-- always "creator"

2. Whitelist has no way to encode intent — src/backend/db/schema.py:109-118:

CREATE TABLE email_whitelist (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    email TEXT UNIQUE NOT NULL,
    added_by TEXT NOT NULL,
    added_at TEXT NOT NULL,
    source TEXT NOT NULL,            -- tracks "how" but not "what role"
    FOREIGN KEY (added_by) REFERENCES users(id)
);

The source column captures how an email was whitelisted (access_request, agent_sharing, cli, manual) but there is no default_role column, and get_or_create_email_user() never consults the whitelist row.

Affected call sites

All callers of add_to_whitelist():

Caller File:line Source value Intent
Access-request approval (Telegram/Slack/web) routers/sharing.py:207 access_request chat-only
/share endpoint routers/sharing.py:79 agent_sharing chat-only
CLI setup routers/auth.py:484 cli admin bootstrap
Admin whitelist UI routers/settings.py:790 caller-specified admin choice

Note: public.py/public_links.py/telegram.py/slack.py do not currently add to the whitelist directly — public-link chat users stay anonymous unless they're approved via an access request, at which point sharing.py:207 fires.

Role capability matrix (for reference)

ROLE_HIERARCHY = [\"user\", \"operator\", \"creator\", \"admin\"] (dependencies.py:174)

Capability user creator
Log into Trinity UI ✅ ✅
See agents shared with them ✅ ✅
Chat with accessible agents ✅ ✅
Create new agents ❌ ✅
Deploy local agents ❌ ✅

So user is already the right role for someone who was only granted access to one agent. It just never gets assigned.

Proposed fix

1. Add default_role column to email_whitelist

ALTER TABLE email_whitelist ADD COLUMN default_role TEXT NOT NULL DEFAULT 'user';

Versioned migration in db/migrations.py.

2. Plumb role through add_to_whitelist()

def add_to_whitelist(
    self, email: str, added_by: str,
    source: str = \"manual\",
    default_role: str = \"user\",
) -> bool:
    ...
    cursor.execute(\"\"\"
        INSERT INTO email_whitelist (email, added_by, added_at, source, default_role)
        VALUES (?, ?, ?, ?, ?)
    \"\"\", (email.lower(), user[\"id\"], now, source, default_role))

3. Update call sites with explicit intent

Call site default_role
sharing.py:207 (access_request approval) \"user\"
sharing.py:79 (/share endpoint) \"user\"
auth.py:484 (CLI setup) \"creator\" (bootstrapping an owner-operator)
settings.py:790 (admin UI) admin-specified, default \"user\"

4. Honor default_role in get_or_create_email_user()

def get_or_create_email_user(self, email: str) -> dict:
    ...
    role = self._get_whitelist_default_role(email) or \"user\"
    cursor.execute(\"\"\"
        INSERT INTO users (username, email, role, created_at, updated_at)
        VALUES (?, ?, ?, ?, ?)
    \"\"\", (username, email.lower(), role, now, now))

Fallback to \"user\" (not \"creator\") when no whitelist row exists — the safer default.

5. Admin UX (nice-to-have, separate follow-up OK)

  • On the whitelist admin page, show default_role per row and allow editing.
  • On the user-management page, expose promote-to-creator for existing accounts, so an owner can intentionally upgrade a collaborator.

Backward compatibility

  • Existing whitelist rows get default_role = 'user' from the migration default. This does not retroactively downgrade existing users (their users.role column is independent); it only affects users who haven't logged in yet.
  • Existing creator users remain creator.
  • If we want to be conservative, the migration could set existing rows to 'creator' to preserve current (buggy) behavior, and only apply 'user' to new rows going forward. Recommend not doing this — the safer default is correct going forward, and anyone already-promoted keeps their current role.

Acceptance criteria

  • email_whitelist.default_role column exists with migration.
  • add_to_whitelist() accepts and persists default_role.
  • All call sites pass an explicit default_role matching the table above.
  • get_or_create_email_user() reads default_role from the whitelist row; fallback is \"user\".
  • New test: Telegram user → access request → approved → logs into web → has user role, cannot create agents.
  • New test: /share recipient → logs into web → has user role.
  • New test: Admin explicitly whitelists with default_role='creator' → user becomes creator on login.
  • Existing tests still pass (tests/test_email_auth.py, tests/test_agent_sharing.py, tests/test_public_links.py).
  • docs/memory/architecture.md section on role hierarchy updated to note whitelist-driven default role.

Out of scope

  • Changing what the user, operator, creator, admin roles are allowed to do. The hierarchy is correct; only the assignment is wrong.
  • Retroactive downgrade of already-promoted users (can be done manually via PUT /api/users/{username}/role if needed).

References

Metadata

Metadata

Assignees

Labels

priority-p2securitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions