PostGpgKeys: Gracefully handle malformed GPG keys input

Bug: Issue 7647
Change-Id: I226e1d560936db1f1ef0d447ea00b59c66189a96
This commit is contained in:
David Pursehouse 2017-11-07 15:53:59 +09:00
parent 021ccccf19
commit 9df753e05e
2 changed files with 11 additions and 0 deletions

View File

@ -787,6 +787,14 @@ public class AccountIT extends AbstractDaemonTest {
ImmutableList.of(key2.getKeyIdString()));
}
@Test
public void addMalformedGpgKey() throws Exception {
String key = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\ntest\n-----END PGP PUBLIC KEY BLOCK-----";
exception.expect(BadRequestException.class);
exception.expectMessage("Failed to parse GPG keys");
addGpgKey(key);
}
@Test
@UseSsh
public void sshKeys() throws Exception {

View File

@ -66,6 +66,7 @@ import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPRuntimeOperationException;
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
@ -183,6 +184,8 @@ public class PostGpgKeys implements RestModifyView<AccountResource, Input> {
"Cannot both add and delete key: " + keyToString(keyRing.getPublicKey()));
}
keyRings.add(keyRing);
} catch (PGPRuntimeOperationException e) {
throw new BadRequestException("Failed to parse GPG keys", e);
}
}
return keyRings;