PgRest.Authorization.Apply (PgRest v0.2.0)

Copy Markdown View Source

Applies a policy to one scoped source. Authorization.check/5 runs first; the query callback can only narrow the base query. Columns are physical database column names.

Differences from PostgREST

Query policies are an application extension to database privileges/RLS. Stars expand to permitted columns; an explicit hidden-column reference is 42501. Counts and aggregates require explicit grants and permission limits cap their input rows, including totals. PostgREST's db-max-rows limits output rows and does not cap exact totals. With a permission cap, all count modes return the exact capped total instead of a planner estimate, so an estimate cannot disclose rows outside the visible set. RPCs with result_scope: :none discard row predicates only; hook denials, columns, count/aggregate grants and permission limits still apply. Hook narrowing needs a primary or unique key for source intersection; otherwise 42501. PostgREST can apply RLS to keyless tables.

Summary

Functions

Checks Ecto field names against a policy's physical column allowlist.

Whether an Ecto field belongs to a physical-column grant.

Intersects grants, retaining the left flags; columns: :left retains a read grant.

Explicit EXPLAIN admission; the plan still explains the original authorized statement.

Intersects physical-column grants.

The smaller non-nil permission limit.

Whether every physical source name belongs to a column grant.

Authorizes and narrows one path/operation. Options :authorize? and :rows? default to true; disabling the bare check never disables query narrowing. With rows?: false, the hook still decides admission, columns, limits and grants, but its row predicate is discarded without requiring a key.

An unrestricted policy over a source (nil for a non-resource result).

Types

policy()

@type policy() :: %{
  query: Ecto.Query.t() | nil,
  columns: :all | MapSet.t(String.t()),
  limit: pos_integer() | nil,
  aggregates?: boolean(),
  count?: boolean()
}

Functions

check_columns(columns, resource, fields)

@spec check_columns(:all | MapSet.t(), module(), [String.t()]) ::
  :ok | {:error, :denied}

Checks Ecto field names against a policy's physical column allowlist.

column_allowed?(columns, resource, field)

@spec column_allowed?(:all | MapSet.t(), module(), atom() | String.t()) :: boolean()

Whether an Ecto field belongs to a physical-column grant.

combine(a, b, resource, opts \\ [])

@spec combine(policy(), policy(), module(), keyword()) ::
  {:ok, policy()} | {:error, :denied}

Intersects grants, retaining the left flags; columns: :left retains a read grant.

explain_granted?(auth, conn, resource, base, context)

@spec explain_granted?(module(), Plug.Conn.t(), module(), Ecto.Query.t(), map()) ::
  boolean()

Explicit EXPLAIN admission; the plan still explains the original authorized statement.

intersect_columns(a, b)

@spec intersect_columns(:all | MapSet.t(), :all | MapSet.t()) :: :all | MapSet.t()

Intersects physical-column grants.

min_limit(a, b)

@spec min_limit(non_neg_integer() | nil, non_neg_integer() | nil) ::
  non_neg_integer() | nil

The smaller non-nil permission limit.

permits?(columns, sources)

@spec permits?(:all | MapSet.t(), [String.t()]) :: boolean()

Whether every physical source name belongs to a column grant.

source(conn, auth, resource, operation, base, context, opts \\ [])

@spec source(
  Plug.Conn.t(),
  module() | nil,
  module(),
  PgRest.Authorization.operation(),
  Ecto.Query.t(),
  map(),
  keyword()
) :: {:ok, policy()} | {:error, :denied}

Authorizes and narrows one path/operation. Options :authorize? and :rows? default to true; disabling the bare check never disables query narrowing. With rows?: false, the hook still decides admission, columns, limits and grants, but its row predicate is discarded without requiring a key.

unrestricted(query)

@spec unrestricted(Ecto.Query.t() | nil) :: policy()

An unrestricted policy over a source (nil for a non-resource result).