public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Thomas Ellmenreich <t.ellmenreich@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	[thread overview]
Message-ID: <20261118103127.165100-8-t.ellmenreich@proxmox.com> (raw)
In-Reply-To: <20261118103127.165100-1-t.ellmenreich@proxmox.com>

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 <t.ellmenreich@proxmox.com>
---
 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





  parent reply	other threads:[~2026-11-18 10:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:31 [PATCH common v2 00/11] pem parser: refactor parsing to cover more edge cases Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 01/11] certificate tests: use q{} for multiline strings Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 02/11] pem parser: add constant to match any label Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 03/11] pem parser: refactor check_pem to use list instead of string Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 04/11] pem parser: stricter handling of newlines Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 05/11] certificate formats: enable multiple parsing for pem-string format Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 06/11] pem parsing: strip all surrounding text from chain entries Thomas Ellmenreich
2026-11-18 10:31 ` Thomas Ellmenreich [this message]
2026-11-18 10:31 ` [PATCH common v2 08/11] pem parsing: default splitting to any label Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 09/11] pem to der: create clearer error boundary Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 10/11] certificate tests: refactor and exapand testsuite Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 11/11] pom parsing: allow for CRLF line ending Thomas Ellmenreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118103127.165100-8-t.ellmenreich@proxmox.com \
    --to=t.ellmenreich@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal