|
| 1 | +import java.lang.reflect.Method; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import javax.servlet.http.HttpServletRequest; |
| 7 | +import org.springframework.stereotype.Controller; |
| 8 | +import org.springframework.util.StringUtils; |
| 9 | +import org.springframework.web.bind.annotation.GetMapping; |
| 10 | +import org.springframework.web.bind.annotation.PathVariable; |
| 11 | +import org.springframework.web.bind.annotation.RequestBody; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 14 | +import org.springframework.web.multipart.MultipartFile; |
| 15 | + |
| 16 | +@Controller |
| 17 | +public class UnsafeReflection { |
| 18 | + |
| 19 | + @RequestMapping(value = {"/service/{beanIdOrClassName}/{methodName}"}, method = {RequestMethod.POST}, consumes = {"application/json"}, produces = {"application/json"}) |
| 20 | + public Object bad1(@PathVariable("beanIdOrClassName") String beanIdOrClassName, @PathVariable("methodName") String methodName, @RequestBody Map<String, Object> body) throws Exception { |
| 21 | + List<Object> rawData = null; |
| 22 | + try { |
| 23 | + rawData = (List<Object>)body.get("methodInput"); |
| 24 | + } catch (Exception e) { |
| 25 | + return e; |
| 26 | + } |
| 27 | + return invokeService(beanIdOrClassName, methodName, null, rawData); |
| 28 | + } |
| 29 | + |
| 30 | + @GetMapping(value = "uf1") |
| 31 | + public void good1(HttpServletRequest request) throws Exception { |
| 32 | + HashSet<String> hashSet = new HashSet<>(); |
| 33 | + hashSet.add("com.example.test1"); |
| 34 | + hashSet.add("com.example.test2"); |
| 35 | + String className = request.getParameter("className"); |
| 36 | + String parameterValue = request.getParameter("parameterValue"); |
| 37 | + if (!hashSet.contains(className)){ |
| 38 | + throw new Exception("Class not valid: " + className); |
| 39 | + } |
| 40 | + try { |
| 41 | + Class clazz = Class.forName(className); |
| 42 | + Object object = clazz.getDeclaredConstructors()[0].newInstance(parameterValue); //good |
| 43 | + } catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @GetMapping(value = "uf2") |
| 49 | + public void good2(HttpServletRequest request) throws Exception { |
| 50 | + String className = request.getParameter("className"); |
| 51 | + String parameterValue = request.getParameter("parameterValue"); |
| 52 | + if (!"com.example.test1".equals(className)){ |
| 53 | + throw new Exception("Class not valid: " + className); |
| 54 | + } |
| 55 | + try { |
| 56 | + Class clazz = Class.forName(className); |
| 57 | + Object object = clazz.getDeclaredConstructors()[0].newInstance(parameterValue); //good |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private Object invokeService(String beanIdOrClassName, String methodName, MultipartFile[] files, List<Object> data) throws Exception { |
| 64 | + BeanFactory beanFactory = new BeanFactory(); |
| 65 | + try { |
| 66 | + Object bean = null; |
| 67 | + Class<?> beanClass = Class.forName(beanIdOrClassName); |
| 68 | + bean = beanFactory.getBean(beanClass); |
| 69 | + byte b; |
| 70 | + int i; |
| 71 | + Method[] arrayOfMethod; |
| 72 | + for (i = (arrayOfMethod = bean.getClass().getMethods()).length, b = 0; b < i; ) { |
| 73 | + Method method = arrayOfMethod[b]; |
| 74 | + if (!method.getName().equals(methodName)) { |
| 75 | + b++; |
| 76 | + continue; |
| 77 | + } |
| 78 | + Object result = method.invoke(bean, data); |
| 79 | + Map<String, Object> map = new HashMap<>(); |
| 80 | + return map; |
| 81 | + } |
| 82 | + } catch (Exception e) { |
| 83 | + return e; |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class BeanFactory { |
| 90 | + |
| 91 | + private static HashMap<String, Object> classNameMap = new HashMap<>(); |
| 92 | + |
| 93 | + private static HashMap<Class<?>, Object> classMap = new HashMap<>(); |
| 94 | + |
| 95 | + static { |
| 96 | + classNameMap.put("xxxx", Runtime.getRuntime()); |
| 97 | + classMap.put(Runtime.class, Runtime.getRuntime()); |
| 98 | + } |
| 99 | + |
| 100 | + public Object getBean(Class<?> clzz) { |
| 101 | + return classMap.get(clzz); |
| 102 | + } |
| 103 | +} |
0 commit comments