Skip to content

Commit

Permalink
fix: issue #661 - don't try to access a zero length body (#734)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike <[email protected]>
  • Loading branch information
orinem and rsdmike authored Nov 22, 2022
1 parent fc56834 commit 98c6a3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/utils/parseWSManResponseBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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('')
})
})
2 changes: 2 additions & 0 deletions src/utils/parseWSManResponseBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 98c6a3f

Please sign in to comment.