Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNDYJ-00ESHg-VT for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 16:34:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uNDYG-00BZCR-K1 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 16:34:49 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNDYG-00BZCJ-1T for pgsql-hackers@lists.postgresql.org; Thu, 05 Jun 2025 16:34:48 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uNDYE-000OCg-0X for pgsql-hackers@lists.postgresql.org; Thu, 05 Jun 2025 16:34:47 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-742af84818cso922124b3a.1 for ; Thu, 05 Jun 2025 09:34:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1749141283; x=1749746083; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=/0UMdipy/q6Vjnx95DpUHpTD5rYAMuyhzmMdD8o/QqY=; b=XoRP/vDXeDn9oZnADHIdBriHN62wPrNWHW+e3cGrf7Mqdk2XjRcOri876sb4iB6aNv CxIGmMewbG8g8l7pol4i7Y0qdCnq9zOC5NZDJRcLY2Tqp3kxVIZYOtBFSCT4ts0LNdF+ jGUQSpuy2nF4jFXxsR3JERPBhnKHS1IhGs4l4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749141283; x=1749746083; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/0UMdipy/q6Vjnx95DpUHpTD5rYAMuyhzmMdD8o/QqY=; b=EoWlpsTQ8s4xMTxIyReMk+q6TaVTvS5PRQP8htYEogehW8f6ZjVxYrL/XU7tns7g8m Sy6A1MwLcO00Rt5hP07M+8128ISztE8UTzIJSuTSqpR3itZ9YX3OwcWxsdugBktD4i0k D0w3i+a8bLcSziQf16cnXG+S4fIoj28HgIBvjo+vx3PIPBAZI/RFddXlvajEyoXN1jRD fpEITR78ymxFGq8jO+rq3bN7US352KyPgvl2TzDW9hUHbdrXfSFs6PkpTZ5tsTlV8u+P aytq0xucH7GeY++swXe9F0qjOKzGi5gVEGdVWlMjz3vDVPwVp3wV6Ag8hoLrqaH40xqD jfzQ== X-Forwarded-Encrypted: i=1; AJvYcCUUnZN+d/kBI2WfUMxuRcEUHi6ZH5004owrwEmYlsh+kriYclR9iBe7reu2GuhkgZ6AcZSLHA97GK0T9S1J@lists.postgresql.org X-Gm-Message-State: AOJu0YzFPg8m/aVvNAGxUXO6QfSeRcAt3zrO2bLjfmj5O1HGYoUzzXYp 15eRSPmNPhy1qj6XRRpEv6+nzoTAsqj0HUxT+iUTD/8PcfhHr09ms0NO5XwFjLPD0Q== X-Gm-Gg: ASbGncvo0lthSTIt5ht+AF8DI3Y8kBV8UTwSZp6pF73iruBXSw81yZIKt0J9kSHQOsT JzQ4ZE+dwZm6FWDEUoqOo4dMrccw3oQWFKLwAkLaUdYN/HETY1X5XpcPnZycdwQ+RdqtQCGh0pZ OjMwlUf+njiHG5iBExAsDCRAsmW/9o7RHFE0n9KhsCVhcq5y0wwIItZy8oIjU4mqUaopZdQnbk5 3T+V2lDcuLKVl40PxReBE9IHRaUozRc/2anzwTpmQmVzLTH6jJ3xc0fm5JCxxgKbn2O0g9EYomK CMmlm5zRawDZB6z7D6qijcOuTMIWqCc9aEOtN0zlK46bfCX7VsakhQFcNLwY1tBqbr+DGA== X-Google-Smtp-Source: AGHT+IH6WbrRQriErZxhuu3z4pBGb6DA2ys1SCEXnakpEVQhVGUeXdRJNmlm8bNSic6WzQW3DbLpEg== X-Received: by 2002:a05:6a00:194d:b0:740:b394:3ebd with SMTP id d2e1a72fcca58-74827e7faa7mr571392b3a.7.1749141283401; Thu, 05 Jun 2025 09:34:43 -0700 (PDT) Received: from google.com ([2601:647:5600:80d0::31cd]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-747affafa64sm13442082b3a.93.2025.06.05.09.34.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jun 2025 09:34:42 -0700 (PDT) Date: Thu, 5 Jun 2025 09:34:41 -0700 From: Noah Misch To: Tom Lane Cc: Laurenz Albe , Pavel Stehule , pgsql-hackers@lists.postgresql.org Subject: Re: Wrong security context for deferred triggers? Message-ID: <20250605163441.2f.nmisch@google.com> References: <6bca669972ef0d4a665fa83c5b751ab116324f8f.camel@cybertec.at> <2653121.1730737337@sss.pgh.pa.us> <51386e72c47d922ef68eaa67451a52dc3f569843.camel@cybertec.at> <3944969.1737570355@sss.pgh.pa.us> <3e4c8fe7adce2e3956e01bae8f1754830bd2d2bf.camel@cybertec.at> <201480.1737653448@sss.pgh.pa.us> <67338f5d321ae6a4bae646e04b9fe0a1f27dd27c.camel@cybertec.at> <20250415155850.9b.nmisch@google.com> <1283937.1749136204@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1283937.1749136204@sss.pgh.pa.us> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Jun 05, 2025 at 11:10:04AM -0400, Tom Lane wrote: > Noah Misch writes: > > Phrase "the role that executed the statement" doesn't match what happens if > > the role changes mid-statement. Example of a statement that does so: > > > select set_config('role', rolname, true), current_user from pg_authid; > > > The term "security context" doesn't otherwise appear in doc/. I would just > > change "run in the security context of the role" to "run as the role". That's > > simpler and less likely to create an impression that this stops attacks. > > I don't mind s/in the security context of/as/, but not sure if we > need to do more here. Changing role mid-query seems sufficiently > unusual that we'd probably just confuse people by mentioning the > case. Unless you have a proposed wording you think is better? In postgr.es/m/1071973.1749075038@sss.pgh.pa.us of yesterday's release notes discussion, you wrote "Execute AFTER triggers as the role that was active at the moment the trigger event was queued." That's a good direction, since it's correct for the mid-query case without raising it explicitly. Maybe this way: diff --git a/doc/src/sgml/trigger.sgml b/doc/src/sgml/trigger.sgml index e9214dc..4361cdd 100644 --- a/doc/src/sgml/trigger.sgml +++ b/doc/src/sgml/trigger.sgml @@ -125,18 +125,17 @@ The execution of an AFTER trigger can be deferred to the end of the transaction, rather than the end of the statement, if it was defined as a constraint trigger. In all cases, a trigger is executed as part of the same transaction as the statement that triggered it, so if either the statement or the trigger causes an error, the effects of both will be rolled back. - Also, the trigger will always run in the security context of the role - that executed the statement that caused the trigger to fire, unless - the trigger function is defined as SECURITY DEFINER, - in which case it will run as the function owner. + Also, the trigger will always run as the role that queued the trigger + event, unless the trigger function is defined as SECURITY + DEFINER, in which case it will run as the function owner. If an INSERT contains an ON CONFLICT DO UPDATE clause, it is possible for row-level BEFORE INSERT and then BEFORE UPDATE triggers