Class Policy

java.lang.Object
org.owasp.validator.html.Policy
Direct Known Subclasses:
InternalPolicy

public class Policy extends Object
This class holds the model for our policy engine.

## Schema validation behavior change starting with AntiSamy 1.6.0 ##

Prior to v1.6.0 AntiSamy was not actually enforcing it's defined XSD. For all of v1.6.x, by default AntiSamy enforced the schema, and wouldn't continue if the AntiSamy policy was invalid. However, we recognized that it might not be possible for developers to fix their AntiSamy policies right away so we provided two ways to (temporarily!) disable schema validation. Via a direct method call and via a System property.

## Starting with AntiSamy 1.7.0, schema validation is Mandatory.

Logging: The logging introduced in 1.6+ uses slf4j. But AntiSamy doesn't actually include an slf4j implementation library. AntiSamy users must import and properly configure an slf4j logging library if they want to see the very few log messages generated by AntiSamy.

Author:
Arshan Dabirsiaghi
  • Field Details

  • Constructor Details

  • Method Details

    • getTagByLowercaseName

      public Tag getTagByLowercaseName(String tagName)
      Get the Tag specified by the provided tag name.
      Parameters:
      tagName - The name of the Tag to return.
      Returns:
      The requested Tag, or null if it doesn't exist.
    • getPropertyByName

      public Property getPropertyByName(String propertyName)
      Retrieves a CSS Property from the Policy.
      Parameters:
      propertyName - The name of the CSS Property to look up.
      Returns:
      The CSS Property associated with the name specified, or null if none is found.
    • getInstance

      public static Policy getInstance() throws PolicyException
      Construct a Policy using the default policy file location ("antisamy.xml").
      Returns:
      A populated Policy object based on the XML policy file located in the default location.
      Throws:
      PolicyException - If the file is not found or there is a problem parsing the file.
    • getInstance

      public static Policy getInstance(String filename) throws PolicyException
      Construct a Policy based on the file whose name is passed in.
      Parameters:
      filename - The path to the XML policy file.
      Returns:
      A populated Policy object based on the XML policy file located in the location passed in.
      Throws:
      PolicyException - If the file is not found or there is a problem parsing the file.
    • getInstance

      public static Policy getInstance(InputStream inputStream) throws PolicyException
      Construct a Policy from the InputStream object passed in.
      Parameters:
      inputStream - An InputStream which contains the XML policy information.
      Returns:
      A populated Policy object based on the XML policy file pointed to by the inputStream parameter.
      Throws:
      PolicyException - If there is a problem parsing the input stream.
    • getInstance

      public static Policy getInstance(File file) throws PolicyException
      Construct a Policy from the File object passed in.
      Parameters:
      file - A File object which contains the XML policy information.
      Returns:
      A populated Policy object based on the XML policy file pointed to by the File parameter.
      Throws:
      PolicyException - If the file is not found or there is a problem parsing the file.
    • getInstance

      public static Policy getInstance(URL url) throws PolicyException
      Construct a Policy from the target of the URL passed in.

      NOTE: This is the only factory method that will work with <include> tags in AntiSamy policy files.

      For security reasons, the provided URL must point to a local file. Currently only 'file:' and 'jar:' URL prefixes are allowed. If you want to use a different URL format, and are confident that the URL points to a safe source, you can open the target of the URL with URL.openStream(), and use the getInstance(InputStream) constructor instead. For example, Spring has classpath: and Wildfly/Jboss supports vfs: for accessing local files. Just be aware that this alternate constructor doesn't support the use of <include> tags, per the NOTE above.
      Parameters:
      url - A URL object which contains the XML policy information.
      Returns:
      A populated Policy object based on the XML policy file pointed to by the File parameter.
      Throws:
      PolicyException - If the file is not found or there is a problem parsing the file.
    • getSimpleParseContext

      protected static Policy.ParseContext getSimpleParseContext(Element topLevelElement) throws PolicyException
      Throws:
      PolicyException
    • getParseContext

      protected static Policy.ParseContext getParseContext(Element topLevelElement, URL baseUrl) throws PolicyException
      Throws:
      PolicyException
    • getTopLevelElement

      protected static Element getTopLevelElement(URL baseUrl) throws PolicyException
      Throws:
      PolicyException
    • getSourceFromUrl

      protected static InputSource getSourceFromUrl(URL baseUrl) throws PolicyException
      Throws:
      PolicyException
    • getTopLevelElement

      protected static Element getTopLevelElement(InputSource source, Callable<InputSource> getResetSource) throws PolicyException
      Throws:
      PolicyException
    • cloneWithDirective

      public Policy cloneWithDirective(String name, String value)
      Creates a copy of this policy with an added/changed directive.
      Parameters:
      name - The directive to add/modify
      value - The value
      Returns:
      A clone of the policy with the updated directive
    • getGlobalAttributeByName

      public Attribute getGlobalAttributeByName(String name)
      A simple method for returning on of the <global-attribute> entries by name.
      Parameters:
      name - The name of the global-attribute we want to look up.
      Returns:
      An Attribute associated with the global-attribute lookup name specified.
    • getDynamicAttributeByName

      public Attribute getDynamicAttributeByName(String name)
      A method for returning one of the dynamic <global-attribute> entries by name.
      Parameters:
      name - The name of the dynamic global-attribute we want to look up.
      Returns:
      An Attribute associated with the global-attribute lookup name specified, or null if not found.
    • getAllowedEmptyTags

      public TagMatcher getAllowedEmptyTags()
      Return all the allowed empty tags configured in the Policy.
      Returns:
      A String array of all the he allowed empty tags configured in the Policy.
    • getRequiresClosingTags

      public TagMatcher getRequiresClosingTags()
      Return all the tags that are required to be closed with an end tag, even if they have no child content.
      Returns:
      A String array of all the tags that are required to be closed with an end tag, even if they have no child content.
    • getDirective

      public String getDirective(String name)
      Return a directive value based on a lookup name.
      Parameters:
      name - The name of the directive we want to look up.
      Returns:
      A String object containing the directive associated with the lookup name, or null if none is found.
    • resolveEntity

      public static InputSource resolveEntity(String systemId, URL baseUrl) throws IOException, SAXException
      Resolves public and system IDs to files stored within the JAR.
      Parameters:
      systemId - The name of the entity we want to look up.
      baseUrl - The base location of the entity.
      Returns:
      A String object containing the directive associated with the lookup name, or null if none is found.
      Throws:
      IOException - if the specified URL can't be opened.
      SAXException - This exception can't actually be thrown, but left in the method signature for API compatibility reasons.
    • getCommonRegularExpressions

      public AntiSamyPattern getCommonRegularExpressions(String name)