1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Improve RegExs for extracting email addresses from keys

Based on https://github.com/pgpainless/pgpainless/pull/257/
Thanks @bratkartoffel for the initial proposed changes
This commit is contained in:
Paul Schaub 2022-03-01 12:14:09 +01:00
parent 1088b6c8ae
commit d55d6a1686
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 67 additions and 3 deletions

View file

@ -50,7 +50,8 @@ import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
*/ */
public class KeyRingInfo { public class KeyRingInfo {
private static final Pattern PATTERN_EMAIL = Pattern.compile("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"); private static final Pattern PATTERN_EMAIL_FROM_USERID = Pattern.compile("<([a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+)>");
private static final Pattern PATTERN_EMAIL_EXPLICIT = Pattern.compile("^([a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+)$");
private final PGPKeyRing keys; private final PGPKeyRing keys;
private final Signatures signatures; private final Signatures signatures;
@ -421,9 +422,14 @@ public class KeyRingInfo {
List<String> userIds = getUserIds(); List<String> userIds = getUserIds();
List<String> emails = new ArrayList<>(); List<String> emails = new ArrayList<>();
for (String userId : userIds) { for (String userId : userIds) {
Matcher matcher = PATTERN_EMAIL.matcher(userId); Matcher matcher = PATTERN_EMAIL_FROM_USERID.matcher(userId);
if (matcher.find()) { if (matcher.find()) {
emails.add(matcher.group()); emails.add(matcher.group(1));
} else {
matcher = PATTERN_EMAIL_EXPLICIT.matcher(userId);
if (matcher.find()) {
emails.add(matcher.group(1));
}
} }
} }
return emails; return emails;

View file

@ -50,6 +50,7 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.key.util.UserId; import org.pgpainless.key.util.UserId;
import org.pgpainless.util.ArmorUtils;
import org.pgpainless.util.DateUtil; import org.pgpainless.util.DateUtil;
import org.pgpainless.util.TestAllImplementations; import org.pgpainless.util.TestAllImplementations;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
@ -701,4 +702,61 @@ public class KeyRingInfoTest {
assertTrue(unboundKeyCreation.after(latestModification)); assertTrue(unboundKeyCreation.after(latestModification));
assertTrue(unboundKeyCreation.after(latestKeyCreation)); assertTrue(unboundKeyCreation.after(latestKeyCreation));
} }
@Test
public void getEmailsTest() throws IOException {
String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"Version: PGPainless\n" +
"Comment: B4A8 9FE8 9D59 31E6 BCF7 DC2F 6BA1 2CC7 9A08 8D73\n" +
"Comment: Alice Anderson <alice@email.tld> [Primary Mail Address]\n" +
"Comment: Alice A. <alice@pgpainless.org>\n" +
"Comment: <alice@openpgp.org>\n" +
"Comment: alice@rfc4880.spec\n" +
"Comment: alice anderson@invalid.mail\n" +
"Comment: Alice Anderson <alice anderson@invalid.mail>\n" +
"\n" +
"lFgEYh39eBYJKwYBBAHaRw8BAQdAegaKui2AnIZ7D4fRozwqEvbHePpU/agSN6Kr\n" +
"11uVHKoAAP4xCyRezCJ04di6+NICghNDPqWBJLtk3MI1ndlBLwcgjw9LtDdBbGlj\n" +
"ZSBBbmRlcnNvbiA8YWxpY2VAZW1haWwudGxkPiBbUHJpbWFyeSBNYWlsIEFkZHJl\n" +
"c3NdiI8EExYKAEEFAmId/XgJEGuhLMeaCI1zFiEEtKif6J1ZMea899wva6Esx5oI\n" +
"jXMCngECmwEFFgIDAQAECwkIBwUVCgkICwKZAQAA1MoBALzi4qecj+tnLdQEWbTI\n" +
"uHIc6NVoUb7p4B8Jro/ehJ1fAQDjt3+VfLUZ8QaX+TtTDGnWHyEOoJ0VxiIKdMmv\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14CRBroSzHmgiNcxYhBLSon+idWTHmvPfcL2uhLMeaCI1z\n" +
"Ap4BApsBBRYCAwEABAsJCAcFFQoJCAsAAA2cAP9ygQbt8oQtRc4oPm/LLPDjH89u\n" +
"LBMVywN0yBdEWO/ASgEAmgl1kgyMRyf28SjISAWAHiTGs0mRAn9kdwJGU4+27AGc\n" +
"XQRiHf14EgorBgEEAZdVAQUBAQdAIvJYcrgjLhPGjJ9YCaPKZcZrgpf93v3zlE/v\n" +
"GGUQrT8DAQgHAAD/WWQiuS/2UBFt97J4htg14ICcjoMnOrI4mimeZwYTtoAPrYh1\n" +
"BBgWCgAdBQJiHf14Ap4BApsMBRYCAwEABAsJCAcFFQoJCAsACgkQa6Esx5oIjXOo\n" +
"qQEAlmUF0RIpnqWqWmtKtbbTSYj6+UgV0L5n2RWtlOVdfMIA/34+rQ45pUqelgCc\n" +
"yzfUm8wDlJjT9ogVGsvtDnLokv4BnFgEYh39eBYJKwYBBAHaRw8BAQdAnQCPdWgk\n" +
"X02oa5RBIRNCAEkdf1FooxlzlDCXBUUMaMoAAP9EhqmoCsUBplDMfnMUtu1g6BLq\n" +
"qGIAOtm/HXtQ4UUo2xCFiNUEGBYKAH0FAmId/XgCngECmwIFFgIDAQAECwkIBwUV\n" +
"CgkIC18gBBkWCgAGBQJiHf14AAoJEIEZZ8Ab4jMdYsUA/ilgaT94y0hEEkEFF2Dm\n" +
"vle6KXtHHPo/G0fkcGras8W9AQDo+IQSzTJylS+AJQfTSTuGUEP8hWPG/1f7SWVo\n" +
"z6/eBgAKCRBroSzHmgiNc7A7AQDEGMAPe4guEgkCfZRFRZoWb8ahpKB3y6cYQ7t1\n" +
"qDzPRwEAhdVBeryRUcwjgwHX0xmMFK7vLkdonn8BR2++nXBO2g8=\n" +
"=ZRAy\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
List<String> emails = info.getEmailAddresses();
assertEquals(emails, Arrays.asList("alice@email.tld", "alice@pgpainless.org", "alice@openpgp.org", "alice@rfc4880.spec"));
}
} }