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 A921E1FF0C0 for ; Wed, 18 Nov 2026 11:32:27 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 65B61218A1; Wed, 18 Nov 2026 11:31:41 +0100 (CET) From: Thomas Ellmenreich To: pve-devel@lists.proxmox.com Subject: [PATCH common v2 07/11] pem parsing: make validation of pem entry stricter Date: Wed, 18 Nov 2026 11:31:21 +0100 Message-ID: <20261118103127.165100-8-t.ellmenreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118103127.165100-1-t.ellmenreich@proxmox.com> References: <20261118103127.165100-1-t.ellmenreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794997889645 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.332 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: 7J6IDDAMVGZAWBFRGTRCSVSFB7WI7I4N X-Message-ID-Hash: 7J6IDDAMVGZAWBFRGTRCSVSFB7WI7I4N X-MailFrom: t.ellmenreich@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 CC: Thomas Ellmenreich X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: By stripping all content around single validated entries, the validation itself can be made a lot stricter. The following changes where made to the validation: * The header is expected to be exactly at the beginning of the string. * The regex matching the base64 lines, now either expects a multiple of 4 or the correct number of padding '='. Also updated the tests to match this new validation. Signed-off-by: Thomas Ellmenreich --- src/PVE/Certificate.pm | 12 +++++++----- test/check_pem_test.pl | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm index 84b97e8..c1e0368 100644 --- a/src/PVE/Certificate.pm +++ b/src/PVE/Certificate.pm @@ -120,15 +120,17 @@ my $footer_re = sub { my $pem_re = sub { my ($label) = @_; - my $b64_char_re = qr![0-9A-Za-z\+/]!; # see RFC 7468 + my $b64_char_re = qr![0-9A-Za-z\+/]\n*!; # see RFC 7468 my $header = $header_re->($label); my $footer = $footer_re->($label); return qr{ - $header - (?:(?:$b64_char_re)+\s*\n)* - (?:$b64_char_re)*(?:=\s*\n=|={0,2})?\s*\n - $footer + \A$header + (?:(?:$b64_char_re){4})* + (?:(?:$b64_char_re){4} + |(?:$b64_char_re){3}=\s* + |(?:$b64_char_re){2}=\s*=\s*) + $footer }x; }; diff --git a/test/check_pem_test.pl b/test/check_pem_test.pl index b1bb6f0..c074505 100755 --- a/test/check_pem_test.pl +++ b/test/check_pem_test.pl @@ -24,7 +24,7 @@ ZXN0IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEv5Q8q1p7qZ2gqkQ0Qn5x 0n9yqv8n8n7n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8aNT MFEwHQYDVR0OBBYEFOu2Y0bq8v3z7qkq1m1Qwqkq1m1QMB8GA1UdIwQYMBaAFOu2 Y0bq8v3z7qkq1m1Qwqkq1m1QMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwID -SAAwRQIhANfakefakefakefakefakefakefakefakefake +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----}, }, { @@ -40,14 +40,14 @@ ZXN0IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEv5Q8q1p7qZ2gqkQ0Qn5x 0n9yqv8n8n7n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8n8aNT MFEwHQYDVR0OBBYEFOu2Y0bq8v3z7qkq1m1Qwqkq1m1QMB8GA1UdIwQYMBaAFOu2 Y0bq8v3z7qkq1m1Qwqkq1m1QMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwID -SAAwRQIhANfakefakefakefakefakefakefakefakefake +TG9yZW0gaXBzdW0= -----END SOME LABEL-----}, }, { expected_success => 1, name => "simple pem", pem => q{-----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----}, }, { @@ -55,7 +55,7 @@ test name => "spaced out pem", pem => q{-----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----}, }, @@ -66,7 +66,7 @@ test -----END CERTIFICATE-----}, }, { - expected_success => 1, + expected_success => 0, name => "many newlines pem", pem => q{-----BEGIN CERTIFICATE----- @@ -86,11 +86,11 @@ test pem => q{ some extended content -----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----}, expected_result => q{-----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----}, }, { @@ -98,11 +98,11 @@ test name => "following content pem", pem => q{ -----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE----- some extended content}, expected_result => q{-----BEGIN CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----END CERTIFICATE-----} }, { @@ -117,7 +117,7 @@ test name => "upsidedown pem", pem => q{ -----END CERTIFICATE----- -test +TG9yZW0gaXBzdW0= -----BEGIN CERTIFICATE-----}, }, { -- 2.47.3