Skip to content

Commit

Permalink
SEBWIN-594: Fixed crash when attempting to download file originating …
Browse files Browse the repository at this point in the history
…from data URL.
  • Loading branch information
dbuechel committed Nov 15, 2022
1 parent 276555c commit 516b759
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions SafeExamBrowser.Browser/Handlers/DownloadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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());
}
}
Expand Down

0 comments on commit 516b759

Please sign in to comment.