Skip to content

Commit

Permalink
Fix GetNotificationTriggers crashing when PRTG fails to properly HTML…
Browse files Browse the repository at this point in the history
… encode quotes in sensor names (#131)
  • Loading branch information
lordmilko committed Mar 1, 2020
1 parent 4e2c629 commit 43c1a88
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ public async Task NotificationTrigger_ReadOnlyUserAsync()
}
}

[UnitTest]
[TestMethod]
public void NotificationTrigger_DoubleEscapeQuotes()
{
var client = Initialize_Client(new NotificationTriggerResponse(NotificationTriggerItem.StateTrigger(sensorName: "Test \"Name\"")));

var triggers = client.GetNotificationTriggers(1001);
}

private void ChangeTrigger_AllFields_HaveValues(string propertyName, object val)
{
switch (propertyName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static NotificationTriggerItem ChangeTrigger(string onNotificationAction
public static NotificationTriggerItem StateTrigger(string latency = "60", string escLatency = "300", string repeatival = "0",
string offNotificationAction = "-1|None", string escNotificationAction = "-1|None", string nodest = "Down",
string onNotificationAction = "301|Email to all members of group PRTG Users Group", string parentId = "0", string subId = "1",
string typeName = "State Trigger")
string typeName = "State Trigger", string sensorName = "Root")
{
ArgumentNullThrower(latency, nameof(latency));
ArgumentNullThrower(escLatency, nameof(escLatency));
Expand All @@ -49,7 +49,7 @@ public static NotificationTriggerItem StateTrigger(string latency = "60", string
var builder = new StringBuilder();
builder.Append($"{{\"type\":\"state\",\"latency\":\"{latency}\",\"esclatency\":\"{escLatency}\",\"repeatival\":\"{repeatival}\", \"offnotificationid\":\"{offNotificationAction}\", ");
builder.Append($"\"escnotificationid\":\"{escNotificationAction}\",\"nodest\":\"{nodest}\", \"typename\":\"{typeName}\", \"onnotificationid\":\"{onNotificationAction}\", ");
builder.Append($"\"objectlink\":\"<a dependency=\\\"-1000\\\" thisid=\\\"{parentId}\\\" class=\\\"rootgroupmenu isnotpaused isnotfavorite fixed\\\" id=\\\"{parentId}\\\" href=\\\"group.htm?id={parentId}\\\">Root </a>\"}}");
builder.Append($"\"objectlink\":\"<a dependency=\\\"-1000\\\" thisid=\\\"{parentId}\\\" class=\\\"rootgroupmenu isnotpaused isnotfavorite fixed\\\" id=\\\"{parentId}\\\" href=\\\"group.htm?id={parentId}\\\">{sensorName} </a>\"}}");

var item = new NotificationTriggerItem
{
Expand Down
25 changes: 25 additions & 0 deletions src/PrtgAPI/Request/ResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ internal static List<NotificationTrigger> ParseNotificationTriggerResponse(Eithe
Id = Convert.ToInt32(x.Element("objid").Value)
}).ToList();

for (var i = 0; i < xmlResponseContent.Count; i++)
{
var str = xmlResponseContent[i].Content;

var m = Regex.Match(str, "\\\"objectlink\\\":\\\".+?>(.+?)<");

if (m.Success)
{
var parentName = m.Groups[1].Value;

var cleaned = WebUtility.HtmlEncode(parentName);

if (cleaned != parentName)
{
var newStr = str.Substring(0, m.Groups[1].Index) + cleaned + str.Substring(m.Groups[1].Index + m.Groups[1].Length);

xmlResponseContent[i] = new
{
Content = newStr,
Id = xmlResponseContent[i].Id
};
}
}
}

var triggers = JsonDeserializer<NotificationTrigger>.DeserializeList(xmlResponseContent, e => e.Content,
(e, o) =>
{
Expand Down

0 comments on commit 43c1a88

Please sign in to comment.