6

MS's AntiXSS (v4.2.1) Sanitizer.GetSafeHtmlFragment(string) method is removing <br> and <br /> tags from my input. Is this supposed to happen? Is there a way around it?

It seems to be removing \n and \r characters too, so I cannot call Replace() after the sanitizer has done its job.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Chaddeus
  • 13,134
  • 29
  • 104
  • 162

1 Answers1

7

The 4.2.x release was motivated by a security vulnerability detected precisely in the HTML sanitizer. More information about this fact:

However, it seems that besides fixing the vulnerability the sanitizer was changed to be much more aggressive to the point of being almost unusable. There is a reported issue about this fact in WPL CodePlex site (GetSafeHtmlFragment replacing all html tags).

If your problem is only with <br> tag and you want to stick with AntiXSS sanitizer then you can implement an ugly workaround resorting to pre-processing your input an then post-process the result of the sanitizer.

Something like this (code for illustrative purposes only):

static void Main(string[] args)
{
    string input = "<br>Hello<br/>World!";

    input = EscapeHtmlBr(input);
    var result = Sanitizer.GetSafeHtmlFragment(input);
    result = UnescapeHtmlBr(result);

    Console.WriteLine(result);
}

const string BrMarker = @"|br|";

private static string UnescapeHtmlBr(string result)
{
    result = result.Replace(BrMarker, "<br />");

    return result;
}

private static string EscapeHtmlBr(string input)
{
    input = input.Replace("<br>", BrMarker);
    input = input.Replace("<br />", BrMarker);
    input = input.Replace("<br/>", BrMarker);

    return input;
}
João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • 2
    Thanks... that was what I was "hoping" for. However, it looks like the AntiXSS library is basically useless now. I wanted to use it on a WYSIWYG text area... it's cleaning up too much. ;( – Chaddeus Jul 08 '12 at 16:34
  • 1
    Just to add, it seems if you want the less aggressive functionality, we need to use the `HTMLsantizationLibary.dll` which is part of the AjaxControlToolkit - http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/HTMLEditorExtender/HTMLEditorExtender.aspx – RemarkLima May 17 '13 at 12:32