Skip to content

Commit 481f319

Browse files
Add support for injecting script tag with Url and RUM key configuration. Allow Retrace as a replacement for Stackify in configuration. Support more config via environment
1 parent 3cd985b commit 481f319

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

Src/StackifyLib/Config.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45
using StackifyLib.Utils;
56

67
namespace StackifyLib
@@ -108,6 +109,26 @@ public static void LoadSettings()
108109
LoggingJsonMaxFields = maxFields;
109110
}
110111
}
112+
113+
var rumScriptUrl = Get("Stackify.Rum_Script_Url", "https://stckjs.stackify.com/stckjs.js");
114+
115+
if (Uri.IsWellFormedUriString(rumScriptUrl, UriKind.Absolute))
116+
{
117+
var uri = new Uri(rumScriptUrl);
118+
119+
var scheme = uri.Scheme;
120+
121+
if (string.Equals(scheme, "https", StringComparison.OrdinalIgnoreCase))
122+
{
123+
RumScriptUrl = rumScriptUrl;
124+
}
125+
}
126+
127+
var rumKey = Get("Stackify.Rum_Key");
128+
if (Regex.IsMatch(rumKey, "^[A-Za-z0-9-]+$"))
129+
{
130+
RumKey = rumKey;
131+
}
111132
}
112133
catch (Exception ex)
113134
{
@@ -150,6 +171,10 @@ public static void LoadSettings()
150171

151172
public static int LoggingJsonMaxFields { get; set; } = 50;
152173

174+
public static string RumScriptUrl { get; set; }
175+
176+
public static string RumKey { get; set; }
177+
153178

154179
/// <summary>
155180
/// Attempts to fetch a setting value given the key.
@@ -171,6 +196,13 @@ internal static string Get(string key, string defaultValue = null)
171196
{
172197
var appSettings = _configuration.GetSection("Stackify");
173198
v = appSettings[key.Replace("Stackify.", string.Empty)];
199+
200+
if (string.IsNullOrEmpty(v))
201+
{
202+
// Search in Retrace, but key will likely still be Stackify.name, not Retrace.name in the code
203+
var retraceAppSettings = _configuration.GetSection("Retrace");
204+
v = retraceAppSettings[key.Replace("Stackify.", string.Empty)];
205+
}
174206
}
175207
#endif
176208

@@ -185,6 +217,22 @@ internal static string Get(string key, string defaultValue = null)
185217
{
186218
v = System.Environment.GetEnvironmentVariable(key);
187219
}
220+
221+
if (string.IsNullOrEmpty(v))
222+
{
223+
v = System.Environment.GetEnvironmentVariable(key.ToUpperInvariant());
224+
}
225+
226+
if (string.IsNullOrEmpty(v))
227+
{
228+
// Linux systems do not allow period in an environment variable name
229+
v = System.Environment.GetEnvironmentVariable(key.Replace('.', '_').ToUpperInvariant());
230+
}
231+
232+
if (string.IsNullOrEmpty(v) && key.StartsWith("Stackify."))
233+
{
234+
v = System.Environment.GetEnvironmentVariable("RETRACE_" + key.Substring(9).Replace('.', '_').ToUpperInvariant());
235+
}
188236
}
189237
}
190238
finally

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Runtime.Remoting.Messaging;
99
#endif
1010
using System.Text;
11+
using System.Text.RegularExpressions;
1112
using Newtonsoft.Json.Linq;
1213

1314

@@ -605,6 +606,83 @@ protected static bool IsBeingProfiled
605606
}
606607
}
607608

609+
public static String MaskReportingUrl(String url)
610+
{
611+
String maskedUrl = "";
612+
613+
try
614+
{
615+
String[] pathFields = url.Split('/');
616+
617+
List<String> stripFields = new List<String>(pathFields.Length);
618+
619+
foreach (String field in pathFields)
620+
{
621+
stripFields.Add(Mask(field));
622+
}
623+
624+
maskedUrl = string.Join("/", stripFields);
625+
626+
if (maskedUrl.EndsWith("/"))
627+
{
628+
maskedUrl = maskedUrl.Substring(0, maskedUrl.Length - 1);
629+
}
630+
}
631+
catch
632+
{
633+
// If we had errors, just return what we got.
634+
return url;
635+
}
636+
637+
return maskedUrl;
638+
}
639+
640+
641+
private static readonly Regex ID_REGEX = new Regex("^(\\d+)$", RegexOptions.Compiled);
642+
643+
private static readonly Regex GUID_REGEX =
644+
new Regex("^(?i)(\\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\\b)$", RegexOptions.Compiled);
645+
646+
private static readonly Regex EMAIL_REGEX =
647+
new Regex(
648+
"^((([!#$%&'*+\\-/=?^_`{|}~\\w])|([!#$%&'*+\\-/=?^_`{|}~\\w][!#$%&'*+\\-/=?^_`{|}~\\.\\w]{0,}[!#$%&'*+\\-/=?^_`{|}~\\w]))[@]\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)$",
649+
RegexOptions.Compiled);
650+
651+
private static readonly Regex IP_REGEX =
652+
new Regex(
653+
"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
654+
RegexOptions.Compiled);
655+
656+
private static String Mask(String field)
657+
{
658+
659+
if (ID_REGEX.IsMatch(field))
660+
{
661+
return "{id}";
662+
}
663+
664+
if (GUID_REGEX.IsMatch(field))
665+
{
666+
return "{guid}";
667+
}
668+
669+
if (EMAIL_REGEX.IsMatch(field))
670+
{
671+
return "{email}";
672+
}
673+
674+
if (IP_REGEX.IsMatch(field))
675+
{
676+
return "{ip}";
677+
}
678+
679+
if (field.Contains(';'))
680+
{
681+
return field.Substring(0, field.IndexOf(';'));
682+
}
683+
684+
return field;
685+
}
608686
}
609687

610688
public class ToStringConverter : JsonConverter

Src/StackifyLib/Web/RealUserMonitoring.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ public static class RealUserMonitoring
1111
{
1212
public static string GetHeaderScript()
1313
{
14+
var rumScriptUrl = Config.RumScriptUrl;
15+
var rumKey = Config.RumKey;
16+
17+
if (string.IsNullOrWhiteSpace(rumScriptUrl) || string.IsNullOrWhiteSpace(rumKey))
18+
{
19+
// If we don't have a key and url, don't insert the script
20+
return "";
21+
}
22+
1423
var settings = new JObject();
1524
var reqId = HelperFunctions.GetRequestId();
1625
if (reqId != null)
@@ -33,10 +42,12 @@ public static string GetHeaderScript()
3342
var reportingUrl = HelperFunctions.GetReportingUrl();
3443
if (reportingUrl != null)
3544
{
45+
reportingUrl = HelperFunctions.MaskReportingUrl(reportingUrl);
3646
settings["Trans"] = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(reportingUrl));
3747
}
3848

39-
return string.Format(@"<script type=""text/javascript"">var StackifySettings = {0};</script>", settings.ToString(Formatting.None));
49+
return string.Format(@"<script type=""text/javascript"">(window.StackifySetting || (window.StackifySettings = {0};))</script><script src=""{1}"" data-key=""{2}"" async></script>",
50+
settings.ToString(Formatting.None), rumScriptUrl, rumKey);
4051
}
4152
}
4253
}

0 commit comments

Comments
 (0)