Skip to content

Commit d20ab4e

Browse files
If enabled, only take the first comma separated ip (#892)
* If enabled, only take the first comma separated ip * Undo changes in getServerPort
1 parent 68dafe1 commit d20ab4e

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

java/src/main/java/com/genexus/webpanels/HttpContextWeb.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class HttpContextWeb extends HttpContext {
7979
private static final String SAME_SITE_LAX = "Lax";
8080
private static final String SAME_SITE_STRICT = "Strict";
8181
private static final String SET_COOKIE = "Set-Cookie";
82+
private static String httpForwardedHeadersEnabled = System.getenv("HTTP_FORWARDEDHEADERS_ENABLED");
8283

8384
public static final int BROWSER_OTHER = 0;
8485
public static final int BROWSER_IE = 1;
@@ -630,8 +631,10 @@ public String getUserId(String key, ModelContext context, int handle, com.genexu
630631
}
631632

632633
public String getRemoteAddr() {
634+
boolean isEnabled = "true".equalsIgnoreCase(httpForwardedHeadersEnabled);
633635
String address = getHeader("X-Forwarded-For");
634-
if (address.length() > 0){
636+
if (isEnabled && address != null && address.length() > 0) {
637+
address = address.split(",")[0].trim();
635638
return address;
636639
}
637640
address = request.getRemoteAddr();
@@ -948,18 +951,16 @@ public byte setCookie(String name, String value, String path, java.util.Date exp
948951
}
949952

950953
public String getServerName() {
954+
boolean isEnabled = "true".equalsIgnoreCase(httpForwardedHeadersEnabled);
951955
String host = getHeader("X-Forwarded-Host");
952-
if (host.length() > 0){
953-
return host;
956+
if (isEnabled && host != null && host.length() > 0) {
957+
return host.split(",")[0].trim();
954958
}
955959
String serverNameProperty = ModelContext.getModelContext().getPreferences().getProperty("SERVER_NAME", "");
956960
if (!StringUtils.isBlank(serverNameProperty)) {
957961
return serverNameProperty;
958962
}
959-
if (request != null)
960-
return request.getServerName();
961-
962-
return "";
963+
return request != null ? request.getServerName() : "";
963964
}
964965

965966
public int getServerPort() {

0 commit comments

Comments
 (0)