From f3413e94b990b472846c2e1c989f7f66ace5cbb4 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Tue, 21 May 2024 12:30:47 +0200 Subject: [PATCH] [network] Make http digest auth cnonce length configurable. --- modules/libpref/init/all.js | 6 ++++++ netwerk/protocol/http/nsHttpDigestAuth.cpp | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index 85aacfd61b..682000aaf2 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -1646,6 +1646,12 @@ pref("network.http.keep_empty_response_headers_as_empty_string", true); // Max size, in bytes, for received HTTP response header. pref("network.http.max_response_header_size", 393216); +// This sets the nonce length to verify the server response (via a +// server-returned Authentication-Info header). Also used for session info. +// Note: Range-checked to 4..256, if OOB defaults to 16. +// Note: Chrome uses 16. Larger values may break sites. See Bug 1892449. +pref("network.http.digest_auth_cnonce_length", 16); + // default values for FTP // in a DSCP environment this should be 40 (0x28, or AF11), per RFC-4594, // Section 4.8 "High-Throughput Data Service Class", and 80 (0x50, or AF22) diff --git a/netwerk/protocol/http/nsHttpDigestAuth.cpp b/netwerk/protocol/http/nsHttpDigestAuth.cpp index ac2808fa8c..5f70b90e50 100644 --- a/netwerk/protocol/http/nsHttpDigestAuth.cpp +++ b/netwerk/protocol/http/nsHttpDigestAuth.cpp @@ -7,6 +7,7 @@ // HttpLog.h should generally be included first #include "HttpLog.h" +#include "mozilla/Preferences.h" #include "mozilla/Sprintf.h" #include "nsHttp.h" @@ -20,6 +21,7 @@ #include "nsCRT.h" #include "nsICryptoHash.h" #include "nsComponentManagerUtils.h" +#include "pk11pub.h" namespace mozilla { namespace net { @@ -302,9 +304,16 @@ nsHttpDigestAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel, // returned Authentication-Info header). also used for session info. // nsAutoCString cnonce; - static const char hexChar[] = "0123456789abcdef"; - for (int i=0; i<16; ++i) { - cnonce.Append(hexChar[(int)(15.0 * rand()/(RAND_MAX + 1.0))]); + nsTArray cnonceBuf; + int cnonceLength = Preferences::GetInt("network.http.digest_auth_cnonce_length", 16); + if (cnonceLength < 4 || cnonceLength > 256) { + cnonceLength = 16; + } + cnonceBuf.SetLength(cnonceLength / 2); + PK11_GenerateRandom(reinterpret_cast(cnonceBuf.Elements()), + cnonceBuf.Length()); + for (auto byte : cnonceBuf) { + cnonce.AppendPrintf("%02x", byte); } LOG((" cnonce=%s\n", cnonce.get()));