simme / node-http-digest-client

Make HTTP requests to servers that employ digest authentication.
ISC License
16 stars 33 forks source link

Code breaks when algorithm=MD5 key value pair occurs in response headers #15

Open himanshu219 opened 5 years ago

himanshu219 commented 5 years ago

Sometimes some apis give algorithm=MD5 in the response headers without quotes which breaks the code with below traceback.

// console output of parts array
realm="MMS Public API"
 domain=""
 nonce="7hziqlF0xTs4PbKVmzPlEGq0yZRNiFuY"
 algorithm=MD5
/Users/hpal/git/sumologic-mongodb-atlas/sumomongodbatlascollector/functions/mongodbatlascollector/sumoutils/node_modules/http-digest-client/http-digest-client.js:107
      if (part.length > 2) {
               ^

TypeError: Cannot read property 'length' of null
    at HTTPDigest.parseChallenge [as _parseChallenge] (/Users/hpal/git/sumologic-mongodb-atlas/sumomongodbatlascollector/functions/mongodbatlascollector/sumoutils/node_modules/http-digest-client/http-digest-client.js:107:16)
    at HTTPDigest.handleResponse [as _handleResponse] (/Users/hpal/git/sumologic-mongodb-atlas/sumomongodbatlascollector/functions/mongodbatlascollector/sumoutils/node_modules/http-digest-client/http-digest-client.js:39:26)
    at ClientRequest.<anonymous> (/Users/hpal/git/sumologic-mongodb-atlas/sumomongodbatlascollector/functions/mongodbatlascollector/sumoutils/node_modules/http-digest-client/http-digest-client.js:29:12)
    at Object.onceWrapper (events.js:285:13)
    at ClientRequest.emit (events.js:197:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:560:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:113:17)
    at TLSSocket.socketOnData (_http_client.js:447:20)
    at TLSSocket.emit (events.js:197:13)
    at addChunk (_stream_readable.js:288:12)

at line number 106 using the following regex fixes the issue var part = parts[i].match(/^\s?([a-zA-Z0-0]+)="(.)"\s?$|^\s?([a-zA-Z0-0]+)=(.)\s?$/);

ma0dubois commented 3 years ago

Hi,

@himanshu219 i think it's better to use this:

var part = parts[i].match(/^\s*?([a-zA-Z0-0]+)="?(.*)"?\s*?$/);
if (part && part.length > 2) {
    params[part[1]] = (part[2]).replace('"', '');
}

because with your suggestion, the group 1 and 2 can be undefined if the regex match your 2nd group