|
| 1 | +namespace Sqlbi.Bravo.Infrastructure.Services; |
| 2 | + |
| 3 | +using Microsoft.Web.WebView2.Core; |
| 4 | +using Sqlbi.Bravo.Infrastructure.Configuration; |
| 5 | +using Sqlbi.Bravo.Infrastructure.Configuration.Settings; |
| 6 | +using Sqlbi.Bravo.Infrastructure.Security; |
| 7 | +using Sqlbi.Bravo.Infrastructure.Telemetry; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Handles WebView2 proxy authentication challenges (HTTP 407). |
| 11 | +/// |
| 12 | +/// The WebView2-hosted UI communicates only with the local Kestrel server (localhost), |
| 13 | +/// which bypasses the proxy. The only external HTTP traffic originating from the WebView2 |
| 14 | +/// is the Application Insights telemetry sent by the TypeScript UI layer directly to the |
| 15 | +/// ingestion endpoint over the internet. |
| 16 | +/// |
| 17 | +/// Therefore, proxy authentication challenges in this context can only be triggered by |
| 18 | +/// telemetry requests going through the configured proxy. |
| 19 | +/// |
| 20 | +/// Security: before providing credentials, we verify that the authentication challenge |
| 21 | +/// comes from the user's configured proxy — not from an arbitrary or rogue server. This |
| 22 | +/// prevents credential leakage to untrusted endpoints (e.g. a server returning a 401 to |
| 23 | +/// harvest proxy credentials). |
| 24 | +/// </summary> |
| 25 | +internal sealed class WebView2ProxyAuthHandler |
| 26 | +{ |
| 27 | + private readonly IWebProxy _webProxy; |
| 28 | + |
| 29 | + public WebView2ProxyAuthHandler(IWebProxy webProxy) |
| 30 | + { |
| 31 | + _webProxy = webProxy; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Attempts to handle a proxy authentication challenge. |
| 36 | + /// Returns true if credentials were provided, false otherwise. |
| 37 | + /// </summary> |
| 38 | + public bool TryHandle(CoreWebView2BasicAuthenticationRequestedEventArgs e) |
| 39 | + { |
| 40 | + var proxy = UserPreferences.Current.Proxy; |
| 41 | + |
| 42 | + // No proxy configured |
| 43 | + if (proxy is null) |
| 44 | + return false; |
| 45 | + |
| 46 | + // Proxy explicitly disabled |
| 47 | + if (proxy.Type == ProxyType.None) |
| 48 | + return false; |
| 49 | + |
| 50 | + // Proxy configured to use system credentials, we can't handle the challenge |
| 51 | + if (proxy.UseDefaultCredentials) |
| 52 | + return false; |
| 53 | + |
| 54 | + // Verify the auth challenge comes from the configured proxy, not from an arbitrary server. |
| 55 | + if (!IsTrustedProxy(e.Uri)) |
| 56 | + return false; |
| 57 | + |
| 58 | + if (CredentialManager.TryGetCredential(targetName: AppEnvironment.CredentialManagerProxyCredentialName, out var genericCredential)) |
| 59 | + { |
| 60 | + var credential = genericCredential.ToNetworkCredential(); |
| 61 | + if (credential is not null) |
| 62 | + { |
| 63 | + e.Response.UserName = credential.UserName; |
| 64 | + e.Response.Password = credential.Password; |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Verifies that the URI from the authentication challenge matches the proxy that |
| 74 | + /// the system would use for the telemetry ingestion endpoint. |
| 75 | + /// |
| 76 | + /// This works for both Custom and System proxy types: <see cref="IWebProxy.GetProxy"/> |
| 77 | + /// resolves the correct proxy URI in both cases (including PAC/WPAD auto-discovery |
| 78 | + /// for system proxies). |
| 79 | + /// |
| 80 | + /// We compare only scheme, host, and port — the path component is irrelevant for |
| 81 | + /// proxy identity verification. |
| 82 | + /// |
| 83 | + /// Known limitation: the comparison is string-based and does not perform DNS resolution. |
| 84 | + /// If one URI uses a hostname (e.g. "proxy.contoso.com") and the other uses its IP address |
| 85 | + /// (e.g. "10.0.0.1"), the match will fail even though they refer to the same host. |
| 86 | + /// In practice this is unlikely because Chromium uses the same proxy URI that was passed |
| 87 | + /// via --proxy-server, and GetProxy() for Custom type returns the same user-configured address. |
| 88 | + /// For System proxies with PAC files the risk is slightly higher but remains an edge case. |
| 89 | + /// </summary> |
| 90 | + private bool IsTrustedProxy(string requestUri) |
| 91 | + { |
| 92 | + var expectedProxyUri = _webProxy.GetProxy(TelemetrySessionInfo.DefaultIngestionEndpoint); |
| 93 | + if (expectedProxyUri is null) |
| 94 | + return false; |
| 95 | + |
| 96 | + var requestedUri = new Uri(requestUri); |
| 97 | + |
| 98 | + return Uri.Compare( |
| 99 | + requestedUri, |
| 100 | + expectedProxyUri, |
| 101 | + UriComponents.Scheme | UriComponents.HostAndPort, |
| 102 | + UriFormat.Unescaped, |
| 103 | + StringComparison.OrdinalIgnoreCase) == 0; |
| 104 | + } |
| 105 | +} |
0 commit comments