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
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 f63c618..78aef91 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 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 139d360..07c00b9 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 @@
+ "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 @@
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]);
+ }
}