|
| 1 | +import java.io.IOException; |
| 2 | +import java.io.Reader; |
| 3 | + |
| 4 | +import javax.servlet.http.HttpServlet; |
| 5 | +import javax.servlet.http.HttpServletRequest; |
| 6 | +import javax.servlet.http.HttpServletResponse; |
| 7 | + |
| 8 | +import flexjson.JSONDeserializer; |
| 9 | +import flexjson.factories.ExistingObjectFactory; |
| 10 | + |
| 11 | +import com.example.User; |
| 12 | +import com.thirdparty.Person; |
| 13 | + |
| 14 | +public class FlexjsonServlet extends HttpServlet { |
| 15 | + |
| 16 | + private static final long serialVersionUID = 1L; |
| 17 | + |
| 18 | + @Override |
| 19 | + // GOOD: a final class type is specified |
| 20 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 21 | + JSONDeserializer<User> deserializer = new JSONDeserializer<>(); |
| 22 | + User user = deserializer.deserialize(req.getReader(), User.class); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + // GOOD: a non-null class type is specified which is not the generic `Object` type |
| 27 | + public void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 28 | + JSONDeserializer<Person> deserializer = new JSONDeserializer<Person>(); |
| 29 | + Person person = deserializer.deserialize(req.getReader(), Person.class); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + // BAD: allow class name to be controlled by remote source |
| 34 | + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 35 | + JSONDeserializer<User> deserializer = new JSONDeserializer<>(); |
| 36 | + User user = (User) deserializer.deserialize(req.getReader()); // $unsafeDeserialization |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + // BAD: allow class name to be controlled by remote source |
| 42 | + public void doTrace(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 43 | + JSONDeserializer deserializer = new JSONDeserializer<>(); |
| 44 | + User user = (User) deserializer.deserialize(req.getReader()); // $unsafeDeserialization |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + // BAD: specify overly generic class type |
| 50 | + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 51 | + JSONDeserializer deserializer = new JSONDeserializer(); |
| 52 | + User user = (User) deserializer.deserialize(req.getReader(), Object.class); // $unsafeDeserialization |
| 53 | + } |
| 54 | + |
| 55 | + private Person fromJsonToPerson(String json) { |
| 56 | + return new JSONDeserializer<Person>().use(null, Person.class).deserialize(json); |
| 57 | + } |
| 58 | + |
| 59 | + // GOOD: Specify the class to deserialize with `use` |
| 60 | + public void doPut2(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 61 | + String json = req.getParameter("json"); |
| 62 | + Person person = fromJsonToPerson(json); |
| 63 | + } |
| 64 | + |
| 65 | + // BAD: Specify a concrete class type to `use` with `ObjectFactory` |
| 66 | + public void doPut3(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 67 | + String json = req.getParameter("json"); |
| 68 | + Person person = new JSONDeserializer<Person>().use(Person.class, new ExistingObjectFactory(new Person())).deserialize(json); // $unsafeDeserialization |
| 69 | + } |
| 70 | + |
| 71 | + // GOOD: Specify a null path to `use` with a concrete class type |
| 72 | + public void doPut4(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 73 | + String json = req.getParameter("json"); |
| 74 | + Person person = new JSONDeserializer<Person>().use(null, Person.class).deserialize(json); |
| 75 | + } |
| 76 | + |
| 77 | + // BAD: Specify a non-null json path to `use` with a concrete class type |
| 78 | + public void doPut5(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 79 | + String json = req.getParameter("json"); |
| 80 | + Person person = new JSONDeserializer<Person>().use("abc", Person.class).deserialize(json); // $unsafeDeserialization |
| 81 | + } |
| 82 | + |
| 83 | + // GOOD: Specify a null json path to `use` with `ObjectFactory` |
| 84 | + public void doPut6(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 85 | + String json = req.getParameter("json"); |
| 86 | + Person person = new JSONDeserializer<Person>().use(new ExistingObjectFactory(new Person()), "abc", null).deserialize(json); |
| 87 | + } |
| 88 | + |
| 89 | + // GOOD: Specify a concrete class type to deserialize with `ObjectFactory` |
| 90 | + public void doPut7(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 91 | + String json = req.getParameter("json"); |
| 92 | + Person person = new JSONDeserializer<Person>().deserialize(json, new ExistingObjectFactory(new Person())); |
| 93 | + } |
| 94 | + |
| 95 | + // GOOD: Specify the class type to deserialize into |
| 96 | + public void doPut8(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 97 | + String json = req.getParameter("json"); |
| 98 | + Person person = new JSONDeserializer<Person>().deserializeInto(json, new Person()); |
| 99 | + } |
| 100 | + |
| 101 | + // GOOD: Specify a null json path to `use` with a concrete class type, interwoven with irrelevant use directives |
| 102 | + public void doPut9(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 103 | + String json = req.getParameter("json"); |
| 104 | + Person person = new JSONDeserializer<Person>().use(Person.class, null).use(null, Person.class).use(String.class, null).deserialize(json); |
| 105 | + } |
| 106 | + |
| 107 | + // GOOD: Specify a null json path to `use` with a concrete class type, interwoven with irrelevant use directives, without using fluent method chaining |
| 108 | + public void doPut10(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 109 | + String json = req.getParameter("json"); |
| 110 | + JSONDeserializer<Person> deserializer = new JSONDeserializer<Person>(); |
| 111 | + deserializer.use(Person.class, null); |
| 112 | + deserializer.use(null, Person.class); |
| 113 | + deserializer.use(String.class, null); |
| 114 | + Person person = deserializer.deserialize(json); |
| 115 | + } |
| 116 | + |
| 117 | + // BAD: Specify a non-null json path to `use` with a concrete class type, interwoven with irrelevant use directives, without using fluent method chaining |
| 118 | + public void doPut11(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 119 | + String json = req.getParameter("json"); |
| 120 | + JSONDeserializer<Person> deserializer = new JSONDeserializer<Person>(); |
| 121 | + deserializer.use(Person.class, null); |
| 122 | + deserializer.use("someKey", Person.class); |
| 123 | + deserializer.use(String.class, null); |
| 124 | + Person person = deserializer.deserialize(json); // $unsafeDeserialization |
| 125 | + } |
| 126 | +} |
0 commit comments