Skip to content
Merged
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
13 changes: 9 additions & 4 deletions lib/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ let kerberos = null;
const https = require('https');
const formData = require('form-data');

function createAuthenticator(client, user, password, challenge) {
const authenticator = createAuthInitializer.call(null, user, password)(challenge);
function createAuthenticator(client, user, password, challenge, expectedScheme) {
const authenticator = createAuthInitializer(user, password, {expectedScheme})(challenge);
if (authenticator.err) {
throw new Error('Authentication failed: ' + authenticator.err);
}
if (!client.authenticator) {
client.authenticator = {};
}
Expand Down Expand Up @@ -248,11 +251,13 @@ function challengeRequest(operation) {
if ((statusCode1 === 401 && hasChallenge) || (successStatus && !isRead)) {
try{
operation.authenticator = (hasChallenge) ? createAuthenticator(
operation.client, options.user, options.password, challenge
operation.client, options.user, options.password, challenge, options.authType
) : null;

} catch(error){
request1.emit('error', new Error('Authentication failed.'));
response1.resume();
request1.emit('error', error);
return;
}
authenticatedRequest(operation);
// should never happen
Expand Down
48 changes: 42 additions & 6 deletions lib/www-authenticate-patched/parsers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/

/*
Expand All @@ -10,10 +10,39 @@
* Licensed under the MIT license.
*/

const ParseAuth= /(\w+)\s+(.*)/ // -> scheme, params
const ParseAuth= /^\s*([\w!#$%&'*+.^`|~-]+)(?:\s+([\s\S]*))?$/ // -> scheme, params
, Separators= /([",=])/
, ChallengeStart= /^\s*[\w!#$%&'*+.^`|~-]+(?=\s|,|$)(?!\s*=)/
;

// Node joins repeated WWW-Authenticate headers with ", ", so one value may hold
// several challenges. A new challenge starts at a top-level comma followed by
// a scheme token rather than an auth-param name followed by "=".
function split_challenges(header) {
const challenges= [];
if (typeof header !== 'string') {return challenges;}
let start= 0, quoted= false, _i;
for (_i = 0; _i < header.length; _i++) {
const ch= header[_i];
if (quoted) {
if (ch === '\\') {_i++;}
else if (ch === '"') {quoted= false;}
}
else if (ch === '"') {quoted= true;}
else if (ch === ',' && ChallengeStart.test(header.slice(_i+1))) {
challenges.push(header.slice(start, _i).trim());
start= _i+1;
}
}
challenges.push(header.slice(start).trim());
return challenges.filter(challenge => challenge.length > 0);
}

function challenge_scheme(challenge) {
const match= (typeof challenge === 'string') ? challenge.match(ParseAuth) : null;
return match ? match[1] : '';
}

function parse_params(header) {
// This parser will definitely fail if there is more than one challenge
let tok, _i, _len, key, value;
Expand Down Expand Up @@ -87,10 +116,15 @@ function parse_params(header) {

function Parse_WWW_Authenticate(to_parse)
{
const m= to_parse.match(ParseAuth);
this.scheme= m[1];
this.scheme= '';
this.parms= {};
const err= this.parse_params(m[2]);
const m= (typeof to_parse === 'string') ? to_parse.match(ParseAuth) : null;
if (!m) {
this.err= 'Invalid www-authenticate header.';
return;
}
this.scheme= m[1];
const err= this.parse_params(m[2] || '');
if (err) {
this.scheme= '';
this.parms= {};
Expand All @@ -115,5 +149,7 @@ Parse_WWW_Authenticate.prototype.parse_params= parse_params;

module.exports = {
WWW_Authenticate: Parse_WWW_Authenticate,
Authentication_Info: Parse_Authentication_Info
Authentication_Info: Parse_Authentication_Info,
split_challenges: split_challenges,
challenge_scheme: challenge_scheme
};
179 changes: 105 additions & 74 deletions lib/www-authenticate-patched/www-authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,90 +46,121 @@ const www_authenticator = function(username,password,options)
{
function Authenticator()
{
function note_error(err)
const note_error = (err) =>
{
this.err= err;
}
};
let nc= 0;

const parsed= new parsers.WWW_Authenticate(www_authenticate);
if (parsed.err) {return note_error(parsed.err);}
const auth_parms= this.parms= parsed.parms;
this.cnonce= cnonce;

switch(parsed.scheme) {
case 'Basic':
var auth_string= 'Basic '+credentials.basic();
this.authorize= function() {
return auth_string;
};
return;
case 'Digest':
var realm= auth_parms.realm;
if (!realm) {
return note_error('Realm not found in www-authenticate header.');
}
// Returns an error message, or nothing when the challenge was applied.
const configure= (selected) => {
const note_error= err => err;
const parsed= new parsers.WWW_Authenticate(selected);
if (parsed.err) {return parsed.err;}
const scheme = parsed.scheme.toLowerCase();
const auth_parms= this.parms= parsed.parms;
this.cnonce= cnonce;

var ha1=
credentials.digest(realm);
var nonce= auth_parms.nonce;
if (!nonce) {
return note_error('Nonce not found in www-authenticate header.');
}
switch(scheme) {
case 'basic':
var auth_string= 'Basic '+credentials.basic();
this.authorize= function() {
return auth_string;
};
return;
case 'digest': {
var realm= auth_parms.realm;
if (!realm) {
return note_error('Realm not found in www-authenticate header.');
}

var fixed= 'Digest username="'+credentials.username+'",'+
' realm="'+realm+'",'+
' nonce="'+nonce+'",';
var qop= auth_parms.qop;
if (!qop) {
this.authorize= function(method,digestURI) {
const ha2= md5(method+':'+digestURI);
return fixed+
' uri="'+digestURI+'",'+
' response="'+md5(ha1+':'+nonce+':'+ha2)+'",';
};
return;
}
else {
const qopa= qop.split(',');
let _i, _len;
for (_i = 0, _len = qopa.length; _i < _len; _i++) {
if ('auth' === qopa[_i]) {
var opaque= auth_parms.opaque;
let algorithm= auth_parms.algorithm;
if (algorithm) {
fixed+= ' algorithm="'+algorithm+'",';
}
else {
algorithm= 'MD5';
}
var a1= 'MD5-sess' == algorithm ?
md5(ha1+':'+nonce+':'+cnonce)
:
ha1;
this.authorize= function(method,digestURI) {
const ha2= md5(method+':'+digestURI);
nc= nc+1;
const hexed_nc= hex8(nc);
let s= fixed+
' uri="'+digestURI+'",'+
' qop=auth,'+
' nc='+hexed_nc+','+
' cnonce="'+cnonce+'",'+
' response="'+md5(a1+':'+nonce+':'+hexed_nc+':'+cnonce+':auth:'+ha2)+'"';
if (opaque) {
s+= ', opaque="'+opaque+'"';
}
return s;
};
return;
var ha1=
credentials.digest(realm);
var nonce= auth_parms.nonce;
if (!nonce) {
return note_error('Nonce not found in www-authenticate header.');
}

var fixed= 'Digest username="'+credentials.username+'",'+
' realm="'+realm+'",'+
' nonce="'+nonce+'",';
var qop= auth_parms.qop;
// RFC 2069 Digest without qop has no client nonce, so an attacker who strips qop could force a weaker exchange.
if (!qop) {
return note_error('Server did not offer a quality of protection (qop) for Digest authentication.');
}
const algorithm= auth_parms.algorithm;
if (algorithm) {
const normalized= algorithm.toLowerCase();
if (normalized !== 'md5' && normalized !== 'md5-sess') {
return note_error('Unsupported Digest algorithm: ' + algorithm);
}
fixed+= ' algorithm="'+algorithm+'",';
}
return note_error('Server does not accept any supported quality of protection techniques.');
const qopa= qop.split(',').map(value => value.trim());
if (qopa.indexOf('auth') < 0) {
return note_error('Server does not accept any supported quality of protection techniques.');
}
var opaque= auth_parms.opaque;
var a1= (algorithm && algorithm.toLowerCase() === 'md5-sess') ?
md5(ha1+':'+nonce+':'+cnonce)
:
ha1;
this.authorize= function(method,digestURI) {
const ha2= md5(method+':'+digestURI);
nc= nc+1;
const hexed_nc= hex8(nc);
let s= fixed+
' uri="'+digestURI+'",'+
' qop=auth,'+
' nc='+hexed_nc+','+
' cnonce="'+cnonce+'",'+
' response="'+md5(a1+':'+nonce+':'+hexed_nc+':'+cnonce+':auth:'+ha2)+'"';
if (opaque) {
s+= ', opaque="'+opaque+'"';
}
return s;
};
return;
}
default:
return note_error('Unknown scheme');
default:
return note_error('Unknown scheme');
}
};

const challenges= parsers.split_challenges(www_authenticate);
if (challenges.length === 0) {
return note_error('Invalid www-authenticate header.');
}
let candidates= challenges;
const expected= (options && options.expectedScheme) ?
String(options.expectedScheme).toLowerCase() : null;
if (expected) {
const offered= challenges.map(challenge =>
parsers.challenge_scheme(challenge).toLowerCase());
candidates= challenges.filter((challenge, index) => offered[index] === expected);
if (candidates.length === 0) {
const schemes= offered.filter(scheme => scheme.length > 0);
if (schemes.length === 0) {
return note_error(new parsers.WWW_Authenticate(challenges[0]).err ||
'Invalid www-authenticate header.');
}
return note_error('Server requested ' + schemes.join(', ').toUpperCase() +
' authentication, but the client is configured for ' + expected.toUpperCase());
}
}

// A server may offer several challenges for the same scheme (for example, Digest
// with different algorithms), so use the first one that is supported.
let firstError;
for (const candidate of candidates) {
const err= configure(candidate);
if (!err) {return;}
if (firstError === undefined) {firstError= err;}
delete this.parms;
delete this.authorize;
}
return note_error(firstError);
}

return new Authenticator();
Expand Down
Loading
Loading