From da9cd97655dba29592147c5100d54c401d7e864a Mon Sep 17 00:00:00 2001 From: vishwach Date: Tue, 17 Nov 2026 23:24:42 +0530 Subject: [PATCH 1/5] MLE-33480: Bugcrowd: node-client-api: a DIGEST client sends the password in cleartext if the server answers with a Basic challenge --- lib/requester.js | 13 +- .../www-authenticate.js | 17 +- test-basic/auth-downgrade-test.js | 167 ++++++++++++++++++ 3 files changed, 188 insertions(+), 9 deletions(-) create mode 100644 test-basic/auth-downgrade-test.js diff --git a/lib/requester.js b/lib/requester.js index efb13400..7582d3ae 100644 --- a/lib/requester.js +++ b/lib/requester.js @@ -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 = {}; } @@ -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 diff --git a/lib/www-authenticate-patched/www-authenticate.js b/lib/www-authenticate-patched/www-authenticate.js index 43599cad..ab7e4cc7 100644 --- a/lib/www-authenticate-patched/www-authenticate.js +++ b/lib/www-authenticate-patched/www-authenticate.js @@ -46,25 +46,32 @@ 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 scheme = parsed.scheme.toLowerCase(); + if (options && options.expectedScheme && + scheme !== options.expectedScheme.toLowerCase()) { + throw new Error('Server requested ' + scheme.toUpperCase() + + ' authentication, but the client is configured for ' + + options.expectedScheme.toUpperCase()); + } const auth_parms= this.parms= parsed.parms; this.cnonce= cnonce; - switch(parsed.scheme) { - case 'Basic': + switch(scheme) { + case 'basic': var auth_string= 'Basic '+credentials.basic(); this.authorize= function() { return auth_string; }; return; - case 'Digest': + case 'digest': var realm= auth_parms.realm; if (!realm) { return note_error('Realm not found in www-authenticate header.'); diff --git a/test-basic/auth-downgrade-test.js b/test-basic/auth-downgrade-test.js new file mode 100644 index 00000000..a359d361 --- /dev/null +++ b/test-basic/auth-downgrade-test.js @@ -0,0 +1,167 @@ +/* +* Copyright (c) 2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. +*/ +'use strict'; + +const assert = require('assert'); +const http = require('http'); +const marklogic = require('../'); +const createAuthInitializer = require('../lib/www-authenticate-patched/www-authenticate'); + +describe('Authentication downgrade prevention (MLE-33480)', function() { + let server; + let client; + let challenge; + let requests; + + beforeEach(async function() { + challenge = 'Basic realm="test-realm"'; + requests = []; + server = http.createServer(function(request, response) { + requests.push({authorization: request.headers.authorization, method: request.method}); + request.resume(); + if (!request.headers.authorization) { + response.writeHead(401, {'WWW-Authenticate': challenge}); + } else { + response.writeHead(200); + } + response.end(); + }); + await new Promise(function(resolve, reject) { + server.once('error', reject); + server.listen(0, '127.0.0.1', resolve); + }); + client = marklogic.createDatabaseClient({ + host: '127.0.0.1', + port: server.address().port, + user: 'testuser', + password: 'testpassword', + authType: 'digest' + }); + }); + + afterEach(async function() { + if (client) { + client.release(); + } + if (server) { + await new Promise(function(resolve, reject) { + server.close(function(error) { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); + } + }); + + for (const basicChallenge of ['Basic realm="test-realm"', 'bAsIc realm="test-realm"']) { + it('rejects ' + basicChallenge + ' without sending credentials or caching it', async function() { + challenge = basicChallenge; + for (let attempt = 0; attempt < 2; attempt++) { + await assert.rejects(client.documents.probe('/test.json').result(), + /Server requested BASIC authentication, but the client is configured for DIGEST/); + } + assert.strictEqual(requests.length, 2); + assert(requests.every(request => request.authorization === undefined)); + }); + } + + it('rejects a downgrade through the callback API', function(done) { + client.documents.probe('/test.json').result(function() { + done(new Error('Expected authentication rejection')); + }, function(error) { + try { + assert.match(error.message, /Server requested BASIC authentication/); + assert.strictEqual(requests.length, 1); + assert.strictEqual(requests[0].authorization, undefined); + done(); + } catch (assertionError) { + done(assertionError); + } + }); + }); + + it('does not send a write after a Basic challenge to the authentication probe', async function() { + await assert.rejects(client.documents.write({ + uri: '/test.json', content: {test: true} + }).result(), /Server requested BASIC authentication/); + assert.deepStrictEqual(requests, [{authorization: undefined, method: 'HEAD'}]); + }); + + for (const invalidChallenge of [ + 'Digest realm', + 'Digest realm="test-realm"', + 'Digest nonce="test-nonce"', + 'Digest realm="test-realm", nonce="test-nonce", qop="auth-int"', + 'Bearer realm="test-realm"' + ]) { + it('rejects an invalid or unsupported challenge: ' + invalidChallenge, async function() { + challenge = invalidChallenge; + await assert.rejects(client.documents.probe('/test.json').result()); + assert.strictEqual(requests.length, 1); + assert.strictEqual(requests[0].authorization, undefined); + }); + } + + it('can authenticate after a rejected downgrade and reuse only the valid authenticator', async function() { + await assert.rejects(client.documents.probe('/test.json').result(), + /Server requested BASIC authentication/); + challenge = 'Digest realm="test-realm", nonce="test-nonce", qop="auth"'; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.strictEqual(requests.length, 4); + assert.strictEqual(requests[0].authorization, undefined); + assert.strictEqual(requests[1].authorization, undefined); + assert.match(requests[2].authorization, /^Digest /); + assert.match(requests[3].authorization, /^Digest /); + }); + + for (const scheme of ['Digest', 'dIgEsT']) { + it('continues to authenticate with a valid ' + scheme + ' challenge', async function() { + challenge = scheme + ' realm="test-realm", nonce="test-nonce", qop="auth"'; + const result = await client.documents.probe('/test.json').result(); + assert.strictEqual(result.exists, true); + assert.strictEqual(requests.length, 2); + assert.strictEqual(requests[0].authorization, undefined); + assert.match(requests[1].authorization, /^Digest /); + assert(!requests[1].authorization.includes('testpassword')); + }); + } + + it('preserves explicitly configured Basic authentication', async function() { + client.release(); + client = marklogic.createDatabaseClient({ + host: '127.0.0.1', + port: server.address().port, + user: 'testuser', + password: 'testpassword', + authType: 'basic' + }); + const result = await client.documents.probe('/test.json').result(); + assert.strictEqual(result.exists, true); + assert.strictEqual(requests.length, 1); + assert.strictEqual(requests[0].authorization, + 'Basic ' + Buffer.from('testuser:testpassword').toString('base64')); + }); + + it('enforces the expected scheme in the authenticator itself', function() { + const initialize = createAuthInitializer('testuser', 'testpassword', {expectedScheme: 'digest'}); + assert.throws(function() { + initialize('Basic realm="test-realm"'); + }, /Server requested BASIC authentication, but the client is configured for DIGEST/); + }); + + it('preserves the RFC Digest response calculation', function() { + const initialize = createAuthInitializer('Mufasa', 'Circle Of Life', { + expectedScheme: 'digest', cnonce: '0a4f113b' + }); + const authenticator = initialize('Digest realm="testrealm@host.com", ' + + 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", qop="auth", ' + + 'opaque="5ccc069c403ebaf9f0171e9517f40e41"'); + assert.match(authenticator.authorize('GET', '/dir/index.html'), + /response="6629fae49393a05397450978507c4ef1"/); + }); +}); From 2db604081b90bd95423a4df591f83a8a57a302c4 Mon Sep 17 00:00:00 2001 From: vishwach Date: Wed, 18 Nov 2026 10:40:30 +0530 Subject: [PATCH 2/5] MLE-33480: Bugcrowd: node-client-api: a DIGEST client sends the password in cleartext if the server answers with a Basic challenge --- lib/www-authenticate-patched/parsers.js | 42 ++++++- .../www-authenticate.js | 106 ++++++++++-------- test-basic/auth-downgrade-test.js | 79 +++++++++++-- 3 files changed, 160 insertions(+), 67 deletions(-) diff --git a/lib/www-authenticate-patched/parsers.js b/lib/www-authenticate-patched/parsers.js index 6998fc40..98bd5063 100644 --- a/lib/www-authenticate-patched/parsers.js +++ b/lib/www-authenticate-patched/parsers.js @@ -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. */ /* @@ -10,10 +10,34 @@ * 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+[\w!#$%&'*+.^`|~-]+\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 bare scheme or a scheme followed by an auth-param. +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 parse_params(header) { // This parser will definitely fail if there is more than one challenge let tok, _i, _len, key, value; @@ -87,10 +111,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= {}; @@ -115,5 +144,6 @@ 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 }; diff --git a/lib/www-authenticate-patched/www-authenticate.js b/lib/www-authenticate-patched/www-authenticate.js index ab7e4cc7..51bb9448 100644 --- a/lib/www-authenticate-patched/www-authenticate.js +++ b/lib/www-authenticate-patched/www-authenticate.js @@ -52,15 +52,30 @@ const www_authenticator = function(username,password,options) }; let nc= 0; - const parsed= new parsers.WWW_Authenticate(www_authenticate); + let selected= www_authenticate; + const expected= (options && options.expectedScheme) ? + String(options.expectedScheme).toLowerCase() : null; + if (expected) { + const challenges= parsers.split_challenges(www_authenticate); + const parsedChallenges= challenges.map(challenge => new parsers.WWW_Authenticate(challenge)); + const offered= parsedChallenges.map(parsedChallenge => + parsedChallenge.err ? '' : parsedChallenge.scheme.toLowerCase()); + const index= offered.indexOf(expected); + if (index < 0) { + const schemes= offered.filter(scheme => scheme.length > 0); + if (schemes.length === 0) { + return note_error(parsedChallenges.length > 0 ? + parsedChallenges[0].err : 'Invalid www-authenticate header.'); + } + return note_error('Server requested ' + schemes.join(', ').toUpperCase() + + ' authentication, but the client is configured for ' + expected.toUpperCase()); + } + selected= challenges[index]; + } + + const parsed= new parsers.WWW_Authenticate(selected); if (parsed.err) {return note_error(parsed.err);} const scheme = parsed.scheme.toLowerCase(); - if (options && options.expectedScheme && - scheme !== options.expectedScheme.toLowerCase()) { - throw new Error('Server requested ' + scheme.toUpperCase() + - ' authentication, but the client is configured for ' + - options.expectedScheme.toUpperCase()); - } const auth_parms= this.parms= parsed.parms; this.cnonce= cnonce; @@ -71,7 +86,7 @@ const www_authenticator = function(username,password,options) return auth_string; }; return; - case 'digest': + case 'digest': { var realm= auth_parms.realm; if (!realm) { return note_error('Realm not found in www-authenticate header.'); @@ -88,52 +103,45 @@ const www_authenticator = function(username,password,options) ' 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) { - this.authorize= function(method,digestURI) { - const ha2= md5(method+':'+digestURI); - return fixed+ - ' uri="'+digestURI+'",'+ - ' response="'+md5(ha1+':'+nonce+':'+ha2)+'",'; - }; - return; + return note_error('Server did not offer a quality of protection (qop) for Digest authentication.'); } - 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; - } + 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+'",'; + } + 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'); } diff --git a/test-basic/auth-downgrade-test.js b/test-basic/auth-downgrade-test.js index a359d361..6aad2507 100644 --- a/test-basic/auth-downgrade-test.js +++ b/test-basic/auth-downgrade-test.js @@ -1,5 +1,5 @@ /* -* Copyright (c) 2026 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. */ 'use strict'; @@ -21,7 +21,8 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { requests.push({authorization: request.headers.authorization, method: request.method}); request.resume(); if (!request.headers.authorization) { - response.writeHead(401, {'WWW-Authenticate': challenge}); + // An array sends one WWW-Authenticate header per element. + response.writeHead(401, [].concat(challenge).map(value => ['WWW-Authenticate', value]).flat()); } else { response.writeHead(200); } @@ -91,21 +92,49 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { assert.deepStrictEqual(requests, [{authorization: undefined, method: 'HEAD'}]); }); - for (const invalidChallenge of [ - 'Digest realm', - 'Digest realm="test-realm"', - 'Digest nonce="test-nonce"', - 'Digest realm="test-realm", nonce="test-nonce", qop="auth-int"', - 'Bearer realm="test-realm"' + for (const [invalidChallenge, expectedError] of [ + ['Digest', /Realm not found/], + ['Digest realm', /Unexpected end of www-authenticate value/], + ['Digest realm="test-realm"', /Nonce not found/], + ['Digest nonce="test-nonce"', /Realm not found/], + ['Digest realm="test-realm", nonce="test-nonce", qop="auth-int"', + /does not accept any supported quality of protection/], + ['Digest realm="test-realm", nonce="test-nonce"', /did not offer a quality of protection \(qop\)/], + ['Digest realm="test-realm", nonce="test-nonce", qop="auth", algorithm=SHA-256', + /Unsupported Digest algorithm: SHA-256/], + ['Digest realm="test-realm", nonce="test-nonce", qop="auth", algorithm="MD5-unknown"', + /Unsupported Digest algorithm: MD5-unknown/], + ['Bearer realm="test-realm"', /Server requested BEARER authentication, but the client is configured for DIGEST/] ]) { it('rejects an invalid or unsupported challenge: ' + invalidChallenge, async function() { challenge = invalidChallenge; - await assert.rejects(client.documents.probe('/test.json').result()); + await assert.rejects(client.documents.probe('/test.json').result(), expectedError); assert.strictEqual(requests.length, 1); assert.strictEqual(requests[0].authorization, undefined); }); } + it('selects the Digest challenge when the server also offers Basic in separate headers', async function() { + challenge = ['Basic realm="test-realm"', 'Digest realm="test-realm", nonce="test-nonce", qop="auth"']; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.strictEqual(requests.length, 2); + assert.match(requests[1].authorization, /^Digest username="testuser", realm="test-realm", nonce="test-nonce",/); + }); + + it('selects the Digest challenge from a single combined header value', async function() { + challenge = 'Basic realm="a, b", Digest realm="test-realm", nonce="test-nonce", qop="auth-int, auth"'; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.match(requests[1].authorization, /^Digest username="testuser", realm="test-realm",/); + assert.match(requests[1].authorization, / qop=auth,/); + }); + + it('lists every offered scheme when none matches', async function() { + challenge = ['Basic realm="test-realm"', 'Bearer realm="test-realm"']; + await assert.rejects(client.documents.probe('/test.json').result(), + /Server requested BASIC, BEARER authentication, but the client is configured for DIGEST/); + assert.strictEqual(requests.length, 1); + }); + it('can authenticate after a rejected downgrade and reuse only the valid authenticator', async function() { await assert.rejects(client.documents.probe('/test.json').result(), /Server requested BASIC authentication/); @@ -149,9 +178,35 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { it('enforces the expected scheme in the authenticator itself', function() { const initialize = createAuthInitializer('testuser', 'testpassword', {expectedScheme: 'digest'}); - assert.throws(function() { - initialize('Basic realm="test-realm"'); - }, /Server requested BASIC authentication, but the client is configured for DIGEST/); + const authenticator = initialize('Basic realm="test-realm"'); + assert.match(authenticator.err, /Server requested BASIC authentication, but the client is configured for DIGEST/); + assert.strictEqual(authenticator.authorize, undefined); + }); + + it('reports a scheme mismatch as an error from the deprecated higher-level API', function() { + const higherLevel = createAuthInitializer.authenticator('testuser', 'testpassword', {expectedScheme: 'digest'}); + const error = higherLevel.get_challenge({ + statusCode: 401, headers: {'www-authenticate': 'Basic realm="test-realm"'} + }); + assert.match(error, /Server requested BASIC authentication/); + }); + + it('reports a malformed header as an error instead of throwing', function() { + for (const header of ['', ' ', '"quoted"', undefined]) { + const authenticator = createAuthInitializer('testuser', 'testpassword', {})(header); + assert.strictEqual(authenticator.err, 'Invalid www-authenticate header.'); + } + }); + + it('supports MD5-sess regardless of algorithm case', function() { + const options = {expectedScheme: 'digest', cnonce: '0a4f113b'}; + const challengeText = 'Digest realm="r", nonce="n", qop="auth", algorithm='; + const upper = createAuthInitializer('u', 'p', options)(challengeText + 'MD5-sess').authorize('GET', '/'); + const lower = createAuthInitializer('u', 'p', options)(challengeText + 'md5-sess').authorize('GET', '/'); + const plain = createAuthInitializer('u', 'p', options)(challengeText + 'MD5').authorize('GET', '/'); + const responseOf = header => header.match(/response="([0-9a-f]+)"/)[1]; + assert.strictEqual(responseOf(upper), responseOf(lower)); + assert.notStrictEqual(responseOf(upper), responseOf(plain)); }); it('preserves the RFC Digest response calculation', function() { From 5b7cddbbea8ff15b932b95947b9000b9c7aa3c8c Mon Sep 17 00:00:00 2001 From: vishwach Date: Wed, 18 Nov 2026 15:36:19 +0530 Subject: [PATCH 3/5] MLE-33480: Recognize bare-scheme and token68 challenge boundaries in combined WWW-Authenticate headers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- lib/www-authenticate-patched/parsers.js | 4 ++-- test-basic/auth-downgrade-test.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/www-authenticate-patched/parsers.js b/lib/www-authenticate-patched/parsers.js index 98bd5063..79016184 100644 --- a/lib/www-authenticate-patched/parsers.js +++ b/lib/www-authenticate-patched/parsers.js @@ -12,12 +12,12 @@ const ParseAuth= /^\s*([\w!#$%&'*+.^`|~-]+)(?:\s+([\s\S]*))?$/ // -> scheme, params , Separators= /([",=])/ - , ChallengeStart= /^\s*[\w!#$%&'*+.^`|~-]+(?:\s*$|\s+[\w!#$%&'*+.^`|~-]+\s*=)/ + , 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 bare scheme or a scheme followed by an auth-param. +// a scheme token rather than an auth-param name followed by "=". function split_challenges(header) { const challenges= []; if (typeof header !== 'string') {return challenges;} diff --git a/test-basic/auth-downgrade-test.js b/test-basic/auth-downgrade-test.js index 6aad2507..917ef5fa 100644 --- a/test-basic/auth-downgrade-test.js +++ b/test-basic/auth-downgrade-test.js @@ -128,6 +128,31 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { assert.match(requests[1].authorization, / qop=auth,/); }); + for (const combined of [ + 'Digest realm="test-realm", nonce="test-nonce", qop="auth", Negotiate, Basic realm="r"', + 'Negotiate YWJj==, Digest realm="test-realm", nonce="test-nonce", qop="auth"', + 'Digest realm="test-realm", nonce="test-nonce", qop="auth", Negotiate YWJj' + ]) { + it('selects the Digest challenge alongside bare-scheme and token68 challenges: ' + combined, async function() { + challenge = combined; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.match(requests[1].authorization, /^Digest username="testuser", realm="test-realm", nonce="test-nonce",/); + }); + } + + it('splits combined challenges only at scheme boundaries', function() { + const split = createAuthInitializer.parsers.split_challenges; + assert.deepStrictEqual( + split('Digest realm="r", nonce="n", qop="auth", Negotiate, Basic realm="r"'), + ['Digest realm="r", nonce="n", qop="auth"', 'Negotiate', 'Basic realm="r"']); + assert.deepStrictEqual( + split('Negotiate YWJj==, Digest realm="r", algorithm=MD5, nonce = "n"'), + ['Negotiate YWJj==', 'Digest realm="r", algorithm=MD5, nonce = "n"']); + assert.deepStrictEqual( + split('Basic realm="a, Digest b", Digest realm="r"'), + ['Basic realm="a, Digest b"', 'Digest realm="r"']); + }); + it('lists every offered scheme when none matches', async function() { challenge = ['Basic realm="test-realm"', 'Bearer realm="test-realm"']; await assert.rejects(client.documents.probe('/test.json').result(), From 6908b48b1bc5089a047fa06bae12de1c7ddcf288 Mon Sep 17 00:00:00 2001 From: vishwach Date: Wed, 18 Nov 2026 15:46:45 +0530 Subject: [PATCH 4/5] MLE-33480: Skip unsupported Digest challenges when a supported one is offered Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../www-authenticate.js | 165 ++++++++++-------- test-basic/auth-downgrade-test.js | 25 +++ 2 files changed, 115 insertions(+), 75 deletions(-) diff --git a/lib/www-authenticate-patched/www-authenticate.js b/lib/www-authenticate-patched/www-authenticate.js index 51bb9448..014e28d1 100644 --- a/lib/www-authenticate-patched/www-authenticate.js +++ b/lib/www-authenticate-patched/www-authenticate.js @@ -52,7 +52,84 @@ const www_authenticator = function(username,password,options) }; let nc= 0; - let selected= www_authenticate; + // 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; + + 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 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+'",'; + } + 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'); + } + }; + + let candidates= [www_authenticate]; const expected= (options && options.expectedScheme) ? String(options.expectedScheme).toLowerCase() : null; if (expected) { @@ -60,8 +137,8 @@ const www_authenticator = function(username,password,options) const parsedChallenges= challenges.map(challenge => new parsers.WWW_Authenticate(challenge)); const offered= parsedChallenges.map(parsedChallenge => parsedChallenge.err ? '' : parsedChallenge.scheme.toLowerCase()); - const index= offered.indexOf(expected); - if (index < 0) { + 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(parsedChallenges.length > 0 ? @@ -70,81 +147,19 @@ const www_authenticator = function(username,password,options) return note_error('Server requested ' + schemes.join(', ').toUpperCase() + ' authentication, but the client is configured for ' + expected.toUpperCase()); } - selected= challenges[index]; } - const parsed= new parsers.WWW_Authenticate(selected); - if (parsed.err) {return note_error(parsed.err);} - const scheme = parsed.scheme.toLowerCase(); - const auth_parms= this.parms= parsed.parms; - this.cnonce= cnonce; - - 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 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+'",'; - } - 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'); + // 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(); diff --git a/test-basic/auth-downgrade-test.js b/test-basic/auth-downgrade-test.js index 917ef5fa..404ddda8 100644 --- a/test-basic/auth-downgrade-test.js +++ b/test-basic/auth-downgrade-test.js @@ -153,6 +153,31 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { ['Basic realm="a, Digest b"', 'Digest realm="r"']); }); + for (const challenges of [ + ['Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256', + 'Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5'], + ['Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5', + 'Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256'], + 'Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256, ' + + 'Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5' + ]) { + it('skips an unsupported Digest challenge when a supported one is offered: ' + [].concat(challenges).join(' | '), + async function() { + challenge = challenges; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.strictEqual(requests.length, 2); + assert.match(requests[1].authorization, /^Digest .* nonce="md5-nonce", algorithm="MD5",/); + }); + } + + it('rejects when no Digest challenge is supported, without falling back to Basic', async function() { + challenge = ['Digest realm="test-realm", nonce="n", qop="auth", algorithm=SHA-256', + 'Digest realm="test-realm", nonce="n", qop="auth-int"', 'Basic realm="test-realm"']; + await assert.rejects(client.documents.probe('/test.json').result(), /Unsupported Digest algorithm: SHA-256/); + assert.strictEqual(requests.length, 1); + assert.strictEqual(requests[0].authorization, undefined); + }); + it('lists every offered scheme when none matches', async function() { challenge = ['Basic realm="test-realm"', 'Bearer realm="test-realm"']; await assert.rejects(client.documents.probe('/test.json').result(), From 89334c18e7035562bbaffdcd6c4ed83c0e4a453b Mon Sep 17 00:00:00 2001 From: vishwach Date: Wed, 18 Nov 2026 15:49:48 +0530 Subject: [PATCH 5/5] MLE-33480: Bugcrowd: node-client-api: a DIGEST client sends the password in cleartext if the server answers with a Basic challenge --- lib/www-authenticate-patched/parsers.js | 8 ++- .../www-authenticate.js | 19 +++--- test-basic/auth-downgrade-test.js | 66 +++++++++++++------ 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/lib/www-authenticate-patched/parsers.js b/lib/www-authenticate-patched/parsers.js index 79016184..dad71845 100644 --- a/lib/www-authenticate-patched/parsers.js +++ b/lib/www-authenticate-patched/parsers.js @@ -38,6 +38,11 @@ function split_challenges(header) { 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; @@ -145,5 +150,6 @@ Parse_WWW_Authenticate.prototype.parse_params= parse_params; module.exports = { WWW_Authenticate: Parse_WWW_Authenticate, Authentication_Info: Parse_Authentication_Info, - split_challenges: split_challenges + split_challenges: split_challenges, + challenge_scheme: challenge_scheme }; diff --git a/lib/www-authenticate-patched/www-authenticate.js b/lib/www-authenticate-patched/www-authenticate.js index 014e28d1..a7b71736 100644 --- a/lib/www-authenticate-patched/www-authenticate.js +++ b/lib/www-authenticate-patched/www-authenticate.js @@ -85,8 +85,7 @@ const www_authenticator = function(username,password,options) ' 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. + // 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.'); } @@ -129,20 +128,22 @@ const www_authenticator = function(username,password,options) } }; - let candidates= [www_authenticate]; + 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 challenges= parsers.split_challenges(www_authenticate); - const parsedChallenges= challenges.map(challenge => new parsers.WWW_Authenticate(challenge)); - const offered= parsedChallenges.map(parsedChallenge => - parsedChallenge.err ? '' : parsedChallenge.scheme.toLowerCase()); + 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(parsedChallenges.length > 0 ? - parsedChallenges[0].err : 'Invalid www-authenticate header.'); + 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()); diff --git a/test-basic/auth-downgrade-test.js b/test-basic/auth-downgrade-test.js index 404ddda8..4f405be1 100644 --- a/test-basic/auth-downgrade-test.js +++ b/test-basic/auth-downgrade-test.js @@ -58,17 +58,18 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { } }); - for (const basicChallenge of ['Basic realm="test-realm"', 'bAsIc realm="test-realm"']) { - it('rejects ' + basicChallenge + ' without sending credentials or caching it', async function() { + it('rejects a Basic challenge in any case without sending credentials or caching it', async function() { + for (const basicChallenge of ['Basic realm="test-realm"', 'bAsIc realm="test-realm"']) { challenge = basicChallenge; + requests = []; for (let attempt = 0; attempt < 2; attempt++) { await assert.rejects(client.documents.probe('/test.json').result(), /Server requested BASIC authentication, but the client is configured for DIGEST/); } assert.strictEqual(requests.length, 2); assert(requests.every(request => request.authorization === undefined)); - }); - } + } + }); it('rejects a downgrade through the callback API', function(done) { client.documents.probe('/test.json').result(function() { @@ -128,17 +129,33 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { assert.match(requests[1].authorization, / qop=auth,/); }); - for (const combined of [ - 'Digest realm="test-realm", nonce="test-nonce", qop="auth", Negotiate, Basic realm="r"', - 'Negotiate YWJj==, Digest realm="test-realm", nonce="test-nonce", qop="auth"', - 'Digest realm="test-realm", nonce="test-nonce", qop="auth", Negotiate YWJj' - ]) { - it('selects the Digest challenge alongside bare-scheme and token68 challenges: ' + combined, async function() { - challenge = combined; - assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); - assert.match(requests[1].authorization, /^Digest username="testuser", realm="test-realm", nonce="test-nonce",/); - }); - } + it('reports the scheme for token68 challenges when the expected scheme differs', function() { + for (const [token68Challenge, scheme] of [ + ['Bearer abc123', 'BEARER'], + ['Negotiate YWJj==', 'NEGOTIATE'] + ]) { + const authenticator = createAuthInitializer('testuser', 'testpassword', { + expectedScheme: 'digest' + })(token68Challenge); + assert.strictEqual(authenticator.err, + 'Server requested ' + scheme + ' authentication, but the client is configured for DIGEST'); + } + }); + + it('splits combined challenges when no expected scheme is configured', function() { + const initialize = createAuthInitializer('testuser', 'testpassword'); + const authenticator = initialize( + 'Negotiate YWJj==, Digest realm="test-realm", nonce="test-nonce", qop="auth"' + ); + assert.strictEqual(authenticator.err, undefined); + assert.match(authenticator.authorize('GET', '/test.json'), /^Digest username="testuser",/); + }); + + it('selects the Digest challenge when followed by a bare-scheme challenge', async function() { + challenge = 'Digest realm="test-realm", nonce="test-nonce", qop="auth", Negotiate, Basic realm="r"'; + assert.strictEqual((await client.documents.probe('/test.json').result()).exists, true); + assert.match(requests[1].authorization, /^Digest username="testuser", realm="test-realm", nonce="test-nonce",/); + }); it('splits combined challenges only at scheme boundaries', function() { const split = createAuthInitializer.parsers.split_challenges; @@ -156,8 +173,6 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { for (const challenges of [ ['Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256', 'Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5'], - ['Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5', - 'Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256'], 'Digest realm="test-realm", nonce="sha-nonce", qop="auth", algorithm=SHA-256, ' + 'Digest realm="test-realm", nonce="md5-nonce", qop="auth", algorithm=MD5' ]) { @@ -198,17 +213,26 @@ describe('Authentication downgrade prevention (MLE-33480)', function() { assert.match(requests[3].authorization, /^Digest /); }); - for (const scheme of ['Digest', 'dIgEsT']) { - it('continues to authenticate with a valid ' + scheme + ' challenge', async function() { + it('continues to authenticate with a valid Digest challenge in any case', async function() { + for (const scheme of ['Digest', 'dIgEsT']) { + client.release(); + client = marklogic.createDatabaseClient({ + host: '127.0.0.1', + port: server.address().port, + user: 'testuser', + password: 'testpassword', + authType: 'digest' + }); challenge = scheme + ' realm="test-realm", nonce="test-nonce", qop="auth"'; + requests = []; const result = await client.documents.probe('/test.json').result(); assert.strictEqual(result.exists, true); assert.strictEqual(requests.length, 2); assert.strictEqual(requests[0].authorization, undefined); assert.match(requests[1].authorization, /^Digest /); assert(!requests[1].authorization.includes('testpassword')); - }); - } + } + }); it('preserves explicitly configured Basic authentication', async function() { client.release();