Skip to content

Commit 4cffffd

Browse files
committed
fix cors sec code
1 parent 2e20dd1 commit 4cffffd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [XXE](https://github.com/JoyChou93/java-sec-code/wiki/XXE)
3232
- [SQLI](https://github.com/JoyChou93/java-sec-code/wiki/SQL-Inject)
3333
- [Fastjson](https://github.com/JoyChou93/java-sec-code/wiki/Fastjson)
34+
- [CORS](https://github.com/JoyChou93/java-sec-code/wiki/CORS)
3435
- [Others](https://github.com/JoyChou93/java-sec-code/wiki/others)
3536

3637

src/main/java/org/joychou/controller/CORS.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class CORS {
2626
*
2727
* @param request
2828
* @param response
29-
* @desc: 当origin为空,即直接访问的情况下,response的header中不会出现Access-Control-Allow-Origin
29+
* @desc https://github.com/JoyChou93/java-sec-code/wiki/CORS
3030
*/
3131
@RequestMapping("/vuls1")
3232
@ResponseBody
@@ -61,7 +61,16 @@ private static String vuls3(HttpServletResponse response) {
6161
private static String seccode(HttpServletRequest request, HttpServletResponse response) {
6262
String origin = request.getHeader("Origin");
6363
Security sec = new Security();
64-
if (!sec.checkSafeUrl(origin, urlwhitelist)) {
64+
Boolean origin_safe = false;
65+
66+
// 如果origin为空,表示是同域过来的请求或者浏览器直接发起的请求,这种直接放过,没有安全问题。
67+
if (origin == null) {
68+
origin_safe = true;
69+
}else if (sec.checkSafeUrl(origin, urlwhitelist)) {
70+
origin_safe = true;
71+
}
72+
73+
if (!origin_safe) {
6574
return "Origin is not safe.";
6675
}
6776
response.setHeader("Access-Control-Allow-Origin", "*");

0 commit comments

Comments
 (0)