Class AntiSamySAXScanner

java.lang.Object
org.owasp.validator.html.scan.AbstractAntiSamyScanner
org.owasp.validator.html.scan.AntiSamySAXScanner

public class AntiSamySAXScanner extends AbstractAntiSamyScanner
This class allows you to use a SAX scanner to scan HTML rather than a DOM scanner. Its primary purpose is the support incremental scanning of large blocks of HTML using the scan(Reader reader, Writer writer) API so the HTML input and output doesn't all have to be held in memory at the same time. It should not be called directly. All scanning should be done through an AntiSamy.scan() method invocation. The HTML sanitization logic built into AntiSamyDOMScanner is leveraged by this class as well.
Author:
Arshan Dabirsiaghi
  • Constructor Details

    • AntiSamySAXScanner

      public AntiSamySAXScanner(Policy policy)
      Create an instance of this class configured to use the specified policy.
      Parameters:
      policy - The policy to use.
  • Method Details

    • getResults

      public CleanResults getResults()
      The SAXScanner can't remember and return the complete scan results in all cases because the scan(Reader reader, Writer writer) version of the scan() API incrementally scans the input to generate incremental output. So to be safe, this class always returns null for getResults(). As such, when using this class, you can only rely on the CleanResults returned by the scan() method you invoked, or the output from the writer passed into scan(Reader reader, Writer writer).
      Specified by:
      getResults in class AbstractAntiSamyScanner
      Returns:
      always returns null
    • scan

      public CleanResults scan(String html) throws ScanException
      Scan the provided HTML using the AntiSamy policy specified when this Scanner instance was constructed, and return a CleanResult object that contains the sanitized output, along with possibly some error messages and scan time statistics. This version of the scan() API is the one that is typically used.
      Specified by:
      scan in class AbstractAntiSamyScanner
      Parameters:
      html - A String whose contents is to be sanitized per the configured AntiSamy policy.
      Returns:
      A CleanResults object with (possibly) an XMLDocumentFragment object and a String representation of the cleaned HTML, as well as some scan statistics. Note that ONLY the cleaned HTML can be considered trustworthy. The absence of errorMessages in the CleanResults does NOT necessarily indicate the input was safe (i.e., contained no attacks).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
    • scan

      public CleanResults scan(String html, Policy policy) throws ScanException
      This is where the magic lives. Scan the provided HTML and return a CleanResult object that contains the sanitized output, along with possibly some error messages and scan time statistics.
      Parameters:
      html - A String whose contents is to be sanitized per the configured AntiSamy policy.
      policy - The policy to use, overriding the policy specified when this class was instantiated.
      Returns:
      A CleanResults object with (possibly) an XMLDocumentFragment object and a String representation of the cleaned HTML, as well as some scan statistics. Note that ONLY the cleaned HTML can be considered trustworthy. The absence of errorMessages in the CleanResults does NOT necessarily indicate the input was safe (i.e., contained no attacks).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
    • scan

      public CleanResults scan(Reader reader, Writer writer) throws ScanException
      Using a SAX parser, this supports Streams for input and output. The use case is where the input is large and the caller does not need or want the entire string in memory all at one time.
      Parameters:
      reader - A Reader which can feed the SAXParser a little input at a time
      writer - A Writer that can take a little output at a time
      Returns:
      CleanResults where the cleanHtml is null. If a caller wants the HTML as a string, it must capture the contents of the writer (i.e., use a StringWriter).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.