Class FieldValidationService


  • public final class FieldValidationService
    extends java.lang.Object
    Provides a per-type, mode-aware validation of user-editable field values.

    This service is feature-agnostic and can be reused by any form that bypasses the global XSS filter and needs to secure values field by field. It mirrors the two modes of the global XSS filter (see fr.paris.lutece.portal.web.xss.SafeRequestFilter), the mode being provided by the caller:

    • block mode (bSanitizeMode = false) — equivalent to SecurityUtil.containsCleanParameters: an XSSSanitizerException is thrown as soon as the value contains one of the forbidden XSS characters, otherwise the value is returned unchanged;
    • sanitize mode (bSanitizeMode = true) — the value is never rejected; it is checked according to its FieldType, and when it is not valid a default placeholder is returned (invalid_url, invalid_color, invalid_email).

    Two distinct strategies are used, and the naming reflects them: URL, color and email values are validated (they are accepted as-is or replaced by a placeholder, never rewritten), whereas HTML content is sanitized (it is rewritten against an allowlist). sanitizeUrlParameters(String) is the only URL rewriting operation and it is deliberately kept independent: it is never applied by the type-driven validation, a caller that wants a URL both validated and cleaned has to chain the two calls explicitly.

    The active mode and the list of forbidden characters are resolved from the admin XSS filter configuration (lutece.safe.request.admin.sanitizeFilterMode and lutece.safe.request.admin.xssCharacters) by validate(FieldType, String). A caller that manages the mode itself can use validate(FieldType, String, boolean, String) instead.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String sanitizeHtml​(java.lang.String strValue)
      Sanitizes an HTML content with the XSS sanitizer allowlist, falling back to a local allowlist when the advanced sanitizer is unavailable or fails.
      static java.lang.String sanitizeUrlParameters​(java.lang.String strUrl)
      Rebuilds a complete URL by cleaning the values of its query parameters, leaving the base path, the parameter names and the fragment untouched.
      static java.lang.String validate​(FieldType type, java.lang.String strValue)
      Validates a value according to a field type, resolving the active mode and the forbidden characters from the admin XSS filter configuration ("lutece.safe.request.admin.sanitizeFilterMode" and "lutece.safe.request.admin.xssCharacters").
      static java.lang.String validate​(FieldType type, java.lang.String strValue, boolean bSanitizeMode, java.lang.String strXssCharacters)
      Validates a value according to a field type and to a caller-provided XSS filter mode.
      static java.lang.String validateColor​(java.lang.String strValue)
      Validates a CSS color value against the configured pattern.
      static java.lang.String validateEmail​(java.lang.String strValue)
      Validates an email value against the email format.
      static java.lang.String validateUrl​(java.lang.String strValue)
      Validates a URL: the value is rejected when it uses a dangerous scheme (see "portal.sanitizer.url.dangerousSchemes"), when it is not a structurally valid URI, or when one of its query parameter values carries a markup character once URL-decoded.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • validate

        public static java.lang.String validate​(FieldType type,
                                                java.lang.String strValue,
                                                boolean bSanitizeMode,
                                                java.lang.String strXssCharacters)
                                         throws XSSSanitizerException
        Validates a value according to a field type and to a caller-provided XSS filter mode.
        Parameters:
        type - the field type
        strValue - the value to process, already XSS-bypass-decoded when the field type requires it
        bSanitizeMode - true to validate the value against its type (never blocks), false to block on forbidden characters
        strXssCharacters - the list of forbidden XSS characters, used in block mode
        Returns:
        the value to use, validated or unchanged depending on the mode
        Throws:
        XSSSanitizerException - in block mode, when the value contains a forbidden XSS character
      • validateUrl

        public static java.lang.String validateUrl​(java.lang.String strValue)
        Validates a URL: the value is rejected when it uses a dangerous scheme (see "portal.sanitizer.url.dangerousSchemes"), when it is not a structurally valid URI, or when one of its query parameter values carries a markup character once URL-decoded. The value is never rewritten: it is returned trimmed when it is valid, and replaced by "invalid_url" otherwise. Use sanitizeUrlParameters(String) to clean the parameters of a URL instead of rejecting it.
        Parameters:
        strValue - the URL value
        Returns:
        the trimmed URL, or "invalid_url"
      • sanitizeUrlParameters

        public static java.lang.String sanitizeUrlParameters​(java.lang.String strUrl)
        Rebuilds a complete URL by cleaning the values of its query parameters, leaving the base path, the parameter names and the fragment untouched. Each value is URL-decoded, stripped of its markup characters and re-encoded, so that the URL structure (?, &, =, fragment) is preserved.

        This method performs no structural nor scheme validation: it is independent from validateUrl(String) and can be applied to any URL, valid or not.

        Parameters:
        strUrl - the complete URL
        Returns:
        the URL with cleaned query parameter values, unchanged when it carries no query string
      • validateColor

        public static java.lang.String validateColor​(java.lang.String strValue)
        Validates a CSS color value against the configured pattern. Returns "invalid_color" when the format is not recognized.
        Parameters:
        strValue - the color value
        Returns:
        the color value, or "invalid_color"
      • validateEmail

        public static java.lang.String validateEmail​(java.lang.String strValue)
        Validates an email value against the email format. Returns "invalid_email" when the value is not a valid email.
        Parameters:
        strValue - the email value
        Returns:
        the email value, or "invalid_email"
      • sanitizeHtml

        public static java.lang.String sanitizeHtml​(java.lang.String strValue)
        Sanitizes an HTML content with the XSS sanitizer allowlist, falling back to a local allowlist when the advanced sanitizer is unavailable or fails.
        Parameters:
        strValue - the HTML value
        Returns:
        the sanitized HTML