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
This commit is contained in:
David Pursehouse 2016-11-22 17:11:41 +09:00
parent c2bdbe7169
commit a291706e96
2 changed files with 17 additions and 1 deletions

View File

@ -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();
}

View File

@ -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]);
}
}