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
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
[source,java]
|
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
|
|
|
|
2022-04-05 14:46:33 +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
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
[source,java]
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[cors.CorsConfiguration]
|
|
|
|
|
|
|
|
[source,java]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
config.addAllowedOrigin("*"); // Sensitive
|
|
|
|
config.applyPermitDefaultValues(); // Sensitive
|
|
|
|
----
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/CorsRegistration.html[servlet.config.annotation.CorsConfiguration]
|
|
|
|
|
|
|
|
[source,java]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
class Insecure implements WebMvcConfigurer {
|
|
|
|
@Override
|
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
registry.addMapping("/**")
|
|
|
|
.allowedOrigins("*"); // Sensitive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
User-controlled origin:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
----
|
|
|
|
public ResponseEntity<String> userControlledOrigin(@RequestHeader("Origin") String origin) {
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
|
responseHeaders.add("Access-Control-Allow-Origin", origin); // Sensitive
|
|
|
|
|
|
|
|
return new ResponseEntity<>("content", responseHeaders, HttpStatus.CREATED);
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
Java Servlet framework:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
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
|
|
|
|
2022-04-05 14:46:33 +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
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-04-05 14:46:33 +02:00
|
|
|
|
|
|
|
* 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
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
config.addAllowedOrigin("http://domain2.com"); // Compliant
|
|
|
|
----
|
2022-04-05 14:46:33 +02:00
|
|
|
|
|
|
|
* 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
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
class Safe implements WebMvcConfigurer {
|
|
|
|
@Override
|
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
registry.addMapping("/**")
|
|
|
|
.allowedOrigins("safe.com"); // Compliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-04-05 14:46:33 +02:00
|
|
|
User-controlled origin validated with an allow-list:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
----
|
|
|
|
public ResponseEntity<String> userControlledOrigin(@RequestHeader("Origin") String origin) {
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
|
if (trustedOrigins.contains(origin)) {
|
|
|
|
responseHeaders.add("Access-Control-Allow-Origin", origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ResponseEntity<>("content", responseHeaders, HttpStatus.CREATED);
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
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-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 13 Jan 2019, 22:57:44 Lars Svensson wrote:
|
|
|
|
https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter
|
|
|
|
|
|
|
|
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
|
|
|
|
|
|
|
|
https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/cors.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|