Files
accounted/supabase/migrations/20260727170000_fix_bankid_personal_number_enc_encoding.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

28 lines
1.3 KiB
SQL

-- Fix bankid_identities.personal_number_enc rows written through supabase-js
-- with a raw Buffer (issue #1232). PostgREST serialized the Buffer as JSON,
-- so the bytea column holds the literal UTF-8 text
-- {"type":"Buffer","data":[...]}
-- instead of the raw iv|tag|ciphertext bytes that decryptPersonalNumber()
-- expects. Rewrite those rows to the raw bytes recovered from the JSON
-- "data" array.
--
-- The WHERE guard uses CASE, not AND: Postgres does not guarantee AND
-- evaluation order, and convert_from() must never run on a row already
-- holding raw ciphertext (not valid UTF-8), such as one written by the
-- fixed code between deploy and apply, or any row on a re-run. CASE
-- guarantees the byte-prefix check gates the convert_from call, keeping
-- the migration idempotent and race-safe.
UPDATE bankid_identities
SET personal_number_enc = (
SELECT decode(string_agg(lpad(to_hex(elem::int), 2, '0'), '' ORDER BY ord), 'hex')
FROM jsonb_array_elements_text(
convert_from(personal_number_enc, 'UTF8')::jsonb -> 'data'
) WITH ORDINALITY AS t(elem, ord)
)
WHERE CASE
WHEN substring(personal_number_enc FROM 1 FOR 16) = convert_to('{"type":"Buffer"', 'UTF8')
THEN jsonb_array_length(convert_from(personal_number_enc, 'UTF8')::jsonb -> 'data') > 0
ELSE false
END;