Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mask request IpAddress #385

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Mindscape.Raygun4Net/Builders/RaygunRequestMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Mindscape.Raygun4Net.Messages;
Expand All @@ -15,7 +14,7 @@ namespace Mindscape.Raygun4Net.Builders
{
public class RaygunRequestMessageBuilder
{
private static readonly Regex IpAddressRegex = new Regex(@"\A(?:\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(:[1-9][0-9]{0,4})?\z", RegexOptions.Compiled);
private static readonly Regex IpAddressRegex = new Regex(@"\A(?:\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(:[1-9][0-9]{0,4})?\z", RegexOptions.Compiled);

private const int MAX_RAW_DATA_LENGTH = 4096; // bytes
private const int MAX_KEY_LENGTH = 256; // Characters
Expand All @@ -27,7 +26,7 @@ public static RaygunRequestMessage Build(HttpRequest request, RaygunRequestMessa

RaygunRequestMessage message = new RaygunRequestMessage()
{
IPAddress = GetIpAddress(request),
IPAddress = GetIpAddress(request, options),
QueryString = GetQueryString(request, options),
Cookies = GetCookies(request, options),
Data = GetServerVariables(request, options),
Expand All @@ -50,7 +49,7 @@ public static RaygunRequestMessage Build(HttpRequest request, RaygunRequestMessa
return message;
}

private static string GetIpAddress(HttpRequest request)
private static string GetIpAddress(HttpRequest request, RaygunRequestMessageOptions options)
{
string strIp = null;

Expand Down Expand Up @@ -95,6 +94,11 @@ private static string GetIpAddress(HttpRequest request)
System.Diagnostics.Trace.WriteLine("Failed to get IP address: {0}", ex.Message);
}

if (options.IsRequestIpAddressMasked)
{
strIp = MaskIpAddress(strIp);
}

return strIp;
}

Expand Down Expand Up @@ -350,6 +354,21 @@ public static bool DataContains(string data, List<string> values)
return exists;
}

private static string MaskIpAddress(string strIp)
{
if (strIp != null)
{
var match = IpAddressRegex.Match(strIp);
if (match.Success && match.Groups.Count > 1)
{
var group = match.Groups[1];
strIp = strIp.Substring(0, group.Index) + "0" + strIp.Substring(group.Index + group.Length);
}
}

return strIp;
}

private static IDictionary ToDictionary(NameValueCollection nameValueCollection, Func<string, bool> ignore, Func<string, bool> isSensitive, bool truncateValues = false)
{
IEnumerable<string> keys;
Expand Down
12 changes: 12 additions & 0 deletions Mindscape.Raygun4Net/RaygunRequestMessageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class RaygunRequestMessageOptions
private readonly List<string> _ignoreServerVariableNames = new List<string>();
private bool _isRawDataIgnored;
private bool _isRawDataIgnoredWhenFilteringFailed;
private bool _isRequestIpAddressMasked;
private bool _useXmlRawDataFilter;
private bool _useKeyValuePairRawDataFilter;

Expand Down Expand Up @@ -102,6 +103,17 @@ public bool IsQueryParameterIgnored(string name)
{
return IsIgnored(name, _ignoredQueryParameterNames);
}

// Masking

public bool IsRequestIpAddressMasked
{
get { return _isRequestIpAddressMasked; }
set
{
_isRequestIpAddressMasked = value;
}
}

// Form fields

Expand Down
7 changes: 7 additions & 0 deletions Mindscape.Raygun4Net/RaygunSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ public bool IsResponseContentIgnored
set { this["isResponseContentIgnored"] = value; }
}

[ConfigurationProperty("isRequestIpAddressMasked", IsRequired = false, DefaultValue = false)]
public bool IsRequestIpAddressMasked
{
get { return (bool)this["isRequestIpAddressMasked"]; }
set { this["isRequestIpAddressMasked"] = value; }
}

[ConfigurationProperty("applicationVersion", IsRequired = false, DefaultValue = "")]
public string ApplicationVersion
{
Expand Down
11 changes: 11 additions & 0 deletions Mindscape.Raygun4Net4/RaygunClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public RaygunClient(string apiKey)

IsRawDataIgnored = RaygunSettings.Settings.IsRawDataIgnored;
IsRawDataIgnoredWhenFilteringFailed = RaygunSettings.Settings.IsRawDataIgnoredWhenFilteringFailed;
IsRequestIpAddressMasked = RaygunSettings.Settings.IsRequestIpAddressMasked;

UseXmlRawDataFilter = RaygunSettings.Settings.UseXmlRawDataFilter;
UseKeyValuePairRawDataFilter = RaygunSettings.Settings.UseKeyValuePairRawDataFilter;
Expand Down Expand Up @@ -238,6 +239,16 @@ public bool IsRawDataIgnoredWhenFilteringFailed
set { _requestMessageOptions.IsRawDataIgnoredWhenFilteringFailed = value; }
}

/// <summary>
/// Specifies whether or not the request IpAddress has the last octet zeroed. This may be
/// required to comply with the European GDPR laws.
/// </summary>
public bool IsRequestIpAddressMasked
{
get { return _requestMessageOptions.IsRequestIpAddressMasked; }
set { _requestMessageOptions.IsRequestIpAddressMasked = value; }
}

/// <summary>
/// Specifies whether or not RawData from web requests is filtered of sensitive values using an XML parser.
/// </summary>
Expand Down