This class provides static methods for adding security-related HTTP headers to server responses. It handles:
- Referrer-Policy header
- Allow header
- Cache-Control header with appropriate policies
- X-XSS-Protection header
- X-Content-Type-Options header
Some headers are only added based on the MIME type of the response content. For example, Referrer-Policy and Allow headers are only added for HTML content. Cache-Control policies vary based on whether the content is static (immutable) or dynamic.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]Cookie strings that should be filtered from Set-Cookie headers.Constructor Summary
ConstructorsConstructorDescriptionEvery add* method is static and the only field is the cookie filter list, so an instance carries nothing.Method Summary
Modifier and TypeMethodDescriptionprivate static voidAdds Allow header if appropriate and not already present.private static voidAdds or modifies Cache-Control header based on MIME type.private static voidaddNoSniffHeader(Map<String, List<String>> headers) Adds X-Content-Type-Options header if not already present.private static voidAdds Referrer-Policy header if appropriate and not already present.static voidAdds all appropriate security headers to a response.private static voidaddXSSProtectionHeader(Map<String, List<String>> headers) Adds X-XSS-Protection header if not already present.static voidfilterCacheControlHeaders(Map<String, List<String>> headers) Filters Cache-Control headers to remove problematic values.static voidfilterSetCookieHeaders(Map<String, List<String>> headers) Filters Set-Cookie headers to remove unwanted cookies.
Field Details
COOKIE_STRINGS
Cookie strings that should be filtered from Set-Cookie headers.
Constructor Details
SecurityHeaderBuilder
public SecurityHeaderBuilder()Every add* method is static and the only field is the cookie filter list, so an instance carries nothing.
Method Details
addSecurityHeaders
Adds all appropriate security headers to a response.This method adds security headers based on the MIME type of the response. Headers are only added if they are not already present in the response.
The following headers may be added:
- Referrer-Policy: same-origin (for HTML/XML/JSON content)
- Allow: GET, POST, HEAD (for HTML/XML/JSON content)
- Cache-Control (appropriate policy based on MIME type)
- X-XSS-Protection: 1; mode=block
- X-Content-Type-Options: nosniff
- Parameters:
headers- the HTTP response headers map to modifymimeType- the MIME type of the response content; may be null
addReferrerPolicyHeader
Adds Referrer-Policy header if appropriate and not already present.The Referrer-Policy header is only added for MIME types in the custom whitelist (text/html, application/xhtml+xml, application/xml, text/plain, application/json).
Sets: Referrer-Policy: same-origin.
- Parameters:
headers- the HTTP response headers mapmimeType- the response MIME type
addAllowHeader
Adds Allow header if appropriate and not already present.The Allow header is only added for MIME types in the custom whitelist.
Sets: Allow: GET, POST, HEAD.
- Parameters:
headers- the HTTP response headers mapmimeType- the response MIME type
addCacheControlHeader
Adds or modifies Cache-Control header based on MIME type.Cache-Control policy is determined as follows:
- For immutable content (images, fonts, video, audio): Cache-Control: private, max-age=31536000, immutable
- For other content: Cache-Control: private, no-cache, max-age=604800
- Existing Cache-Control headers are preserved unless they contain "none" or "post-check" which are stripped
If an existing Cache-Control contains "no-cache" and the content is immutable, it is upgraded to immutable caching.
- Parameters:
headers- the HTTP response headers mapmimeType- the response MIME type
addXSSProtectionHeader
addNoSniffHeader
Adds X-Content-Type-Options header if not already present.Sets: X-Content-Type-Options: nosniff
This header prevents browsers from MIME-sniffing a response away from the declared content-type.
- Parameters:
headers- the HTTP response headers map
filterSetCookieHeaders
Filters Set-Cookie headers to remove unwanted cookies.This method removes Set-Cookie headers containing specific strings (STYXKEY, visited=yes) that are known to be irrelevant or unwanted for I2P HTTP proxying.
- Parameters:
headers- the HTTP response headers map
filterCacheControlHeaders
Filters Cache-Control headers to remove problematic values.This method removes "none" and "post-check" values from Cache-Control headers, as these can cause issues with I2P HTTP proxying.
- Parameters:
headers- the HTTP response headers map