From 98c6a3f4d832c025e6401aa7ed55016098f90876 Mon Sep 17 00:00:00 2001 From: Orin Date: Tue, 22 Nov 2022 09:40:43 -0800 Subject: [PATCH] fix: issue #661 - don't try to access a zero length body (#734) Co-authored-by: Mike --- src/utils/parseWSManResponseBody.test.ts | 31 +++++++++++++++++++++++- src/utils/parseWSManResponseBody.ts | 2 ++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/utils/parseWSManResponseBody.test.ts b/src/utils/parseWSManResponseBody.test.ts index f1d21f8c7..5b61f1b89 100644 --- a/src/utils/parseWSManResponseBody.test.ts +++ b/src/utils/parseWSManResponseBody.test.ts @@ -48,7 +48,7 @@ describe('Check parseWSManResponseBody', () => { expect(response).toEqual(generalSettings) }) - it('Should fail and return null when a message does not contain \r\n', async () => { + it('Should fail and return an empty response when a message does not contain \r\n', async () => { const xmlResponse: HttpZResponseModel = { protocolVersion: 'HTTP/1.1', statusCode: 200, @@ -78,4 +78,33 @@ describe('Check parseWSManResponseBody', () => { const response = parseBody(xmlResponse) expect(response).toBe('') }) + + it('Should fail and return an empty response when there is no message body', async () => { + // See issue #661; this response is documented there. + const xmlResponse: HttpZResponseModel = { + protocolVersion: 'HTTP/1.1', + statusCode: 400, + statusMessage: 'Bad Request', + headersSize: 143, + bodySize: 0, + headers: [ + { + name: 'Date', + value: 'Mon, 1 Aug 2022 18:54:20 GMT' + }, + { + name: 'Server', + value: 'Intel(R) Active Management Technology 15.0.41.2142' + }, + { + name: 'Content-Length', + value: '0' + } + ], + body: null + } + delete xmlResponse.body // Yes, really! It's not there in this case. + const response = parseBody(xmlResponse) + expect(response).toBe('') + }) }) diff --git a/src/utils/parseWSManResponseBody.ts b/src/utils/parseWSManResponseBody.ts index 5d7e125da..6a4b12673 100644 --- a/src/utils/parseWSManResponseBody.ts +++ b/src/utils/parseWSManResponseBody.ts @@ -7,6 +7,8 @@ import { HttpZResponseModel } from 'http-z' export function parseBody (message: HttpZResponseModel): string { let xmlBody: string = '' + // 'Bad' requests to some devices return no body (issue #661) - prevent exceptions below + if (message.bodySize === 0) return '' // parse the body until its length is greater than 5, because body ends with '0\r\n\r\n' while (message.body.text.length > 5) { const chunkLength = message.body.text.indexOf('\r\n')