views:

864

answers:

1

I need an applet to open a socket and communicate with a server listening on the local host to which the applet downloaded (the end user machine).

contrary to what I have read about applet security, it seems that even signed applets cannot open a socket to a different host from which they were downloaded (on the same machine it works perfectly)

I have certified the applet using -selfcert, signed it using jarsigner, and still, whenever it tries to open a socket to a different host, I get:

Java.lang.Exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:9999 connect,resolve)

I even tried changing the Java policy file, although with signed applets it is not required to do so:

grant codeBase "http://applethost:8080/socket" { permission java.security.AllPermission; permission java.lang.RuntimePermission "usePolicy"; };

What is the deal with sigend applets, can they connet to a different host or not?

A: 

Yes, when you load your applet, if you choose to accept its certificate and trust it, it is granted AllPermission, which includes SocketPermission. I have written a signed applet before that connects to a host other than the one from which it was loaded. You could try temporarily changing your java policy file to just have

grant {
  permission java.security.AllPermission;
};
  • Look in your policy file to see if it defines any other policy.url locations, perhaps they are interfering.
  • Check your browser settings for javascript maybe.
  • Make sure that you accepted the certificate for the applet and that it gets installed to your list of site certificates.
  • Make sure the grant codeBase line you have is the same as the codebase in your applet's manifest.
  • You could try printing out the list of permissions that your applet has before you try the connect.
  • You could try to programatically grant AllPermission from within the applet.
John Ellinwood
@John, it seems that just granting permission java.security.AllPermission; works but that is still the only way I could make it work. I made the applet print its codeBase and it seems fine. Very frustrating, but at least thanks to you I know it is security related indeed and not some weird issue.
adilei
@John, I am going to accept this answer since it's true - it is possible for an applet to open a socket to a different host. I am going to set up a follow up question though if you are interested in following me there. Cheers, Adi.
adilei
How can you "programatically grant AllPermission"? What is the point of having a security manager to prevent execution of malicious code if the code can just grant itself permissions to do whatever it wants? It that is even possible, it shouldn't be!. @John, if you read this, could you please delete that last bullet to prevent other people from going down the wrong trail by trying to implement something the Java standard explicitly prevents. Either that or explain why I'm wrong. Thanks!
A. Levy
I know this is old, but you can't do this programatically. It would defeat the whole purpose of applet security.The proper way to get the permissions you need is to sign the jar. And wrap all of your code in a privileged block.
Knife-Action-Jesus