Add guard against invalid port numbers

Fixes #4362
This commit is contained in:
Raymond Lai
2025-04-06 11:12:29 +08:00
parent c64b2cc5e3
commit e91b994e1d
2 changed files with 33 additions and 1 deletions
@@ -25,6 +25,8 @@ import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.FTP_URI_
import com.amaze.filemanager.filesystem.ftp.NetCopyClientConnectionPool.SSH_URI_PREFIX
import com.amaze.filemanager.filesystem.ftp.NetCopyConnectionInfo.Companion.COLON
import com.amaze.filemanager.filesystem.smb.CifsContexts.SMB_URI_PREFIX
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* Container object for SSH/FTP/FTPS URL, encapsulating logic for splitting information from given
@@ -58,6 +60,9 @@ class NetCopyConnectionInfo(url: String) {
private set
companion object {
@JvmStatic
private val LOGGER: Logger = LoggerFactory.getLogger(NetCopyConnectionInfo::class.java)
// Regex taken from https://blog.stevenlevithan.com/archives/parseuri
// (No, don't break it down to lines)
@@ -104,7 +109,12 @@ class NetCopyConnectionInfo(url: String) {
* Invalid string would have been trapped to other branches. Strings fell into
* this branch must be integer
*/
it[7].toInt()
try {
it[7].toInt()
} catch (e: NumberFormatException) {
LOGGER.warn("Unable to parse port number: ${it[7]}", e)
0
}
} else {
0
}
@@ -334,4 +334,26 @@ class NetCopyConnectionInfoTest {
assertEquals("test.log", this.lastPathSegment())
}
}
/**
* Test parsing invalid port number.
*/
@Test
fun testParseInvalidPortNumber() {
NetCopyConnectionInfo("ssh://user:pass@127.0.0.1:22/a/b/c/d/e").run {
assertEquals(22, this.port)
}
NetCopyConnectionInfo("ssh://user:pass@127.0.0.1:21097/a/b/c/d/e").run {
assertEquals(21097, this.port)
}
NetCopyConnectionInfo("ssh://user:pass@127.0.0.1/a/b/c/d/e").run {
assertEquals(0, this.port)
}
NetCopyConnectionInfo("ssh://user:pass@127.0.0.1:2109775003564/a/b/c/d/e").run {
assertEquals(0, this.port)
}
NetCopyConnectionInfo("ssh://user:pass@127.0.0.1:${Long.MAX_VALUE}/a/b/c/d/e").run {
assertEquals(0, this.port)
}
}
}