Skip to content

Commit 50f5396

Browse files
committed
Fix #35.
Ensure read buffer is empty when switching to SSL/TLS.
1 parent 777b995 commit 50f5396

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Sources/PostgresClientKit/Connection.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public class Connection: CustomStringConvertible {
136136
throw PostgresError.sslNotSupported
137137
}
138138

139+
// The read buffer should be fully consumed at this point, so that the next byte read
140+
// will have passed through SSL/TLS decryption. If this is not the case, there must
141+
// either be a server protocol error or a man-in-the-middle attack.
142+
try verifyReadBufferFullyConsumed()
143+
139144
do {
140145
let sslConfig = configuration.sslServiceConfiguration
141146
let sslService = try SSLService(usingConfiguration: sslConfig)!
@@ -1212,6 +1217,12 @@ public class Connection: CustomStringConvertible {
12121217
return c
12131218
}
12141219

1220+
private func verifyReadBufferFullyConsumed() throws {
1221+
guard readBufferPosition == readBuffer.count else {
1222+
throw PostgresError.serverError(description: "response too long")
1223+
}
1224+
}
1225+
12151226
private func refillReadBuffer() throws {
12161227

12171228
assert(readBufferPosition == readBuffer.count)

0 commit comments

Comments
 (0)