r/javahelp • u/davidalayachew • Jun 21 '24
How does Java handle untrusted code?
When I run Java code, Java has access to basically everything on my computer.
It can read/update/delete most of my files.
It can send messages to/from the internet.
It can pop up a million pop ups whenever it wants.
It can even control my mouse, keyboard, screen, printer, etc.
As a result, it seems like the only recourse for non-technical users is to pray really hard, or phone a technical friend. Otherwise, there's no way for them to know whether or not this Java program will tear their computer apart.
Compare that to the JavaScript world. Yes, they run on the browser instead of directly on the OS. But in the JavaScript world, code is untrusted by default.
If I want access to the camera or microphone, I must ask the user for permission.
If I want to read or save a file, I must ask the user for permission.
If I want to make a popup or go full screen, I must ask the user for permission.
Obviously, this strategy isn't flawless. The user's personal info that is stored in the browser can still be exploited. But even then, the level of damage one can do with Java code on the desktop vs JavaScript code in the browser is night and day.
I understand that that is comparing apples and oranges, but my question remains -- does Java have any way to sandbox an application, so that it can only break out with explicit acknowledgement and permission from the user?
2
u/blobjim Jun 21 '24
It looks like it might be possible to use Java inside a flatpak on Linux: https://github.com/flathub/org.freedesktop.Sdk.Extension.openjdk
It looks like the file org.freedesktop.Sdk.Extension.openjdk.yaml contains the details. I'm not sure if there are any actually published apps using it.
Someone has a java project to use the GTK UI library from Java (there used to be an official set of bindings a long time ago I think, but it wasn't really updated): https://github.com/bailuk/java-gtk
probably works better than trying to use java swing from a flatpak sandbox, because GTK presumably has integration with the various flatpak portals.
It would definitely be difficult to create a truly sandboxed app on something other than Android but it may be possible with enough work to do so on "desktop" linux. I assume it's gonna be a lot of work learning flatpak and making it work though, but I could be wrong.
It's a really good question!