Let support act as a customer without handing itself a key
Almost every multi-tenant app I build ends up with a “sign in as this customer” button. Someone emails saying a screen looks wrong, and the fastest way to help is to see exactly what they see. So the platform admin clicks a button, gets a session scoped to that organization, and looks around.
That button is worth having. It is also the most powerful thing in the app, and in one of my side projects I realized the question I had been asking about it was too narrow.
The question I was asking
The first version asked “what can support see?” That project handles money for small organizations, so the obvious answer was to hide per-person payment details from a support session. The token already carried a marker saying it came from the impersonation route, so I made the server treat sensitive reads as off whenever that marker was present, and the screen said so plainly instead of showing an empty table.
That fixed the reads. Then, writing up the behavior notes, I listed what support could still do, and two lines stood out:
- Support could invite a new finance user to an email address it controls.
- Support could change the admin’s email address and then reset the password.
Either one ends with a normal login. No marker, no restrictions, nothing that says “this came from support.” Every rule I had just written for support sessions would simply not apply to the account support had made for itself. Hiding the reads was pointless if the session could mint a key that wasn’t hidden from anything.
The better question
The useful question is: what can this session do that outlives the session?
Looking around is temporary. Fixing a typo in a profile is fine. But anything that creates or redirects lasting access turns a short, audited support visit into a permanent one. When I went looking for those actions, the list was longer than I expected:
- Granting access. Staff invites, resending invites, promoting someone to admin or finance, flipping any flag that grants access to payment data.
- Changing logins. Email, password, password hash, the change-password form on the signed-in account.
- Reactivating people who would come back with elevated access.
- Member paths that reach private data. Approving an account claim, inviting someone to an address that isn’t already on their record, editing a person’s emails or phone numbers, or attaching a new person to an existing household.
- Where reset links and sign-in codes go. Swapping the email or SMS provider credentials, the from address, or the sending number. If you control where reset emails are sent, you control every account.
- Payout setup, and adding new platform admins.
The last two categories are the ones I would have missed if I had only searched for “invite” and “role.” Account recovery is an access grant. So is anything that decides where a recovery message lands.
Keep the policy in one place
I didn’t want those checks scattered across a dozen route files with slightly different wording. So there is one helper, and every risky route calls it before it does anything else:
from fastapi import HTTPException
SUPPORT_BLOCKED = "This isn't available while support is signed in."
def is_support_session(claims: dict) -> bool:
return bool(claims.get("impersonating"))
def deny_for_support(claims: dict, action: str, audit) -> None:
if is_support_session(claims):
audit.log("support_denied", action=action, actor=claims["sub"])
raise HTTPException(status_code=403, detail=SUPPORT_BLOCKED)
@router.patch("/users/{user_id}")
def update_user(user_id: str, body: UserUpdate, claims=Depends(current_claims)):
if body.email is not None or body.password is not None:
deny_for_support(claims, "change_login", audit)
if body.role in {"admin", "finance"} or body.can_view_payments:
deny_for_support(claims, "grant_access", audit)
# ordinary profile edits continue for everyone
...
A few things about that shape matter.
The check is on the request, not the stored user. Nothing about the admin account changes and no role gets downgraded. The marker lives on the token, so the same person using the same account gets full ability the moment they sign in normally.
The denial happens before any write. A support request to change an email doesn’t update the name first and then fail. It fails with nothing written, nothing sent, and nothing created.
And the marker has to survive token refresh. My first pass at the read restrictions found that refresh rebuilt the token from the stored user and quietly dropped the marker. A support session that lasted longer than one access token would have turned into an ordinary one on its own. Refresh now copies the marker forward, and there is a test for it.
What support can still do
A policy like this fails if it makes support useless, because then people stop using the button and start asking customers for their passwords. So the allowed list matters as much as the blocked one:
- Revoking access, downgrading roles, and deactivating users.
- Cancelling pending invites.
- Ordinary profile edits and creating new records that don’t attach to anyone else’s private data.
- Diagnostics, like sending a test email or checking that a texting connection works.
The pattern is that taking access away is fine and giving it out is not. If a support session gets misused, the worst case is that someone loses access and has to be re-invited by a real admin. That is annoying, but recoverable.
Clean up the side channels
Two smaller fixes came out of the same sweep.
Password reset links and invite links were being written to the app log. That meant anyone who could read logs could take over an account. Those links no longer get logged. If an email fails, you resend it.
Pending invite links were also visible in the admin screen. Support sessions don’t get them anymore, since a link you can copy is just an invite to yourself with extra steps.
Test denial as “nothing happened”
The tests for this are mostly the same shape repeated: sign in as support, call the risky route, and assert three things. The response is a 403. The database looks the same as before. And no email or text went out. Then the same call as a real admin, which must succeed. Checking only the status code would miss a route that writes first and refuses second.
Tradeoffs
There is still a residual I accepted on purpose. Support can create a brand new person with no household and file an access request for them. That yields a member login to an empty record with no private data behind it. Closing it would block real support work, and the payoff for an attacker is nothing.
It also costs real admins a little. If an admin asks support to “just add my new bookkeeper,” the answer is now “send the invite yourself, here is where the button is.” I think that is the right answer anyway.
The short version
If your app lets support sign in as a customer, don’t stop at limiting what the session can see. List everything that creates or redirects lasting access, including recovery email and sending credentials, and block those for marked sessions in one helper. Make sure the marker survives refresh, keep reset links out of your logs, and test that a denied request leaves nothing behind.