Skip to content

bug: can_agent_message_email crashes on NULL owner email #417

Closed as not planned

Description

@dolho

Summary

db/agent_settings/sharing.py:206 raises AttributeError: 'NoneType' object has no attribute 'lower' when the agent owner's users.email is NULL. This blocks any proactive messaging operation (auth check, rate-limit check, delivery) for agents owned by users without an email — the admin user is the canonical example on fresh installs.

Component

Backend / agent sharing / proactive messaging

Priority

P2 — silently breaks the entire POST /api/agents/{name}/messages surface for admin-owned agents. Returns generic HTTP 500 "Internal error sending message", which is also a DX/observability regression.

Root cause

# src/backend/db/agent_settings/sharing.py:206
owner_user = self._user_ops.get_user_by_username(owner["owner_username"])
if owner_user and owner_user.get("email", "").lower() == email.lower():
    return True

dict.get(key, default) returns the default only if the key is absent. When the key exists but the value is None (NULL column), .get() returns None and .lower() crashes.

Reproduction

# 1. Agent owned by admin (admin.email is NULL by default)
curl -X POST http://localhost:8000/api/agents/blank-agent/messages \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"recipient_email":"[email protected]","text":"hi","channel":"telegram"}'
# -> HTTP 500 {"detail":"Internal error sending message"}

Backend log:

ERROR routers.messages: Proactive message failed: 'NoneType' object has no attribute 'lower'
  File "db/agent_settings/sharing.py", line 206, in can_agent_message_email
    if owner_user and owner_user.get("email", "").lower() == email.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

Suggested fix

One line:

if owner_user and (owner_user.get("email") or "").lower() == email.lower():

Related

Acceptance criteria

  • can_agent_message_email returns False (no crash) when owner has NULL email and recipient ≠ owner.
  • Admin-owned POST /api/agents/{name}/messages with non-opted-in recipient returns clean 403 not_authorized and writes a proactive_message audit row.
  • Regression unit test covering NULL-email owner.
  • Grep sweep for identical .get("<col>", "").lower()/.strip() pattern in the codebase.

Environment

  • Trinity version: current main (rev 88e5525)

Activity

  1. added
    priority-p2
    type-bug
    status-in-progress
    on Apr 20, 2026
  2. self-assigned this
    on Apr 20, 2026
  3. dolho commented on Apr 20, 2026

    @dolho
    ContributorAuthor

    Duplicate of #372 — fixed on main by commit d961621 (Apr 17, 2026) with identical (x or "") coercion. Filed this after rebasing, hadn't noticed the existing fix.

    The regression test from PR #416 (tests/test_sharing_null_email_unit.py) still lands as a useful hardening — #372 shipped the fix without a unit test.

  4. added
    complexity-low
    on Jun 3, 2026
  5. removed
    status-in-progress
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p2type-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions