You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2016/02/21 15:14:59 UTC

mina-sshd git commit: Expose SSH version line length value explicitly

Repository: mina-sshd
Updated Branches:
  refs/heads/master d7932443b -> 562fdc3b8


Expose SSH version line length value explicitly


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/562fdc3b
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/562fdc3b
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/562fdc3b

Branch: refs/heads/master
Commit: 562fdc3b874946bf0900d6317c19b44d3a877045
Parents: d793244
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Sun Feb 21 16:15:42 2016 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Sun Feb 21 16:15:42 2016 +0200

----------------------------------------------------------------------
 .../org/apache/sshd/common/session/Session.java | 12 ++++++++++
 .../common/session/impl/AbstractSession.java    |  8 +------
 .../session/impl/AbstractSessionTest.java       | 23 ++++++++++----------
 3 files changed, 25 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/562fdc3b/sshd-core/src/main/java/org/apache/sshd/common/session/Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/Session.java b/sshd-core/src/main/java/org/apache/sshd/common/session/Session.java
index 62ffc97..4cf9bf8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/Session.java
@@ -58,9 +58,21 @@ public interface Session
 
     /**
      * Default prefix expected for the client / server identification string
+     * @see <A HREF="https://tools.ietf.org/html/rfc4253#section-4.2">RFC 4253 - section 4.2</A>
      */
     String DEFAULT_SSH_VERSION_PREFIX = "SSH-2.0-";
 
+    /**
+     * Maximum number of characters for any single line sent as part
+     * of the initial handshake - according to
+     * <A HREF="https://tools.ietf.org/html/rfc4253#section-4.2">RFC 4253 - section 4.2</A>:</BR>
+     *
+     * <P><CODE>
+     *      The maximum length of the string is 255 characters,
+     *      including the Carriage Return and Line Feed.
+     * </CODE></P>
+     */
+    int MAX_VERSION_LINE_LENGTH = 256;
 
     /**
      * Timeout status.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/562fdc3b/sshd-core/src/main/java/org/apache/sshd/common/session/impl/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/impl/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/impl/AbstractSession.java
index 97e946f..28e9476 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/impl/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/impl/AbstractSession.java
@@ -1342,13 +1342,7 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
     protected String doReadIdentification(Buffer buffer, boolean server) {
         int maxIdentSize = PropertyResolverUtils.getIntProperty(this,
                 FactoryManager.MAX_IDENTIFICATION_SIZE, FactoryManager.DEFAULT_MAX_IDENTIFICATION_SIZE);
-        /*
-         * see https://tools.ietf.org/html/rfc4253 section 4.2
-         *
-         *      The maximum length of the string is 255 characters,
-         *      including the Carriage Return and Line Feed.
-         */
-        for (byte[] data = new byte[256];;) {
+        for (byte[] data = new byte[MAX_VERSION_LINE_LENGTH];;) {
             int rpos = buffer.rpos();
             int pos = 0;
             boolean needLf = false;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/562fdc3b/sshd-core/src/test/java/org/apache/sshd/common/session/impl/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/session/impl/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/common/session/impl/AbstractSessionTest.java
index 49b7d44..5221812 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/session/impl/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/session/impl/AbstractSessionTest.java
@@ -38,7 +38,7 @@ import org.apache.sshd.common.io.IoService;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.kex.KexProposalOption;
-import org.apache.sshd.common.session.impl.AbstractSession;
+import org.apache.sshd.common.session.Session;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.buffer.Buffer;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
@@ -113,24 +113,25 @@ public class AbstractSessionTest extends BaseTestSupport {
 
     @Test(expected = IllegalStateException.class)
     public void testReadIdentLongLine() {
-        Buffer buf = new ByteArrayBuffer(("SSH-2.0-software" +
-                "01234567890123456789012345678901234567890123456789" +
-                "01234567890123456789012345678901234567890123456789" +
-                "01234567890123456789012345678901234567890123456789" +
-                "01234567890123456789012345678901234567890123456789" +
-                "01234567890123456789012345678901234567890123456789" +
-                "01234567890123456789012345678901234567890123456789").getBytes(StandardCharsets.UTF_8));
+        StringBuilder sb = new StringBuilder(Session.MAX_VERSION_LINE_LENGTH + Integer.SIZE);
+        sb.append("SSH-2.0-software");
+        do {
+            sb.append("01234567890123456789012345678901234567890123456789");
+        } while (sb.length() < Session.MAX_VERSION_LINE_LENGTH);
+
+        Buffer buf = new ByteArrayBuffer(sb.toString().getBytes(StandardCharsets.UTF_8));
         String ident = session.doReadIdentification(buf);
         fail("Unexpected success: " + ident);
     }
 
     @Test(expected = IllegalStateException.class)
     public void testReadIdentLongHeader() {
-        StringBuilder sb = new StringBuilder(32768);
-        for (int i = 0; i < 500; i++) {
+        StringBuilder sb = new StringBuilder(FactoryManager.DEFAULT_MAX_IDENTIFICATION_SIZE + Integer.SIZE);
+        do {
             sb.append("01234567890123456789012345678901234567890123456789\r\n");
-        }
+        } while (sb.length() < FactoryManager.DEFAULT_MAX_IDENTIFICATION_SIZE);
         sb.append("SSH-2.0-software\r\n");
+
         Buffer buf = new ByteArrayBuffer(sb.toString().getBytes(StandardCharsets.UTF_8));
         String ident = session.doReadIdentification(buf);
         fail("Unexpected success: " + ident);