From 0fae3c4b4b8feea3d01d50910a4d0708431114cc Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Wed, 22 Nov 2023 14:05:55 +0100 Subject: [PATCH] Fix bug in parsing USERID_HINT status causing missing real user name in BadPassphrases array --- Crypt/GPG/ProcessHandler.php | 2 +- tests/DecryptTest.php | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Crypt/GPG/ProcessHandler.php b/Crypt/GPG/ProcessHandler.php index 7d57649..dcfaee1 100644 --- a/Crypt/GPG/ProcessHandler.php +++ b/Crypt/GPG/ProcessHandler.php @@ -251,7 +251,7 @@ public function handleStatus($line) // remember the user id for pretty exception messages // GnuPG 2.1.15 gives me: "USERID_HINT 0000000000000000 [?]" $keyId = $tokens[1]; - if (strcspn($keyId, '0')) { + if (preg_match('/[1-9A-F]/', $keyId)) { $username = implode(' ', array_splice($tokens, 2)); $this->data['BadPassphrases'][$keyId] = $username; } diff --git a/tests/DecryptTest.php b/tests/DecryptTest.php index ad94144..86c6835 100644 --- a/tests/DecryptTest.php +++ b/tests/DecryptTest.php @@ -228,8 +228,6 @@ public function testDecryptBadPassphraseException_missing() */ public function testDecryptBadPassphraseException_bad() { - $this->expectException('Crypt_GPG_BadPassphraseException'); - // encrypted with first-keypair@example.com // {{{ encrypted data $encryptedData = <<gpg->addDecryptKey('first-keypair@example.com', 'incorrect'); - $this->gpg->decrypt($encryptedData); + try { + $this->gpg->addDecryptKey('first-keypair@example.com', 'incorrect'); + $this->gpg->decrypt($encryptedData); + } + catch (\Crypt_GPG_BadPassphraseException $e) { + $badKeys = $e->getBadPassphrases(); + $missingKeys = $e->getMissingPassphrases(); + + $this->assertCount(1, $badKeys); + $this->assertCount(0, $missingKeys); + $this->assertSame( + $badKeys[key($badKeys)], + "First Keypair Test Key (do not encrypt important data with this key) " + ); + } } /**