javaeclipsesecuritymanagercodebasesecurity-policy

Exception: access denied ("java.net.SocketPermission" "localhost:80" "listen,resolve")


Using Eclipse Kepler (Windows 7) for a project which opens a ServerSocket on localhost, port 80.

I use a security manager with a policy file located at:

C:\Users\John\Developpement\workspace\security\my.policy

In Eclipse, for the project launch configuration properties, for VM arguments:

-Djava.security.manager
-Djava.security.policy=${workspace_loc}/security/my.policy

The bin file executed is (I use separate source and output folders in Eclipse):

C:\Users\John\Developpement\workspace\SocketApps\bin\TinyHttpd.class

In my.policy:

grant codeBase "file:\C:\Users\John\Developpement\workspace\SocketApps\bin\-" {
    permission java.net.SocketPermission "localhost:80", "listen,resolve";
};

When running from Eclipse:

Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:80" "listen,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
    at java.security.AccessController.checkPermission(AccessController.java:559)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkListen(SecurityManager.java:1134)
    at java.net.ServerSocket.bind(ServerSocket.java:375)
    at java.net.ServerSocket.<init>(ServerSocket.java:237)
    at java.net.ServerSocket.<init>(ServerSocket.java:128)
    at TinyHttpd.main(TinyHttpd.java:35)

when reaching code:

ServerSocket ss = new ServerSocket(80));

If I remove the codeBase filter:

grant {
    permission java.net.SocketPermission "localhost:80", "listen,resolve";
};

the problem disappears, so I imagine this is the way the codeBase is expressed that is wrong.

I've tried the solution proposed for this question, but it doesn't work. Can you help me?


Solution

  • Answering my own question since I found what was the problem. Not sure if it is better to remove the question, it seems to me that keeping it would help other persons. Moderators to say.

    Taken from Oracle documentation:

    Note: a codeBase value is a URL and thus should always utilize slashes (never backslashes) as the directory separator, even when the code source is actually on a Win32 system. Thus, if the source location for code on a Win32 system is actually C:\somepath\api\, then the policy codeBase entry should look like:

    grant codeBase "file:/C:/somepath/api/" {
        ...
    }
    

    This is a beginner mistake.