Skip to content

Commit b21c664

Browse files
committed
chore: follow up for review comments
1 parent 3fa1c30 commit b21c664

6 files changed

Lines changed: 26 additions & 19 deletions

File tree

sshlib/src/main/kotlin/org/connectbot/sshlib/client/AgentChannel.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ internal class AgentChannel(
9191
while (window.remoteRemaining <= 0) {
9292
windowAvailable.receive()
9393
}
94-
val chunkSize = minOf(
95-
data.size - offset,
96-
window.remoteRemaining.toInt(),
97-
maxPacketSize,
98-
)
94+
val chunkSize = window.sendChunkSize(data.size - offset, maxPacketSize)
9995
val chunk = data.copyOfRange(offset, offset + chunkSize)
10096
connection.sendChannelData(remoteChannelNumber, chunk)
10197
window.consumeRemote(chunkSize)

sshlib/src/main/kotlin/org/connectbot/sshlib/client/ForwardingChannel.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ internal class ForwardingChannel(
3131
) {
3232
companion object {
3333
private val logger = LoggerFactory.getLogger(ForwardingChannel::class.java)
34+
private const val WINDOW_ADJUST_THRESHOLD = 64 * 1024
3435
}
3536

3637
private var _isOpen = true
3738
private var closeSent = false
38-
private val window = LocalChannelWindow(initialWindowSize, remoteInitial = remoteWindowSizeInitial)
39+
private val window = LocalChannelWindow(
40+
initialWindowSize,
41+
adjustThreshold = WINDOW_ADJUST_THRESHOLD,
42+
remoteInitial = remoteWindowSizeInitial,
43+
)
3944
private val windowAvailable = Channel<Unit>(Channel.CONFLATED)
4045

4146
private val _incomingData = Channel<ByteArray>(Channel.UNLIMITED)
@@ -85,11 +90,7 @@ internal class ForwardingChannel(
8590
while (window.remoteRemaining <= 0) {
8691
windowAvailable.receive()
8792
}
88-
val chunkSize = minOf(
89-
data.size - offset,
90-
window.remoteRemaining.toInt(),
91-
maxPacketSize,
92-
)
93+
val chunkSize = window.sendChunkSize(data.size - offset, maxPacketSize)
9394
val chunk = data.copyOfRange(offset, offset + chunkSize)
9495
connection.sendChannelData(remoteChannelNumber, chunk)
9596
window.consumeRemote(chunkSize)

sshlib/src/main/kotlin/org/connectbot/sshlib/client/LocalChannelWindow.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ internal class LocalChannelWindow(
8181
fun consumeRemote(size: Int) {
8282
remoteRemaining -= size
8383
}
84+
85+
/**
86+
* Return the next send chunk size without narrowing the uint32 remote
87+
* window until it is bounded by Int-sized data and packet limits.
88+
*/
89+
fun sendChunkSize(remainingData: Int, maxPacketSize: Int): Int = minOf(
90+
remainingData.toLong(),
91+
remoteRemaining,
92+
maxPacketSize.toLong(),
93+
).toInt()
8494
}

sshlib/src/main/kotlin/org/connectbot/sshlib/client/SessionChannel.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ class SessionChannel internal constructor(
151151
while (window.remoteRemaining <= 0) {
152152
windowAvailable.receive()
153153
}
154-
val chunkSize = minOf(
155-
data.size - offset,
156-
window.remoteRemaining.toInt(),
157-
maxPacketSize,
158-
)
154+
val chunkSize = window.sendChunkSize(data.size - offset, maxPacketSize)
159155
val chunk = data.copyOfRange(offset, offset + chunkSize)
160156
connection.sendChannelData(_remoteChannelNumber, chunk)
161157
window.consumeRemote(chunkSize)

sshlib/src/test/kotlin/org/connectbot/sshlib/client/LocalChannelWindowTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ class LocalChannelWindowTest {
8484
w.consumeRemote(300)
8585
assertEquals(700L, w.remoteRemaining)
8686
}
87+
88+
@Test
89+
fun `sendChunkSize handles uint32 remote window without overflow`() {
90+
val w = LocalChannelWindow(initialSize = 1024, remoteInitial = LocalChannelWindow.MAX_WINDOW_SIZE)
91+
92+
assertEquals(32768, w.sendChunkSize(64 * 1024, 32768))
93+
}
8794
}

sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/DiffieHellmanGroupExchangeTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class DiffieHellmanGroupExchangeTest {
3737
// A tiny 16-bit prime — far below the min=2048 requested by the client
3838
private val tinyPrime = BigInteger("65537")
3939

40-
// A 2048-bit composite (GROUP14_P with the last bit flipped) — not a safe prime
41-
private val compositeLikePrime = DhGroups.GROUP14_P.subtract(BigInteger.ONE)
42-
4340
@Test
4441
fun `setGroup rejects p smaller than min bits`() {
4542
val gex = DiffieHellmanGroupExchange("SHA-256")

0 commit comments

Comments
 (0)