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 E2C551FF09D for ; Sun, 15 Nov 2026 20:43:57 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 840AB21979; Sun, 15 Nov 2026 20:41:12 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v2 access-control 4/19] fix #7805: api: token: enforce datacenter token policy Date: Sun, 15 Nov 2026 20:40:25 +0100 Message-ID: <20261115194043.3947825-5-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: 1794771649261 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.074 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) 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: OZTWHC2NPUUXWMVWBRWU7RPS5J5OUWMS X-Message-ID-Hash: OZTWHC2NPUUXWMVWBRWU7RPS5J5OUWMS 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: Check the new datacenter.cfg token-policy option on API token creation and update, so admins can centrally require expiration dates, bound the maximum token lifetime, forbid extending existing expiration dates, and require privilege separation, as commonly mandated by compliance rules like PCI DSS, SOC 2, or ISO 27001. Existing tokens stay valid, and an update only checks settings it actually changes, so unrelated edits or clients resubmitting unchanged fields are not rejected for tokens that predate the policy. The check runs before the secret is generated to avoid leaving an orphaned entry in token.cfg on rejection. Regenerating the secret of an existing token is deliberately not restricted: rotation does not extend validity, and blocking it would only discourage rotating old credentials. Changing the policy itself requires Sys.Modify on '/', like any other datacenter option. No extra auth-specific privilege is added for now, as it would end up in the same built-in roles that already carry Sys.Modify and thus allow the same users, and anyone allowed to change datacenter options can adapt the policy in any case. Signed-off-by: Thomas Lamprecht --- changes since v1: - pass the current expiration date instead of 'is-update', so that 'disallow-expiry-extension' only rejects extending it - use v5.36 in the tests src/PVE/API2/User.pm | 31 +++- src/PVE/AccessControl.pm | 62 ++++++++ src/test/Makefile | 2 + src/test/token-policy-api-test.pl | 115 ++++++++++++++ src/test/token-policy-test.pl | 242 ++++++++++++++++++++++++++++++ 5 files changed, 449 insertions(+), 3 deletions(-) create mode 100644 src/test/token-policy-api-test.pl create mode 100644 src/test/token-policy-test.pl diff --git a/src/PVE/API2/User.pm b/src/PVE/API2/User.pm index 31cf44b..78e5fc9 100644 --- a/src/PVE/API2/User.pm +++ b/src/PVE/API2/User.pm @@ -806,14 +806,20 @@ __PACKAGE__->register_method({ die "Token already exists.\n" if defined(PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid, 1)); - $full_tokenid = PVE::AccessControl::join_tokenid($userid, $tokenid); - $value = PVE::TokenConfig::generate_token($full_tokenid); - $token = {}; $token->{privsep} = defined($param->{privsep}) ? $param->{privsep} : 1; $token->{expire} = $param->{expire} if defined($param->{expire}); $token->{comment} = $param->{comment} if $param->{comment}; + # check policy before generating the secret to avoid leaving an orphan in token.cfg + PVE::AccessControl::assert_token_policy({ + expire => $token->{expire} // 0, + 'privilege-separation' => $token->{privsep}, + }); + + $full_tokenid = PVE::AccessControl::join_tokenid($userid, $tokenid); + $value = PVE::TokenConfig::generate_token($full_tokenid); + $usercfg->{users}->{$userid}->{tokens}->{$tokenid} = $token; cfs_write_file("user.cfg", $usercfg); }; @@ -899,11 +905,30 @@ __PACKAGE__->register_method({ $usercfg = cfs_read_file("user.cfg"); $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); + my $old_expire = $token->{expire} // 0; + my $old_privilege_separation = $token->{privsep} // 0; + $token->{privsep} = $param->{privsep} if defined($param->{privsep}); $token->{expire} = $param->{expire} if defined($param->{expire}); $token->{comment} = $param->{comment} if defined($param->{comment}); delete $token->{comment} if (!length $token->{comment}); + my $new_expire = $token->{expire} // 0; + my $new_privilege_separation = $token->{privsep} // 0; + + # only check what this update changes, so that tokens that predate the policy + # stay editable + my $expire_change = $new_expire != $old_expire ? $new_expire : undef; + my $privilege_separation_change = + $new_privilege_separation != $old_privilege_separation + ? $new_privilege_separation + : undef; + PVE::AccessControl::assert_token_policy({ + expire => $expire_change, + 'privilege-separation' => $privilege_separation_change, + 'current-expire' => $old_expire, + }); + my $deletable = { comment => 1, }; diff --git a/src/PVE/AccessControl.pm b/src/PVE/AccessControl.pm index a4e1c36..e2027cf 100644 --- a/src/PVE/AccessControl.pm +++ b/src/PVE/AccessControl.pm @@ -21,6 +21,7 @@ use PVE::OTP; use PVE::Ticket; use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print); use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); +use PVE::DataCenterConfig; # ensure the 'datacenter.cfg' cfs parser is registered use PVE::JSONSchema qw(register_standard_option get_standard_option); use PVE::RS::TFA; @@ -691,6 +692,67 @@ sub check_user_enabled { return 1; # enabled and not expired } +# Assert that a token's expiration date and privilege separation setting conform to the +# datacenter token policy. Dies with a descriptive message on violation. +# +# Takes a hash reference with the following keys: +# - expire: the expiration date to check (epoch seconds, 0 for none); undef skips the expiration +# 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 +# - ctime, policy: override the reference time for the maximum lifetime and the policy from the +# datacenter config, for testing +# +# Callers pass only the values an operation sets or changes, so that unrelated updates do not +# reject tokens that predate the policy. +sub assert_token_policy { + my ($param) = @_; + + # integer API parameters keep their submitted spelling, and zero spellings like '00' or '-0' + # are true in Perl, so numify to not mistake them for a set expiration date + my $expire = defined($param->{expire}) ? int($param->{expire}) : undef; + my $privilege_separation = $param->{'privilege-separation'}; + + return if !defined($expire) && !defined($privilege_separation); + + my $policy = $param->{policy} // cfs_read_file('datacenter.cfg')->{'token-policy'}; + return if !$policy; + + if (defined($expire)) { + # removing the expiration date or moving it later extends the token + my $current_expire = $param->{'current-expire'}; + if ( + $policy->{'disallow-expiry-extension'} + && $current_expire + && (!$expire || $expire > $current_expire) + ) { + die "the datacenter token policy does not allow extending the expiration date of an" + . " existing token\n"; + } + + my $ctime = $param->{ctime} // time(); + my $max_lifetime = $policy->{'max-lifetime'}; + # a token that never expires exceeds any maximum lifetime + my $require_expiry = $policy->{'require-expiry'} || $max_lifetime; + + die "the datacenter token policy requires an expiration date\n" + if $require_expiry && !$expire; + + if ($max_lifetime && $expire && $expire > $ctime + $max_lifetime) { + die "the expiration date exceeds the maximum token lifetime of $max_lifetime seconds" + . " set by the datacenter token policy\n"; + } + } + + if ( + defined($privilege_separation) + && $policy->{'require-privilege-separation'} + && !$privilege_separation + ) { + die "the datacenter token policy requires privilege separation\n"; + } +} + sub check_token_exist { my ($usercfg, $username, $tokenid, $noerr) = @_; diff --git a/src/test/Makefile b/src/test/Makefile index 8f3ba47..357bb02 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -16,6 +16,8 @@ check: perl -I.. realm_sync_test.pl perl -I.. pve-realm-password-hash-test.pl perl -I.. pool-name-0-test.pl + perl -I.. token-policy-test.pl + perl -I.. token-policy-api-test.pl perl -I.. user-api-groups-test.pl perl -I.. acl-api-members-test.pl perl -I.. realm-group-name-0-test.pl diff --git a/src/test/token-policy-api-test.pl b/src/test/token-policy-api-test.pl new file mode 100644 index 0000000..5af4e58 --- /dev/null +++ b/src/test/token-policy-api-test.pl @@ -0,0 +1,115 @@ +#!/usr/bin/perl + +use v5.36; + +use Test::More; +use Test::MockModule; + +use PVE::AccessControl; +use PVE::API2::User; + +# Exercise the token create and update API handlers with the real schema validation and user +# config parser/writer, mocking out the cluster file system and secret generation. The integer +# schema accepts zero spellings like '00', '+0', or '-0' without canonicalizing them, so the +# policy enforcement at the API boundary must not mistake them for a set expiration date. + +my ($policy, $user_cfg_raw, $secret_calls); + +my $read = sub { + my ($file) = @_; + return PVE::AccessControl::parse_user_config($file, $user_cfg_raw) if $file eq 'user.cfg'; + return { 'token-policy' => $policy } if $file eq 'datacenter.cfg'; + die "unexpected cfs_read_file('$file') call\n"; +}; + +my $access_control_module = Test::MockModule->new('PVE::AccessControl'); +$access_control_module->mock(cfs_read_file => $read); +$access_control_module->mock(lock_user_config => sub { $_[0]->() }); + +my $api_user_module = Test::MockModule->new('PVE::API2::User'); +$api_user_module->mock(cfs_read_file => $read); +$api_user_module->mock( + cfs_write_file => sub { + my ($file, $cfg) = @_; + die "unexpected cfs_write_file('$file') call\n" if $file ne 'user.cfg'; + $user_cfg_raw = PVE::AccessControl::write_user_config($file, $cfg); + }, +); + +my $token_config_module = Test::MockModule->new('PVE::TokenConfig'); +$token_config_module->mock( + generate_token => sub { + $secret_calls++; + return 'test-secret'; + }, +); + +my $reset = sub { + my ($test_policy, $expire) = @_; + $policy = $test_policy; + $user_cfg_raw = "user:test\@pve:1:0::::::\n"; + $user_cfg_raw .= "token:test\@pve!old:$expire:1:some comment:\n" if defined($expire); + $secret_calls = 0; +}; + +my $invoke = sub { + my ($method, $param) = @_; + my $ok = eval { + PVE::API2::User->$method({ userid => 'test@pve', %$param }); + 1; + }; + return ($ok, $@); +}; + +my $now = time(); + +for my $policy_variant ({ 'require-expiry' => 1 }, { 'max-lifetime' => 86400 }) { + my $desc = join(',', map { "$_=$policy_variant->{$_}" } sort keys $policy_variant->%*); + + for my $zero ('0', '00', '+0', '-0') { + $reset->($policy_variant); + my ($ok, $err) = $invoke->('generate_token', { tokenid => 'new', expire => $zero }); + ok( + !$ok && $err =~ /requires an expiration date/, + "$desc: create with expire=$zero rejected", + ); + is($secret_calls, 0, "$desc: rejected create generates no secret"); + ok($user_cfg_raw !~ /!new/, "$desc: rejected create writes no token"); + + $reset->($policy_variant, $now + 600); + my $before = $user_cfg_raw; + ($ok, $err) = + $invoke->('update_token_info', { tokenid => 'old', expire => $zero, regenerate => 1 }); + ok( + !$ok && $err =~ /requires an expiration date/, "$desc: update to expire=$zero rejected", + ); + is($secret_calls, 0, "$desc: rejected update rotates no secret"); + is($user_cfg_raw, $before, "$desc: rejected update writes nothing"); + } + + $reset->($policy_variant); + my ($ok, $err) = $invoke->('generate_token', { tokenid => 'new', expire => $now + 600 }); + ok($ok, "$desc: compliant create accepted") or diag($err); + is($secret_calls, 1, "$desc: accepted create generates a secret"); +} + +# unrelated updates of a token that predates the policy must keep working +$reset->({ 'require-expiry' => 1 }, 0); +my ($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', comment => 'changed' }); +ok($ok, 'unrelated update of a non-conforming token stays allowed') or diag($err); + +# disallow-expiry-extension only forbids what would prolong a token +$reset->({ 'disallow-expiry-extension' => 1 }, $now + 600); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 1200 }); +ok(!$ok && $err =~ /does not allow extending/, 'extending the expiry is rejected'); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => 0 }); +ok(!$ok && $err =~ /does not allow extending/, 'removing the expiry is rejected'); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 300 }); +ok($ok, 'shortening the expiry stays allowed') or diag($err); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 300 }); +ok($ok, 'resubmitting the unchanged expiry stays allowed') or diag($err); +$reset->({ 'disallow-expiry-extension' => 1 }, 0); +($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 600 }); +ok($ok, 'setting an expiry on a token without one stays allowed') or diag($err); + +done_testing(); diff --git a/src/test/token-policy-test.pl b/src/test/token-policy-test.pl new file mode 100644 index 0000000..d29c781 --- /dev/null +++ b/src/test/token-policy-test.pl @@ -0,0 +1,242 @@ +#!/usr/bin/perl + +use v5.36; + +use Test::More; +use Test::MockModule; + +use PVE::AccessControl; + +# tests pass the policy explicitly; keep the fallback read of datacenter.cfg from hitting a +# real pmxcfs so the cases without a policy exercise that path against an empty config +my $access_control_module = Test::MockModule->new('PVE::AccessControl'); +$access_control_module->mock( + cfs_read_file => sub { + my ($file) = @_; + die "unexpected cfs_read_file('$file') call\n" if $file ne 'datacenter.cfg'; + return {}; + }, +); + +# fixed reference time and a 90-day maximum lifetime for deterministic bounds checks +my $now = 1_000_000_000; +my $max = 90 * 24 * 60 * 60; + +sub check { + my ($desc, $policy, $param, $should_die) = @_; + + my $err; + eval { + PVE::AccessControl::assert_token_policy({ + ctime => $now, + policy => $policy, + $param->%*, + }); + 1; + } or $err = $@; + + if ($should_die) { + ok($err, "$desc: rejected") or diag("expected rejection but call succeeded"); + } else { + ok(!$err, "$desc: accepted") or diag("unexpected rejection: $err"); + } +} + +# no policy configured -> everything is allowed +check( + 'no policy, never-expiring full-priv token', + undef, + { expire => 0, 'privilege-separation' => 0 }, + 0, +); +check('no policy, undef dimensions', undef, {}, 0); +check( + 'empty policy, never-expiring full-priv token', + {}, + { expire => 0, 'privilege-separation' => 0 }, + 0, +); + +# require-expiry +my $req_expire = { 'require-expiry' => 1 }; +check('require-expiry, no expiry', $req_expire, { expire => 0 }, 1); +check('require-expiry, future expiry', $req_expire, { expire => $now + 100 }, 0); +check('require-expiry, expiry dimension not touched', $req_expire, {}, 0); +check( + 'require-expiry, update removing the expiry', + $req_expire, + { expire => 0, 'current-expire' => $now + 50 }, + 1, +); +check( + 'require-expiry, does not constrain privsep', + $req_expire, + { expire => $now + 100, 'privilege-separation' => 0 }, + 0, +); + +# max-lifetime implies an expiry is required and bounds how far it may reach +my $max_life = { 'max-lifetime' => $max }; +check('max-lifetime, never-expiring is rejected', $max_life, { expire => 0 }, 1); +check('max-lifetime, within bound', $max_life, { expire => $now + 100 }, 0); +check('max-lifetime, exactly at bound', $max_life, { expire => $now + $max }, 0); +check('max-lifetime, one second past bound', $max_life, { expire => $now + $max + 1 }, 1); +check('max-lifetime, expiry dimension not touched', $max_life, {}, 0); +check( + 'max-lifetime, update prolonging past the bound', + $max_life, + { expire => $now + $max + 1, 'current-expire' => $now + 50 }, + 1, +); +check('max-lifetime, expiry in the past accepted', $max_life, { expire => $now - 100 }, 0); +check( + 'max-lifetime with explicit require-expiry=0 still requires an expiry', + { 'require-expiry' => 0, 'max-lifetime' => $max }, + { expire => 0 }, + 1, +); + +# the integer schema passes zero spellings like '00' through with their submitted string +# spelling, and those are true as Perl scalars; they must still count as "no expiration date" +for my $zero ('0', '00', '+0', '-0') { + check("require-expiry, create with zero spelling '$zero'", $req_expire, { expire => $zero }, 1); + check( + "require-expiry, update removing the expiry via zero spelling '$zero'", + $req_expire, + { expire => $zero, 'current-expire' => $now + 50 }, + 1, + ); + check("max-lifetime, create with zero spelling '$zero'", $max_life, { expire => $zero }, 1); +} + +# disallow-expiry-extension rejects removing or moving the expiration date later on update, +# but neither shortening it, the initial expiration date on creation, nor updates that leave the +# expiration date untouched +my $no_extension = { 'disallow-expiry-extension' => 1 }; +check( + 'disallow-expiry-extension, update extending expiry', + $no_extension, + { expire => $now + 200, 'current-expire' => $now + 100 }, + 1, +); +check( + 'disallow-expiry-extension, update extending expiry by one second', + $no_extension, + { expire => $now + 101, 'current-expire' => $now + 100 }, + 1, +); +check( + 'disallow-expiry-extension, update removing expiry', + $no_extension, + { expire => 0, 'current-expire' => $now + 100 }, + 1, +); +check( + 'disallow-expiry-extension, update shortening expiry', + $no_extension, + { expire => $now + 100, 'current-expire' => $now + 200 }, + 0, +); +check( + 'disallow-expiry-extension, update moving expiry into the past', + $no_extension, + { expire => $now - 100, 'current-expire' => $now + 100 }, + 0, +); +check( + 'disallow-expiry-extension, update setting an expiry on a token without one', + $no_extension, + { expire => $now + 100, 'current-expire' => 0 }, + 0, +); +check( + 'disallow-expiry-extension, update not touching expiry', + $no_extension, + { 'current-expire' => $now + 100 }, + 0, +); +check('disallow-expiry-extension, creation sets expiry', $no_extension, { expire => $now + 100 }, + 0); +check('disallow-expiry-extension, creation without expiry', $no_extension, { expire => 0 }, 0); +check( + 'disallow-expiry-extension, does not constrain privsep', + $no_extension, + { 'privilege-separation' => 0, 'current-expire' => $now + 100 }, + 0, +); +check( + 'disallow-expiry-extension wins over a compliant extension under require-expiry', + { 'disallow-expiry-extension' => 1, 'require-expiry' => 1 }, + { expire => $now + 200, 'current-expire' => $now + 100 }, + 1, +); +check( + 'disallow-expiry-extension, shortening under max-lifetime stays allowed', + { 'disallow-expiry-extension' => 1, 'max-lifetime' => $max }, + { expire => $now + 100, 'current-expire' => $now + 200 }, + 0, +); + +# require-privilege-separation +my $require_privilege_separation = { 'require-privilege-separation' => 1 }; +check( + 'require-privilege-separation, full-priv token rejected', + $require_privilege_separation, + { 'privilege-separation' => 0 }, + 1, +); +check( + 'require-privilege-separation, privsep token accepted', + $require_privilege_separation, + { 'privilege-separation' => 1 }, + 0, +); +check( + 'require-privilege-separation, privsep dimension not touched', + $require_privilege_separation, + {}, + 0, +); +check( + 'require-privilege-separation, does not constrain expiry', + $require_privilege_separation, + { expire => 0, 'privilege-separation' => 1 }, + 0, +); + +# combined policy, as an admin would set for a compliance baseline +my $full = { 'require-expiry' => 1, 'max-lifetime' => $max, 'require-privilege-separation' => 1 }; +check( + 'full policy, compliant token', + $full, + { expire => $now + 100, 'privilege-separation' => 1 }, + 0, +); +check('full policy, missing expiry', $full, { expire => 0, 'privilege-separation' => 1 }, 1); +check( + 'full policy, expiry too far out', + $full, + { expire => $now + $max + 1, 'privilege-separation' => 1 }, + 1, +); +check( + 'full policy, full-priv token', + $full, + { expire => $now + 100, 'privilege-separation' => 0 }, + 1, +); +# partial updates only check what they change +check( + 'full policy, update touching only a valid expiry', + $full, + { expire => $now + 100, 'current-expire' => $now + 50 }, + 0, +); +check( + 'full policy, update touching only privsep', + $full, + { 'privilege-separation' => 1, 'current-expire' => $now + 50 }, + 0, +); + +done_testing(); -- 2.47.3