Skip to content

Commit fec8c68

Browse files
authored
Merge pull request #4380 from TeamAmaze/bugfix/4362
Add guard against invalid port numbers passed into NetCopyConnectionInfo
2 parents e8202ad + c9c5978 commit fec8c68

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

app/src/main/java/com/amaze/filemanager/filesystem/ftp/NetCopyConnectionInfo.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.FTP_URI_
2525
import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.SSH_URI_PREFIX
2626
import com.amaze.filemanager.filesystem.ftp.NetCopyConnectionInfo.Companion.COLON
2727
import 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
}

app/src/test/java/com/amaze/filemanager/filesystem/ftp/NetCopyConnectionInfoTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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:[email protected]:22/a/b/c/d/e").run {
344+
assertEquals(22, this.port)
345+
}
346+
NetCopyConnectionInfo("ssh://user:[email protected]:21097/a/b/c/d/e").run {
347+
assertEquals(21097, this.port)
348+
}
349+
NetCopyConnectionInfo("ssh://user:[email protected]:99999/a/b/c/d/e").run {
350+
assertEquals(0, this.port)
351+
}
352+
NetCopyConnectionInfo("ssh://user:[email protected]/a/b/c/d/e").run {
353+
assertEquals(0, this.port)
354+
}
355+
NetCopyConnectionInfo("ssh://user:[email protected]:2109775003564/a/b/c/d/e").run {
356+
assertEquals(0, this.port)
357+
}
358+
NetCopyConnectionInfo("ssh://user:[email protected]:${Long.MAX_VALUE}/a/b/c/d/e").run {
359+
assertEquals(0, this.port)
360+
}
361+
}
337362
}

0 commit comments

Comments
 (0)