Skip to content

TL-DP-03 · A read-only v1 login for the photo copy

Why: v1 keeps its files in one private Storage bucket, mop-files, with 53,843 files (TL-DP-02's run, #225). v2_reader can list them, but Postgres can't hand over a file's bytes: those come from v1's Storage API, which wants a signed-in user. D68 says how: a v1 login made only for the photo copy, and one Storage policy that lets it read the objects in mop-files and nothing else. Never v1's secret key or its S3 keys, which can delete files.

What block 1 found (2026-10-21, on #240). A new v1 login signs in as Postgres's authenticated role, like every v1 staff member, so v1's own policies decide what it may do. They are wide:

  • 19 of v1's tables (antrian, asuransi_claims, bc, checkpoints, claim_letters, debts, deleted_leads, etle, exit_tickets, handovers, leads, maintenance, onboarding, preparation, the three stock_opname tables, vehicle_master and vehicle_status) let any signed-in user read and write every row: for all to authenticated using (true).
  • Storage lets any signed-in user upload, overwrite and delete in mop-files, and read all of it (mop_files_auth_select).
  • No trigger on auth.users gives a new login a v1 profile, and every other write policy needs a v1 role, which this login won't have.

So this step gives the photo reader nothing: v1 already lets it read mop-files. Instead, it takes away everything else with restrictive policies. Postgres ANDs a restrictive policy with v1's own, so a policy that reads "not the photo reader" denies that one login and changes nothing for anyone else. One goes on every public table with row-level security, and three on Storage, for upload, overwrite and delete. Block 2 first checks the two things a policy can't stop: views that write, and security-definer functions that write without checking who is calling.

It writes to v1 production (a login and a policy), so it is L0 for good: you run it by hand (the automation ladder). Its password lives in your password manager, and later in the staging environment beside v2_reader's (D28, D36).

ID TL-DP-03 · Level L0 · Run order data phase row 8b · Issue #240

v1 has a login, <PHOTO_READER_EMAIL>, that can download any file in mop-files through v1's Storage API, and can't upload, overwrite or delete one, or read or write any row in v1's public tables. Its password is in the password manager entry "Opleet v1 · photo reader" and nowhere else. Nothing changes for v1's staff.

  • TL-DP-02 is done (#225).
  • Owner access to v1's dashboard. Python 3 on your Mac (python3 --version; BE-M0-01 found 3.14).
  • An email address for the login, <PHOTO_READER_EMAIL>: a company address nobody signs in with, such as v2-photo-reader@ followed by the company's domain. It needs no inbox: block 4 confirms it by hand.
  • v1's public API key, <V1_PUBLISHABLE_KEY>: Project Settings › API Keys, the publishable key (starts with sb_publishable_), or the legacy anon key if v1 has no publishable key. It isn't a secret: v1's web app already sends it to every browser. Never the secret or service_role key.
  • A new password manager entry, "Opleet v1 · photo reader", with the email and the project ref in it.

In v1's dashboard: SQL Editor › New query. It reads only the catalog: triggers, policies and grants, never a row.

select 'trigger on auth.users' as kind, t.tgname::text as name, pg_get_triggerdef(t.oid) as detail
from pg_trigger t
where t.tgrelid = 'auth.users'::regclass and not t.tgisinternal
union all
select 'write policy', p.schemaname || '.' || p.tablename || ' · ' || p.policyname,
p.cmd || ' to ' || array_to_string(p.roles, ',') || ' · using ' || coalesce(p.qual, '-') || ' · check ' || coalesce(p.with_check, '-')
from pg_policies p
where p.schemaname in ('public', 'storage') and p.cmd in ('INSERT', 'UPDATE', 'DELETE', 'ALL')
and p.roles && array['authenticated', 'public']::name[]
union all
select 'storage read policy', p.policyname,
p.cmd || ' to ' || array_to_string(p.roles, ',') || ' · using ' || coalesce(p.qual, '-')
from pg_policies p
where p.schemaname = 'storage' and p.tablename = 'objects' and p.cmd = 'SELECT'
union all
select 'no RLS, authenticated may write', n.nspname || '.' || c.relname, ''
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'r' and not c.relrowsecurity
and has_table_privilege('authenticated', c.oid, 'insert, update, delete')
order by 1, 2;

Done on 2026-10-21 (#240), and the step below follows what it found. If you ever run it again, post the whole output on #240: it is schema, not data, but replace any real person's email in an expression with "an email" first. Delete the query from the editor's list afterwards.

Two things in v1 run with their owner's rights, so the restrictive policies of block 5 don't reach them: views that can be written through, and security-definer functions. SQL Editor › New query. It reads only the catalog and prints names, never data.

select 'writable view' as kind, v.table_name::text as name
from information_schema.views v
where v.table_schema = 'public'
and 'YES' in (v.is_updatable, v.is_insertable_into, v.is_trigger_updatable, v.is_trigger_deletable, v.is_trigger_insertable_into)
and has_table_privilege('authenticated', format('%I.%I', v.table_schema, v.table_name), 'insert, update, delete')
union all
select 'unguarded writing function', p.proname::text
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prosecdef
and has_function_privilege('authenticated', p.oid, 'execute')
and p.prosrc ~* '\m(insert|update|delete)\M'
and p.prosrc !~* '(auth_role|auth\.uid|auth\.jwt|auth\.role)'
order by 1, 2;
  • No rows: carry on with block 3.
  • Any row: post the output on #240 and stop. Each is something any signed-in v1 user can write through today, and the CTO decides what the photo reader needs before you create it.

The function check is a screen, not a proof: it looks for a write and for no check of the caller in each function's own text. A function that writes through another function shows up only if its own text writes.

In the password manager entry, generate a password of 32 characters, letters and digits only. Below it is <V1_PHOTO_READER_PASSWORD>. You type it only into the dashboard form in block 4 and at the Check's prompt.

In v1's dashboard: Authentication › Users › Add user › Create new user.

  • Email: <PHOTO_READER_EMAIL>
  • Password: paste <V1_PHOTO_READER_PASSWORD>.
  • Auto Confirm User: on.

Create it. Open the new user and copy its User UID into the password manager entry, and post it on #240: it isn't a secret, and the data phase's access step must leave this login out of the logins it moves (D50). Below it is <PHOTO_READER_UID>.

Until block 5 runs, this login can do what any signed-in v1 user can, so run block 5 straight away.

5 · Shut the login out of everything but reading the bucket

Section titled “5 · Shut the login out of everything but reading the bucket”

SQL Editor › New query. Replace <PHOTO_READER_UID> in the first line only, keeping the quotes, and select Run. It runs as one transaction.

do $$
declare
reader constant uuid := '<PHOTO_READER_UID>';
t regclass;
tables int := 0;
begin
for t in
select c.oid::regclass
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind in ('r', 'p') and c.relrowsecurity
loop
execute format(
'create policy "v2 photo reader: nothing" on %s as restrictive for all to authenticated '
'using ((select auth.uid()) is distinct from %L::uuid) with check ((select auth.uid()) is distinct from %L::uuid)',
t, reader, reader);
tables := tables + 1;
end loop;
execute format(
'create policy "v2 photo reader: no upload" on storage.objects as restrictive for insert to authenticated '
'with check ((select auth.uid()) is distinct from %L::uuid)', reader);
execute format(
'create policy "v2 photo reader: no overwrite" on storage.objects as restrictive for update to authenticated '
'using ((select auth.uid()) is distinct from %L::uuid) with check ((select auth.uid()) is distinct from %L::uuid)', reader, reader);
execute format(
'create policy "v2 photo reader: no delete" on storage.objects as restrictive for delete to authenticated '
'using ((select auth.uid()) is distinct from %L::uuid)', reader);
raise notice 'restrictive policies: % public tables, 3 on storage.objects', tables;
end $$;

For every staff member auth.uid() is their own id, never the reader's, so each policy reads true and v1 behaves exactly as before. Delete the query from the editor's list afterwards.

  • Block 1: done on 2026-10-21; the CTO's answer on #240 led to this version.
  • Block 2: no rows.
  • Block 4: the user appears in the list, confirmed, with a User UID.
  • Block 5: "Success. No rows returned", with the notice restrictive policies: N public tables, 3 on storage.objects. N is the number of v1's public tables with row-level security, about 34.

6 · Sign in, download one file, try to upload one, try to read a table

Section titled “6 · Sign in, download one file, try to upload one, try to read a table”

From any folder on your Mac, with v1's project ref and public key in place of the placeholders. The script asks for the password and prints status codes and sizes only, never a file name, its contents or a row. It keeps nothing on disk.

Terminal window
V1_REF=<V1_PROJECT_REF> V1_KEY=<V1_PUBLISHABLE_KEY> PHOTO_EMAIL=<PHOTO_READER_EMAIL> python3 - <<'EOF'
import getpass, json, os, urllib.error, urllib.parse, urllib.request, uuid
base = f"https://{os.environ['V1_REF']}.supabase.co"
key = os.environ["V1_KEY"]
def call(method, path, token=None, body=None, raw=None, headers=None):
h = {"apikey": key, **(headers or {})}
if token:
h["Authorization"] = f"Bearer {token}"
data = raw
if body is not None:
data = json.dumps(body).encode()
h["Content-Type"] = "application/json"
req = urllib.request.Request(base + path, data=data, method=method, headers=h)
try:
with urllib.request.urlopen(req) as r:
return r.status, r.read()
except urllib.error.HTTPError as e:
return e.code, e.read()
pw = getpass.getpass("Photo reader password: ")
status, out = call("POST", "/auth/v1/token?grant_type=password", body={"email": os.environ["PHOTO_EMAIL"], "password": pw})
print("sign in:", status)
if status != 200:
raise SystemExit(out[:200].decode(errors="replace"))
token = json.loads(out)["access_token"]
# Find one file: walk down the first folder at each level until an entry is a file.
prefix = ""
for _ in range(6):
status, out = call("POST", "/storage/v1/object/list/mop-files", token, body={"prefix": prefix, "limit": 1, "offset": 0})
entries = json.loads(out) if status == 200 else []
if not entries:
break
entry = entries[0]
path = (prefix + "/" if prefix else "") + entry["name"]
if entry.get("id"):
break
prefix = path
print("list:", status, "· found a file:", bool(entries and entries[0].get("id")))
if entries and entries[0].get("id"):
quoted = "/".join(urllib.parse.quote(part) for part in path.split("/"))
status, out = call("GET", f"/storage/v1/object/authenticated/mop-files/{quoted}", token)
print("download:", status, "·", len(out), "bytes")
probe = f"v2-probe-{uuid.uuid4().hex}.txt"
status, out = call("POST", f"/storage/v1/object/mop-files/{probe}", token, raw=b"probe", headers={"Content-Type": "text/plain", "x-upsert": "false"})
print("upload:", status, "·", "refused" if status >= 400 else "ACCEPTED: stop and post on #240")
status, out = call("GET", "/rest/v1/onboarding?select=id&limit=1", token)
rows = json.loads(out) if status == 200 else None
print("table read:", status, "·", "no rows" if rows == [] else ("ROWS RETURNED: stop and post on #240" if rows else "error"))
EOF
Line It should print
sign in sign in: 200
list list: 200 · found a file: True
download download: 200 · and a number of bytes above 0
upload A code of 400 or 403, then refused. Storage answers a row-level security refusal with 400 or 403.
table read table read: 200 · no rows. onboarding lets every signed-in user read all of it, so an empty answer proves the restrictive policy holds. The same policy refuses writes, so nothing is written to test it.

7 · As the owner, the policies are in place

Section titled “7 · As the owner, the policies are in place”

SQL Editor › New query, with <PHOTO_READER_UID> replaced. It reads the catalog only.

select p.schemaname, count(*) as restrictive_policies,
(select count(*) from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind in ('r', 'p') and c.relrowsecurity) as public_tables_with_rls
from pg_policies p
where p.permissive = 'RESTRICTIVE' and coalesce(p.qual, p.with_check) like '%<PHOTO_READER_UID>%'
group by p.schemaname order by 1;

Two rows: public | N | N, with the same number twice, and storage | 3 | N.

  • Block 4 refuses the password: v1's password rules want something else, such as a symbol. Generate one that meets them, of letters, digits and - or _ only, and carry on.
  • sign in prints 400 with "Email not confirmed": Auto Confirm was off. Open the user in the dashboard and confirm it, then run the Check again.
  • list prints 200 but found a file: False: the policy didn't match. Check that v1's own mop_files_auth_select is still there (block 1 listed it), and post block 7's output on #240.
  • download prints 400 or 404: post the code on #240, never the file's name.
  • upload prints ACCEPTED, or table read prints ROWS RETURNED: a restrictive policy didn't take. Delete the probe file in the dashboard if there is one (Storage › mop-files, a file named v2-probe- followed by letters and digits), run the Undo, and post block 7's output on #240. A login that isn't read-only is what D68 forbids.
  • Block 5 errors with "must be owner of table objects": postgres may not add policies on storage.objects here. Nothing was applied, because the block is one transaction. Delete the login (Undo, step 3), and post the error on #240.

In v1's dashboard, as the owner. It writes to v1 production: only this step's own login and policies.

  1. SQL Editor:
do $$
declare p record;
begin
for p in
select schemaname, tablename, policyname from pg_policies
where policyname in ('v2 photo reader: nothing', 'v2 photo reader: no upload',
'v2 photo reader: no overwrite', 'v2 photo reader: no delete')
loop
execute format('drop policy %I on %I.%I', p.policyname, p.schemaname, p.tablename);
end loop;
end $$;
  1. Authentication › Users › the photo reader's login › Delete user. Delete the user after the policies only if you mean to keep v1 as it was; deleting the login first is also safe, since a deleted login can't sign in.
  2. Delete the password from the password manager entry.

The login and its policies are removed after cutover, when nothing reads v1 any more. A public table added to v1 after block 5 has no such policy: if v1 gains a table before cutover, run block 7 again and post it on #240.

Log each run as a comment on #240: the date, block 2's output, the UID, block 5's notice, the Check's five lines and block 7's rows. Never the password, the API key's value or a file's name.

2026-10-21, block 1 (zuki, on #240): v1's policies let any signed-in user write 19 tables and upload, overwrite and delete in mop-files. The step changed from adding a read policy to shutting the login out with restrictive policies.