Check permissions on every person a response names, not just the record

  • security
  • api
  • privacy

A side project I work on has a member directory. Each card shows a person, and each person belongs to a household, so the card also says who else lives there: “Lives with Dana and Lee.” The directory itself had careful privacy rules. You only appear if you chose to be listed, finished your privacy setup, and have not blocked the person looking.

This week I found that those rules only applied to the card, not to the names inside it. The household list on every card included every adult in that household. That meant people who were unlisted, people who had not set up privacy yet, people who keep their household private, and even people the viewer had blocked or who had blocked the viewer. The newest version of the directory screen did not render that list, so nothing looked wrong. But the API still sent it, an older screen still showed it, and older app builds out in the wild still displayed it.

That is a common shape for a leak, and it is worth naming.

The record passed, the nested people did not

Most authorization checks answer one question: may this viewer see this record? For a directory card, the record is a person, and the check was correct. The problem is that a response is rarely just one record. It joins in related rows: a household, a group roster, the leader of a team, the author of a comment, the people tagged in a photo. Each of those is a person too, with their own privacy settings, and the join does not know about any of them.

So the rule I use now is simple. Every person a response names has to pass the same check as the person it is about. Not the parent record. Each one.

One helper that filters the nested list

The fix was a single function that takes the viewer and the household lists, and returns the same structure with anyone who fails the check removed. An adult is named on someone else’s card only if all of these hold:

  1. They are listed in the directory and have finished privacy setup.
  2. Neither person has blocked the other. That includes the viewer’s own blocks and reports, and also anyone who blocked the viewer.
  3. Their own household privacy setting allows this viewer to see it.

The viewer always sees themselves.

async def visible_adults(viewer, adults_by_household):
    everyone = [a for group in adults_by_household.values() for a in group]
    if not everyone:
        return adults_by_household

    hidden = await blocked_either_way(viewer['id'])
    candidates = [
        a for a in everyone
        if a['id'] != viewer['id']
        and a.get('listed')
        and a.get('privacy_completed')
        and a['id'] not in hidden
    ]
    allowed = await resolve_visibility(viewer, candidates) if candidates else {}
    ok = {a['id'] for a in candidates if 'household' in allowed[a['id']]} | {viewer['id']}

    return {h: [a for a in group if a['id'] in ok] for h, group in adults_by_household.items()}

A few choices in there matter more than they look.

Blocks go both ways. It is easy to only check “people I blocked”. If someone blocked you, they also should not show up by name on a neighbor’s card you happen to be looking at. The helper builds one set from both directions before filtering.

Reuse the existing visibility resolver. The app already had a function that decides which fields of a person a given viewer may see. The helper calls it for the nested people instead of writing a second, slightly different version of the rules. Two copies of a privacy rule will drift, and the one that drifts is always the one nobody is looking at.

Batch it. The resolver is called once for all the candidates, not once per card. A directory page can have dozens of households, and an authorization check that costs a query per nested person is the kind of check someone later removes “for performance”.

Apply it everywhere the join happens

The household list was built in more than one place. The directory used it, and so did the roster on a group page, which shows “With Dana” under each member. I applied the same helper to both. The bug was not in one endpoint. It was in a shared join that several endpoints reused, so the fix belongs at that same level.

Your own profile and your own data export are different on purpose. Those still list every adult in your own household, because it is your household, except people blocked in either direction. They contain names and roles only.

Keep the response shape

The filtered result has exactly the same shape as before. People who do not qualify are just missing from the list. There is no new field like hidden_count and no placeholder like “1 private member”.

That matters for two reasons. Older app builds keep working, because they get the structure they expect with fewer entries. And a placeholder would itself leak something: “this household has another adult you are not allowed to see” is information.

Test the people who should not appear

The tests set up one household with a mix of people: someone visible, someone the viewer blocked, someone who blocked the viewer, someone unlisted, someone who never finished privacy setup, someone who keeps their household private, and a child. Then they assert on the raw response, not on what a screen renders:

def test_household_never_names_hidden_people(env):
    body = json.dumps([card['household'] for card in directory()['people']])
    for name in ('Alex', 'Sam', 'Riley'):
        assert name not in body

Searching the serialized body for names is blunt, and that is the point. It catches the name showing up anywhere in the payload, including in a field someone adds next month. There is also a test that removes the block and checks the person reappears, so the filter is not just hiding everyone. Several of these tests failed on the old code, which is how I know they test the actual gap.

Tradeoffs

  • More work per request. Resolving visibility for nested people costs more than returning the raw join. Batching keeps it to a fixed number of queries, which has been fine.
  • The UI was not the bug. It is tempting to fix this by hiding the list on screen. That does nothing for older clients or for anyone reading the API directly. The filter has to run on the server.
  • Some edges are still open. A few other places name people, like group leaders in a summary, and they are not filtered by blocks yet. I flagged those rather than quietly widening the change, so each one can be decided on its own.

The pattern

When I review an endpoint now, I list every person whose name or id ends up in the response, not just the one the URL is about. Each of them gets the same check the top-level record gets. The leak in a join is easy to miss precisely because the main check is correct, and the screen you are testing might not even show the field that is leaking.