Class FieldValidationService
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 toSecurityUtil.containsCleanParameters: anXSSSanitizerExceptionis 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 itsFieldType, 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
Modifier and TypeMethodDescriptionstatic StringsanitizeHtml(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 StringsanitizeUrlParameters(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 StringValidates 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 StringValidates a value according to a field type and to a caller-provided XSS filter mode.static StringvalidateColor(String strValue) Validates a CSS color value against the configured pattern.static StringvalidateEmail(String strValue) Validates an email value against the email format.static StringvalidateUrl(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.
-
Method Details
-
validate
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").- Parameters:
type- the field typestrValue- the value to process, already XSS-bypass-decoded when the field type requires it- 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
-
validate
public static String validate(FieldType type, String strValue, boolean bSanitizeMode, String strXssCharacters) throws XSSSanitizerException Validates a value according to a field type and to a caller-provided XSS filter mode.- Parameters:
type- the field typestrValue- the value to process, already XSS-bypass-decoded when the field type requires itbSanitizeMode-trueto validate the value against its type (never blocks),falseto block on forbidden charactersstrXssCharacters- 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
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. UsesanitizeUrlParameters(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
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
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
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
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
-