Class SvgWhitelistValidator
Unlike a sanitizer, this validator does not rewrite or strip content from the
input: it performs a read-only pass over the XML structure and either accepts
the document as-is, or rejects it entirely with a descriptive
SvgWhitelistValidator.SvgValidationException. This makes the outcome predictable and avoids
the risk of a sanitizer silently producing a subtly different (and
potentially still unsafe) document.
What is checked
- Well-formedness — the input must be valid XML.
- Root element — must be
<svg>. - Elements — every element's local name must be present in
ALLOWED_ELEMENTS(e.g.<script>,<foreignObject>,<iframe>are rejected). - Namespaces — every element and attribute must belong to one of
ALLOWED_NAMESPACES(SVG, XLink, or no namespace). - Attributes — every attribute's local (or prefixed) name must be
present in
ALLOWED_ATTRIBUTES. Event-handler attributes (on*, e.g.onload,onclick) are explicitly rejected regardless of the whitelist. href/xlink:hrefvalues — must matchALLOWED_HREF: either a local fragment reference (#someId) or adata:image/...URI. External URLs (e.g.https://...) are rejected.- Attribute value content — any attribute value containing
javascript:orexpression(is rejected, regardless of which attribute it appears on. - DOCTYPE / external entities (XXE) — the underlying
XMLInputFactoryis configured withSUPPORT_DTD=falseandIS_SUPPORTING_EXTERNAL_ENTITIES=false; any DOCTYPE declaration that nonetheless surfaces is rejected. - Processing instructions — e.g.
<?xml-stylesheet ?>are rejected.
What is NOT checked
- The textual content of elements such as
<text>/<tspan>is not inspected (it is not executable in SVG by itself). - XML comments are not inspected.
- The
styleattribute and<style>element are not part of the whitelist and will therefore cause rejection — CSS-based attacks (e.g.url(javascript:...)insidestyle) are out of scope precisely becausestyleis never allowed. - Recursive structures (e.g.
<use>referencing itself, or deeply nested<svg>) are not depth-limited.
Usage
// Detailed validation with error message:
try (InputStream in = uploadedFile.getInputStream()) {
SvgWhitelistValidator.validate(in);
// accepted -> store the original bytes as-is
} catch (SvgWhitelistValidator.SvgValidationException e) {
// rejected -> e.getMessage() describes the offending element/attribute
}
// Simple boolean check:
if (SvgWhitelistValidator.isValidSvg(uploadedFile)) {
// accepted
}
Important: because this class does not rewrite content, callers must
store the original, unmodified file/bytes only if validation succeeds. Do not
attempt to reconstruct the document from the
XMLStreamReader.
This class is stateless and thread-safe; all allow-lists are immutable and shared across invocations.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisValidSvg(File svgFile) Convenience method: returnstrueif the given file is a valid SVG according to the whitelist,falseotherwise (including I/O errors, malformed XML, or any disallowed element/attribute/value).static voidvalidate(InputStream svgInput) Validates an SVG document against a strict whitelist of elements, attributes, namespaces and attribute values.
-
Method Details
-
validate
public static void validate(InputStream svgInput) throws SvgWhitelistValidator.SvgValidationException Validates an SVG document against a strict whitelist of elements, attributes, namespaces and attribute values.- Throws:
SvgWhitelistValidator.SvgValidationException- if the document is malformed, or contains any element/attribute/value that is not explicitly allowed.
-
isValidSvg
Convenience method: returnstrueif the given file is a valid SVG according to the whitelist,falseotherwise (including I/O errors, malformed XML, or any disallowed element/attribute/value).Does not throw — suitable for simple boolean checks (e.g. form validation). For detailed error reporting, call
validate(InputStream)directly.
-