There were some corner cases that were not handled (such as logging the instance URL being processed). Additionally, code was simplified greatly by centralizing the sanitization logic.pull/76/head
parent
243d076087
commit
601f144afe
@ -1,8 +1,8 @@
|
||||
using Flurl.Http;
|
||||
using Flurl;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
public interface IServerInfo
|
||||
{
|
||||
IFlurlRequest BuildRequest();
|
||||
Url BuildRequest();
|
||||
}
|
||||
|
@ -1,29 +1,23 @@
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
using Serilog;
|
||||
using TrashLib.Extensions;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
internal class ServerInfo : IServerInfo
|
||||
{
|
||||
private readonly IConfigurationProvider _config;
|
||||
private readonly ILogger _log;
|
||||
|
||||
public ServerInfo(IConfigurationProvider config, ILogger log)
|
||||
public ServerInfo(IConfigurationProvider config)
|
||||
{
|
||||
_config = config;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
public IFlurlRequest BuildRequest()
|
||||
public Url BuildRequest()
|
||||
{
|
||||
var apiKey = _config.ActiveConfiguration.ApiKey;
|
||||
var baseUrl = _config.ActiveConfiguration.BaseUrl;
|
||||
|
||||
return baseUrl
|
||||
.AppendPathSegment("api/v3")
|
||||
.SetQueryParams(new {apikey = apiKey})
|
||||
.SanitizedLogging(_log);
|
||||
.SetQueryParams(new {apikey = apiKey});
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,16 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
using Serilog;
|
||||
|
||||
namespace TrashLib.Extensions;
|
||||
|
||||
public static class FlurlExtensions
|
||||
{
|
||||
public static IFlurlRequest SanitizedLogging(this Url url, ILogger log)
|
||||
=> new FlurlRequest(url).SanitizedLogging(log);
|
||||
|
||||
public static IFlurlRequest SanitizedLogging(this IFlurlRequest request, ILogger log)
|
||||
{
|
||||
return request.ConfigureRequest(settings => FlurlLogging.SetupLogging(settings, log, SanitizeUrl));
|
||||
}
|
||||
|
||||
public static string SanitizedExceptionMessage(this FlurlHttpException exception)
|
||||
{
|
||||
const string expression =
|
||||
@"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}(\:[0-9]+)?\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)";
|
||||
|
||||
return Regex.Replace(exception.ToString(), expression, match => SanitizeUrl(match.Value).ToString());
|
||||
}
|
||||
|
||||
private static Url SanitizeUrl(Url url)
|
||||
{
|
||||
// Replace hostname and API key for user privacy
|
||||
url.Host = "hostname";
|
||||
if (url.QueryParams.Contains("apikey"))
|
||||
{
|
||||
url.QueryParams.AddOrReplace("apikey", "SNIP");
|
||||
}
|
||||
|
||||
return url;
|
||||
return Regex.Replace(exception.ToString(), expression,
|
||||
match => FlurlLogging.SanitizeUrl(match.Value).ToString());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue