From 516b75942a04aae80e53a36e3006ca299cdcfc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Tue, 15 Nov 2022 15:04:12 +0100 Subject: [PATCH] SEBWIN-594: Fixed crash when attempting to download file originating from data URL. --- .../Handlers/DownloadHandler.cs | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs index 5b21c2fc4..4f7cc440d 100644 --- a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs @@ -52,16 +52,26 @@ public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string public void OnBeforeDownload(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { - var uri = new Uri(downloadItem.Url); - var uriExtension = Path.GetExtension(uri.AbsolutePath); var fileExtension = Path.GetExtension(downloadItem.SuggestedFileName); var isConfigurationFile = false; + var url = downloadItem.Url; + var urlExtension = default(string); + + if (downloadItem.Url.StartsWith("data:")) + { + url = downloadItem.Url.Length <= 100 ? downloadItem.Url : downloadItem.Url.Substring(0, 100) + "..."; + } + + if (Uri.TryCreate(downloadItem.Url, UriKind.RelativeOrAbsolute, out var uri)) + { + urlExtension = Path.GetExtension(uri.AbsolutePath); + } isConfigurationFile |= string.Equals(appConfig.ConfigurationFileExtension, fileExtension, StringComparison.OrdinalIgnoreCase); - isConfigurationFile |= string.Equals(appConfig.ConfigurationFileExtension, uriExtension, StringComparison.OrdinalIgnoreCase); + isConfigurationFile |= string.Equals(appConfig.ConfigurationFileExtension, urlExtension, StringComparison.OrdinalIgnoreCase); isConfigurationFile |= string.Equals(appConfig.ConfigurationFileMimeType, downloadItem.MimeType, StringComparison.OrdinalIgnoreCase); - logger.Debug($"Detected download request{(windowSettings.UrlPolicy.CanLog() ? $" for '{uri}'" : "")}."); + logger.Debug($"Detected download request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")}."); if (isConfigurationFile) { @@ -73,7 +83,7 @@ public void OnBeforeDownload(IWebBrowser webBrowser, IBrowser browser, DownloadI } else { - logger.Info($"Aborted download request{(windowSettings.UrlPolicy.CanLog() ? $" for '{uri}'" : "")}, as downloading is not allowed."); + logger.Info($"Aborted download request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")}, as downloading is not allowed."); Task.Run(() => DownloadAborted?.Invoke()); } }