Skip to content

Commit

Permalink
Fix bug in parsing USERID_HINT status causing missing real user name …
Browse files Browse the repository at this point in the history
…in BadPassphrases array
  • Loading branch information
alecpl committed Nov 22, 2023
1 parent e38fef2 commit 0fae3c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Crypt/GPG/ProcessHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 15 additions & 4 deletions tests/DecryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ public function testDecryptBadPassphraseException_missing()
*/
public function testDecryptBadPassphraseException_bad()
{
$this->expectException('Crypt_GPG_BadPassphraseException');

// encrypted with [email protected]
// {{{ encrypted data
$encryptedData = <<<TEXT
Expand All @@ -255,8 +253,21 @@ public function testDecryptBadPassphraseException_bad()
TEXT;
// }}}

$this->gpg->addDecryptKey('[email protected]', 'incorrect');
$this->gpg->decrypt($encryptedData);
try {
$this->gpg->addDecryptKey('[email protected]', '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) <[email protected]>"
);
}
}

/**
Expand Down

0 comments on commit 0fae3c4

Please sign in to comment.