From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id E8A9C1FF09D for ; Sun, 15 Nov 2026 20:43:22 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id C6407218E0; Sun, 15 Nov 2026 20:41:10 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v2 proxmox 19/19] access-control: allow extending tokens on secret regeneration Date: Sun, 15 Nov 2026 20:40:40 +0100 Message-ID: <20261115194043.3947825-20-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com> References: <20261115194043.3947825-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794771650361 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.225 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: OVCN7ZK4AD5464G7LCDTFV6KWS4MQZAN X-Message-ID-Hash: OVCN7ZK4AD5464G7LCDTFV6KWS4MQZAN X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Counterpart of the new 'allow-extension-on-regenerate' token policy setting in PVE: with it, an update that regenerates the secret may also extend the expiration date despite 'disallow-expiry-extension', as the old credential stops working just like it would on recreating the token. The maximum lifetime still bounds the new date, and the check still runs before the secret is regenerated. Suggested-by: Shannon Sterz Signed-off-by: Thomas Lamprecht --- new in v2 proxmox-access-control/src/api/tokens.rs | 8 +++- proxmox-access-control/src/types.rs | 56 ++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/proxmox-access-control/src/api/tokens.rs b/proxmox-access-control/src/api/tokens.rs index 0ddcfdd3..0c4d408f 100644 --- a/proxmox-access-control/src/api/tokens.rs +++ b/proxmox-access-control/src/api/tokens.rs @@ -220,8 +220,12 @@ pub fn update_token( if let Some(expire) = expire { if let Some(policy) = crate::init::access_conf().token_policy()? { - let current = data.expire.unwrap_or(0); - policy.check_expiry_update(expire, current, proxmox_time::epoch_i64())?; + policy.check_expiry_update( + expire, + data.expire.unwrap_or(0), + regenerate == Some(true), + proxmox_time::epoch_i64(), + )?; } data.expire = if expire > 0 { Some(expire) } else { None }; } diff --git a/proxmox-access-control/src/types.rs b/proxmox-access-control/src/types.rs index 1377bc50..7b8bb5ca 100644 --- a/proxmox-access-control/src/types.rs +++ b/proxmox-access-control/src/types.rs @@ -193,6 +193,10 @@ pub struct TokenApiEntry { optional: true, default: false, }, + "allow-extension-on-regenerate": { + optional: true, + default: false, + }, }, )] #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize, Updater)] @@ -224,6 +228,12 @@ pub struct TokenPolicy { /// `max-lifetime`. #[serde(default, skip_serializing_if = "Option::is_none")] pub default_to_max_lifetime: Option, + + /// Allow extending the expiration date of an existing API token when its secret is + /// regenerated in the same request, despite `disallow-expiry-extension`. The maximum lifetime + /// still applies. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub allow_extension_on_regenerate: Option, } impl TokenPolicy { @@ -248,13 +258,22 @@ impl TokenPolicy { /// /// `expire` is the submitted and `current` the stored expiration date, 0 meaning none for both. /// An unchanged date always passes, so updates of tokens that predate the policy keep working. - pub fn check_expiry_update(&self, expire: i64, current: i64, now: i64) -> Result<(), Error> { + /// `regenerate` tells whether the update also regenerates the secret. + pub fn check_expiry_update( + &self, + expire: i64, + current: i64, + regenerate: bool, + now: i64, + ) -> Result<(), Error> { if expire == current { return Ok(()); } - // removing the expiration date or moving it later extends the token + // removing the expiration date or moving it later extends the token; regenerating the + // secret along with it is like recreating the token, which the policy may allow if self.disallow_expiry_extension.unwrap_or_default() + && !(regenerate && self.allow_extension_on_regenerate.unwrap_or_default()) && current != 0 && (expire == 0 || expire > current) { @@ -445,6 +464,7 @@ mod tests { max_lifetime: None, disallow_expiry_extension: None, default_to_max_lifetime: None, + allow_extension_on_regenerate: None, }; const REQUIRE_EXPIRY: TokenPolicy = TokenPolicy { @@ -467,7 +487,16 @@ mod tests { } fn update_ok(policy: &TokenPolicy, expire: i64, current: i64) -> bool { - policy.check_expiry_update(expire, current, NOW).is_ok() + policy + .check_expiry_update(expire, current, false, NOW) + .is_ok() + } + + // an update that also regenerates the secret + fn rotate_ok(policy: &TokenPolicy, expire: i64, current: i64) -> bool { + policy + .check_expiry_update(expire, current, true, NOW) + .is_ok() } #[test] @@ -558,6 +587,27 @@ mod tests { assert!(create_ok(&NO_EXTENSION, 0)); } + #[test] + fn allow_extension_on_regenerate() { + let policy = TokenPolicy { + allow_extension_on_regenerate: Some(true), + ..NO_EXTENSION + }; + // extending or removing the date passes only together with a secret regeneration + assert!(rotate_ok(&policy, NOW + 200, CURRENT)); + assert!(rotate_ok(&policy, 0, CURRENT)); + assert!(!update_ok(&policy, NOW + 200, CURRENT)); + // a policy without the setting ignores the regeneration + assert!(!rotate_ok(&NO_EXTENSION, NOW + 200, CURRENT)); + // the maximum lifetime still bounds the extension + let bounded = TokenPolicy { + max_lifetime: Some(MAX as u64), + ..policy + }; + assert!(rotate_ok(&bounded, NOW + MAX, CURRENT)); + assert!(!rotate_ok(&bounded, NOW + MAX + 1, CURRENT)); + } + #[test] fn disallow_expiry_extension_combined() { let policy = TokenPolicy { -- 2.47.3