-
Notifications
You must be signed in to change notification settings - Fork 45
fix: amm-1927 http options vulnerability fixed through cors #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e41a47e
4081825
6112133
106515c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,13 @@ | |
| package com.iemr.common.utils.http; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import javax.ws.rs.core.MediaType; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.servlet.HandlerInterceptor; | ||
|
|
@@ -45,6 +47,9 @@ public class HTTPRequestInterceptor implements HandlerInterceptor { | |
|
|
||
| Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); | ||
|
|
||
| @Value("${cors.allowed-origins}") | ||
| private String allowedOrigins; | ||
|
|
||
| @Autowired | ||
| public void setValidator(Validator validator) { | ||
| this.validator = validator; | ||
|
|
@@ -140,7 +145,14 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons | |
|
|
||
| response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401 | ||
| response.setContentType(MediaType.APPLICATION_JSON); | ||
| response.setHeader("Access-Control-Allow-Origin", "*"); | ||
|
|
||
| String origin = request.getHeader("Origin"); | ||
| if (origin != null && isOriginAllowed(origin)) { | ||
| response.setHeader("Access-Control-Allow-Origin", origin); | ||
| response.setHeader("Access-Control-Allow-Credentials", "true"); | ||
| } else if (origin != null) { | ||
| logger.warn("CORS headers NOT added for error response | Unauthorized origin: {}", origin); | ||
| } | ||
|
|
||
| // Better to use getBytes().length for accurate byte size | ||
| byte[] responseBytes = jsonErrorResponse.getBytes(StandardCharsets.UTF_8); | ||
|
|
@@ -182,4 +194,27 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp | |
| throws Exception { | ||
| logger.debug("In afterCompletion Request Completed"); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the given origin is allowed based on configured allowedOrigins. | ||
| * Uses the same logic as JwtUserIdValidationFilter for consistency. | ||
| * | ||
| * @param origin The origin to validate | ||
| * @return true if origin is allowed, false otherwise | ||
| */ | ||
| private boolean isOriginAllowed(String origin) { | ||
| if (origin == null || allowedOrigins == null || allowedOrigins.trim().isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| return Arrays.stream(allowedOrigins.split(",")) | ||
| .map(String::trim) | ||
| .anyMatch(pattern -> { | ||
| String regex = pattern | ||
| .replace(".", "\\.") | ||
| .replace("*", ".*") | ||
| .replace("http://localhost:.*", "http://localhost:\\d+"); // special case for wildcard port | ||
|
||
| return origin.matches(regex); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.