Files
accounted/supabase/migrations/20260813164931_peppol_delivery_payload_hash_guards.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

55 lines
1.6 KiB
PL/PgSQL

-- Ensure immutable Peppol documents are cryptographically bound to their
-- stored SHA-256 values even when an RPC is called outside the application.
CREATE OR REPLACE FUNCTION public.enforce_peppol_delivery_payload_hash()
RETURNS trigger
LANGUAGE plpgsql
SECURITY INVOKER
SET search_path = pg_catalog, public, extensions
AS $$
BEGIN
IF encode(extensions.digest(NEW.xml_payload, 'sha256'), 'hex')
IS DISTINCT FROM NEW.xml_sha256
THEN
RAISE EXCEPTION 'Peppol XML SHA-256 does not match the staged payload'
USING ERRCODE = '23514';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER enforce_peppol_delivery_payload_hash
BEFORE INSERT ON public.peppol_deliveries
FOR EACH ROW EXECUTE FUNCTION public.enforce_peppol_delivery_payload_hash();
CREATE OR REPLACE FUNCTION public.enforce_peppol_evidence_document_hash()
RETURNS trigger
LANGUAGE plpgsql
SECURITY INVOKER
SET search_path = pg_catalog, public, extensions
AS $$
BEGIN
IF NEW.document_payload IS NOT NULL
AND encode(extensions.digest(NEW.document_payload, 'sha256'), 'hex')
IS DISTINCT FROM NEW.document_sha256
THEN
RAISE EXCEPTION 'Peppol evidence document SHA-256 does not match the payload'
USING ERRCODE = '23514';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER enforce_peppol_evidence_document_hash
BEFORE INSERT ON public.peppol_delivery_evidence
FOR EACH ROW EXECUTE FUNCTION public.enforce_peppol_evidence_document_hash();
REVOKE ALL ON FUNCTION public.enforce_peppol_delivery_payload_hash()
FROM PUBLIC, anon, authenticated;
REVOKE ALL ON FUNCTION public.enforce_peppol_evidence_document_hash()
FROM PUBLIC, anon, authenticated;
NOTIFY pgrst, 'reload schema';