feat(supabase): db:trial grants owner with one verified TOTP factor

Owner decision 2026-11-11: on the trial stack one verified factor suffices
for owner as for admin, a second one optional. The count before the typed
confirmation and the recount inside the grant transaction stay; SQL, aal2
and the rule for every other stack are unchanged.
This commit is contained in:
bchanot committed 2026-11-11 22:37:40 +01:00
1 parent 0573d66864
commit 45d74536f5
4 files changed
+30 -18

No files matched your search

+12 -8
View File
@@ -1210,10 +1210,10 @@ check "account: the body is a confirmed account" stdin_has '"email_confirm":true
check "account: the state folder holds no file that is not 600" bash -c '[ -z "$(find "$1" -type f ! -perm 600)" ]' _ "$STATE_DIR"
fresh_log; trial grant owner@trial.test root
check "grant: an unknown right is refused before any call" refused_early "right:"
set_factors 1
set_factors 0
fresh_stdin; fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner with one factor: refused (two needed), nothing assigned" bash -c '[ "$1" -ne 0 ] && grep -qF "owner needs 2" "$2" && grep -qF "nothing was granted" "$2"' _ "$RC" "$CASE/out"
check "grant owner with one factor: no role SQL ran" no_role_assigned
check "grant owner with no factor: refused (one needed), nothing assigned" bash -c '[ "$1" -ne 0 ] && grep -qF "owner needs 1" "$2" && grep -qF "nothing was granted" "$2"' _ "$RC" "$CASE/out"
check "grant owner with no factor: no role SQL ran" no_role_assigned
set_factors 2
fresh_log; trial_tty no grant owner@trial.test owner
check "grant owner, wrong phrase: nothing assigned" bash -c '[ "$1" -ne 0 ] && grep -qF "not confirmed" "$2"' _ "$RC" "$CASE/out"
@@ -1231,9 +1231,13 @@ check "grant owner: the role SQL failing is told" bash -c '[ "$1" -ne 0 ] && gre
rm -f "$CASE/stub/fail/grant-role"; fresh_stdin
fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner: factors shown, operator path, audit row" bash -c '[ "$1" -eq 0 ] && grep -qF "Tablet | 2026-11-09 10:05 UTC" "$2" && grep -rlF "private.assign_platform_role(null, :'"'"'uid'"'"'::uuid" "$3" | grep -q . && grep -rlF "platform_role.operator_bootstrap" "$3" | grep -q .' _ "$RC" "$CASE/out" "$CASE/stub/stdin"
check "grant owner: email, role and operator as psql variables" log_has "-v email=owner@trial.test -v role=owner -v need=2 -v operator="
check "grant owner: email, role and operator as psql variables" log_has "-v email=owner@trial.test -v role=owner -v need=1 -v operator="
check "grant owner: no secret in calls or output" secrets_absent "$STATE_DIR/secrets.env"
set_factors 1
fresh_stdin; fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner with one factor: granted, one factor suffices" bash -c '[ "$1" -eq 0 ] && grep -qF "owner granted to owner@trial.test" "$2"' _ "$RC" "$CASE/out"
check "grant owner with one factor: need=1 passed as a psql variable" log_has "-v email=owner@trial.test -v role=owner -v need=1 -v operator="
fresh_stdin
fresh_log; trial_tty 'grant admin' grant practitioner@trial.test admin
check "grant admin with one factor: granted, one factor suffices" bash -c '[ "$1" -eq 0 ] && grep -qF "admin granted to practitioner@trial.test" "$2"' _ "$RC" "$CASE/out"
check "grant admin: role passed as a psql variable" log_has "-v email=practitioner@trial.test -v role=admin -v need=1 -v operator="
@@ -1313,13 +1317,13 @@ set_factors 2; fresh_stdin; fresh_log; trial_tty 'grant owner' grant owner@trial
SANITISED="left(regexp_replace(coalesce(nullif(f.friendly_name, ''), '(no name)'), '[^[:alnum:] ._-]', '?', 'g'), 40)"
check "grant: SQL text check, the displayed name is sanitised in SQL (no newline, CR or ESC reaches the terminal)" bash -c 'grep -qF -- "$2" "$1"' _ "$(stdin_of grant-factors)" "$SANITISED"
# 2: the transaction counts again.
echo 1 > "$CASE/stub/answers/txn-factors"; fresh_stdin
echo 0 > "$CASE/stub/answers/txn-factors"; fresh_stdin
fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner: script handling of a failing grant session (stub emulates \\gset with zero rows, count 2 then 1): nothing granted" bash -c '[ "$1" -ne 0 ] && grep -qF "nothing was granted" "$2" && ! grep -qF "owner granted to owner@trial.test" "$2"' _ "$RC" "$CASE/out"
check "grant owner: script handling of a failing grant session (stub emulates \\gset with zero rows, count 2 then 0): nothing granted" bash -c '[ "$1" -ne 0 ] && grep -qF "nothing was granted" "$2" && ! grep -qF "owner granted to owner@trial.test" "$2"' _ "$RC" "$CASE/out"
check "grant owner: the transaction SQL counts the factors against :need" bash -c 'grep -q "mfa_factors" "$1" && grep -q ">= :need" "$1"' _ "$(stdin_of grant-role)"
check "grant role: explicit begin and commit, never --single-transaction" bash -c 'f="$1"; [ "$(sed -n 2p "$f")" = "begin;" ] && [ "$(tail -n 1 "$f")" = "commit;" ] && ! grep -q -- "--single-transaction" "$2"' _ "$(stdin_of grant-role)" "$CASE/log"
echo 2 > "$CASE/stub/answers/txn-factors"; fresh_stdin; fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner: two factors still there in the transaction: granted" bash -c '[ "$1" -eq 0 ] && grep -qF "owner granted to owner@trial.test" "$2"' _ "$RC" "$CASE/out"
echo 1 > "$CASE/stub/answers/txn-factors"; fresh_stdin; fresh_log; trial_tty 'grant owner' grant owner@trial.test owner
check "grant owner: one factor still there in the transaction: granted" bash -c '[ "$1" -eq 0 ] && grep -qF "owner granted to owner@trial.test" "$2"' _ "$RC" "$CASE/out"
rm -f "$CASE/stub/answers/txn-factors"
# 7: the e-mail is lowercased before any use.
fresh_stdin; fresh_log; trial_tty 'grant sheet_reviewer' grant Foo@Example.com sheet_reviewer
+6 -5
View File
@@ -1690,14 +1690,16 @@ cmd_account() {
read_new_password password
create_account "$email" "$password"
say "account $email created and confirmed"
echo "Next: pnpm db:trial app, sign in, enrol TOTP in Mon compte > Sécurité (two factors for the owner account), then pnpm db:trial grant"
echo "Next: pnpm db:trial app, sign in, enrol TOTP in Mon compte > Sécurité (one factor; a second one is an optional backup), then pnpm db:trial grant"
}
# Platform roles follow V1-2_ANATOMICAL_SHEETS.md §5.7.1: the account enrols its factors first, the
# operator reads them (name, creation time) and confirms, then the role is assigned last through
# the operator path (null actor, JWT-less session) and an audit row records the reason and the
# operator. Owner needs two verified TOTP factors, admin one. The database counts them, here and
# again inside the grant transaction; a factor name is sanitised in SQL before it reaches the terminal.
# operator. Owner and admin each need one verified TOTP factor (owner decision 2026-11-11, db:trial
# only; a second factor is an optional backup; the rule for other stacks is V1-2 §5.7.1, unchanged).
# The database counts them, here and again inside the grant transaction; a factor name is sanitised
# in SQL before it reaches the terminal.
factor_count() { # email: verified TOTP factors of the account, or no_account
sb_psql -t -A -v email="$1" <<'SQL'
-- db-trial:grant-factor-count
@@ -1718,7 +1720,6 @@ SQL
}
grant_platform_role() { # email role
local need=1 factors count
[ "$2" != owner ] || need=2
count="$(factor_count "$1")" || die "the verified TOTP factors of $1 cannot be counted: nothing was granted"
[ "$count" != no_account ] || die "no account $1 (pnpm db:trial account $1 first): nothing was granted"
[[ "$count" =~ ^[0-9]+$ ]] || die "the verified TOTP factors of $1 cannot be counted: nothing was granted"
@@ -1733,7 +1734,7 @@ grant_platform_role() { # email role
-- db-trial:grant-role
begin;
select id::text as uid from auth.users where email = :'email' \gset
-- The factors are counted again here: when one went away since the check, no row comes back,
-- The factors are counted again here: when fewer than :need remain since the check, no row comes back,
-- \gset fails, ON_ERROR_STOP ends the session and the transaction rolls back unfinished.
select private.assign_platform_role(null, :'uid'::uuid, :'role'::public.platform_role)::text as role_id
where (select count(*) from auth.mfa_factors f