Class AntiSamy

java.lang.Object
org.owasp.validator.html.AntiSamy

public class AntiSamy extends Object
This and the CleanResults class are generally the only classes which the outside world should be calling. The scan() method holds the meat and potatoes of AntiSamy. The file contains a number of ways for scan()'ing, depending on the accessibility of the policy file. However, it should be noted that the SAX scan type, which uses a SAX-based parser should be the preferred way of using AntiSamy as it is much more efficient, and generally faster, than the DOM-based parser.
Author:
Arshan Dabirsiaghi
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Designates DOM scan type which calls the DOM parser.
    static final int
    Designates SAX scan type which calls the SAX parser.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    AntiSamy(Policy policy)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    scan(Reader reader, Writer writer, Policy policy)
    Use this method if caller has Streams rather than Strings for I/O.
    scan(String taintedHTML)
    The scan() family of methods are the only methods the outside world should be calling to invoke AntiSamy.
    scan(String taintedHTML, int scanType)
    This method scans the supplied HTML input and produces clean/sanitized results per the previously configured AntiSamy policy using the specified DOM or SAX parser.
    scan(String taintedHTML, File policyFile)
    This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy file using the DOM parser.
    scan(String taintedHTML, String policyFilename)
    This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy file using the DOM parser.
    scan(String taintedHTML, Policy policy)
    This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy using the DOM parser.
    scan(String taintedHTML, Policy policy, int scanType)
    This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy using the specified DOM or SAX parser.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DOM

      public static final int DOM
      Designates DOM scan type which calls the DOM parser.
      See Also:
    • SAX

      public static final int SAX
      Designates SAX scan type which calls the SAX parser.
      See Also:
  • Constructor Details

    • AntiSamy

      public AntiSamy()
    • AntiSamy

      public AntiSamy(Policy policy)
  • Method Details

    • scan

      public CleanResults scan(String taintedHTML) throws ScanException, PolicyException
      The scan() family of methods are the only methods the outside world should be calling to invoke AntiSamy. This is the primary method that most AntiSamy users should be using. This method scans the supplied HTML input and produces clean/sanitized results per the previously configured AntiSamy policy using the SAX parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.
    • scan

      public CleanResults scan(String taintedHTML, int scanType) throws ScanException, PolicyException
      This method scans the supplied HTML input and produces clean/sanitized results per the previously configured AntiSamy policy using the specified DOM or SAX parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      scanType - The type of scan (DOM or SAX).
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.
    • scan

      public CleanResults scan(String taintedHTML, Policy policy) throws ScanException, PolicyException
      This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy using the DOM parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      policy - The custom policy to enforce.
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.
    • scan

      public CleanResults scan(String taintedHTML, Policy policy, int scanType) throws ScanException, PolicyException
      This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy using the specified DOM or SAX parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      policy - The custom policy to enforce.
      scanType - The type of scan (DOM or SAX).
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.
    • scan

      public CleanResults scan(Reader reader, Writer writer, Policy policy) throws ScanException
      Use this method if caller has Streams rather than Strings for I/O. This uses the SAX parser. It is useful for when the input being processed is expected to be very large and we don't validate, but rather simply encode as bytes are consumed from the stream.
      Parameters:
      reader - Reader that produces the input, possibly a little at a time
      writer - Writer that receives the cleaned output, possibly a little at a time
      policy - Policy that directs the scan
      Returns:
      CleanResults where the cleanHtml is null. If caller wants the clean HTML, it must capture the writer's contents. When using Streams, caller generally doesn't want to create a single string containing clean HTML.
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
    • scan

      public CleanResults scan(String taintedHTML, String policyFilename) throws ScanException, PolicyException
      This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy file using the DOM parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      policyFilename - The file name of the custom policy to enforce.
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.
    • scan

      public CleanResults scan(String taintedHTML, File policyFile) throws ScanException, PolicyException
      This method scans the supplied HTML input and produces clean/sanitized results per the supplied AntiSamy policy file using the DOM parser.
      Parameters:
      taintedHTML - Untrusted HTML which may contain malicious code.
      policyFile - The File object of the custom policy to enforce.
      Returns:
      A CleanResults object which contains information about the scan (including the results).
      Throws:
      ScanException - When there is a problem encountered while scanning the HTML input.
      PolicyException - When there is a problem validating or parsing the policy file.