Description
Problem 1.
If you name a class sun.misc.Unsafe and put it in your classpath, it does not change the fact, any code calling that will still be calling the real Unsafe. But it does hide it from forbidden-apis!!!
This is because classloader order, when the application is actually used, is Bootstrap->Extensions->System (App), as explained here: http://docs.oracle.com/javase/tutorial/ext/basics/load.html
But forbidden-apis checks the wrong order, it checks the ones you provide first, because it uses lookupRelatedClass():
forbidden-apis/src/main/java/de/thetaphi/forbiddenapis/Checker.java
Lines 303 to 306 in 9de5e1f
This causes the interesting scenario when trying to cleanup crazy classpaths, where removing a jar can cause new violations in your build :)
Problem 2:
The current isRuntimeClass()
does not seem to check for extensions at all, but only against bootstrap classpath. This hides additional internal accesses, e.g. jdk.nashorn.internal
, which will cause a SecurityException if you try to use it.
So can we use the following code on pre-jigsaw, to identify extensions jars and treat them as "internal" too? I think this extensions idea goes away with jigsaw, and everything is just modules, so it should not be a problem that we can't get extensions jars/directories there.
// of course with proper checks and best-effort, not guaranteed but works.
URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader().getParent();
URL extensions[] = loader.getURLs();
Problem 3:
Internal checking has a hardcoded list of simple patterns:
Can we use java.security.Security.getProperty("package.access")
instead? That property is set by the JDK, in e.g. the jre/lib/security/java.security
configuration file for security checks against internal apis:
#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted.
package.access=sun.,\
com.sun.xml.internal.,\
com.sun.imageio.,\
com.sun.istack.internal.,\
com.sun.jmx.,\
com.sun.media.sound.,\
com.sun.naming.internal.,\
com.sun.proxy.,\
... (many lines) ...