Skip to content

Commit 358a69e

Browse files
authored
Merge pull request #30 from FusionAuth/degroff/get_host_handle_multiple
Account for more than one host value
2 parents a68c379 + 40f9b85 commit 358a69e

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/main/java/io/fusionauth/http/server/HTTPRequest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2024, FusionAuth, All Rights Reserved
2+
* Copyright (c) 2022-2025, FusionAuth, All Rights Reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -389,12 +389,17 @@ public String getHost() {
389389
return host;
390390
}
391391

392+
String[] xHosts = xHost.split(",");
393+
if (xHosts.length > 1) {
394+
xHost = xHosts[0];
395+
}
396+
392397
int colon = xHost.indexOf(':');
393398
if (colon > 0) {
394399
return xHost.substring(0, colon);
395400
}
396401

397-
return xHost;
402+
return xHost.trim();
398403
}
399404

400405
public void setHost(String host) {

src/test/java/io/fusionauth/http/HTTPRequestTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2024, FusionAuth, All Rights Reserved
2+
* Copyright (c) 2022-2025, FusionAuth, All Rights Reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,6 +104,36 @@ public void getBaseURL() {
104104
"X-Forwarded-Proto", "http");
105105
}
106106

107+
@Test
108+
public void getHost() {
109+
// Single host
110+
assertGetHost("acme.com", "acme.com");
111+
112+
// Single host with port
113+
assertGetHost("acme.com:42", "acme.com");
114+
115+
// Multiple hosts
116+
assertGetHost("acme.com, example.com", "acme.com");
117+
118+
// Multiple hosts with spacing
119+
assertGetHost("acme.com , example.com ", "acme.com");
120+
121+
// Multiple hosts with spacing and ports
122+
assertGetHost("acme.com:41 , example.com:42 ", "acme.com");
123+
}
124+
125+
@Test
126+
public void getIPAddress() {
127+
// Single IP
128+
assertGetIPAddress("192.168.1.1", "192.168.1.1");
129+
130+
// Multiple IPs
131+
assertGetIPAddress("192.168.1.1, 192.168.1.2", "192.168.1.1");
132+
133+
// Multiple IPs with spacing
134+
assertGetIPAddress("192.168.1.1 , 192.168.1.2 ", "192.168.1.1");
135+
}
136+
107137
@Test
108138
public void hostHeaderPortHandling() {
109139
// positive cases
@@ -203,6 +233,18 @@ private void assertBaseURL(String expected, String... headers) {
203233
assertBaseURL(expected, null, headers);
204234
}
205235

236+
private void assertGetHost(String header, @SuppressWarnings("SameParameterValue") String expected) {
237+
HTTPRequest request = new HTTPRequest();
238+
request.setHeader(Headers.XForwardedHost, header);
239+
assertEquals(request.getHost(), expected);
240+
}
241+
242+
private void assertGetIPAddress(String header, @SuppressWarnings("SameParameterValue") String expected) {
243+
HTTPRequest request = new HTTPRequest();
244+
request.setHeader(Headers.XForwardedFor, header);
245+
assertEquals(request.getIPAddress(), expected);
246+
}
247+
206248
private void assertURLs(String scheme, String source, String host, int port, String baseURL) {
207249
HTTPRequest request = new HTTPRequest();
208250
request.setScheme(scheme);

0 commit comments

Comments
 (0)