انتقل إلى المحتوى
شعار أوراباسي
aurabase

Public read access

Open some rows of a table to visitors and to signed-in users without opening writes, check what the anon key can and cannot do afterwards, and understand why the per-table public-read switch only applies to MongoDB projects.

دليل عملي6 د قراءةالمستوى مبتدئ

هذه الصفحة غير متوفرة بلغتك بعد. يتم عرضه باللغة الإنجليزية.

On a PostgreSQL project, a table is public when a select policy lets the role of the anon key, aura_anon, read its rows. There is no separate switch: the policy is the switch, and it can open every row or only some of them. Writes stay closed until another policy opens them.

Prerequisites#

  • A linked project: aura link <project-id>.

  • The example schema, installed with the service role key of the project:

    export AURABASE_SERVICE_ROLE_KEY=aura_sk_…
    aura db seed --example docs

    It creates profiles, orgs, org_members, posts, todos, the view open_todos and the function todo_count(p_user uuid). See Example schema.

The examples create the table sec_pub_articles, then remove it:

aura db raw "create table sec_pub_articles (
  id bigint generated always as identity primary key,
  user_id uuid not null default auth.uid(),
  title text not null,
  published boolean not null default false)"
aura db raw "insert into sec_pub_articles (user_id, title, published) values
  ('00000000-0000-4000-8000-0000000000a1', 'Release notes', true),
  ('00000000-0000-4000-8000-0000000000a1', 'Unannounced feature', false)"
  ℹ No results (0 rows).
  ℹ No results (0 rows).

Like every table created through Aurabase, it has row level security on and no policy: nobody but the service_role key reads it yet.

Open rows to everyone#

Name both public roles: the anon key alone is aura_anon, but as soon as a user signs in, its requests run as aura_authenticated, which a policy for aura_anon does not cover.

create policy sec_pub_articles_read on sec_pub_articles
  for select to aura_anon, aura_authenticated
  using (published);
  ℹ No results (0 rows).

Check what the anon key can do#

The anon key now reads the published article, and only that one; writing is still refused:

curl -s "$AURABASE_URL/v1/db/$PROJECT_ID/sec_pub_articles?select=title" -H "apikey: $AURABASE_ANON_KEY"
echo
curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/sec_pub_articles" \
  -H "apikey: $AURABASE_ANON_KEY" -H 'content-type: application/json' \
  -d '{"user_id": "00000000-0000-4000-8000-0000000000a1", "title": "Spam", "published": true}' \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, e.message)'
{"data":[{"title":"Release notes"}],"meta":{}}
INSUFFICIENT_PRIVILEGE new row violates row-level security policy for table "sec_pub_articles"

From the SDK, the same read needs no sign-in:

import { createClient } from '@aurabase/aurabase-js'

const aura = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const { data, error } = await aura.from('sec_pub_articles').select('title, published')
console.log(data, error)
[ { title: 'Release notes', published: true } ] null

Open a whole table#

using (true) opens every row:

create policy <table>_read on <table> for select to aura_anon, aura_authenticated using (true);

Do it only for data that is public by nature (a catalog, a list of countries). The security advisor (GET /v1/db/{project_id}/schema/advisors/security) reports every policy whose using is true as a warning, so that an open table is a decision you can find again. A policy that leaves out to … applies to every role, which for a select policy means the same as naming the two public roles.

Do not publish a table through a view created with security_invoker = false or through a materialized view: both ignore the policies of the table and show every row (Views).

Keep some columns private#

A policy opens whole rows. If a public row has a column visitors must not read (an email, an internal note), restrict the columns with privileges, not with the policy: Column-level security. Column privileges do not filter Realtime events: do not enable postgres_changes on such a table.

The public-read endpoint#

GET and PUT /v1/db/{project_id}/schema/tables/{table}/public-read exist for MongoDB projects, which have no row level security: there, PUT with {"public_read": true} lets anyone holding the anon key read the documents of one collection, writes stay limited to the owner of each document, and every change is recorded in an audit log. Collections are private until this call. On a PostgreSQL project, both methods refuse and point to a policy:

curl -s -X GET "$AURABASE_URL/v1/db/$PROJECT_ID/schema/tables/sec_pub_articles/public-read" \
  -H "apikey: $AURABASE_SERVICE_ROLE_KEY" \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, e.message)'
curl -s -X PUT "$AURABASE_URL/v1/db/$PROJECT_ID/schema/tables/sec_pub_articles/public-read" \
  -H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' -d '{"public_read": true}' \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, e.message)'
BAD_REQUEST Invalid request: per-collection public access is not managed by this engine (on PostgreSQL, use an RLS policy)
BAD_REQUEST Invalid request: per-collection public access is not managed by this engine (on PostgreSQL, use an RLS policy)

Both methods require the service_role key (the anon key gets 403 FORBIDDEN), and only GET and PUT exist (PATCH answers 405). The CLI has no command for this endpoint. The MongoDB behavior above is described from the code and was not replayed for this page.

Result#

aura db raw --as anon "select title from sec_pub_articles"
aura db raw "select count(*) as all_rows from sec_pub_articles"
  ℹ Executed as anon.
  title        
  ─────────────
  Release notes

  ℹ 1 row(s).
  all_rows
  ────────
  2       

  ℹ 1 row(s).

Common errors#

MessageCauseFix
data: [] for the anon key, with a meta.hintNo select policy for aura_anon, or no row matches it.Add the policy above; check the condition (published).
A visitor sees the rows, a signed-in user does notThe policy names aura_anon only.Add aura_authenticated to to ….
403 INSUFFICIENT_PRIVILEGE, new row violates row-level security policy … on a write with the anon keyExpected: a select policy opens reads only.Add an insert policy only if visitors must write.
400 BAD_REQUEST, per-collection public access is not managed by this engine (on PostgreSQL, use an RLS policy)The public-read endpoint on a PostgreSQL project.Write a policy.
403 INSUFFICIENT_PRIVILEGE, permission denied for table …The privilege was revoked from the role (or the table was created over the direct connection with the project's login role).Grant select (Roles and privileges).

Clean up#

drop table sec_pub_articles;
  ℹ No results (0 rows).

See also#

@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ الإبلاغ عن مشكلة