001/* 002 * Copyright 2022-2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import com.soklet.internal.spring.LinkedCaseInsensitiveMap; 020import org.jspecify.annotations.NonNull; 021import org.jspecify.annotations.Nullable; 022 023import javax.annotation.concurrent.NotThreadSafe; 024import java.net.InetAddress; 025import java.net.InetSocketAddress; 026import java.util.List; 027import java.util.Map; 028import java.util.Optional; 029import java.util.Set; 030import java.util.function.Predicate; 031 032import static java.util.Objects.requireNonNull; 033 034/** 035 * Resolves a client's effective origin (scheme + host + optional port) from request headers. 036 * <p> 037 * Forwarded headers can be spoofed if Soklet is reachable directly. Choose a {@link TrustPolicy} that matches your 038 * deployment and, for {@link TrustPolicy#TRUST_PROXY_ALLOWLIST}, provide a trusted proxy predicate or allowlist. 039 * If the remote address is missing or not trusted, forwarded headers are ignored. 040 * <p> 041 * Extraction order is: trusted forwarded headers → {@code Host} → (optional) {@code Origin} fallback. 042 * Under {@link TrustPolicy#TRUST_PROXY_ALLOWLIST}, origin metadata comes from the nearest proxy 043 * (the rightmost entry); earlier entries cannot supply missing or invalid values. {@link TrustPolicy#TRUST_ALL} 044 * retains leftmost selection. An absolute request target supplies the effective {@code Host} and fallback scheme. {@code Origin} 045 * never overrides a conflicting host value; it only fills missing scheme/port or supplies host when absent. 046 * <p> 047 * Trusted {@code Forwarded host=} and {@code X-Forwarded-Host} values are validated against the same host grammar 048 * Soklet applies to {@code Host}; invalid forwarded host values are ignored. 049 * <p> 050 * This resolves origin metadata, not an application URL allowlist. Trusted 051 * forwarded protocols are parsed as URI schemes; applications using the value 052 * for HTTP redirects or outbound requests must apply their intended scheme policy. 053 * <p> 054 * Defaults: if {@link #allowOriginFallback(Boolean)} is left unset, {@code Origin} fallback is enabled only for 055 * {@link TrustPolicy#TRUST_ALL}; otherwise it is disabled. 056 */ 057@NotThreadSafe 058public final class EffectiveOriginResolver { 059 @NonNull 060 private final Map<@NonNull String, @NonNull List<@NonNull String>> headers; 061 @NonNull 062 private final TrustPolicy trustPolicy; 063 @Nullable 064 private InetSocketAddress remoteAddress; 065 @Nullable 066 private Predicate<@NonNull InetSocketAddress> trustedProxyPredicate; 067 @Nullable 068 private Boolean allowOriginFallback; 069 @Nullable 070 private String requestTargetScheme; 071 072 /** 073 * Acquires a resolver seeded with raw request headers and a trust policy. 074 * 075 * @param headers HTTP request headers 076 * @param trustPolicy how forwarded headers should be trusted 077 * @return the resolver 078 */ 079 @NonNull 080 public static EffectiveOriginResolver withHeaders(@NonNull Map<@NonNull String, @NonNull List<@NonNull String>> headers, 081 @NonNull TrustPolicy trustPolicy) { 082 requireNonNull(headers); 083 requireNonNull(trustPolicy); 084 return new EffectiveOriginResolver(headers, trustPolicy); 085 } 086 087 /** 088 * Acquires a resolver seeded with a {@link Request} and a trust policy. 089 * 090 * @param request the current request 091 * @param trustPolicy how forwarded headers should be trusted 092 * @return the resolver 093 */ 094 @NonNull 095 public static EffectiveOriginResolver withRequest(@NonNull Request request, 096 @NonNull TrustPolicy trustPolicy) { 097 requireNonNull(request); 098 EffectiveOriginResolver resolver = withHeaders(request.getHeaders(), trustPolicy); 099 resolver.remoteAddress = request.getRemoteAddress().orElse(null); 100 resolver.requestTargetScheme = request.getRequestTargetScheme(); 101 return resolver; 102 } 103 104 private EffectiveOriginResolver(@NonNull Map<@NonNull String, @NonNull List<@NonNull String>> headers, 105 @NonNull TrustPolicy trustPolicy) { 106 this.headers = Utilities.immutableValueLists(headers, true); 107 this.trustPolicy = trustPolicy; 108 } 109 110 /** 111 * Resolves the effective origin. 112 * 113 * @return the effective origin, or {@link Optional#empty()} if it could not be determined 114 * @throws IllegalStateException if {@link TrustPolicy#TRUST_PROXY_ALLOWLIST} is selected without a trusted-proxy predicate or allowlist 115 */ 116 @NonNull 117 public Optional<@NonNull String> resolve() { 118 return Utilities.extractEffectiveOrigin(this); 119 } 120 121 /** 122 * The remote address of the client connection. 123 * 124 * @param remoteAddress the remote address, or {@code null} if unavailable 125 * @return this resolver 126 */ 127 @NonNull 128 public EffectiveOriginResolver remoteAddress(@Nullable InetSocketAddress remoteAddress) { 129 this.remoteAddress = remoteAddress; 130 return this; 131 } 132 133 /** 134 * Replaces the predicate used when {@link TrustPolicy#TRUST_PROXY_ALLOWLIST} is in effect. 135 * 136 * @param trustedProxyPredicate predicate that returns {@code true} for trusted proxies, or {@code null} to clear the 137 * configured predicate; resolving with {@link TrustPolicy#TRUST_PROXY_ALLOWLIST} then 138 * fails until a predicate or allowlist is configured 139 * @return this resolver 140 */ 141 @NonNull 142 public EffectiveOriginResolver trustedProxyPredicate( 143 @Nullable Predicate<@NonNull InetSocketAddress> trustedProxyPredicate) { 144 this.trustedProxyPredicate = trustedProxyPredicate; 145 return this; 146 } 147 148 /** 149 * Replaces the configured trusted-proxy predicate with one backed by the supplied IP allowlist. 150 * 151 * @param trustedProxyAddresses replacement IP addresses of trusted proxies; an empty set trusts no proxies 152 * @return this resolver 153 */ 154 @NonNull 155 public EffectiveOriginResolver trustedProxyAddresses(@NonNull Set<@NonNull InetAddress> trustedProxyAddresses) { 156 requireNonNull(trustedProxyAddresses); 157 Set<InetAddress> normalizedAddresses = Set.copyOf(trustedProxyAddresses); 158 this.trustedProxyPredicate = remoteAddress -> { 159 if (remoteAddress == null) 160 return false; 161 162 InetAddress address = remoteAddress.getAddress(); 163 return address != null && normalizedAddresses.contains(address); 164 }; 165 return this; 166 } 167 168 /** 169 * Controls whether {@code Origin} is used as a fallback signal when determining the client URL prefix. 170 * 171 * @param allowOriginFallback {@code true} to allow {@code Origin} fallback, {@code false} to disable it 172 * @return this resolver 173 */ 174 @NonNull 175 public EffectiveOriginResolver allowOriginFallback(@Nullable Boolean allowOriginFallback) { 176 this.allowOriginFallback = allowOriginFallback; 177 return this; 178 } 179 180 @NonNull 181 Map<@NonNull String, @NonNull List<@NonNull String>> getHeaders() { 182 return this.headers; 183 } 184 185 @Nullable 186 String getRequestTargetScheme() { 187 return this.requestTargetScheme; 188 } 189 190 @NonNull 191 TrustPolicy getTrustPolicy() { 192 return this.trustPolicy; 193 } 194 195 @Nullable 196 InetSocketAddress getRemoteAddress() { 197 return this.remoteAddress; 198 } 199 200 @Nullable 201 Predicate<@NonNull InetSocketAddress> getTrustedProxyPredicate() { 202 return this.trustedProxyPredicate; 203 } 204 205 @Nullable 206 Boolean getAllowOriginFallback() { 207 return this.allowOriginFallback; 208 } 209 210 /** 211 * Forwarded header trust policy. 212 * <p> 213 * This type may gain reviewed constants in Soklet 4.x; use a forward-compatible default when switching over its values. 214 * 215 */ 216 public enum TrustPolicy { 217 /** 218 * Trust forwarded headers from any source. 219 */ 220 TRUST_ALL, 221 222 /** 223 * Trust forwarded headers only from proxies in a configured allowlist. 224 */ 225 TRUST_PROXY_ALLOWLIST, 226 227 /** 228 * Ignore forwarded headers entirely. 229 */ 230 TRUST_NONE 231 } 232}