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?
14
u/NitronHX Jun 21 '24 edited Jun 21 '24
This question is not related to java but to any non-js language. C c++ kotlin .Net(C#), rust, zig ....
Also this is not a issue of aforementioned languages but platforms they run on. For example in android java is somewhat sandboxed since Android apps require permissions on this platform for camera, files ...
So the default way to run desktop apps is unsafe/trusted let's call it the "windows/Linux platform". There are ways to run desktop apps in a sandboxed environment if untrusted. For example flatpack is a Linux packaging format where all programms run sandboxed. On Linux you can use
chrootif it's about file access that you are. Windows has a desktop app called sandboxie that's a well sandbox for other desktop apps. Maybe you run a Linux distro that is focused on sandboxing and security, where every time you login a empheral container is started.When you run java in the browser "platform" (yes this is still possible) it runs as "safely" as js.
Binaries are as unsafe as the platforms they run on, and the OS is not a safe platform
Do I assume correctly that your question is "how do I deal with untrusted binaries?"