Privacy
Sharing client email with a team without opening anyone's mailbox
A teammate connects their own mailbox and shares only client correspondence. One rule, checked per message, decides what colleagues and their agents can read.
The problem
The platform syncs each teammate's Gmail so colleagues and their AI agents can search client conversations. The only boundary was the organization. Every member could read everything the organization's mailboxes held.
Each mailbox had a sharing setting, but nothing read it. A mailbox marked private was still fully readable.
I wanted sharing through a proxy. A teammate connects their own mailbox and opts in to sharing client communications. A message then becomes visible to colleagues and their agents only when a known contact is a participant on it. The design was mine. I directed AI models to stress-test it and review the work.
Three checks, three modes
A message must pass three checks to reach a reader or an agent.
- It belongs to the reader's organization.
- The mailbox owner connected it and chose a sharing mode.
- A catalogued external person is on that message.
Each mailbox is in one of three modes: private, client correspondence only, or shared with the organization.
-- Every connected mailbox has exactly one of three modes.
ALTER TABLE mailboxes
ADD CONSTRAINT mailbox_sharing_mode_check
CHECK (mode IN ('private', 'client_correspondence', 'shared'));The middle mode requires an owner. A shared intake mailbox has nobody to exempt, so a second constraint refuses the mode there instead of leaving the case unhandled.
Make the setting mean something
The first step was to give the setting one definition. This function returns the mailboxes a caller may read whole: active, in the caller's organization, and either shared or their own.
-- The single definition of "mailboxes this person can read in full".
CREATE FUNCTION visible_mailbox_ids() RETURNS uuid[] AS $$
SELECT array_agg(m.id)
FROM mailboxes m
WHERE m.is_active
AND m.organization_id = ANY (my_organization_ids())
AND (m.mode = 'shared' OR m.owner_id = current_user_id());
$$ LANGUAGE sql STABLE;Only the owner can change the setting. An organization admin cannot, because consent for a mailbox belongs to the person whose mail it is. I also fixed two write paths that quietly reset a mailbox to shared whenever it reconnected.
Row-level security on messages reads the function. When it shipped, all 65 mailboxes were shared, so nobody's access changed. That was the plan: ship the mechanism, change no one's access, then offer the choice.
Per message, never per thread
A single CC defeats thread-level authorization.
So I authorized each message on its own. In a client-correspondence mailbox, a message is visible to the organization exactly when it carries a link to a catalogued person. Everything else in that mailbox stays with its owner, who always sees all of it.
CREATE POLICY read_messages ON messages FOR SELECT USING (
organization_id = ANY (my_organization_ids())
AND (
-- a mailbox I can read in full
mailbox_id = ANY (visible_mailbox_ids())
-- or a client-correspondence mailbox, for this one message only
OR (
mailbox_id = ANY (client_correspondence_mailbox_ids())
AND has_external_contact(id)
)
)
);Review earned its keep here. The first test fixtures could not tell per-message enforcement from per-mailbox enforcement, so a wrong rule would have passed. The fixed fixtures put linked and unlinked messages in the same mailbox.
Visibility is derived when someone reads. It is not stored. Adding a contact exposes their earlier mail at that moment, and removing them retracts it.
The same rule on every surface
The search functions run with service privileges, so row-level security does not apply to them. They take the viewer as a parameter and apply the same rule in their own bodies.
-- Search runs with elevated access, so it takes the viewer explicitly
-- and applies the same rule itself, before ranking.
AND (
m.mailbox_id IN (SELECT id FROM viewer_visible_mailboxes)
OR (
m.mailbox_id IN (SELECT id FROM viewer_client_correspondence_mailboxes)
AND EXISTS (SELECT 1 FROM message_contacts c WHERE c.message_id = m.id)
)
)A message that passes is available to full-text search, vector search, the assistant's context, MCP tools, the activity feed and counts. A new surface cannot quietly apply a weaker check.
Vector search needed its own step, because the same email exists as one copy per mailbox. A hit survives only when the viewer can read a concrete copy, and it returns that copy. Review of that step reproduced an access bug before it shipped: the policy admitted a hit through any copy but checked the contact link on a different one.
I also required every count to match what the viewer can actually open.
What I rejected
- A stricter rule that required an active client. It hid about four thousand more messages per ninety days and reopened a gap between organizations. The risk I accepted is over-sharing vendor or lead mail, never internal mail.
- A cheaper fix that hid email from AI chat by default. That hides junk, not secrets. The problem was privacy, not relevance.
What changed
Before building the rule, I found that almost no stored messages were linked to the people on them. I restored the link at ingest, added a nightly sweep and ran a backfill. Afterwards about 38% of mail carried a link to an external contact. The rest was internal threads and automated mail, which is the intended result.
- I piloted the client-correspondence mode on my own mailbox in August 2026. The preview of what would be hidden matched an independent database count exactly.
- Restricted search measured 406 ms at the 95th percentile on the server, against a 500 ms target.
- By my count, 25 people use the mailbox sharing today.