From a291706e964e3c76383a92f68ad179788b582582 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 22 Nov 2016 17:11:41 +0900 Subject: AccountSshKey: Strip newline characters out of public key string When an ssh public key is migrated from the database to the git backend, it is stored in the authorized_keys file in the user's ref in All-Users. If the public key has newlines, each line is read back as a separate key, each of which will be considered invalid. Strip newlines out of the public key string to prevent this situation. Bug: Issue 4643 Change-Id: If3971fc1c432c79364206b2f3633db1629267ba0 --- .../com/google/gerrit/reviewdb/client/AccountSshKey.java | 2 +- .../google/gerrit/reviewdb/client/AccountSshKeyTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java index f63c618c0b..78aef91906 100644 --- a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java +++ b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java @@ -67,7 +67,7 @@ public final class AccountSshKey { public AccountSshKey(final AccountSshKey.Id i, final String pub) { id = i; - sshPublicKey = pub; + sshPublicKey = pub.replace("\n", "").replace("\r", ""); valid = id.isValid(); } diff --git a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java index 139d360d52..07c00b991a 100644 --- a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java +++ b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java @@ -25,6 +25,12 @@ public class AccountSshKeyTest { + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T" + "w== john.doe@example.com"; + private static final String KEY_WITH_NEWLINES = + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCgug5VyMXQGnem2H1KVC4/HcRcD4zzBqS\n" + + "uJBRWVonSSoz3RoAZ7bWXCVVGwchtXwUURD689wFYdiPecOrWOUgeeyRq754YWRhU+W28\n" + + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T\n" + + "w== john.doe@example.com"; + private final Account.Id accountId = new Account.Id(1); @Test @@ -47,4 +53,14 @@ public class AccountSshKeyTest { assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]); assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]); } + + @Test + public void testKeyWithNewLines() throws Exception { + AccountSshKey key = new AccountSshKey( + new AccountSshKey.Id(accountId, 1), KEY_WITH_NEWLINES); + assertThat(key.getSshPublicKey()).isEqualTo(KEY); + assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]); + assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]); + assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]); + } } -- cgit v1.2.3