Skip to content

Commit

Permalink
Fix "CORS Rejected - Invalid origin" issue when origin header is empty (
Browse files Browse the repository at this point in the history
hyperledger#6988)

Signed-off-by: Ameziane H <[email protected]>
  • Loading branch information
ahamlat authored Apr 24, 2024
1 parent 12723ac commit 27a7de9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ private Router buildRouter() {
router
.route()
.handler(
CorsHandler.create(buildCorsRegexFromConfig())
CorsHandler.create()
.addRelativeOrigin(buildCorsRegexFromConfig())
.allowedHeader("*")
.allowedHeader("content-type"));
router
Expand Down Expand Up @@ -569,7 +570,7 @@ private String buildCorsRegexFromConfig() {
return "";
}
if (config.getCorsAllowedDomains().contains("*")) {
return ".*://.*";
return ".*://.*|.*";
} else {
final StringJoiner stringJoiner = new StringJoiner("|");
config.getCorsAllowedDomains().stream().filter(s -> !s.isEmpty()).forEach(stringJoiner::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ public void requestWithAnyOriginShouldSucceedWhenCorsIsStart() throws Exception
}
}

@Test
public void requestWithAnyOriginAndEmptyActualOriginShouldSucceed() throws Exception {
jsonRpcHttpService = createJsonRpcHttpServiceWithAllowedDomains("*");

final Request request =
new Request.Builder().url(jsonRpcHttpService.url()).header("Origin", "").build();

try (final Response response = client.newCall(request).execute()) {
assertThat(response.isSuccessful()).isTrue();
}
}

@Test
public void requestFromBrowserExtensionShouldSucceedWhenCorsIsStar() throws Exception {
jsonRpcHttpService = createJsonRpcHttpServiceWithAllowedDomains("*");
Expand Down

0 comments on commit 27a7de9

Please sign in to comment.