Skip to content

Commit

Permalink
Merge pull request #1895 from ballerina-platform/host-fix-passthrough
Browse files Browse the repository at this point in the history
[Master] Update Host header only when the user intentionally provide a value
  • Loading branch information
TharmiganK authored Mar 13, 2024
2 parents d4f517e + 7e68fc6 commit 6c13ef7
Show file tree
Hide file tree
Showing 31 changed files with 270 additions and 73 deletions.
6 changes: 3 additions & 3 deletions ballerina-tests/http-advanced-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_advanced_tests"
version = "2.10.10"
version = "2.10.11"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.10"
version = "2.10.11"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.10.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.11-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-advanced-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -105,7 +105,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_advanced_tests"
version = "2.10.10"
version = "2.10.11"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
Expand All @@ -125,7 +125,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-client-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_client_tests"
version = "2.10.10"
version = "2.10.11"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.10"
version = "2.10.11"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.10.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.11-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -102,7 +102,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_client_tests"
version = "2.10.10"
version = "2.10.11"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -121,7 +121,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,179 @@ function testHttp2ClientHostHeader3() returns error? {
host = check http2ClientHost2->post("/host", req, {"Host": "mock3.com"});
test:assertEquals(host, "mock3.com");
}

service /api on new http:Listener(passthroughHostTestPort1) {

resource function 'default [string test]/host(@http:Header string host) returns string {
return host;
}
}

final http:Client httpHostPassthroughClientEP = check new (string `localhost:${passthroughHostTestPort1}`, httpVersion = http:HTTP_1_1);
final http:Client http2HostPassthroughClientEP = check new (string `localhost:${passthroughHostTestPort1}`);
final http:Client http2HostPassthroughClientEPWithPriorKnowledge = check new (string `localhost:${passthroughHostTestPort1}`, http2Settings = {http2PriorKnowledge: true});

final http:Client httpHostPassthroughTestClient = check new (string `localhost:${passthroughHostTestPort2}`);

service /api on new http:Listener(passthroughHostTestPort2) {

resource function get http/host(http:Request req) returns http:Response|error {
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get http2/host(http:Request req) returns http:Response|error {
return http2HostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get http2prior/host(http:Request req) returns http:Response|error {
return http2HostPassthroughClientEPWithPriorKnowledge->execute(req.method, req.rawPath, req);
}

resource function get update1/host(string host, http:Request req) returns http:Response|error {
req.setHeader("Host", host);
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get update2/host(string host, http:Request req) returns http:Response|error {
req.setHeader("host", host);
return http2HostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get update3/host(string host, http:Request req) returns http:Response|error {
req.setHeader("HOST", host);
return http2HostPassthroughClientEPWithPriorKnowledge->execute(req.method, req.rawPath, req);
}

resource function get update4/host(string host, http:Request req) returns http:Response|error {
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req, {host: host});
}

resource function get update5/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEP->execute(req.method, req.rawPath, req, {host: host});
}

resource function get update6/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEPWithPriorKnowledge->execute(req.method, req.rawPath, req, {host: host});
}

resource function get update7/host(string host, http:Request req) returns http:Response|error {
return httpHostPassthroughClientEP->post(req.rawPath, req, {host: host});
}

resource function get update8/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEP->post(req.rawPath, req, {host: host});
}

resource function get update9/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEPWithPriorKnowledge->post(req.rawPath, req, {host: host});
}

resource function get update10/host(string host, http:Request req) returns http:Response|error {
return httpHostPassthroughClientEP->/api/test/host.post(req, {host: host});
}

resource function get update11/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEP->/api/test/host.post(req, {host: host});
}

resource function get update12/host(string host, http:Request req) returns http:Response|error {
return http2HostPassthroughClientEPWithPriorKnowledge->/api/test/host.post(req, {host: host});
}

resource function get negative1/host(string host, http:Request req) returns http:Response|error {
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req, {host123: host});
}

resource function get negative2/host(string host, http:Request req) returns http:Response|error {
req.setHeader("X-Host", host);
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get negative3/host(string host, http:Request req) returns http:Response|error {
req.setHeader("Host", host);
req.removeHeader("Host");
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req);
}

resource function get negative4/host(string host, http:Request req) returns http:Response|error {
req.setHeader("Host", host);
req.removeHeader("Host");
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req, {host: "random.com"});
}

resource function get negative5/host(string host, http:Request req) returns http:Response|error {
req.setHeader("Host", host);
req.removeHeader("Host");
req.setHeader("Host", "random.com");
return httpHostPassthroughClientEP->execute(req.method, req.rawPath, req);
}
}

@test:Config {}
function testHostHeaderInPassthrough() returns error? {
string host = check httpHostPassthroughTestClient->/api/http/host.get();
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/http2/host.get();
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/http2prior/host.get();
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");
}

@test:Config {}
function testUpdateHostHeaderInPassthrough() returns error? {
string host = check httpHostPassthroughTestClient->/api/update1/host.get(host = "random-update-1.com");
test:assertEquals(host, "random-update-1.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update2/host.get(host = "random-update-2.com");
test:assertEquals(host, "random-update-2.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update3/host.get(host = "random-update-3.com");
test:assertEquals(host, "random-update-3.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update4/host.get(host = "random-update-4.com");
test:assertEquals(host, "random-update-4.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update5/host.get(host = "random-update-5.com");
test:assertEquals(host, "random-update-5.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update6/host.get(host = "random-update-6.com");
test:assertEquals(host, "random-update-6.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update7/host.get(host = "random-update-7.com");
test:assertEquals(host, "random-update-7.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update8/host.get(host = "random-update-8.com");
test:assertEquals(host, "random-update-8.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update9/host.get(host = "random-update-9.com");
test:assertEquals(host, "random-update-9.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update10/host.get(host = "random-update-10.com");
test:assertEquals(host, "random-update-10.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update11/host.get(host = "random-update-11.com");
test:assertEquals(host, "random-update-11.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/update12/host.get(host = "random-update-12.com");
test:assertEquals(host, "random-update-12.com", "Invalid host header in passthrough");
}

@test:Config {}
function testNegativeCasesInPassthrough() returns error? {
string host = check httpHostPassthroughTestClient->/api/negative1/host.get(host = "random-update-1.com");
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/negative2/host.get(host = "random-update-2.com");
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/negative3/host.get(host = "random-update-3.com");
test:assertEquals(host, "localhost:9607", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/negative4/host.get(host = "random-update-4.com");
test:assertEquals(host, "random.com", "Invalid host header in passthrough");

host = check httpHostPassthroughTestClient->/api/negative5/host.get(host = "random-update-5.com");
test:assertEquals(host, "random.com", "Invalid host header in passthrough");
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ const int clientFormUrlEncodedTestPort = 9604;

const int http2ClientHostHeaderTestPort = 9605;
const int httpClientHostHeaderTestPort = 9606;
const int passthroughHostTestPort1 = 9607;
const int passthroughHostTestPort2 = 9608;
6 changes: 3 additions & 3 deletions ballerina-tests/http-dispatching-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_dispatching_tests"
version = "2.10.10"
version = "2.10.11"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.10"
version = "2.10.11"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.10.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.11-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-dispatching-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -102,7 +102,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_dispatching_tests"
version = "2.10.10"
version = "2.10.11"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -124,7 +124,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-interceptor-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_interceptor_tests"
version = "2.10.10"
version = "2.10.11"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.10"
version = "2.10.11"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.10.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.11-SNAPSHOT.jar"
6 changes: 3 additions & 3 deletions ballerina-tests/http-interceptor-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -99,7 +99,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_interceptor_tests"
version = "2.10.10"
version = "2.10.11"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "http_test_common"},
Expand All @@ -115,7 +115,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_test_common"
version = "2.10.10"
version = "2.10.11"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "lang.string"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina-tests/http-misc-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
org = "ballerina"
name = "http_misc_tests"
version = "2.10.10"
version = "2.10.11"

[[dependency]]
org = "ballerina"
name = "http_test_common"
repository = "local"
version = "2.10.10"
version = "2.10.11"

[platform.java17]
graalvmCompatible = true

[[platform.java17.dependency]]
scope = "testOnly"
path = "../../test-utils/build/libs/http-test-utils-2.10.10.jar"
path = "../../test-utils/build/libs/http-test-utils-2.10.11-SNAPSHOT.jar"
Loading

0 comments on commit 6c13ef7

Please sign in to comment.