2020-06-30 12:50:28 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
include::../ask-yourself.adoc[]
|
|
|
|
|
|
|
|
include::../recommended.adoc[]
|
|
|
|
|
|
|
|
== Sensitive Code Example
|
|
|
|
|
|
|
|
Java servlet framework:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
@Override
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
resp.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
resp.setHeader("Access-Control-Allow-Origin", "*"); // Sensitive
|
|
|
|
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
|
resp.setHeader("Access-Control-Allow-Methods", "GET");
|
|
|
|
resp.getWriter().write("response");
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
Spring MVC framework:
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/CrossOrigin.html[CrossOrigin]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
@CrossOrigin // Sensitive
|
|
|
|
@RequestMapping("")
|
|
|
|
public class TestController {
|
|
|
|
public String home(ModelMap model) {
|
|
|
|
model.addAttribute("message", "ok ");
|
|
|
|
return "view";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[cors.CorsConfiguration]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
config.addAllowedOrigin("*"); // Sensitive
|
|
|
|
config.applyPermitDefaultValues(); // Sensitive
|
|
|
|
----
|
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/CorsRegistration.html[servlet.config.annotation.CorsConfiguration]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
class Insecure implements WebMvcConfigurer {
|
|
|
|
@Override
|
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
registry.addMapping("/**")
|
|
|
|
.allowedOrigins("*"); // Sensitive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
Java Servlet framework:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
@Override
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
resp.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
resp.setHeader("Access-Control-Allow-Origin", "trustedwebsite.com"); // Compliant
|
|
|
|
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
|
resp.setHeader("Access-Control-Allow-Methods", "GET");
|
|
|
|
resp.getWriter().write("response");
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
Spring MVC framework:
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/CrossOrigin.html[CrossOrigin]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
@CrossOrigin("trustedwebsite.com") // Compliant
|
|
|
|
@RequestMapping("")
|
|
|
|
public class TestController {
|
|
|
|
public String home(ModelMap model) {
|
|
|
|
model.addAttribute("message", "ok ");
|
|
|
|
return "view";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[cors.CorsConfiguration]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
config.addAllowedOrigin("http://domain2.com"); // Compliant
|
|
|
|
----
|
|
|
|
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/CorsRegistration.html[servlet.config.annotation.CorsConfiguration]
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
class Safe implements WebMvcConfigurer {
|
|
|
|
@Override
|
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
registry.addMapping("/**")
|
|
|
|
.allowedOrigins("safe.com"); // Compliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::comments-and-links.adoc[]
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|