1616@ Controller
1717public class UnsafeReflection {
1818
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-
4519 @ 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 {
4721 List <Object > rawData = null ;
4822 try {
4923 rawData = (List <Object >)body .get ("methodInput" );
@@ -53,7 +27,7 @@ public Object bad3(@PathVariable("beanIdOrClassName") String beanIdOrClassName,
5327 return invokeService (beanIdOrClassName , methodName , null , rawData );
5428 }
5529
56- @ GetMapping (value = "uf3 " )
30+ @ GetMapping (value = "uf1 " )
5731 public void good1 (HttpServletRequest request ) throws Exception {
5832 HashSet <String > hashSet = new HashSet <>();
5933 hashSet .add ("com.example.test1" );
@@ -71,7 +45,7 @@ public void good1(HttpServletRequest request) throws Exception {
7145 }
7246 }
7347
74- @ GetMapping (value = "uf4 " )
48+ @ GetMapping (value = "uf2 " )
7549 public void good2 (HttpServletRequest request ) throws Exception {
7650 String className = request .getParameter ("className" );
7751 String parameterValue = request .getParameter ("parameterValue" );
@@ -86,21 +60,6 @@ public void good2(HttpServletRequest request) throws Exception {
8660 }
8761 }
8862
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-
10463 private Object invokeService (String beanIdOrClassName , String methodName , MultipartFile [] files , List <Object > data ) throws Exception {
10564 BeanFactory beanFactory = new BeanFactory ();
10665 try {
0 commit comments