I am working on Web Application and asked to run the VAPT
against it before release.
Then I downloaded Vega and quickly scanned my webapp and came across a VAPT issue, as follows:
Vega has detected that the resource has set an insecure Cross-Origin Resource Sharing (CORS) access control. CORS provides mechanisms that allow a server to restrict resource access for cross-site requests to certain trusted domains. The server in question has allowed resource from any origin by setting the value of the "Access-Control-Allow-Origin" response header to a wildcard value. This presents a security risk because any site can issue requests to access resources, regardless of origin.
Then I started finding a solution to fix it and came across this post and implemented a filter
as suggested in the answer as follows:
@Component
public class CORSFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods",
"POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(request, response);
}
public void destroy() {
}
}
Now, when again I scanned the Vega against webapp, it doesn't listed the same issue again, which I believe saved my webapp against CSRF
attacks.
Now, after reading this post, I am thinking over how request.getHeader("Origin")
prevents from Cross Site Request Forgery attacks
, as whatever the origin is either https://webapp.com or https://evil.com, request is always valid for the application as I am picking "Access-Control-Allow-Origin"
from request header itself.
Can anyone please help me in understanding the concept, how setting the request.getHeader("Origin")
saves from CSRF attacks
?
Thanks.
Understanding after reading @rism answer and Patrick Grimard post :
When a client application makes an AJAX request, the browser initially sends a preflight OPTIONS
request to server to determine what the client is permitted to do, for request other than GET
and that's the reason we should set Access-Control-Allow-Origin
either to origin or specific domain as part of response header.
Taking the example of POST
, when client send request, browser first makes preflight OPTIONS
request to the server and the server response to the OPTIONS
request contains headers that instruct the browser for which all origin
request is permitted. Besides adding response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
site is still vulnerable so we need to explicitly whitelist
the IP's either in Apache(for application deployed in cluster) as done here or in Tomcat as described here.
Still I have one doubt, if we are restricting the IP address at server level itself than do we really need to set "Access-Control-Allow-Origin"
as part of response header?