From 09141d44cc567629cfc6c926499e6c9c6ea8bbd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Tue, 31 Aug 2021 18:15:26 +0200 Subject: [PATCH] SEBWIN-514: Fixed issue with URL-encoded requests. --- .../Handlers/RequestHandler.cs | 22 +++++++++++++------ .../Handlers/ResourceHandler.cs | 13 ++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs index bf27d222..85f20f31 100644 --- a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs @@ -8,6 +8,7 @@ using System; using System.IO; +using System.Net; using System.Text.RegularExpressions; using CefSharp; using SafeExamBrowser.Browser.Contracts.Filters; @@ -190,30 +191,37 @@ namespace SafeExamBrowser.Browser.Handlers private bool Block(IRequest request) { var block = false; + var url = WebUtility.UrlDecode(request.Url); + var isValidUrl = Uri.TryCreate(url, UriKind.Absolute, out _); - if (settings.Filter.ProcessMainRequests && request.ResourceType == ResourceType.MainFrame) + if (settings.Filter.ProcessMainRequests && request.ResourceType == ResourceType.MainFrame && isValidUrl) { - var result = filter.Process(new Request { Url = request.Url }); + var result = filter.Process(new Request { Url = url }); // We apparently can't filter chrome extension requests, as this prevents the rendering of PDFs. - if (result == FilterResult.Block && !request.Url.StartsWith("chrome-extension://")) + if (result == FilterResult.Block && !url.StartsWith("chrome-extension://")) { block = true; - logger.Info($"Blocked main request{(windowSettings.UrlPolicy.CanLog() ? $" for '{request.Url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); + logger.Info($"Blocked main request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); } } - if (settings.Filter.ProcessContentRequests && request.ResourceType != ResourceType.MainFrame) + if (settings.Filter.ProcessContentRequests && request.ResourceType != ResourceType.MainFrame && isValidUrl) { - var result = filter.Process(new Request { Url = request.Url }); + var result = filter.Process(new Request { Url = url }); if (result == FilterResult.Block) { block = true; - logger.Info($"Blocked content request{(windowSettings.UrlPolicy.CanLog() ? $" for '{request.Url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); + logger.Info($"Blocked content request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); } } + if (!isValidUrl) + { + logger.Warn($"Filter could not process request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")} ({request.ResourceType}, {request.TransitionType})!"); + } + return block; } } diff --git a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs index 930a71c1..dc7baf06 100644 --- a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Specialized; using System.Linq; +using System.Net; using System.Net.Http; using System.Net.Mime; using System.Security.Cryptography; @@ -146,17 +147,23 @@ namespace SafeExamBrowser.Browser.Handlers private bool Block(IRequest request) { var block = false; + var url = WebUtility.UrlDecode(request.Url); + var isValidUri = Uri.TryCreate(url, UriKind.Absolute, out _); - if (settings.Filter.ProcessContentRequests) + if (settings.Filter.ProcessContentRequests && isValidUri) { - var result = filter.Process(new Request { Url = request.Url }); + var result = filter.Process(new Request { Url = url }); if (result == FilterResult.Block) { block = true; - logger.Info($"Blocked content request{(windowSettings.UrlPolicy.CanLog() ? $" for '{request.Url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); + logger.Info($"Blocked content request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")} ({request.ResourceType}, {request.TransitionType})."); } } + else if (!isValidUri) + { + logger.Warn($"Filter could not process request{(windowSettings.UrlPolicy.CanLog() ? $" for '{url}'" : "")} ({request.ResourceType}, {request.TransitionType})!"); + } return block; }