Skip to content

Commit 72b5f6d

Browse files
committed
Add test & fix packed packet parsing error for SASL authentication messages
1 parent 08afb12 commit 72b5f6d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/pg-protocol/src/inbound-parser.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,21 @@ describe('PgPacketStream', function () {
210210
testForMessage(md5PasswordBuffer, expectedMD5PasswordMessage)
211211
testForMessage(SASLBuffer, expectedSASLMessage)
212212
testForMessage(SASLContinueBuffer, expectedSASLContinueMessage)
213+
214+
// this exercises a found bug in the parser:
215+
// https://github.com/brianc/node-postgres/pull/2210#issuecomment-627626084
216+
// and adds a test which is deterministic, rather than relying on network packet chunking
217+
const extendedSASLContinueBuffer = Buffer.concat([SASLContinueBuffer, Buffer.from([1, 2, 3, 4])])
218+
testForMessage(extendedSASLContinueBuffer, expectedSASLContinueMessage)
219+
213220
testForMessage(SASLFinalBuffer, expectedSASLFinalMessage)
214221

222+
// this exercises a found bug in the parser:
223+
// https://github.com/brianc/node-postgres/pull/2210#issuecomment-627626084
224+
// and adds a test which is deterministic, rather than relying on network packet chunking
225+
const extendedSASLFinalBuffer = Buffer.concat([SASLFinalBuffer, Buffer.from([1, 2, 4, 5])])
226+
testForMessage(extendedSASLFinalBuffer, expectedSASLFinalMessage)
227+
215228
testForMessage(paramStatusBuffer, expectedParameterStatusMessage)
216229
testForMessage(backendKeyDataBuffer, expectedBackendKeyDataMessage)
217230
testForMessage(readyForQueryBuffer, expectedReadyForQueryMessage)

packages/pg-protocol/src/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ export class Parser {
296296
break
297297
case 11: // AuthenticationSASLContinue
298298
message.name = MessageName.authenticationSASLContinue
299-
message.data = this.reader.string(length - 4)
299+
message.data = this.reader.string(length - 8)
300300
break
301301
case 12: // AuthenticationSASLFinal
302302
message.name = MessageName.authenticationSASLFinal
303-
message.data = this.reader.string(length - 4)
303+
message.data = this.reader.string(length - 8)
304304
break
305305
default:
306306
throw new Error('Unknown authenticationOk message type ' + code)

0 commit comments

Comments
 (0)