- All Known Implementing Classes:
AbstractValidationPipeline,CharacterValidationStage,DecodingStage,HTTPHeaderValidationPipeline,LengthValidationStage,NormalizationStage,PatternMatchingStage,URLParameterValidationPipeline,URLPathValidationPipeline
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This interface defines the contract for validating HTTP components against security threats. It follows the "String in, String out, throws on violation" pattern consistently across all implementations, enabling clean functional programming patterns and easy composition.
Design Principles
- Functional Interface - Can be used with lambda expressions and method references
- Fail Secure - Throws UrlSecurityException on any security violation
- String/throws Pattern - Simple contract: input string, output string, throws on failure
- Composable - Multiple validators can be chained or combined
- Thread Safe - Implementations should be thread-safe for concurrent use
Usage Examples
// Simple validation
HttpSecurityValidator pathValidator = input -> {
if (input.contains("../")) {
throw UrlSecurityException.builder()
.failureType(UrlSecurityFailureType.PATH_TRAVERSAL_DETECTED)
.validationType(ValidationType.URL_PATH)
.originalInput(input)
.build();
}
return input;
};
// Using with streams
List<String> validPaths = paths.stream()
.map(pathValidator::validate)
.collect(Collectors.toList());
// Composing validators
HttpSecurityValidator compositeValidator = input ->
secondValidator.validate(firstValidator.validate(input));
Implementation Guidelines
- Always validate the most dangerous patterns first
- Provide clear, actionable error messages in exceptions
- Consider performance - validators may be called frequently
- Be consistent with null handling (typically reject null inputs)
- Log security violations appropriately without exposing sensitive data
- Since:
- 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault HttpSecurityValidatorandThen(HttpSecurityValidator after) Creates a composite validator that applies this validator followed by the given validator.default HttpSecurityValidatorcompose(HttpSecurityValidator before) Creates a composite validator that applies the given validator followed by this validator.static HttpSecurityValidatoridentity()Creates an identity validator that always returns the input unchanged.static HttpSecurityValidatorreject(UrlSecurityFailureType failureType, ValidationType validationType) Creates a validator that always rejects input with the specified failure type.Validates the input string and returns the sanitized/normalized version.default HttpSecurityValidatorCreates a validator that applies this validator only if the given predicate is true.
-
Method Details
-
validate
Validates the input string and returns the sanitized/normalized version.This method should examine the input for security violations and either:
- Return the input unchanged if it's safe
- Return a sanitized/normalized version if safe transformations are possible
- Return Optional.empty() if the input was null
- Throw UrlSecurityException if the input represents a security threat
The decision between sanitization and rejection depends on the specific validator and security requirements. Critical security validators should prefer rejection over sanitization to avoid bypasses.
- Parameters:
value- The input to validate. May be null.- Returns:
- The validated, potentially sanitized or normalized value wrapped in Optional. Returns Optional.empty() if the input was null.
- Throws:
UrlSecurityException- If the input represents a security violation that cannot be safely sanitized. The exception should include detailed context about the failure for logging and debugging purposes.IllegalArgumentException- If the input is malformed in a way that prevents security analysis (distinct from security violations).
-
andThen
Creates a composite validator that applies this validator followed by the given validator.This is a convenience method for chaining validators. The resulting validator will:
- Apply this validator to the input
- Apply the
aftervalidator to the result - Return the final result
If either validator throws an exception, the composition stops and the exception is propagated.
- Parameters:
after- The validator to apply after this validator- Returns:
- A composite validator that applies both validators in sequence
- Throws:
NullPointerException- ifafteris null- Since:
- 1.0
-
compose
Creates a composite validator that applies the given validator followed by this validator.This is a convenience method for chaining validators. The resulting validator will:
- Apply the
beforevalidator to the input - Apply this validator to the result
- Return the final result
- Parameters:
before- The validator to apply before this validator- Returns:
- A composite validator that applies both validators in sequence
- Throws:
NullPointerException- ifbeforeis null- Since:
- 1.0
- Apply the
-
when
Creates a validator that applies this validator only if the given predicate is true. If the predicate is false, the input is returned unchanged.This is useful for conditional validation based on input characteristics:
// Only validate non-empty strings HttpSecurityValidator conditionalValidator = validator.when(s -> s != null && !s.isEmpty()); // Only validate strings that look like URLs HttpSecurityValidator urlValidator = validator.when(s -> s.startsWith("http"));- Parameters:
predicate- The condition under which to apply this validator- Returns:
- A conditional validator that only applies this validator when the predicate is true
- Throws:
NullPointerException- ifpredicateis null- Since:
- 1.0
-
identity
Creates an identity validator that always returns the input unchanged. This is useful as a no-op validator or as a starting point for composition.- Returns:
- An identity validator that performs no validation
- Since:
- 1.0
-
reject
static HttpSecurityValidator reject(UrlSecurityFailureType failureType, ValidationType validationType) Creates a validator that always rejects input with the specified failure type. This is useful for creating validators that unconditionally block certain inputs.- Parameters:
failureType- The failure type to use in the exceptionvalidationType- The validation type context- Returns:
- A validator that always throws UrlSecurityException
- Throws:
NullPointerException- if either parameter is null- Since:
- 1.0
-