Skip to content

Commit a582fd6

Browse files
authored
Treat errors on handling income bolt messages (#958)
Invalid income messages which could not be parsed or handled correctly where throwing exceptions in non-safe context. This leads the driver to crash hard since the error is bubble up to the socket thread.
1 parent 45be341 commit a582fd6

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/bolt-connection/src/bolt/create.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export default function create ({
7272

7373
// setup dechunker to dechunk messages and forward them to the message handler
7474
dechunker.onmessage = buf => {
75-
responseHandler.handleResponse(protocol.unpack(buf))
75+
try {
76+
responseHandler.handleResponse(protocol.unpack(buf))
77+
} catch (e) {
78+
return observer.onError(e)
79+
}
7680
}
7781

7882
return responseHandler

packages/bolt-connection/test/bolt/index.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,60 @@ describe('#unit Bolt', () => {
276276
expect(receivedMessage.signature).toEqual(expectedMessage.signature)
277277
expect(receivedMessage.fields).toEqual(expectedMessage.fields)
278278
})
279+
280+
it(`it should configure the channel.onmessage to dechunk and notify unpacking issues ${version}`, () => {
281+
const params = createBoltCreateParams({ version })
282+
let receivedMessage = null
283+
const message = {
284+
signature: 0x10,
285+
fields: [123]
286+
}
287+
const protocol = Bolt.create(params)
288+
protocol._responseHandler.handleResponse = msg => {
289+
receivedMessage = msg
290+
}
291+
292+
protocol.packer().packStruct(
293+
message.signature,
294+
message.fields.map(field => protocol.packer().packable(field))
295+
)
296+
297+
params.chunker.messageBoundary()
298+
params.chunker.flush()
299+
300+
const expectedError = newError('Something went wrong')
301+
protocol.unpack = () => {
302+
throw expectedError
303+
}
304+
305+
params.channel.onmessage(params.channel.toBuffer())
306+
307+
expect(receivedMessage).toBeNull()
308+
expect(params.observer.errors).toEqual([expectedError])
309+
})
310+
311+
it(`it should configure the channel.onmessage to dechunk and notify response handler issues ${version}`, () => {
312+
const params = createBoltCreateParams({ version })
313+
const expectedMessage = {
314+
signature: 0x10,
315+
fields: [123]
316+
}
317+
const protocol = Bolt.create(params)
318+
const expectedError = newError('Something went wrong')
319+
protocol._responseHandler.handleResponse = msg => {
320+
throw expectedError
321+
}
322+
323+
protocol.packer().packStruct(
324+
expectedMessage.signature,
325+
expectedMessage.fields.map(field => protocol.packer().packable(field))
326+
)
327+
params.chunker.messageBoundary()
328+
params.chunker.flush()
329+
params.channel.onmessage(params.channel.toBuffer())
330+
331+
expect(params.observer.errors).toEqual([expectedError])
332+
})
279333
})
280334

281335
forEachUnknownProtocolVersion(version => {

0 commit comments

Comments
 (0)