Skip to content

feat(go-parser): detect tenant isolation for Gin + GORM - #15

Merged
Kidkender merged 2 commits into
mainfrom
feat/go-tenant-isolation
Nov 13, 2026
Merged

Kidkender merged 2 commits into
mainfrom
feat/go-tenant-isolation

Conversation

@Kidkender

@Kidkender Kidkender commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

go-parser emitted none of the nodes the missing_tenant_scope detector needs, so the finding never fired on Gin projects. Merge after #12 for a green CI build.

What it does

  • On a route that resolves a tenant — a middleware whose name carries a tenant key, a c.Get("tenant_id")-style read from the Gin context, an accessor such as GetTenantID(c) / tenant.FromContext(ctx), a tenant-named function handed the context (TenantScope(c)), or a tenant field read (claims.TenantID) — go-parser emits an ir:tenant_context node.
  • Every query in the handler and up to two calls below it (handler → service → repository) becomes ir:scoped_query when it carries a tenant signal, otherwise ir:unscoped_query / ir:unscoped_write with a missing_tenant_scope edge. A write whose record sets the tenant counts as scoped.
  • Calls are followed through struct fields, typed locals, constructor return types, package-level registries and embedded structs (service.GroupApp.Board.X); route handlers registered that way (api.Controllers.BoardApi.X, roles := NewSysRoleController(); roles.List) are resolved too.
  • Besides GORM chains, read/write calls on DAO / repository / model wrappers count as queries. A wrapper taking its conditions as variadic arguments (gorm-gen First(ctx, conds...), Find(c, scopes...)) is judged at the call site; Scopes(funcs...) fed by a parameter is judged by the caller's arguments.
  • An operation covered by a GORM callback that mentions the tenant counts as scoped. Models declared without a tenant field (shared tables) are not reported; Preload("Tenant") does not scope; conditions built in variables (subqueries) are followed.
  • Routes that resolve no tenant get none of these nodes, so single-tenant apps and existing topology baselines (ir:unscoped_query is a danger type) are unaffected.
  • archmind.json isolation.tenantSignals / isolation.tenantContextKeys (defaults: tenant, organization_id, org_id).
  • explainer: missing_tenant_scope also accepts ir:tenant_context as the tenant node, and gives GORM-style advice for Gin graphs.

Limits

Syntactic only: GORM plugins, global scopes outside a registered callback, and Scopes(fn) whose name and arguments carry no tenant signal are not seen. Queries global by design (login, uniqueness checks, super-admin setup) are reported too. Types are matched by bare name across packages. At most 25 query nodes per route. Documented in packages/go-parser/README.md.

Verified

go-parser 101/101 (with #12's scanner fix), including 10 new cases replicating the real layouts below; typecheck clean. explainer 282/282.

On real projects

The detection rules were written against these two apps, so these are not blind numbers.

App Routes with tenant Scoped Distinct unscoped sites
thingspanel-backend-community 1 → 129 0 → 14 15
ginfast 0 → 40 0 → 52 8

Every unscoped site was read by hand: about 10 are lookups by id or full loads without a tenant filter (worth reporting); about 13 are global by design (login, username/email uniqueness, super-admin, a user's own record by token id). No site reported as unscoped is actually tenant-filtered.

go-parser emitted none of the nodes the missing_tenant_scope detector needs,
so the finding never fired on Gin projects.

On a route that resolves a tenant (a middleware whose name carries a tenant
key, a c.Get("tenant_id")-style read from the Gin context, or an accessor such
as GetTenantID(c) / tenant.FromContext(ctx)), go-parser now emits an
ir:tenant_context node and classifies every GORM query in the handler and up
to two calls below it (handler -> service -> repository, through struct
fields): ir:scoped_query when its Where/Scopes/Joins arguments carry a tenant
signal, otherwise ir:unscoped_query / ir:unscoped_write with a
missing_tenant_scope edge. A write whose record literal sets the tenant counts
as scoped. Routes that resolve no tenant get none of these nodes, so
single-tenant apps and existing topology baselines are unaffected.

Signals are configurable in archmind.json `isolation.tenantSignals` /
`isolation.tenantContextKeys` (defaults: tenant, organization_id, org_id).

explainer: missing_tenant_scope also accepts ir:tenant_context as the tenant
node (ir:runtime_inject would have raised hidden_runtime_dependency on every
tenant route), and gives GORM-style advice for Gin graphs.

Limits: syntactic only; GORM global scopes, callbacks, plugins and Scopes(fn)
without a tenant name are not seen; at most 25 query nodes per route.
… model wrappers

Real multi-tenant Gin apps rarely query GORM straight from a struct field, so
the first version found no queries on them. Isolation now:

- resolves handlers and callees through package-level registries, embedded
  structs, typed locals and constructor return types
  (api.Controllers.BoardApi.X, service.GroupApp.Board.X, NewSysRoleList());
- reads the tenant from tenant-named fields (claims.TenantID) and
  tenant-named functions handed the Gin context (TenantScope(c));
- treats read/write calls on DAO, repository and model wrappers as queries,
  judged at the call site when the wrapper takes its conditions as variadic
  arguments, and judges Scopes(funcs...) by the caller's arguments;
- counts operations covered by a tenant-setting GORM callback as scoped;
- skips models declared without a tenant field (shared tables), ignores
  Preload("Tenant"), and follows conditions built in variables (subqueries).

Measured on two real apps: thingspanel goes from 1 to 129 routes with a
tenant context and 15 distinct unscoped sites; ginfast from 0 to 40, with
8 distinct unscoped sites.
@Kidkender Kidkender mentioned this pull request Nov 13, 2026
@Kidkender
Kidkender merged commit 9ba12ca into main Nov 13, 2026
1 check failed
@Kidkender
Kidkender deleted the feat/go-tenant-isolation branch November 13, 2026 04:45
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.

1 participant