Am I affected?
Description
Incorrect Authorization vulnerability in ash_sql allows a caller in a multitenant application to receive aggregate values computed from another tenant's rows.
An aggregate can be given a tenant with the tenant option of Ash.Query.aggregate/5. AshSql.Aggregate.Lateral.add_aggregates/6 read only part of such an aggregate in that tenant. A field that is an aggregate or calculation, and what the aggregate's filter and sort refer to, were read in the tenant of the enclosing query, and with no tenant on that query they were read across every tenant. Aggregates over the same relationship were grouped into one lateral join without regard to their tenants, and with strategy(:context) multitenancy the aggregate was read in the schema of the enclosing query.
This issue affects ash_sql: from 0.1.0 before 0.9.1.
Technical analysis
1. Grouping. AshSql.Aggregate.Lateral.add_aggregates/6 groups the aggregates of a query by relationship path, resource, join filters, read action and multitenancy bypass, and builds one lateral subquery per group from the query of the first aggregate. The tenant is not part of that key, so aggregates that were given different tenants share the subquery of the first one.
2. Nested reads. The subquery is built from the bindings of the enclosing query. What is resolved inside it (a field that is an aggregate or a calculation, and the aggregates and calculations in the aggregate's filter and sort) takes context[:private][:tenant] from those bindings, which is the tenant of the enclosing read and not the tenant of the aggregate. AshSql.Join.handle_attribute_multitenancy/3 adds no tenant filter when that tenant is nil, so with no tenant on the read these nested reads cover every tenant. Ash itself refuses the same aggregate when no tenant is given anywhere.
3. Joined first aggregates. optimizable_first_aggregate?/3 lets a first aggregate over a to-one relationship be added as a join on the enclosing query, which reads the relationship in that query's tenant.
4. Schema multitenancy. With strategy(:context) the observed effect covers the aggregate as a whole: its subquery is read in the schema of the enclosing query's tenant, or in the repo's default schema when that query has no tenant.
Proof of concept
- With AshPostgres, define
Group(has_many :orgs) andOrg(has_many :notes, aggregatecount :notes_count, :notes) without multitenancy, andNotewithmultitenancyusingstrategy :attributeandattribute :tenant_id. - Create one group with two orgs. Give the first org one note in tenant
"a"and two in tenant"b", and the second org two notes in tenant"a". - Without a tenant on the read, run
Group |> Ash.Query.aggregate(:n, :sum, :orgs, field: :notes_count, tenant: "a") |> Ash.read!(). The value for tenant"a"is 3; an affected version returns 5, the count over both tenants. - Run the same query with
Ash.read!(tenant: "b"). An affected version returns 2, the value of tenant"b". - Again without a tenant on the read, add the aggregate twice, as
:awithtenant: "a"and as:bwithtenant: "b". The values are 3 and 2; an affected version returns 5 for both. - For comparison, the aggregate without its
tenantoption in a read without a tenant is refused by Ash with a tenant-required error.
Weaknesses & attack patterns
Weakness
CWE-863
·
Incorrect Authorization
in catalog →
MITRE ↗
Attack patterns
CAPEC-1
·
Accessing Functionality Not Properly Constrained by ACLs
MITRE ↗
A user of the application receives counts, sums, lists or first values computed from rows of a tenant other than the one the application asked for, and from the rows of every tenant when the enclosing read has no tenant. No input from the user is needed beyond reaching the read that builds the aggregate.
Affected — Hex / ash_sql Hex.pm ↗ Repository ↗
modules · source files · routines
Affected — GitHub / ash-project/ash_sql Repository ↗
modules · source files · routines
Workarounds
Set the tenant on the read (Ash.Query.set_tenant/2 or the tenant option of the read) instead of on the aggregate, so that both are read in the same tenant. Aggregates that have to be read in different tenants can be run as separate reads, one per tenant.
Configurations
An application must use a data layer built on ash_sql (verified with AshPostgres), have a resource with multitenancy, and build an aggregate with the tenant option (Ash.Query.aggregate/5 or Ash.Query.Aggregate.new/4) inside a read that has no tenant or a different tenant. With the :attribute strategy the aggregate must also have a field, filter or sort that refers to an aggregate or calculation over the multitenant resource, or share its relationship with an aggregate that was given a different tenant. With the :context strategy any such aggregate over the multitenant resource is affected. Aggregates that take their tenant from the read are not affected.
References
Credits
CVSS breakdown
CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:L/VI:N/VA:N/SC:N/SI:N/SA:N
Exploitability
- Attack Vector
- Local
- Attack Complexity
- Low
- Attack Requirements
- Present
- Privileges Required
- None
- User Interaction
- None
Vulnerable System
- Confidentiality
- Low
- Integrity
- None
- Availability
- None
Subsequent Systems
- Confidentiality
- None
- Integrity
- None
- Availability
- None