Browse Source

Prevent adding empty or invalid SSH public keys

tags/v1.5.1
James Moger 10 years ago
parent
commit
039686c54a

+ 2
- 0
releases.moxie View File

fixes: fixes:
- Fix subdirectory links in pages servlet (issue-411) - Fix subdirectory links in pages servlet (issue-411)
- Fix subdirectory navigation in pages servlet (issue-412) - Fix subdirectory navigation in pages servlet (issue-412)
- Fix bug in adding invalid or empty SSH keys (ticket-50)
changes: changes:
- improve French translation (pr-176) - improve French translation (pr-176)
- simplify current plugin release detection and ignore the currentRelease registry field - simplify current plugin release detection and ignore the currentRelease registry field
- Julien Kirch - Julien Kirch
- Ralph Hoffman - Ralph Hoffman
- Olivier Rouits - Olivier Rouits
- Owen Nelson
} }


# #

+ 1
- 1
src/main/java/com/gitblit/transport/ssh/SshKey.java View File

try { try {
publicKey = new Buffer(bin).getRawPublicKey(); publicKey = new Buffer(bin).getRawPublicKey();
} catch (SshException e) { } catch (SshException e) {
e.printStackTrace();
throw new RuntimeException(e);
} }
} }
return publicKey; return publicKey;

+ 9
- 6
src/main/java/com/gitblit/transport/ssh/keys/BaseKeyCommand.java View File

throws UnsupportedEncodingException, IOException { throws UnsupportedEncodingException, IOException {
int idx = -1; int idx = -1;
if (sshKeys.isEmpty() || (idx = sshKeys.indexOf("-")) >= 0) { if (sshKeys.isEmpty() || (idx = sshKeys.indexOf("-")) >= 0) {
String sshKey = "";
String content = "";
BufferedReader br = new BufferedReader(new InputStreamReader( BufferedReader br = new BufferedReader(new InputStreamReader(
in, Charsets.UTF_8)); in, Charsets.UTF_8));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
sshKey += line + "\n";
content += line + "\n";
} }
if (idx == -1) {
sshKeys.add(sshKey.trim());
} else {
sshKeys.set(idx, sshKey.trim());
final String sshKey = content.trim();
if (!sshKey.isEmpty()) {
if (idx == -1) {
sshKeys.add(sshKey);
} else {
sshKeys.set(idx, sshKey);
}
} }
} }
return sshKeys; return sshKeys;

+ 13
- 0
src/main/java/com/gitblit/transport/ssh/keys/KeysDispatcher.java View File

public void run() throws IOException, Failure { public void run() throws IOException, Failure {
String username = getContext().getClient().getUsername(); String username = getContext().getClient().getUsername();
List<String> keys = readKeys(addKeys); List<String> keys = readKeys(addKeys);
if (keys.isEmpty()) {
throw new UnloggedFailure("No public keys were read from STDIN!");
}
for (String key : keys) { for (String key : keys) {
SshKey sshKey = parseKey(key); SshKey sshKey = parseKey(key);
try {
// this method parses the rawdata and produces a public key
// if it fails it will throw a Buffer.BufferException
// the null check is a QC verification on top of that
if (sshKey.getPublicKey() == null) {
throw new RuntimeException();
}
} catch (RuntimeException e) {
throw new UnloggedFailure("The data read from SDTIN can not be parsed as an SSH public key!");
}
if (!StringUtils.isEmpty(permission)) { if (!StringUtils.isEmpty(permission)) {
AccessPermission ap = AccessPermission.fromCode(permission); AccessPermission ap = AccessPermission.fromCode(permission);
if (ap.exceeds(AccessPermission.NONE)) { if (ap.exceeds(AccessPermission.NONE)) {

+ 14
- 0
src/test/java/com/gitblit/tests/SshKeysDispatcherTest.java View File

assertEquals(sb.toString(), result); assertEquals(sb.toString(), result);
} }


@Test
public void testKeysAddBlankCommand() throws Exception {
testSshCommand("keys add --permission R", "\n");
List<SshKey> keys = getKeyManager().getKeys(username);
assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
}

@Test
public void testKeysAddInvalidCommand() throws Exception {
testSshCommand("keys add --permission R", "My invalid key\n");
List<SshKey> keys = getKeyManager().getKeys(username);
assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
}

@Test @Test
public void testKeysCommentCommand() throws Exception { public void testKeysCommentCommand() throws Exception {
List<SshKey> keys = getKeyManager().getKeys(username); List<SshKey> keys = getKeyManager().getKeys(username);

Loading…
Cancel
Save