@ParametersAreNonnullByDefault public final class SanitizedContents extends Object
This should contain utilities that have extremely broad application. More specific utilities should reside with the specific project.
All utilities here should be extremely difficult to abuse in a way that could create attacker-controlled SanitizedContent objects. Java's type system is a great tool to achieve this.
| Modifier and Type | Method and Description |
|---|---|
static SanitizedContent |
concatHtml(SanitizedContent... contents)
Concatenate the contents of multiple
SanitizedContent objects of kind HTML. |
static SanitizedContent |
constantHtml(String constant)
Wraps an assumed-safe constant string that specifies a safe, balanced, document fragment.
|
static SanitizedContent |
constantUri(String constant)
Wraps an assumed-safe URI constant.
|
static SanitizedContent |
emptyString(SanitizedContent.ContentKind kind)
Creates an empty string constant.
|
static SanitizedContent |
fromResource(Class<?> contextClass,
String resourceName,
Charset charset,
SanitizedContent.ContentKind kind)
Loads assumed-safe content from a Java resource.
|
static SanitizedContent |
fromResource(String resourceName,
Charset charset,
SanitizedContent.ContentKind kind)
Loads assumed-safe content from a Java resource.
|
static SanitizedContent |
fromSafeHtml(com.google.common.html.types.SafeHtml html)
Converts a
SafeHtml into a Soy SanitizedContent of kind HTML. |
static SanitizedContent |
fromSafeHtmlProto(com.google.common.html.types.SafeHtmlProto html)
Converts a
SafeHtmlProto into a Soy SanitizedContent of kind HTML. |
static SanitizedContent |
fromSafeScript(com.google.common.html.types.SafeScript script)
Converts a
SafeScript into a Soy SanitizedContent of kind JS. |
static SanitizedContent |
fromSafeScriptProto(com.google.common.html.types.SafeScriptProto script)
Converts a
SafeScriptProto into a Soy SanitizedContent of kind JS. |
static SanitizedContent |
fromSafeStyle(com.google.common.html.types.SafeStyle style)
Converts a
SafeStyle into a Soy SanitizedContent of kind CSS. |
static SanitizedContent |
fromSafeStyleProto(com.google.common.html.types.SafeStyleProto style)
Converts a
SafeStyleProto into a Soy SanitizedContent of kind CSS. |
static SanitizedContent |
fromSafeStyleSheet(com.google.common.html.types.SafeStyleSheet styleSheet)
Converts a
SafeStyleSheet into a Soy SanitizedContent of kind CSS. |
static SanitizedContent |
fromSafeStyleSheetProto(com.google.common.html.types.SafeStyleSheetProto styleSheet)
Converts a
SafeStyleSheetProto into a Soy SanitizedContent of kind CSS. |
static SanitizedContent |
fromSafeUrl(com.google.common.html.types.SafeUrl url)
Converts a
SafeUrl into a Soy SanitizedContent of kind URI. |
static SanitizedContent |
fromSafeUrlProto(com.google.common.html.types.SafeUrlProto url)
Converts a
SafeUrlProto into a Soy SanitizedContent of kind URI. |
static SanitizedContent |
fromTrustedResourceUrl(com.google.common.html.types.TrustedResourceUrl url)
Converts a
TrustedResourceUrl into a Soy SanitizedContent of kind
TRUSTED_RESOURCE_URI. |
static SanitizedContent |
fromTrustedResourceUrlProto(com.google.common.html.types.TrustedResourceUrlProto url)
Converts a
TrustedResourceUrlProto into a Soy SanitizedContent of kind
TRUSTED_RESOURCE_URI. |
static SanitizedContent |
unsanitizedText(String text)
Creates a SanitizedContent object of kind TEXT and unknown direction.
|
static SanitizedContent |
unsanitizedText(String text,
Dir dir)
Creates a SanitizedContent object of kind TEXT of a given direction (null if unknown).
|
public static SanitizedContent emptyString(SanitizedContent.ContentKind kind)
public static SanitizedContent unsanitizedText(String text, @Nullable Dir dir)
This is useful when stubbing out a function that needs to create a SanitizedContent object.
public static SanitizedContent unsanitizedText(String text)
This is useful when stubbing out a function that needs to create a SanitizedContent object.
public static SanitizedContent concatHtml(SanitizedContent... contents)
SanitizedContent objects of kind HTML.contents - The HTML content to combine.public static SanitizedContent fromResource(Class<?> contextClass, String resourceName, Charset charset, SanitizedContent.ContentKind kind) throws IOException
This performs ZERO VALIDATION of the data, and takes you on your word that the input is valid. We assume that resources should be safe because they are part of the binary, and therefore not attacker controlled, unless the source code is compromised (in which there's nothing we can do).
contextClass - Class relative to which to load the resource.resourceName - The name of the resource, relative to the context class.charset - The character set to use, usually Charsets.UTF_8.kind - The content kind of the resource.IOExceptionpublic static SanitizedContent fromResource(String resourceName, Charset charset, SanitizedContent.ContentKind kind) throws IOException
This performs ZERO VALIDATION of the data, and takes you on your word that the input is valid. We assume that resources should be safe because they are part of the binary, and therefore not attacker controlled, unless the source code is compromised (in which there's nothing we can do).
resourceName - The name of the resource to be found using context class loader.charset - The character set to use, usually Charsets.UTF_8.kind - The content kind of the resource.IOExceptionpublic static SanitizedContent constantUri(@CompileTimeConstant String constant)
This only accepts compile-time constants, based on the assumption that URLs that are controlled by the application (and not user input) are considered safe.
public static SanitizedContent constantHtml(@CompileTimeConstant String constant)
This only accepts compile-time constants, based on the assumption that HTML snippets that are controlled by the application (and not user input) are considered safe.
public static SanitizedContent fromSafeHtml(com.google.common.html.types.SafeHtml html)
SafeHtml into a Soy SanitizedContent of kind HTML.public static SanitizedContent fromSafeHtmlProto(com.google.common.html.types.SafeHtmlProto html)
SafeHtmlProto into a Soy SanitizedContent of kind HTML.public static SanitizedContent fromSafeScript(com.google.common.html.types.SafeScript script)
SafeScript into a Soy SanitizedContent of kind JS.public static SanitizedContent fromSafeScriptProto(com.google.common.html.types.SafeScriptProto script)
SafeScriptProto into a Soy SanitizedContent of kind JS.public static SanitizedContent fromSafeStyle(com.google.common.html.types.SafeStyle style)
SafeStyle into a Soy SanitizedContent of kind CSS.public static SanitizedContent fromSafeStyleProto(com.google.common.html.types.SafeStyleProto style)
SafeStyleProto into a Soy SanitizedContent of kind CSS.public static SanitizedContent fromSafeStyleSheet(com.google.common.html.types.SafeStyleSheet styleSheet)
SafeStyleSheet into a Soy SanitizedContent of kind CSS.public static SanitizedContent fromSafeStyleSheetProto(com.google.common.html.types.SafeStyleSheetProto styleSheet)
SafeStyleSheetProto into a Soy SanitizedContent of kind CSS.public static SanitizedContent fromSafeUrl(com.google.common.html.types.SafeUrl url)
SafeUrl into a Soy SanitizedContent of kind URI.public static SanitizedContent fromSafeUrlProto(com.google.common.html.types.SafeUrlProto url)
SafeUrlProto into a Soy SanitizedContent of kind URI.public static SanitizedContent fromTrustedResourceUrl(com.google.common.html.types.TrustedResourceUrl url)
TrustedResourceUrl into a Soy SanitizedContent of kind
TRUSTED_RESOURCE_URI.public static SanitizedContent fromTrustedResourceUrlProto(com.google.common.html.types.TrustedResourceUrlProto url)
TrustedResourceUrlProto into a Soy SanitizedContent of kind
TRUSTED_RESOURCE_URI.