Skip to content

Account for more than one host value #30

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/io/fusionauth/http/server/HTTPRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, FusionAuth, All Rights Reserved
* Copyright (c) 2022-2025, FusionAuth, All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -389,12 +389,17 @@ public String getHost() {
return host;
}

String[] xHosts = xHost.split(",");
if (xHosts.length > 1) {
xHost = xHosts[0];
}

int colon = xHost.indexOf(':');
if (colon > 0) {
return xHost.substring(0, colon);
}

return xHost;
return xHost.trim();
}

public void setHost(String host) {
Expand Down
44 changes: 43 additions & 1 deletion src/test/java/io/fusionauth/http/HTTPRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, FusionAuth, All Rights Reserved
* Copyright (c) 2022-2025, FusionAuth, All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -104,6 +104,36 @@ public void getBaseURL() {
"X-Forwarded-Proto", "http");
}

@Test
public void getHost() {
// Single host
assertGetHost("acme.com", "acme.com");

// Single host with port
assertGetHost("acme.com:42", "acme.com");

// Multiple hosts
assertGetHost("acme.com, example.com", "acme.com");

// Multiple hosts with spacing
assertGetHost("acme.com , example.com ", "acme.com");

// Multiple hosts with spacing and ports
assertGetHost("acme.com:41 , example.com:42 ", "acme.com");
}

@Test
public void getIPAddress() {
// Single IP
assertGetIPAddress("192.168.1.1", "192.168.1.1");

// Multiple IPs
assertGetIPAddress("192.168.1.1, 192.168.1.2", "192.168.1.1");

// Multiple IPs with spacing
assertGetIPAddress("192.168.1.1 , 192.168.1.2 ", "192.168.1.1");
}

@Test
public void hostHeaderPortHandling() {
// positive cases
Expand Down Expand Up @@ -203,6 +233,18 @@ private void assertBaseURL(String expected, String... headers) {
assertBaseURL(expected, null, headers);
}

private void assertGetHost(String header, @SuppressWarnings("SameParameterValue") String expected) {
HTTPRequest request = new HTTPRequest();
request.setHeader(Headers.XForwardedHost, header);
assertEquals(request.getHost(), expected);
}

private void assertGetIPAddress(String header, @SuppressWarnings("SameParameterValue") String expected) {
HTTPRequest request = new HTTPRequest();
request.setHeader(Headers.XForwardedFor, header);
assertEquals(request.getIPAddress(), expected);
}

private void assertURLs(String scheme, String source, String host, int port, String baseURL) {
HTTPRequest request = new HTTPRequest();
request.setScheme(scheme);
Expand Down