Class SvgWhitelistValidator

java.lang.Object
com.jalios.jcms.security.SvgWhitelistValidator

public class SvgWhitelistValidator extends Object
Validates SVG documents against a strict allow-list of elements, attributes, namespaces and attribute values, with the goal of preventing XSS, XXE and related injection attacks via uploaded SVG files.

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:href values — must match ALLOWED_HREF: either a local fragment reference (#someId) or a data:image/... URI. External URLs (e.g. https://...) are rejected.
  • Attribute value content — any attribute value containing javascript: or expression( is rejected, regardless of which attribute it appears on.
  • DOCTYPE / external entities (XXE) — the underlying XMLInputFactory is configured with SUPPORT_DTD=false and IS_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 style attribute and <style> element are not part of the whitelist and will therefore cause rejection — CSS-based attacks (e.g. url(javascript:...) inside style) are out of scope precisely because style is 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.

  • 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

      public static boolean isValidSvg(File svgFile)
      Convenience method: returns true if the given file is a valid SVG according to the whitelist, false otherwise (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.