File.canRead() doesnt throw SecurityException, If the absolute path of the file
is current working directory or any of its child directory, even SecurityMangaer is set,and Policy file doesnt contain grant for the file.
Not only File.canRead(), some of the methods of java.io.File
File.exists(),File.isDirectory(),File.isFile(),File.isHidden();File.lastModified(),File.length() behaves as mentioned above.
Consider the Directory Structure : C:/Parent/Child/GrandChild and move to the C:/Parent/Child
1) new File("C:\Parent\Child\testfile").canRead();
2) new File("C:\Parent\Child\GrandChild\testfile").canRead();
Both 1 & 2 doesnt throw SecurityException, But
3) new File("C:\Parent\testfile").canRead(); throws SecurityException.
To Reproduce the Bug.
1) create the Directory Strcture c:/Parent/Child/GrandChild
2) Move to c:/Parent/Child and copy the attached files TestSecurityEx.java,PolicyAllow.txt,TestSecurityEx.html.
3) Run using: java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
It doesnt throw any SecurityException though there is no read grant for the files c:/Parent/Child/testfile;c:/Parent/Child/GrandChild/testfile;
5) Uncomment the Line1 in TestSecurityEx.java and Run using Step3. Now it throws Exception
java.security.AccessControlException: access denied (java.io.FilePermission c:\Parent\testfile read)
It is observed that , if the absolute path of the file is not the PWD or any of its child the SecurityException is thrown otherwise it wont throw the SecurityException
If SecurityManager is set and Policy file doesnt conatin any read grant for the file it should throw SecuirtyException even though it is PWD.
That is how File.canWrite(),File.canExecute() works.
<Result>
C:\Parent\Child>java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
can read c:/Parent/Child/testfile :false
can read c:/Parent/Child/GrandChild/testfile :false
Uncomment Line1 in TestSecurityEx.java and run.
C:\Parent\Child>javac TestSecurityEx.java
C:\Parent\Child>java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
java.security.AccessControlException: access denied (java.io.FilePermission c:\Parent\testfile read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:321)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.File.canRead(File.java:689)
at TestSecurityEx.test(TestSecurityEx.java:10)
at TestSecurityEx.start(TestSecurityEx.java:23)
at sun.applet.AppletPanel.run(AppletPanel.java:457)
at java.lang.Thread.run(Thread.java:620)
</Result>
is current working directory or any of its child directory, even SecurityMangaer is set,and Policy file doesnt contain grant for the file.
Not only File.canRead(), some of the methods of java.io.File
File.exists(),File.isDirectory(),File.isFile(),File.isHidden();File.lastModified(),File.length() behaves as mentioned above.
Consider the Directory Structure : C:/Parent/Child/GrandChild and move to the C:/Parent/Child
1) new File("C:\Parent\Child\testfile").canRead();
2) new File("C:\Parent\Child\GrandChild\testfile").canRead();
Both 1 & 2 doesnt throw SecurityException, But
3) new File("C:\Parent\testfile").canRead(); throws SecurityException.
To Reproduce the Bug.
1) create the Directory Strcture c:/Parent/Child/GrandChild
2) Move to c:/Parent/Child and copy the attached files TestSecurityEx.java,PolicyAllow.txt,TestSecurityEx.html.
3) Run using: java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
It doesnt throw any SecurityException though there is no read grant for the files c:/Parent/Child/testfile;c:/Parent/Child/GrandChild/testfile;
5) Uncomment the Line1 in TestSecurityEx.java and Run using Step3. Now it throws Exception
java.security.AccessControlException: access denied (java.io.FilePermission c:\Parent\testfile read)
It is observed that , if the absolute path of the file is not the PWD or any of its child the SecurityException is thrown otherwise it wont throw the SecurityException
If SecurityManager is set and Policy file doesnt conatin any read grant for the file it should throw SecuirtyException even though it is PWD.
That is how File.canWrite(),File.canExecute() works.
<Result>
C:\Parent\Child>java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
can read c:/Parent/Child/testfile :false
can read c:/Parent/Child/GrandChild/testfile :false
Uncomment Line1 in TestSecurityEx.java and run.
C:\Parent\Child>javac TestSecurityEx.java
C:\Parent\Child>java -Djava.security.manager -Djava.security.policy==PolicyAllow.txt sun.applet.AppletViewer TestSecurityEx.html
java.security.AccessControlException: access denied (java.io.FilePermission c:\Parent\testfile read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:321)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.File.canRead(File.java:689)
at TestSecurityEx.test(TestSecurityEx.java:10)
at TestSecurityEx.start(TestSecurityEx.java:23)
at sun.applet.AppletPanel.run(AppletPanel.java:457)
at java.lang.Thread.run(Thread.java:620)
</Result>