Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/pg-protocol/src/inbound-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,24 @@ describe('PgPacketStream', function () {
parser.parse(oneFieldBuf, () => {})
assert.strictEqual((parser as any).reader.buffer.byteLength, 0)
})

describe('error and notice messages with an empty body', function () {
for (const code of [0x45, 0x4e]) {
it('returns for code ' + String.fromCharCode(code) + ' at the end of the buffer', function () {
const messages: BackendMessage[] = []
new Parser().parse(Buffer.from([code, 0, 0, 0, 4]), (msg) => messages.push(msg))
assert.strictEqual(messages.length, 1)
assert.strictEqual(messages[0].name, 'error')
})

it('returns an error for code ' + String.fromCharCode(code) + ' and keeps parsing', function () {
const empty = Buffer.from([code, 0, 0, 0, 4])
const messages: BackendMessage[] = []
new Parser().parse(Buffer.concat([empty, readyForQueryBuffer]), (msg) => messages.push(msg))
assert.strictEqual(messages.length, 2)
assert.strictEqual(messages[0].name, 'error')
assert.strictEqual(messages[1].name, 'readyForQuery')
})
}
})
})
18 changes: 18 additions & 0 deletions packages/pg-protocol/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ export class Parser {
message = parseBackendKeyData(reader)
break
case MessageCodes.ErrorMessage:
reader.setBuffer(0, packetBody(bytes, offset, length))
message = parseErrorMessage(reader, 'error')
break
case MessageCodes.NoticeMessage:
reader.setBuffer(0, packetBody(bytes, offset, length))
message = parseErrorMessage(reader, 'notice')
break
case MessageCodes.RowDescriptionMessage:
Expand Down Expand Up @@ -379,10 +381,26 @@ const parseAuthenticationResponse = (reader: BufferReader, length: number) => {
return message
}

// `length` includes the int32 length field and excludes the message code.
// Clamp ErrorResponse/NoticeResponse reads to that body so a short frame
// cannot consume the next message or run off the end of the buffer.
const packetBody = (bytes: Buffer, offset: number, length: number): Buffer => {
const bodyLength = length - 4
if (bodyLength <= 0) {
return emptyBuffer
}
return bytes.slice(offset, offset + bodyLength)
}

const parseErrorMessage = (reader: BufferReader, name: MessageName) => {
const fields: Record<string, string> = {}
let fieldType = reader.string(1)
while (fieldType !== '\0') {
// Past the declared body, BufferReader.string returns '' rather than NUL.
// Continuing would spin forever.
if (fieldType === '') {
return new DatabaseError('unterminated error or notice message', LATEINIT_LENGTH, 'error')
}
fields[fieldType] = reader.cstring()
fieldType = reader.string(1)
}
Expand Down
Loading