java.lang.Object
java.lang.Record
de.cuioss.http.security.validation.LengthValidationStage
- All Implemented Interfaces:
HttpSecurityValidator
public record LengthValidationStage(SecurityConfiguration config, ValidationType validationType)
extends Record
implements HttpSecurityValidator
Length validation stage with configurable limits for HTTP components.
This stage enforces length limits on various HTTP components to prevent denial-of-service attacks, buffer overflow attempts, and resource exhaustion. The stage validates input length against component-specific limits:
- Path Length Validation - Enforces maximum URL path length
- Parameter Length Validation - Validates parameter names and values
- Header Length Validation - Checks header names and values
- Cookie Length Validation - Validates cookie names and values
- Body Size Validation - Enforces request/response body size limits
Design Principles
- DoS Protection - Prevents resource exhaustion through size limits
- Context-Sensitive - Different limits for different HTTP components
- Performance Optimized - Simple length checks with O(1) complexity
- RFC Compliant - Follows HTTP specification recommendations
Security Validations
- Path Length - Prevents extremely long URL paths
- Parameter Length - Limits parameter name and value sizes
- Header Length - Enforces HTTP header size restrictions
- Cookie Length - Validates cookie name and value sizes
- Body Size - Prevents large payload attacks
Usage Examples
// Create length validation stage
SecurityConfiguration config = SecurityConfiguration.defaults();
LengthValidationStage lengthValidator = new LengthValidationStage(config, ValidationType.URL_PATH);
// Validate path length
try {
lengthValidator.validate("/api/users/123"); // Passes if within limit
} catch (UrlSecurityException e) {
logger.warn("Path too long: {}", e.getFailureType());
}
// Validate parameter value
LengthValidationStage paramValidator = new LengthValidationStage(config, ValidationType.PARAMETER_VALUE);
try {
paramValidator.validate("very_long_parameter_value"); // May fail if too long
} catch (UrlSecurityException e) {
logger.warn("Parameter value too long: {}", e.getDetail());
}
// Validate with custom limits
SecurityConfiguration strictConfig = SecurityConfiguration.builder()
.maxPathLength(1024)
.maxParameterValueLength(256)
.build();
LengthValidationStage strictValidator = new LengthValidationStage(strictConfig, ValidationType.URL_PATH);
Performance Characteristics
- O(1) time complexity - simple length comparison
- Minimal memory overhead - no string manipulation
- Early termination on limit exceeded
- No regex or pattern matching overhead
Configuration Dependencies
- maxPathLength - Maximum allowed path length
- maxParameterNameLength/maxParameterValueLength - Parameter size limits
- maxHeaderNameLength/maxHeaderValueLength - Header size limits
- maxCookieNameLength/maxCookieValueLength - Cookie size limits
- maxBodySize - Maximum body size in bytes
Implements: Task V4 from HTTP verification specification
- Since:
- 1.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLengthValidationStage(SecurityConfiguration config, ValidationType validationType) Creates an instance of aLengthValidationStagerecord class. -
Method Summary
Modifier and TypeMethodDescriptionconfig()Returns the value of theconfigrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.final StringtoString()Returns a string representation of this record class.Validates input length against component-specific limits.Returns the value of thevalidationTyperecord component.Creates a conditional validator that only processes inputs matching the condition.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface de.cuioss.http.security.core.HttpSecurityValidator
andThen, compose
-
Constructor Details
-
LengthValidationStage
Creates an instance of aLengthValidationStagerecord class.- Parameters:
config- the value for theconfigrecord componentvalidationType- the value for thevalidationTyperecord component
-
-
Method Details
-
validate
Validates input length against component-specific limits.Processing logic:
- Input validation - handles null/empty inputs
- Length calculation - gets input length in characters or bytes
- Limit lookup - determines appropriate limit based on validation type
- Comparison - checks if input exceeds configured limit
- Specified by:
validatein interfaceHttpSecurityValidator- Parameters:
value- The input string to validate for length limits- Returns:
- The original input wrapped in Optional if validation passes, Optional.empty() if input was null
- Throws:
UrlSecurityException- if length limits are exceeded:- PATH_TOO_LONG - if URL path exceeds maximum length
- INPUT_TOO_LONG - if other components exceed their limits
-
when
Creates a conditional validator that only processes inputs matching the condition.- Specified by:
whenin interfaceHttpSecurityValidator- Parameters:
condition- The condition to test before validation- Returns:
- A conditional HttpSecurityValidator that applies length validation conditionally
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
config
Returns the value of theconfigrecord component.- Returns:
- the value of the
configrecord component
-
validationType
Returns the value of thevalidationTyperecord component.- Returns:
- the value of the
validationTyperecord component
-