Skip to content

Commit

Permalink
Fix CORS error for non-CORS requests
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Jun 14, 2021
1 parent a181840 commit d4ce542
Showing 1 changed file with 10 additions and 8 deletions.
Expand Up @@ -98,6 +98,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
HttpServletResponse httpResponse = WebUtils.toHttp(response);
HttpServletRequest httpRequest = WebUtils.toHttp(request);

int errorCode = httpResponse.getStatus();
if (errorCode >= 400) {
// if there's an error code at this point, return it and stop the chain
httpResponse.sendError(errorCode, null);
return;
}

String origin = httpRequest.getHeader(HttpHeaders.ORIGIN);
if (StringUtils.isEmpty(origin)) {
// Not a CORS request. Move along.
Expand All @@ -113,22 +120,17 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
// For the actual request it will be available and we will check the CORS according to the scope.
KapuaId scopeId = KapuaSecurityUtils.getSession() != null ? KapuaSecurityUtils.getSession().getScopeId() : null;

String msg = null;
if (checkOrigin(origin, scopeId)) {
// Origin matches at least one defined Endpoint
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
httpResponse.addHeader("Vary", HttpHeaders.ORIGIN);
} else {
msg = scopeId != null ?
String errorMessage = scopeId != null ?
String.format("HTTP Origin not allowed: %s for scope: %s", origin, scopeId.toCompactId()) :
String.format("HTTP Origin not allowed: %s", origin);
logger.error(msg);
}
int errorCode = httpResponse.getStatus();
if (errorCode >= 400) {
// if there's an error code at this point, return it and stop the chain
httpResponse.sendError(errorCode, msg);
logger.error(errorMessage);
httpResponse.sendError(errorCode, errorMessage);
return;
}
chain.doFilter(request, response);
Expand Down

0 comments on commit d4ce542

Please sign in to comment.