From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id CD13F1FF09D for ; Sun, 15 Nov 2026 20:41:45 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 779B12170D; Sun, 15 Nov 2026 20:41:03 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v2 access-control 6/19] api: token: allow extending tokens on secret regeneration Date: Sun, 15 Nov 2026 20:40:27 +0100 Message-ID: <20261115194043.3947825-7-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: 1794771649383 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.078 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) KAM_SHORT 0.001 Use of a URL Shortener for very short URL POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes 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: 3425I4WGZKPV3KHCVAGBGE57V5TMFRLU X-Message-ID-Hash: 3425I4WGZKPV3KHCVAGBGE57V5TMFRLU 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: Honor the new 'allow-extension-on-regenerate' policy setting: 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, so a rejected extension does not rotate it. Suggested-by: Shannon Sterz Signed-off-by: Thomas Lamprecht --- new in v2 src/PVE/API2/User.pm | 1 + src/PVE/AccessControl.pm | 8 +++++- src/test/token-policy-api-test.pl | 11 ++++++++ src/test/token-policy-test.pl | 45 +++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/PVE/API2/User.pm b/src/PVE/API2/User.pm index 6490060..453d317 100644 --- a/src/PVE/API2/User.pm +++ b/src/PVE/API2/User.pm @@ -932,6 +932,7 @@ __PACKAGE__->register_method({ expire => $expire_change, 'privilege-separation' => $privilege_separation_change, 'current-expire' => $old_expire, + regenerate => $regenerate, }); my $deletable = { diff --git a/src/PVE/AccessControl.pm b/src/PVE/AccessControl.pm index 930cb5a..3192fed 100644 --- a/src/PVE/AccessControl.pm +++ b/src/PVE/AccessControl.pm @@ -700,6 +700,8 @@ sub check_user_enabled { # date checks # - privilege-separation: the privilege separation flag to check; undef skips that check # - current-expire: the expiration date of an existing token before the update, 0 for none +# - regenerate: whether the update also regenerates the secret, see +# 'allow-extension-on-regenerate' # - ctime, policy: override the reference time for the maximum lifetime and the policy from the # datacenter config, for testing # @@ -719,10 +721,14 @@ sub assert_token_policy { return if !$policy; if (defined($expire)) { - # 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 my $current_expire = $param->{'current-expire'}; + my $regenerate_allows_extension = + $param->{regenerate} && $policy->{'allow-extension-on-regenerate'}; if ( $policy->{'disallow-expiry-extension'} + && !$regenerate_allows_extension && $current_expire && (!$expire || $expire > $current_expire) ) { diff --git a/src/test/token-policy-api-test.pl b/src/test/token-policy-api-test.pl index ebaece7..090d576 100644 --- a/src/test/token-policy-api-test.pl +++ b/src/test/token-policy-api-test.pl @@ -134,4 +134,15 @@ $reset->({ 'default-to-max-lifetime' => 1, 'require-expiry' => 1 }); ($ok, $err) = $invoke->('generate_token', { tokenid => 'new' }); ok(!$ok && $err =~ /requires an expiration date/, 'no default without a maximum lifetime'); +# allow-extension-on-regenerate: extending needs the secret regenerated in the same request +$reset->({ 'disallow-expiry-extension' => 1, 'allow-extension-on-regenerate' => 1 }, $now + 600); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 1200 }); +ok(!$ok && $err =~ /does not allow extending/, 'extending without regeneration is rejected'); +is($secret_calls, 0, 'the rejected update does not touch the secret'); +($ok, $err) = + $invoke->('update_token_info', { tokenid => 'old', expire => $now + 1200, regenerate => 1 }); +ok($ok, 'extending together with regeneration is accepted') or diag($err); +is($secret_calls, 1, 'the accepted update regenerates the secret'); +like($user_cfg_raw, qr/^token:test\@pve!old:${\($now + 1200)}:/m, 'the extended expiry is stored'); + done_testing(); diff --git a/src/test/token-policy-test.pl b/src/test/token-policy-test.pl index f43fc8c..2310fdc 100644 --- a/src/test/token-policy-test.pl +++ b/src/test/token-policy-test.pl @@ -177,6 +177,51 @@ check( 0, ); +# allow-extension-on-regenerate lifts the extension ban only together with a secret regeneration +my $rotation = { 'disallow-expiry-extension' => 1, 'allow-extension-on-regenerate' => 1 }; +check( + 'allow-extension-on-regenerate, extension with regeneration', + $rotation, + { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 1 }, + 0, +); +check( + 'allow-extension-on-regenerate, removal with regeneration', + $rotation, + { expire => 0, 'current-expire' => $now + 100, regenerate => 1 }, + 0, +); +check( + 'allow-extension-on-regenerate, extension without regeneration', + $rotation, + { expire => $now + 200, 'current-expire' => $now + 100 }, + 1, +); +check( + 'allow-extension-on-regenerate, extension with regenerate=0', + $rotation, + { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 0 }, + 1, +); +check( + 'disallow-expiry-extension alone ignores regeneration', + $no_extension, + { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 1 }, + 1, +); +check( + 'allow-extension-on-regenerate, max-lifetime still bounds the extension', + { $rotation->%*, 'max-lifetime' => $max }, + { expire => $now + $max + 1, 'current-expire' => $now + 100, regenerate => 1 }, + 1, +); +check( + 'allow-extension-on-regenerate, extension within max-lifetime with regeneration', + { $rotation->%*, 'max-lifetime' => $max }, + { expire => $now + $max, 'current-expire' => $now + 100, regenerate => 1 }, + 0, +); + # require-privilege-separation my $require_privilege_separation = { 'require-privilege-separation' => 1 }; check( -- 2.47.3