File tree Expand file tree Collapse file tree
main/java/com/amaze/filemanager/filesystem/ftp
test/java/com/amaze/filemanager/filesystem/ftp Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.FTP_URI_
2525import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.SSH_URI_PREFIX
2626import com.amaze.filemanager.filesystem.ftp.NetCopyConnectionInfo.Companion.COLON
2727import com.amaze.filemanager.filesystem.smb.CifsContexts.SMB_URI_PREFIX
28+ import org.slf4j.Logger
29+ import org.slf4j.LoggerFactory
2830
2931/* *
3032 * Container object for SSH/FTP/FTPS URL, encapsulating logic for splitting information from given
@@ -58,6 +60,9 @@ class NetCopyConnectionInfo(url: String) {
5860 private set
5961
6062 companion object {
63+ @JvmStatic
64+ private val LOGGER : Logger = LoggerFactory .getLogger(NetCopyConnectionInfo ::class .java)
65+
6166 // Regex taken from https://blog.stevenlevithan.com/archives/parseuri
6267 // (No, don't break it down to lines)
6368
@@ -104,7 +109,18 @@ class NetCopyConnectionInfo(url: String) {
104109 * Invalid string would have been trapped to other branches. Strings fell into
105110 * this branch must be integer
106111 */
107- it[7 ].toInt()
112+ try {
113+ // Need to make sure port number is in range
114+ if (it[7 ].toInt() in 1 .. 65535 ) {
115+ it[7 ].toInt()
116+ } else {
117+ LOGGER .warn(" Port number is out of range: ${it[7 ]} " )
118+ 0
119+ }
120+ } catch (e: NumberFormatException ) {
121+ LOGGER .warn(" Unable to parse port number: ${it[7 ]} " , e)
122+ 0
123+ }
108124 } else {
109125 0
110126 }
Original file line number Diff line number Diff line change @@ -334,4 +334,29 @@ class NetCopyConnectionInfoTest {
334334 assertEquals(" test.log" , this .lastPathSegment())
335335 }
336336 }
337+
338+ /* *
339+ * Test parsing invalid port number.
340+ */
341+ @Test
342+ fun testParseInvalidPortNumber () {
343+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1:22/a/b/c/d/e" ).run {
344+ assertEquals(22 , this .port)
345+ }
346+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1:21097/a/b/c/d/e" ).run {
347+ assertEquals(21097 , this .port)
348+ }
349+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1:99999/a/b/c/d/e" ).run {
350+ assertEquals(0 , this .port)
351+ }
352+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1/a/b/c/d/e" ).run {
353+ assertEquals(0 , this .port)
354+ }
355+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1:2109775003564/a/b/c/d/e" ).run {
356+ assertEquals(0 , this .port)
357+ }
358+ NetCopyConnectionInfo (" ssh://user:pass@127.0.0.1:${Long .MAX_VALUE } /a/b/c/d/e" ).run {
359+ assertEquals(0 , this .port)
360+ }
361+ }
337362}
You can’t perform that action at this time.
0 commit comments