Skip to content

Commit 6844b0a

Browse files
committed
add configure code of json to jsonp
1 parent 85ca363 commit 6844b0a

File tree

7 files changed

+117
-74
lines changed

7 files changed

+117
-74
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Each vulnerability type code has a security vulnerability by default unless ther
1515

1616
Sort by letter.
1717

18-
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/resources/logback.xml)
18+
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/resources/logback-online.xml)
1919
- [CORS](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/CORS.java)
2020
- [CRLF Injection](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/CRLFInjection.java)
2121
- [CSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/WebSecurityConfig.java)

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## 漏洞代码
1414

15-
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/resources/logback.xml)
15+
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/resources/logback-online.xml)
1616
- [CORS](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/CORS.java)
1717
- [CRLF Injection](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/CRLFInjection.java)
1818
- [CSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/WebSecurityConfig.java)

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

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.joychou.controller.jsonp;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONObject;
5+
import org.joychou.security.SecurityUtil;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.bind.annotation.*;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
12+
13+
/**
14+
* @author JoyChou ([email protected]) @ 2018.10.24
15+
* https://github.com/JoyChou93/java-sec-code/wiki/JSONP
16+
*/
17+
18+
@RestController
19+
@RequestMapping("/jsonp")
20+
public class JSONP {
21+
22+
private static String info = "{\"name\": \"JoyChou\", \"phone\": \"18200001111\"}";
23+
private static String[] urlwhitelist = {"joychou.com", "joychou.org"};
24+
25+
26+
/**
27+
* Set the response content-type to application/javascript.
28+
*
29+
* http://localhost:8080/jsonp/referer?callback=test
30+
*
31+
*/
32+
@RequestMapping(value = "/referer", produces = "application/javascript")
33+
private static String referer(HttpServletRequest request, HttpServletResponse response) {
34+
String callback = request.getParameter("callback");
35+
return callback + "(" + info + ")";
36+
}
37+
38+
/**
39+
* Direct access does not check Referer, non-direct access check referer.
40+
* Developer like to do jsonp testing like this.
41+
*
42+
* http://localhost:8080/jsonp/emptyReferer?callback=test
43+
*
44+
*/
45+
@RequestMapping(value = "/emptyReferer", produces = "application/javascript")
46+
private static String emptyReferer(HttpServletRequest request, HttpServletResponse response) {
47+
String referer = request.getHeader("referer");
48+
49+
if (null != referer && !SecurityUtil.checkURLbyEndsWith(referer, urlwhitelist)) {
50+
return "error";
51+
}
52+
53+
String callback = request.getParameter("callback");
54+
return callback + "(" + info + ")";
55+
}
56+
57+
/**
58+
* Adding callback or cback on parameter can automatically return jsonp data.
59+
* http://localhost:8080/jsonp/advice?callback=test
60+
* http://localhost:8080/jsonp/advice?cback=test
61+
*
62+
* @return Only return object, AbstractJsonpResponseBodyAdvice can be used successfully.
63+
* Such as JSONOjbect or JavaBean. String type cannot be used.
64+
*/
65+
@RequestMapping(value = "/advice", produces = MediaType.APPLICATION_JSON_VALUE)
66+
public JSONObject advice() {
67+
return JSON.parseObject(info);
68+
69+
}
70+
71+
/**
72+
* Safe code.
73+
* http://localhost:8080/jsonp/sec?callback=test
74+
*/
75+
@RequestMapping(value = "/sec", produces = "application/javascript")
76+
private static String safecode(HttpServletRequest request, HttpServletResponse response) {
77+
String referer = request.getHeader("referer");
78+
79+
if (!SecurityUtil.checkURLbyEndsWith(referer, urlwhitelist)) {
80+
return "error";
81+
}
82+
83+
String callback = request.getParameter("callback");
84+
return callback + "(" + info + ")";
85+
}
86+
87+
88+
89+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.joychou.controller.jsonp;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
5+
6+
@ControllerAdvice
7+
public class JSONPAdvice extends AbstractJsonpResponseBodyAdvice {
8+
9+
public JSONPAdvice() {
10+
super("callback", "cback"); // Can set multiple paramNames
11+
}
12+
}

src/main/java/org/joychou/security/secFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.servlet.http.HttpServletResponse;
88
import java.io.IOException;
99
import org.apache.commons.lang.StringUtils;
10+
import org.springframework.beans.factory.annotation.Value;
1011

1112

1213
/**
@@ -19,6 +20,9 @@
1920
@WebFilter(filterName = "referSecCheck", urlPatterns = "/*")
2021
public class secFilter implements Filter {
2122

23+
@Value("${org.joychou.security.jsonp}")
24+
private Boolean jsonpSwitch; // get application.properties configure
25+
2226
@Override
2327
public void init(FilterConfig filterConfig) throws ServletException {
2428

@@ -28,6 +32,12 @@ public void init(FilterConfig filterConfig) throws ServletException {
2832
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
2933
throws IOException, ServletException {
3034

35+
36+
// If don't check referer, return.
37+
if (!jsonpSwitch) {
38+
return ;
39+
}
40+
3141
HttpServletRequest request = (HttpServletRequest) req;
3242
HttpServletResponse response = (HttpServletResponse) res;
3343

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

22
# Spring Boot Actuator Vulnerable Config
33
management.security.enabled=false
4-
logging.config=classpath:logback-online.xml
4+
logging.config=classpath:logback-online.xml
5+
6+
# jsonp check referer switch
7+
org.joychou.security.jsonp = true

0 commit comments

Comments
 (0)