3

I see in the API that it's possible but I can't figure out how to use that sanitize() method. There's even a forum post where someone says to use it but they don't explain how. In essence I have no idea what CTX means in that method signature. If someone can provide sample code of how to get a list of items that were sanitized that would be appreciated.

9
  • CTX is a generic type in the method. Commented Nov 17, 2017 at 6:39
  • I understand that but I'm still unsure how to use it... Commented Nov 17, 2017 at 6:47
  • You said "I have no idea what CTX means in that method signature.", now you say "I understand that". Which is it? Commented Nov 17, 2017 at 6:49
  • The question is how do you use the method? There's no sample code to look at... Commented Nov 17, 2017 at 6:50
  • What about that sample code right in the beginning of your first link? The one titled Usage? Commented Nov 17, 2017 at 6:56

1 Answer 1

3

You need to setup the HtmlChangeListener to catch all elements that are sanitized. The code then looks something like:

List<String> results = new ArrayList<String>();

HtmlChangeListener<List<String>> htmlChangeListener = new HtmlChangeListener<>()
{
    @Override
    public void discardedTag(List<String> context, String elementName)
    {
        context.add(elementName);
    }

    @Override
    public void discardedAttributes(List<String> context, String tagName, String... attributeNames)
    {
        context.add(tagName);
    }
};

String sanitizedHtml = POLICY_DEFINITION.sanitize(rawHtml, htmlChangeListener, results);
System.out.println("Sanitized elements include: " + String.join(",", results));
Sign up to request clarification or add additional context in comments.

2 Comments

It should be StringUtils.join()
So this can be the indirect way to validate input right? Because the library doesn’t provide it github.com/OWASP/java-html-sanitizer/blob/main/docs/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.