If there is a system where the client writes Java code (*.java files), and submits them to a server running in Java, how can I make the submitted code not do unwanted and malicious things?
For a little more depth: The client would write the plain text Java code (their classes), and the code would be sent to the server. On the server, the code would be inspected to make sure it only does allowed actions. If it passes the test, it gets compiled into a .class file, where it is then loaded with Java's class loader. Finally, using Reflection, methods can then be called and passed parameters.
I think I can see how to do most of that, but how do I make sure code that's compiled is "safe"? For example, I wouldn't want it to spawn threads, or perform file IO, or interact with the system it's running on in any way. I was thinking that I could scan the input file for imports, and if they are not on a white list, reject the file. On top of that, I was thinking that if I did want to limit something, like say the number of threads, I could allow the import of a proxy class that would keep track of threads per user.
Would that work, or if not is there a way?