|
| 1 | +package com.appsmith.server.helpers.ce; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.beans.factory.annotation.Value; |
| 5 | +import org.springframework.http.HttpHeaders; |
| 6 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 7 | +import org.springframework.util.StringUtils; |
| 8 | +import org.springframework.web.server.ServerWebExchange; |
| 9 | + |
| 10 | +import static com.google.common.net.HttpHeaders.FORWARDED; |
| 11 | +import static com.google.common.net.HttpHeaders.X_FORWARDED_HOST; |
| 12 | +import static com.google.common.net.HttpHeaders.X_FORWARDED_PROTO; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +public class HostUrlHelperCE { |
| 16 | + |
| 17 | + @Value("${appsmith.domain:}") |
| 18 | + private String configuredRedirectDomain; |
| 19 | + |
| 20 | + @Value("${APPSMITH_BASE_URL:}") |
| 21 | + private String configuredBaseUrl; |
| 22 | + |
| 23 | + public static final String APPSMITH_FORWARDED_HOST = "appsmith-forwarded-host"; |
| 24 | + public static final String APPSMITH_FORWARDED_PROTO = "appsmith-forwarded-proto"; |
| 25 | + |
| 26 | + /** |
| 27 | + * Resolve the client facing base URL for the current request. |
| 28 | + * <p> |
| 29 | + * The method first prefers the {@code APPSMITH_BASE_URL} environment variable to ensure that an operator supplied |
| 30 | + * canonical URL is always respected. When that variable is not configured, it derives the host from the incoming |
| 31 | + * request metadata (forwarded headers, host header etc). The {@code Origin} header is deliberately ignored for |
| 32 | + * deriving the base URL because it is supplied by the client and can be trivially spoofed. Instead, we only log the |
| 33 | + * {@code Origin} value when it disagrees with the derived host to aid troubleshooting. |
| 34 | + * |
| 35 | + * @param originInHeader origin header value supplied by the client, used solely for logging discrepancies |
| 36 | + * @param exchange current request exchange used to derive request based host details |
| 37 | + * @return canonical base URL that should be used when communicating with the client. Returns an empty string when |
| 38 | + * it cannot be determined. |
| 39 | + */ |
| 40 | + public String getClientFacingBaseUrl(String originInHeader, ServerWebExchange exchange) { |
| 41 | + String derivedHost; |
| 42 | + |
| 43 | + if (StringUtils.hasText(configuredBaseUrl)) { |
| 44 | + derivedHost = sanitizeConfiguredDomain(configuredBaseUrl, exchange); |
| 45 | + } else { |
| 46 | + derivedHost = getHostUrl(exchange.getRequest()); |
| 47 | + } |
| 48 | + |
| 49 | + if (!StringUtils.hasText(derivedHost)) { |
| 50 | + log.warn("Unable to determine client facing host from configured base URL or request headers"); |
| 51 | + derivedHost = ""; |
| 52 | + } |
| 53 | + |
| 54 | + // Log mismatch only when both values are present and not equal |
| 55 | + if (StringUtils.hasText(originInHeader) |
| 56 | + && StringUtils.hasText(derivedHost) |
| 57 | + && !originInHeader.equalsIgnoreCase(derivedHost)) { |
| 58 | + log.warn( |
| 59 | + "Origin header and derived host mismatch; origin in header: {}, derived host: {}", |
| 60 | + originInHeader, |
| 61 | + derivedHost); |
| 62 | + } |
| 63 | + |
| 64 | + return derivedHost; |
| 65 | + } |
| 66 | + |
| 67 | + public static String getHostUrl(ServerHttpRequest request) { |
| 68 | + HttpHeaders headers = request.getHeaders(); |
| 69 | + String hostUrl = null; |
| 70 | + |
| 71 | + String query = request.getURI().getQuery(); |
| 72 | + // Check for appsmith-forwarded parameters in query string |
| 73 | + // This is done so that we only check for the query parameter when the request is for the OAuth2 login |
| 74 | + if (request.getURI().toString().contains("/login/oauth2/code/google") && query != null && !query.isEmpty()) { |
| 75 | + String hostFromQuery = getHostUrlFromQuery(query); |
| 76 | + if (hostFromQuery != null) { |
| 77 | + log.info("Using host URL from query parameters: {}", hostFromQuery); |
| 78 | + return hostFromQuery; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Prefer Forwarded header (RFC 7239) |
| 83 | + String forwarded = headers.getFirst(FORWARDED); |
| 84 | + if (forwarded != null) { |
| 85 | + try { |
| 86 | + if (forwarded.contains("host=")) { |
| 87 | + hostUrl = forwarded.split("host=")[1].split("[;,]")[0].trim(); |
| 88 | + log.trace("Using Forwarded header host: {}", hostUrl); |
| 89 | + return ensureScheme(hostUrl, request); |
| 90 | + } |
| 91 | + } catch (Exception e) { |
| 92 | + log.debug("Failed to parse Forwarded header: {}", forwarded, e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Use X-Forwarded-Host if available |
| 97 | + String xForwardedHost = headers.getFirst(X_FORWARDED_HOST); |
| 98 | + if (xForwardedHost != null) { |
| 99 | + String scheme = headers.getFirst(X_FORWARDED_PROTO); |
| 100 | + if (scheme == null) { |
| 101 | + scheme = request.getURI().getScheme(); |
| 102 | + } |
| 103 | + hostUrl = scheme + "://" + xForwardedHost; |
| 104 | + log.trace("Using X-Forwarded-Host: {}", hostUrl); |
| 105 | + return hostUrl; |
| 106 | + } |
| 107 | + |
| 108 | + // Fall back to standard Host header |
| 109 | + String host = headers.getFirst(HttpHeaders.HOST); |
| 110 | + if (host != null) { |
| 111 | + String scheme = request.getURI().getScheme(); |
| 112 | + hostUrl = scheme + "://" + host; |
| 113 | + log.trace("Using standard Host header: {}", hostUrl); |
| 114 | + return hostUrl; |
| 115 | + } |
| 116 | + |
| 117 | + log.debug("No host information found in request headers"); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + private static String ensureScheme(String hostUrl, ServerHttpRequest request) { |
| 122 | + if (!hostUrl.contains("://")) { |
| 123 | + String scheme = request.getHeaders().getFirst(X_FORWARDED_PROTO); |
| 124 | + if (scheme == null) { |
| 125 | + scheme = request.getURI().getScheme(); |
| 126 | + } |
| 127 | + return scheme + "://" + hostUrl; |
| 128 | + } |
| 129 | + return hostUrl; |
| 130 | + } |
| 131 | + |
| 132 | + public static boolean isSecureScheme(ServerWebExchange exchange) { |
| 133 | + String scheme = exchange.getRequest().getHeaders().getFirst(X_FORWARDED_PROTO); |
| 134 | + if (scheme == null) { |
| 135 | + scheme = exchange.getRequest().getURI().getScheme(); |
| 136 | + } |
| 137 | + return "https".equals(scheme); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Extract host URL from query parameters if they contain appsmith-forwarded-host and appsmith-forwarded-proto |
| 142 | + * @param query The query string to parse |
| 143 | + * @return The host URL constructed from query parameters, or null if parameters not found |
| 144 | + */ |
| 145 | + private static String getHostUrlFromQuery(String query) { |
| 146 | + String host = null; |
| 147 | + String proto = null; |
| 148 | + |
| 149 | + // Parse the query string |
| 150 | + String[] params = query.split("&"); |
| 151 | + for (String param : params) { |
| 152 | + if (param.startsWith(APPSMITH_FORWARDED_HOST + "=")) { |
| 153 | + host = param.substring(APPSMITH_FORWARDED_HOST.length() + 1); |
| 154 | + } else if (param.startsWith(APPSMITH_FORWARDED_PROTO + "=")) { |
| 155 | + proto = param.substring(APPSMITH_FORWARDED_PROTO.length() + 1); |
| 156 | + } |
| 157 | + |
| 158 | + // If we have both parameters, we can return the host URL |
| 159 | + if (host != null && proto != null) { |
| 160 | + return proto + "://" + host; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + private static String sanitizeConfiguredDomain(String domain, ServerWebExchange exchange) { |
| 168 | + String sanitizedDomain = domain; |
| 169 | + while (sanitizedDomain.endsWith("/")) { |
| 170 | + sanitizedDomain = sanitizedDomain.substring(0, sanitizedDomain.length() - 1); |
| 171 | + } |
| 172 | + |
| 173 | + if (sanitizedDomain.startsWith("http://") || sanitizedDomain.startsWith("https://")) { |
| 174 | + return sanitizedDomain; |
| 175 | + } |
| 176 | + |
| 177 | + String scheme = exchange.getRequest().getHeaders().getFirst(X_FORWARDED_PROTO); |
| 178 | + if (!StringUtils.hasText(scheme)) { |
| 179 | + scheme = exchange.getRequest().getURI().getScheme(); |
| 180 | + } |
| 181 | + if (!StringUtils.hasText(scheme)) { |
| 182 | + scheme = "https"; |
| 183 | + } |
| 184 | + |
| 185 | + return scheme + "://" + sanitizedDomain; |
| 186 | + } |
| 187 | +} |
0 commit comments