Repository navigation
feat(go-parser): detect tenant isolation for Gin + GORM - #15
Merged
Merged
Conversation
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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
go-parser emitted none of the nodes the
missing_tenant_scopedetector needs, so the finding never fired on Gin projects. Merge after #12 for a green CI build.What it does
c.Get("tenant_id")-style read from the Gin context, an accessor such asGetTenantID(c)/tenant.FromContext(ctx), a tenant-named function handed the context (TenantScope(c)), or a tenant field read (claims.TenantID) — go-parser emits anir:tenant_contextnode.ir:scoped_querywhen it carries a tenant signal, otherwiseir:unscoped_query/ir:unscoped_writewith amissing_tenant_scopeedge. A write whose record sets the tenant counts as scoped.service.GroupApp.Board.X); route handlers registered that way (api.Controllers.BoardApi.X,roles := NewSysRoleController(); roles.List) are resolved too.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.Preload("Tenant")does not scope; conditions built in variables (subqueries) are followed.ir:unscoped_queryis a danger type) are unaffected.archmind.jsonisolation.tenantSignals/isolation.tenantContextKeys(defaults: tenant, organization_id, org_id).missing_tenant_scopealso acceptsir:tenant_contextas 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 inpackages/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.
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.