r/javahelp • • 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?

3 Upvotes

27 comments sorted by

View all comments

5

u/_jetrun Jun 21 '24 edited Jun 21 '24

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.

Yes. That's true for ANY native application. It has nothing to do with Java specifically.

But in the JavaScript world, code is untrusted by default.

No it's not.

Run JavaScript in a node container or as an electron app and see what happens. You'll be able to access any OS resource at the privilege level of the user that executed the process.

Browsers, on the other hand, need to heavily sandbox executing code because otherwise any random page you accidentally navigated to could maliciously take over your PC. That's not quite the same kind of risk vector that native application expose, because installing/executing a native application requires you to give EXPLCIT permission, unlike navigating to some page.

Browser sandboxing does come at a cost in that there are types of applications that you cannot build. For example, I cannot build a web application that can position itself on a specific monitor and/or span multiple monitors. I cannot build a web application that can index my hard drive and sync files on my hard drive with the cloud. I cannot build an application that can connect to an SFTP server on my local network and pull down some files. etc. etc. etc.

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.

Because one is a native application, and one is a browser application. Native applications will have direct access to most OS resources, doesn't matter if they are built with Java, C++, Go, Rust, .NET, and yes, even JavaScript.

OP, there has been a trend by OS-vendors to introduce more and more sandboxing of native applications, with some platforms being completely closed-off and fully controlled by the OS-vendor (i.e. iOS). Those OS-vendors can then demand a percentage cut of all your revenue (doesn't matter the source), or capriciously prevent you from building certain types of applications (like a competing browser engine), or kick you off the platform and you're not long able to build any application. I contend that this isn't a good thing, even if there are security benefits. Sandboxing has its place, but don't dismiss the freedom you have with non-sandboxed code.

1

u/davidalayachew Jun 21 '24

Very insightful, ty vm.

I now understand the difference between a native application and a browser application. And it makes sense why the fence is there for one and not the other.

I also see the weakness in sandboxing -- the fences that protect the users can also be used to exploit content creators and force them to follow every unreasonable demand you please, if they want to be on your platform. I've seen the fences rising, so what you are saying makes clear sense to me.

Based on this comment, I think my opinion has changed. It seems that the only true recourse here is that users just need to be smarter about software. They need to be able to better understand what they are running, and why it is running. Frankly, they need to start understanding code.

I had always assumed that we could someday live in a world where each person could specialize in the things that they wanted to, and delegate even the basic understanding of the rest to those who choose to be experts in this. But I think that that simply isn't possible. It sounds like, no matter what, no true trust or safety can occur until people truly understand what it is they are using, and how it came to be. They don't necessarily need to be an expert, and they certainly don't need to know how to make the things they rely on. But if they don't even understand the basics of the software running on their screen, then I now doubt that they are capable of safely executing any non-trivial software.

Very insightful, ty vm.