1+ import com .alibaba .fastjson .JSONObject ;
2+ import com .fasterxml .jackson .databind .ObjectMapper ;
3+ import com .google .gson .Gson ;
4+ import java .io .PrintWriter ;
5+ import java .util .HashMap ;
6+ import java .util .Random ;
7+ import javax .servlet .http .HttpServletRequest ;
8+ import javax .servlet .http .HttpServletResponse ;
9+ import org .springframework .stereotype .Controller ;
10+ import org .springframework .web .bind .annotation .GetMapping ;
11+ import org .springframework .web .bind .annotation .ResponseBody ;
12+
13+ @ Controller
14+ public class JsonpInjection {
15+ private static HashMap hashMap = new HashMap ();
16+
17+ static {
18+ hashMap .put ("username" ,"admin" );
19+ hashMap .put ("password" ,"123456" );
20+ }
21+
22+
23+ @ GetMapping (value = "jsonp1" )
24+ @ ResponseBody
25+ public String bad1 (HttpServletRequest request ) {
26+ String resultStr = null ;
27+ String jsonpCallback = request .getParameter ("jsonpCallback" );
28+
29+ Gson gson = new Gson ();
30+ String result = gson .toJson (hashMap );
31+ resultStr = jsonpCallback + "(" + result + ")" ;
32+ return resultStr ;
33+ }
34+
35+ @ GetMapping (value = "jsonp2" )
36+ @ ResponseBody
37+ public String bad2 (HttpServletRequest request ) {
38+ String resultStr = null ;
39+ String jsonpCallback = request .getParameter ("jsonpCallback" );
40+
41+ resultStr = jsonpCallback + "(" + JSONObject .toJSONString (hashMap ) + ")" ;
42+
43+ return resultStr ;
44+ }
45+
46+ @ GetMapping (value = "jsonp3" )
47+ @ ResponseBody
48+ public String bad3 (HttpServletRequest request ) {
49+ String resultStr = null ;
50+ String jsonpCallback = request .getParameter ("jsonpCallback" );
51+ String jsonStr = getJsonStr (hashMap );
52+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
53+ return resultStr ;
54+ }
55+
56+ @ GetMapping (value = "jsonp4" )
57+ @ ResponseBody
58+ public String bad4 (HttpServletRequest request ) {
59+ String resultStr = null ;
60+ String jsonpCallback = request .getParameter ("jsonpCallback" );
61+ String restr = JSONObject .toJSONString (hashMap );
62+ resultStr = jsonpCallback + "(" + restr + ");" ;
63+ return resultStr ;
64+ }
65+
66+ @ GetMapping (value = "jsonp5" )
67+ @ ResponseBody
68+ public void bad5 (HttpServletRequest request ,
69+ HttpServletResponse response ) throws Exception {
70+ response .setContentType ("application/json" );
71+ String jsonpCallback = request .getParameter ("jsonpCallback" );
72+ PrintWriter pw = null ;
73+ Gson gson = new Gson ();
74+ String result = gson .toJson (hashMap );
75+
76+ String resultStr = null ;
77+ pw = response .getWriter ();
78+ resultStr = jsonpCallback + "(" + result + ")" ;
79+ pw .println (resultStr );
80+ }
81+
82+ @ GetMapping (value = "jsonp6" )
83+ @ ResponseBody
84+ public void bad6 (HttpServletRequest request ,
85+ HttpServletResponse response ) throws Exception {
86+ response .setContentType ("application/json" );
87+ String jsonpCallback = request .getParameter ("jsonpCallback" );
88+ PrintWriter pw = null ;
89+ ObjectMapper mapper = new ObjectMapper ();
90+ String result = mapper .writeValueAsString (hashMap );
91+ String resultStr = null ;
92+ pw = response .getWriter ();
93+ resultStr = jsonpCallback + "(" + result + ")" ;
94+ pw .println (resultStr );
95+ }
96+
97+ @ GetMapping (value = "jsonp7" )
98+ @ ResponseBody
99+ public String good (HttpServletRequest request ) {
100+ String resultStr = null ;
101+ String jsonpCallback = request .getParameter ("jsonpCallback" );
102+
103+ String val = "" ;
104+ Random random = new Random ();
105+ for (int i = 0 ; i < 10 ; i ++) {
106+ val += String .valueOf (random .nextInt (10 ));
107+ }
108+ // good
109+ jsonpCallback = jsonpCallback + "_" + val ;
110+ String jsonStr = getJsonStr (hashMap );
111+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
112+ return resultStr ;
113+ }
114+
115+ @ GetMapping (value = "jsonp8" )
116+ @ ResponseBody
117+ public String good1 (HttpServletRequest request ) {
118+ String resultStr = null ;
119+ String jsonpCallback = request .getParameter ("jsonpCallback" );
120+
121+ String token = request .getParameter ("token" );
122+
123+ // good
124+ if (verifToken (token )){
125+ System .out .println (token );
126+ String jsonStr = getJsonStr (hashMap );
127+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
128+ return resultStr ;
129+ }
130+
131+ return "error" ;
132+ }
133+
134+ @ GetMapping (value = "jsonp9" )
135+ @ ResponseBody
136+ public String good2 (HttpServletRequest request ) {
137+ String resultStr = null ;
138+ String jsonpCallback = request .getParameter ("jsonpCallback" );
139+
140+ String referer = request .getHeader ("Referer" );
141+
142+ boolean result = verifReferer (referer );
143+ // good
144+ if (result ){
145+ String jsonStr = getJsonStr (hashMap );
146+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
147+ return resultStr ;
148+ }
149+
150+ return "error" ;
151+ }
152+
153+ public static String getJsonStr (Object result ) {
154+ return JSONObject .toJSONString (result );
155+ }
156+
157+ public static boolean verifToken (String token ){
158+ if (token != "xxxx" ){
159+ return false ;
160+ }
161+ return true ;
162+ }
163+
164+ public static boolean verifReferer (String referer ){
165+ if (!referer .startsWith ("http://test.com/" )){
166+ return false ;
167+ }
168+ return true ;
169+ }
170+ }
0 commit comments