summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse <dpursehouse@collab.net>2016-11-22 17:11:41 +0900
committerDavid Pursehouse <dpursehouse@collab.net>2016-11-24 13:21:58 +0900
commita291706e964e3c76383a92f68ad179788b582582 (patch)
tree242610359878c6a83d29a8c5f7d5b9ffa2d02411
parentc2bdbe7169051947bd635a9c9a4403f278402245 (diff)
AccountSshKey: Strip newline characters out of public key stringv2.13.3
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
-rw-r--r--gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java2
-rw-r--r--gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java16
2 files changed, 17 insertions, 1 deletions
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]);
+ }
}