16
16
@ Controller
17
17
public class UnsafeReflection {
18
18
19
- @ GetMapping (value = "uf1" )
20
- public void bad1 (HttpServletRequest request ) {
21
- String className = request .getParameter ("className" );
22
- String parameterValue = request .getParameter ("parameterValue" );
23
- try {
24
- Class clazz = Class .forName (className );
25
- Object object = clazz .getDeclaredConstructors ()[0 ].newInstance (parameterValue ); //bad
26
- } catch (Exception e ) {
27
- e .printStackTrace ();
28
- }
29
- }
30
-
31
- @ GetMapping (value = "uf2" )
32
- public void bad2 (HttpServletRequest request ) {
33
- String className = request .getParameter ("className" );
34
- String parameterValue = request .getParameter ("parameterValue" );
35
- try {
36
- ClassLoader classLoader = ClassLoader .getSystemClassLoader ();
37
- Class clazz = classLoader .loadClass (className );
38
- Object object = clazz .newInstance ();
39
- clazz .getDeclaredMethods ()[0 ].invoke (object , parameterValue ); //bad
40
- } catch (Exception e ) {
41
- e .printStackTrace ();
42
- }
43
- }
44
-
45
19
@ RequestMapping (value = {"/service/{beanIdOrClassName}/{methodName}" }, method = {RequestMethod .POST }, consumes = {"application/json" }, produces = {"application/json" })
46
- public Object bad3 (@ PathVariable ("beanIdOrClassName" ) String beanIdOrClassName , @ PathVariable ("methodName" ) String methodName , @ RequestBody Map <String , Object > body ) throws Exception {
20
+ public Object bad1 (@ PathVariable ("beanIdOrClassName" ) String beanIdOrClassName , @ PathVariable ("methodName" ) String methodName , @ RequestBody Map <String , Object > body ) throws Exception {
47
21
List <Object > rawData = null ;
48
22
try {
49
23
rawData = (List <Object >)body .get ("methodInput" );
@@ -53,7 +27,7 @@ public Object bad3(@PathVariable("beanIdOrClassName") String beanIdOrClassName,
53
27
return invokeService (beanIdOrClassName , methodName , null , rawData );
54
28
}
55
29
56
- @ GetMapping (value = "uf3 " )
30
+ @ GetMapping (value = "uf1 " )
57
31
public void good1 (HttpServletRequest request ) throws Exception {
58
32
HashSet <String > hashSet = new HashSet <>();
59
33
hashSet .add ("com.example.test1" );
@@ -71,7 +45,7 @@ public void good1(HttpServletRequest request) throws Exception {
71
45
}
72
46
}
73
47
74
- @ GetMapping (value = "uf4 " )
48
+ @ GetMapping (value = "uf2 " )
75
49
public void good2 (HttpServletRequest request ) throws Exception {
76
50
String className = request .getParameter ("className" );
77
51
String parameterValue = request .getParameter ("parameterValue" );
@@ -86,21 +60,6 @@ public void good2(HttpServletRequest request) throws Exception {
86
60
}
87
61
}
88
62
89
- @ GetMapping (value = "uf5" )
90
- public void good3 (HttpServletRequest request ) throws Exception {
91
- String className = request .getParameter ("className" );
92
- String parameterValue = request .getParameter ("parameterValue" );
93
- if (!className .equals ("com.example.test1" )){ //good
94
- throw new Exception ("Class not valid: " + className );
95
- }
96
- try {
97
- Class clazz = Class .forName (className );
98
- Object object = clazz .getDeclaredConstructors ()[0 ].newInstance (parameterValue ); //good
99
- } catch (Exception e ) {
100
- e .printStackTrace ();
101
- }
102
- }
103
-
104
63
private Object invokeService (String beanIdOrClassName , String methodName , MultipartFile [] files , List <Object > data ) throws Exception {
105
64
BeanFactory beanFactory = new BeanFactory ();
106
65
try {
0 commit comments