From f695c4db040f84e54a28aa04c0e9bff2ded8cc9a Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 15 Sep 2026 12:53:55 +0200 Subject: [PATCH] [net] Make authentication modules trimmable Delay creation of the built-in Basic and Digest authentication modules until PreAuthenticationData is configured, allowing unused implementations to be removed by the linker. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../AndroidMessageHandler.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs index b2eb1d0eae6..94a34ad15b9 100644 --- a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs +++ b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs @@ -507,13 +507,9 @@ or ObjectDisposedException "Last-Modified" }; - static readonly List authModules = new List { - new AuthModuleBasic (), - new AuthModuleDigest () - }; - CookieContainer? _cookieContainer; DecompressionMethods _decompressionMethods; + IAndroidAuthenticationModule []? authModules; bool disposed; bool started; @@ -759,7 +755,20 @@ public int MaxAutomaticRedirections /// /// /// The pre authentication data. - public AuthenticationData? PreAuthenticationData { get; set; } + public AuthenticationData? PreAuthenticationData { + get; + set { + // Keep these constructor references in the setter so apps that never configure pre-authentication can trim the built-in modules. + if (value != null && authModules == null) { + authModules = [ + new AuthModuleBasic (), + new AuthModuleDigest () + ]; + } + + field = value; + } + } /// /// If the website requires authentication, this property will contain data about each scheme supported @@ -1768,7 +1777,7 @@ void HandlePreAuthentication (HttpURLConnection httpConnection) return; } - var auth = data.Scheme == AuthenticationScheme.Unsupported ? data.AuthModule : authModules.Find (m => m?.Scheme == data.Scheme); + var auth = data.Scheme == AuthenticationScheme.Unsupported ? data.AuthModule : authModules?.FirstOrDefault (m => m.Scheme == data.Scheme); if (auth == null) { if (Logger.LogNet) Logger.Log (LogLevel.Info, LOG_APP, $"Authentication module for scheme '{data.Scheme}' not found. No authentication will be performed");