javaappletaccess-deniedgetparameter

Java.security.Access.ControlException: access denied("java.io.FilePermission" "[object file]" "read")


I have the following problem:

I have a Java program that receives via Applet a binary file.

I received that file with getParameter(file) and read that file with java.io.FileInputStream(file).

I put this file on web server and call the java program via javascript

First, when I was running the program, was occuring the message error:

Java.security.Access.ControlException: access denied("java.io.FilePermission" "[object file]" "read")

I created a key via keytool and signed the jar file with jarsigner.

But, even executing the command jarsigner, when I run the Java program again, the error message continues occuring:

Java.security.Access.ControlException: access denied("java.io.FilePermission" "[object file]" "read"). 

Therefore, the error persists even after signing.

I really do not know what to do.

Can anyone help me?

Below the java code:

public class InJava extends Applet{
    String parametro;    
    public void sayHello() {

        parametro = getParameter("parametro");

        java.io.FileInputStream fis = null;

        try  {
            fis = new java.io.FileInputStream(parametro);
        }
        catch (Exception e)  {
            String retorno_exc = e.toString();
            return ;
        }

    }

Solution

  • You need to wrap your code in AccessController.doPrivileged, like:

    public class InJava extends Applet{
    
      public void sayHello() {
    
        final String parametro = getParameter("parametro");
    
        FileInputStream fis =  AccessController.doPrivileged(new PrivilegedAction<FileInputStream>() {
          public FileInputStream run() {
            try  {
              retrun new FileInputStream(parametro);
            } catch (IOException e)  {
              // handle exception
            }
          }
        });
      }
    

    Make sure that your applet jar(s) are signed, and that you understand all other consequences of running an applet.