This class encapsulates all request validation logic for I2PTunnelHTTPServer, including hostname validation (detecting attempts to access localhost/loopback), inproxy rejection, referer validation, and user-agent filtering.
Validations are performed based on configuration options set in the tunnel's client options properties.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEncapsulates the result of hostname validation.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BlocklistManagerLists the peer may be blocked by.private final LogLogger for this class.private final PropertiesTunnel options the request arrived on.private final StringBase32 of the peer being checked.Constructor Summary
ConstructorsConstructorDescriptionRequestValidator(Log log, String peerB32, Properties opts, BlocklistManager blocklistManager) Creates a new RequestValidator.Method Summary
All MethodsModifier and TypeMethodDescriptionbuildInproxyRejectLog(Map<String, List<String>> headers) Builds a detailed log message for inproxy rejection.getRefererForLog(Map<String, List<String>> headers) Extracts the referer URL for logging purposes.getUserAgentForCheck(Map<String, List<String>> headers) Extracts the user agent for blacklisting checks.booleanshouldRejectInproxy(Map<String, List<String>> headers) Checks if the request should be rejected based on inproxy headers.booleanshouldRejectReferer(Map<String, List<String>> headers) Checks if the request should be rejected based on referer header.booleanshouldRejectUserAgent(Map<String, List<String>> headers) Checks if the request should be rejected based on user-agent header.validateHostname(String hostname, StringBuilder command) Validates a hostname from an HTTP request.
Field Details
_log
Logger for this class._peerB32
Base32 of the peer being checked._opts
Tunnel options the request arrived on._blocklistManager
Lists the peer may be blocked by.
Constructor Details
RequestValidator
public RequestValidator(Log log, String peerB32, Properties opts, BlocklistManager blocklistManager) Creates a new RequestValidator.- Parameters:
log- the Log instance for logging validation eventspeerB32- the Base32-encoded peer destination for loggingopts- the tunnel client options for validation configurationblocklistManager- the BlocklistManager for logging blocked destinations; may be null
Method Details
validateHostname
Validates a hostname from an HTTP request.This method checks for several security issues:
- Attempts to access localhost or loopback addresses
- DNS blocking (0.0.0.0 or :: responses)
- Non-I2P/non-onion hostnames that resolve to private addresses
Valid hostnames ending in .i2p or .onion are passed through without validation, as these are expected I2P/eepSite or Tor addresses.
- Parameters:
hostname- the Host header value from the request; may be nullcommand- the full request command for blocklist checking- Returns:
- a ValidationResult containing the validation outcome and any error response
shouldRejectInproxy
Checks if the request should be rejected based on inproxy headers.When the OPT_REJECT_INPROXY option is enabled, requests containing X-Forwarded-For, X-Forwarded-Server, Forwarded, or X-Forwarded-Host headers are rejected to prevent proxy abuse.
- Parameters:
headers- the HTTP request headers- Returns:
- true if the request should be rejected, false otherwise
buildInproxyRejectLog
Builds a detailed log message for inproxy rejection.This method extracts all relevant headers for logging purposes when an inproxy request is rejected.
- Parameters:
headers- the HTTP request headers- Returns:
- a formatted string suitable for logging
shouldRejectReferer
Checks if the request should be rejected based on referer header.When the OPT_REJECT_REFERER option is enabled, requests with absolute URIs (starting with http:// or https://) in the Referer header are rejected to prevent information leakage.
- Parameters:
headers- the HTTP request headers- Returns:
- true if the request should be rejected, false otherwise
getRefererForLog
shouldRejectUserAgent
Checks if the request should be rejected based on user-agent header.When the OPT_REJECT_USER_AGENTS option is enabled, requests with blacklisted user agents are rejected. The blacklist is configured via the OPT_USER_AGENTS option as a comma-separated list.
A special case is made for user agents starting with "MYOB" which are always allowed. Also, if the blacklist contains "none", requests without a User-Agent header are rejected.
- Parameters:
headers- the HTTP request headers- Returns:
- true if the request should be rejected, false otherwise
getUserAgentForCheck