---
title: "Filter search results by access inside the · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/search/search-access-filter
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Filter search results by access inside the query

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Search](https://agentwranglers.se/playbook/data/search.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers). Updated 2026-10-21.

- ID: DAT-61
- Level: Required
- Chapter: [Search](https://agentwranglers.se/playbook/data/search)
- Rule: Put the user's access filter into the search query itself. Hits, totals, facet counts, suggestions and excerpts then come only from records the user may see.
- Risk: Data leaked or lost
- Applies when: The project has user accounts.
- How: Add the account or owner condition next to the match condition, in the same `WHERE` clause or in the search engine's filter. Build that filter on the server from the session, never from a request field. Filtering hits after the search leaks through totals, facets and autocomplete, and breaks paging. Store the account ID on every document in a separate index, so the filter works there too. In PostgreSQL, the row-level security from [Keep each customer's data apart](https://agentwranglers.se/playbook/data/schema-design/tenant-isolation) applies to full-text queries as well. Each record a user opens from the results still needs the check in [Check ownership on every record](https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/object-access). Search for AI answers follows [Copy access rights into the index and check them again](https://agentwranglers.se/playbook/ai-features/retrieval/retrieval-permissions).
- Check: Create two test accounts, each with a record holding a made-up word only it contains. Signed in as account A, search for account B's word. The answer has 0 hits, a total of 0, no suggestion and no facet count above 0.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
// The account comes from the session, never from the request body
const hits = await db.many(
  `SELECT id, name FROM products
   WHERE account_id = $1 AND search_text @@ websearch_to_tsquery('english', $2)
   LIMIT 20`,
  [session.accountId, query]);
```

### Python example

```python
# The account comes from the session, never from the request body
cursor.execute(
    """SELECT id, name FROM products
       WHERE account_id = %s AND search_text @@ websearch_to_tsquery('english', %s)
       LIMIT 20""",
    (session.account_id, query))
```

## Sources

- [OWASP Authorization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html)

## Related rules

- [Keep each customer's data apart](https://agentwranglers.se/playbook/data/schema-design/tenant-isolation) in Data
- [Check ownership on every record](https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/object-access) in Security testing
- [Copy access rights into the index and check them again](https://agentwranglers.se/playbook/ai-features/retrieval/retrieval-permissions) in AI features

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data/search/search-access-filter">Agent Wranglers data playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Search with the database's own full-text index](https://agentwranglers.se/playbook/data/search/database-full-text-search)
- Next: [Index only the fields people search](https://agentwranglers.se/playbook/data/search/search-index-fields)
- [Search](https://agentwranglers.se/playbook/data/search)
