mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
Fixed bug that crept in 0edc4a5
This commit is contained in:
@@ -44,6 +44,7 @@ test {
|
|||||||
exceptionFormat = 'full'
|
exceptionFormat = 'full'
|
||||||
}
|
}
|
||||||
include "**/*Test.*"
|
include "**/*Test.*"
|
||||||
|
include "**/*Spec.*"
|
||||||
if (!project.hasProperty("allTests")) {
|
if (!project.hasProperty("allTests")) {
|
||||||
useJUnit {
|
useJUnit {
|
||||||
excludeCategories 'com.hierynomus.sshj.test.SlowTests'
|
excludeCategories 'com.hierynomus.sshj.test.SlowTests'
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ public class IdentificationStringParser {
|
|||||||
if (b == '\n') {
|
if (b == '\n') {
|
||||||
if (checkForIdentification(lineBuffer)) {
|
if (checkForIdentification(lineBuffer)) {
|
||||||
return readIdentification(lineBuffer);
|
return readIdentification(lineBuffer);
|
||||||
|
} else {
|
||||||
|
logHeaderLine(lineBuffer);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -55,6 +57,10 @@ public class IdentificationStringParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void logHeaderLine(Buffer.PlainBuffer lineBuffer) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private String readIdentification(Buffer.PlainBuffer lineBuffer) throws Buffer.BufferException, TransportException {
|
private String readIdentification(Buffer.PlainBuffer lineBuffer) throws Buffer.BufferException, TransportException {
|
||||||
byte[] bytes = new byte[lineBuffer.available()];
|
byte[] bytes = new byte[lineBuffer.available()];
|
||||||
lineBuffer.readRawBytes(bytes);
|
lineBuffer.readRawBytes(bytes);
|
||||||
@@ -64,9 +70,12 @@ public class IdentificationStringParser {
|
|||||||
throw new TransportException("Incorrect identification: line too long: " + ByteArrayUtils.printHex(bytes, 0, bytes.length));
|
throw new TransportException("Incorrect identification: line too long: " + ByteArrayUtils.printHex(bytes, 0, bytes.length));
|
||||||
}
|
}
|
||||||
if (bytes[bytes.length - 2] != '\r') {
|
if (bytes[bytes.length - 2] != '\r') {
|
||||||
logger.error("Incorrect identification, was expecting a '\\r\\n' however got: '{}' (hex: {})", bytes[bytes.length - 2], Integer.toHexString(bytes[bytes.length - 2] & 0xFF));
|
String ident = new String(bytes, 0, bytes.length - 1);
|
||||||
logger.error("Data received up til here was: {}", new String(bytes));
|
logger.warn("Server identification has bad line ending, was expecting a '\\r\\n' however got: '{}' (hex: {})", (char) (bytes[bytes.length - 2] & 0xFF), Integer.toHexString(bytes[bytes.length - 2] & 0xFF));
|
||||||
throw new TransportException("Incorrect identification: bad line ending: " + ByteArrayUtils.toHex(bytes, 0, bytes.length));
|
logger.warn("Will treat the identification of this server '{}' leniently", ident);
|
||||||
|
return ident;
|
||||||
|
// logger.error("Data received up til here was: {}", new String(bytes));
|
||||||
|
// throw new TransportException("Incorrect identification: bad line ending: " + ByteArrayUtils.toHex(bytes, 0, bytes.length));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip off the \r\n
|
// Strip off the \r\n
|
||||||
@@ -74,6 +83,9 @@ public class IdentificationStringParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkForIdentification(Buffer.PlainBuffer lineBuffer) throws Buffer.BufferException {
|
private boolean checkForIdentification(Buffer.PlainBuffer lineBuffer) throws Buffer.BufferException {
|
||||||
|
if (lineBuffer.available() < 4) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
byte[] buf = new byte[4];
|
byte[] buf = new byte[4];
|
||||||
lineBuffer.readRawBytes(buf);
|
lineBuffer.readRawBytes(buf);
|
||||||
// Reset
|
// Reset
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import spock.lang.Unroll
|
|||||||
|
|
||||||
import static org.codehaus.groovy.runtime.IOGroovyMethods.withCloseable
|
import static org.codehaus.groovy.runtime.IOGroovyMethods.withCloseable
|
||||||
|
|
||||||
class SFTPClientTest extends Specification {
|
class SFTPClientSpec extends Specification {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public SshFixture fixture = new SshFixture()
|
public SshFixture fixture = new SshFixture()
|
||||||
@@ -33,6 +33,18 @@ class IdentificationStringParserSpec extends Specification {
|
|||||||
ident == "SSH-2.0-OpenSSH-6.13"
|
ident == "SSH-2.0-OpenSSH-6.13"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "should leniently parse identification string without carriage return"() {
|
||||||
|
given:
|
||||||
|
def buffer = new Buffer.PlainBuffer()
|
||||||
|
buffer.putRawBytes("SSH-2.0-OpenSSH-6.13\n".bytes)
|
||||||
|
|
||||||
|
when:
|
||||||
|
def ident = new IdentificationStringParser(buffer).parseIdentificationString()
|
||||||
|
|
||||||
|
then:
|
||||||
|
ident == "SSH-2.0-OpenSSH-6.13"
|
||||||
|
}
|
||||||
|
|
||||||
def "should not parse header lines as part of ident"() {
|
def "should not parse header lines as part of ident"() {
|
||||||
given:
|
given:
|
||||||
def buffer = new Buffer.PlainBuffer()
|
def buffer = new Buffer.PlainBuffer()
|
||||||
@@ -75,4 +87,17 @@ class IdentificationStringParserSpec extends Specification {
|
|||||||
then:
|
then:
|
||||||
ident == "SSH-2.0-OpenSSH-6.13"
|
ident == "SSH-2.0-OpenSSH-6.13"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "should not fail on very short header line"() {
|
||||||
|
given:
|
||||||
|
def buffer = new Buffer.PlainBuffer()
|
||||||
|
buffer.putRawBytes("h1\n".bytes)
|
||||||
|
buffer.putRawBytes("SSH-2.0-OpenSSH-6.13\r\n".bytes)
|
||||||
|
|
||||||
|
when:
|
||||||
|
def ident = new IdentificationStringParser(buffer).parseIdentificationString()
|
||||||
|
|
||||||
|
then:
|
||||||
|
ident == "SSH-2.0-OpenSSH-6.13"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user