Class AntiSamySAXScanner
java.lang.Object
org.owasp.validator.html.scan.AbstractAntiSamyScanner
org.owasp.validator.html.scan.AntiSamySAXScanner
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
-
Field Summary
Fields inherited from class org.owasp.validator.html.scan.AbstractAntiSamyScanner
errorMessages, isNofollowAnchors, isNoopenerAndNoreferrerAnchors, isValidateParamAsEmbed, locale, messages, policy -
Constructor Summary
ConstructorsConstructorDescriptionAntiSamySAXScanner(Policy policy) Create an instance of this class configured to use the specified policy. -
Method Summary
Modifier and TypeMethodDescriptionThe 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.Using a SAX parser, this supports Streams for input and output.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 is where the magic lives.Methods inherited from class org.owasp.validator.html.scan.AbstractAntiSamyScanner
addError, getHTMLSerializer, getOutputFormat, trim
-
Constructor Details
-
AntiSamySAXScanner
Create an instance of this class configured to use the specified policy.- Parameters:
policy- The policy to use.
-
-
Method Details
-
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:
getResultsin classAbstractAntiSamyScanner- Returns:
- always returns null
-
scan
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:
scanin classAbstractAntiSamyScanner- Parameters:
html- A String whose contents is to be sanitized per the configured AntiSamy policy.- Returns:
- A
CleanResultsobject with (possibly) anXMLDocumentFragmentobject 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
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
CleanResultsobject with (possibly) anXMLDocumentFragmentobject 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
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 timewriter- 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.
-