A platform for community discussion. Free, open, simple.
SECURITY: Use nonce-based restrictions during restore
Gerhard Schlager2b6675f064c6
diff --git a/lib/backup_restore/database_restorer.rb b/lib/backup_restore/database_restorer.rbindex c210269f2ac..d88bc3477c8 100644--- a/lib/backup_restore/database_restorer.rb+++ b/lib/backup_restore/database_restorer.rb@@ -104,6 +104,8 @@ module BackupRestore"CREATE SCHEMA", # PostgreSQL 11+"COMMENT ON SCHEMA", # PostgreSQL 11+"SET default_table_access_method", # PostgreSQL 12+ "\\\\restrict",+ "\\\\unrestrict",].join("|")command = "sed -E '/^(#{unwanted_sql})/d' #{@db_dump_path}"@@ -114,7 +116,15 @@ module BackupRestoreenddef restore_dump_command- "#{sed_command} | #{self.class.psql_command} 2>&1"+ nonce = SecureRandom.hex++ <<~CMD+ (+ printf '%s\\n' "\\\\restrict #{nonce}"+ #{sed_command}+ printf '%s\\n' "\\\\unrestrict #{nonce}"+ ) | #{self.class.psql_command} 2>&1+ CMDenddef self.psql_commanddiff --git a/spec/fixtures/db/restore/postgresql_15.14.sql b/spec/fixtures/db/restore/postgresql_15.14.sqlnew file mode 100644index 00000000000..8ba7a3f0755--- /dev/null+++ b/spec/fixtures/db/restore/postgresql_15.14.sql@@ -0,0 +1,55 @@+--+-- PostgreSQL database dump+--++\restrict VpojEJAqEpwQGC51Qahqy5HuxojaPyXwIn1sY1NOaXoE3DEeFgA2kgTwfKSGDV5++-- Dumped from database version 15.14 (Debian 15.14-1.pgdg12+1)+-- Dumped by pg_dump version 15.14 (Debian 15.14-1.pgdg12+1)++-- Started on 2025-09-14 20:35:33 UTC++SET statement_timeout = 0;+SET lock_timeout = 0;+SET idle_in_transaction_session_timeout = 0;+SET client_encoding = 'UTF8';+SET standard_conforming_strings = on;+SELECT pg_catalog.set_config('search_path', '', false);+SET check_function_bodies = false;+SET xmloption = content;+SET client_min_messages = warning;+SET row_security = off;++--+-- TOC entry 9 (class 2615 OID 2200)+-- Name: public; Type: SCHEMA; Schema: -; Owner: -+--++CREATE SCHEMA public;++--+-- TOC entry 697 (class 1259 OID 21550)+-- Name: admin_notices; Type: TABLE; Schema: public; Owner: -+--++COMMENT ON SCHEMA public IS 'standard public schema';+++SET default_tablespace = '';++SET default_table_access_method = heap;++--+-- TOC entry 782 (class 1259 OID 10556041)+-- Name: foo; Type: TABLE; Schema: public; Owner: -+--++CREATE TABLE public.foo (+ id integer NOT NULL+);++--+-- PostgreSQL database dump complete+--++\unrestrict VpojEJAqEpwQGC51Qahqy5HuxojaPyXwIn1sY1NOaXoE3DEeFgA2kgTwfKSGDV5diff --git a/spec/lib/backup_restore/database_restorer_spec.rb b/spec/lib/backup_restore/database_restorer_spec.rbindex 50ccf3f8d18..04364131b41 100644--- a/spec/lib/backup_restore/database_restorer_spec.rb+++ b/spec/lib/backup_restore/database_restorer_spec.rb@@ -73,6 +73,10 @@ RSpec.describe BackupRestore::DatabaseRestorer doexpect_restore_to_work("postgresql_12.1.sql")end+ it "restores from PostgreSQL 15.14" do+ expect_restore_to_work("postgresql_15.14.sql")+ end+it "detects error during restore" doexpect { restore("error.sql", stub_migrate: false) }.to raise_error(BackupRestore::DatabaseRestoreError,