Skip to content

Add orgId parameter to query atoms for multi-tenant support#9

Open
Makisuo wants to merge 1 commit intomainfrom
claude/fix-autocomplete-org-leak-B0IP2
Open

Add orgId parameter to query atoms for multi-tenant support#9
Makisuo wants to merge 1 commit intomainfrom
claude/fix-autocomplete-org-leak-B0IP2

Conversation

@Makisuo
Copy link
Owner

@Makisuo Makisuo commented Mar 2, 2026

Summary

This PR adds organization ID (orgId) as a parameter to all query atom functions throughout the application to support multi-tenant data isolation. The orgId is now extracted from the route context and passed to every data fetching operation.

Key Changes

  • New hook: Created useOrgId() hook that retrieves the orgId from route context via useAuth(), providing a centralized way to access the current organization ID
  • Query atom updates: Modified makeQueryAtomFamily() in tinybird-query-atoms.ts to accept and encode orgId as part of the cache key, ensuring different organizations' data is cached separately
  • Widget data atom updates: Updated widgetDataResultAtom() to include orgId in the cache key for dashboard widget data
  • Component updates: Added useOrgId() calls and passed orgId parameter to all atom value calls across:
    • Query builder components (lab and widget config)
    • Dashboard and service pages
    • Traces, logs, metrics, errors, and services components
    • Filter sidebars and detail panels
    • Service map and quick start pages

Implementation Details

  • The orgId is encoded into the atom family cache key using a _orgId prefix, which is then stripped during deserialization to maintain backward compatibility with existing input types
  • All query atom factory functions now have the signature: (input: Input, orgId: string) => Atom
  • The orgId is sourced from the authenticated user's context, ensuring it's always available in authenticated routes
  • This change ensures that cached query results are properly isolated per organization, preventing data leakage between tenants

https://claude.ai/code/session_01JanCyQqgmYrKzHo2yj85ps

The client-side atom cache (Atom.family) keyed query results solely by
input parameters (startTime, endTime, etc.) without including the
organization context. When an org switch triggered router.invalidate()
without a full page reload, cached autocomplete suggestions from org A
could be served to org B.

Changes:
- makeQueryAtomFamily now requires orgId as a second parameter, which is
  included in the cache key but stripped before passing to the query fn
- Created useOrgId() hook using TanStack Router's root route context,
  works in both Clerk and self-hosted auth modes
- Updated all ~35 atom call sites across routes and components to pass
  orgId, giving compile-time enforcement that no call site can omit it
- Applied same fix to widgetDataResultAtom in use-widget-data.ts

TypeScript will now error if any new atom call site omits orgId.

https://claude.ai/code/session_01JanCyQqgmYrKzHo2yj85ps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants