private static CodeSource createCodeSource(final String codeBase) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new GroovyCodeSourcePermission(codeBase)); } try { return new CodeSource(new URL("file", "", codeBase), (java.security.cert.Certificate[]) null); } catch (MalformedURLException e) { throw new RuntimeException("A CodeSource file URL cannot be constructed from the supplied codeBase: " + codeBase); } }
private PermissionCollection getPermissionCollection(Subject subject) { return getPolicy().getPermissions( new ProtectionDomain( new CodeSource(null, (Certificate[]) null), null, null, subject.getPrincipals().toArray(new Principal[subject.getPrincipals().size()]) ) ); }
public static boolean hasAccess(String uri, Subject subject) { return getPolicy().implies( new ProtectionDomain( new CodeSource(null, (Certificate[]) null), null, null, subject.getPrincipals().toArray(new Principal[subject.getPrincipals().size()]) ), new WebResourcePermission(uri, "GET") ); } }
public Object[] run() throws IOException { // retrieve the content of the file using the provided encoding if (encoding != null) { scriptText = ResourceGroovyMethods.getText(infile, encoding); } else { scriptText = ResourceGroovyMethods.getText(infile); } Object[] info = new Object[2]; URL url = file.toURI().toURL(); info[0] = url.toExternalForm(); //toURI().toURL() will encode, but toURL() will not. info[1] = new CodeSource(url, (Certificate[]) null); return info; } });
private ProtectionDomain get(String name) { URL url = url(name); if (url==null) return null; ProtectionDomain pd = null; synchronized (cache) { pd = cache.get(url); if (pd == null) { CodeSource cs = new CodeSource(url,(CodeSigner []) null); pd = new ProtectionDomain(cs, null, this, null); cache.put(url, pd); } } return pd; }
Class<?> createClass(InputStream is, String packageName, String origName) { if (is == null) { return null; } byte[] clBuf; try { clBuf = Streams.readFully(is); } catch (IOException e) { return null; } if (packageName != null) { String packageDotName = packageName.replace('/', '.'); Package packageObj = getPackage(packageDotName); if (packageObj == null) { definePackage(packageDotName, null, null, null, null, null, null, null); } else { if (packageObj.isSealed()) { throw new SecurityException("Package is sealed"); } } } return defineClass(origName, clBuf, 0, clBuf.length, new CodeSource(codeSourceUrl, (Certificate[]) null)); }
public GroovyCodeSource(URL url) { if (url == null) { throw new RuntimeException("Could not construct a GroovyCodeSource from a null URL"); } this.url = url; // TODO: GROOVY-6561: GroovyMain got the name this way: script.substring(script.lastIndexOf("/") + 1) this.name = url.toExternalForm(); this.codeSource = new CodeSource(url, (java.security.cert.Certificate[]) null); try { String contentEncoding = getContentEncoding(url); if (contentEncoding != null) { this.scriptText = ResourceGroovyMethods.getText(url, contentEncoding); } else { this.scriptText = ResourceGroovyMethods.getText(url); // falls-back on default encoding } } catch (IOException e) { throw new RuntimeException("Impossible to read the text content from " + name, e); } }
classData, 0, classData.length, targetClassLoader, new ProtectionDomain(new CodeSource(null, (Certificate[]) null), new Permissions()));
CodeSource codeS = new CodeSource(codeSourceUrl, entry.getCertificates()); return defineClass(origName, clBuf, 0, clBuf.length, codeS);
/** * Loads the given class node returning the implementation Class. * <p> * WARNING: this compilation is not synchronized * * @param classNode * @return a class */ public Class defineClass(ClassNode classNode, String file, String newCodeBase) { CodeSource codeSource = null; try { codeSource = new CodeSource(new URL("file", "", newCodeBase), (java.security.cert.Certificate[]) null); } catch (MalformedURLException e) { //swallow } CompilationUnit unit = createCompilationUnit(config, codeSource); ClassCollector collector = createCollector(unit, classNode.getModule().getContext()); try { unit.addClassNode(classNode); unit.setClassgenCallback(collector); unit.compile(Phases.CLASS_GENERATION); definePackageInternal(collector.generatedClass.getName()); return collector.generatedClass; } catch (CompilationFailedException e) { throw new RuntimeException(e); } }
/** * Define a class given its bytes * * @param container the container from which the class data has been read * may be a directory or a jar/zip file. * * @param classData the bytecode data for the class * @param classname the name of the class * * @return the Class instance created from the given data * * @throws IOException if the class data cannot be read. */ protected Class defineClassFromData(File container, byte[] classData, String classname) throws IOException { definePackage(container, classname); ProtectionDomain currentPd = Project.class.getProtectionDomain(); String classResource = getClassFilename(classname); CodeSource src = new CodeSource(FILE_UTILS.getFileURL(container), getCertificates(container, classResource)); ProtectionDomain classesPd = new ProtectionDomain(src, currentPd.getPermissions(), this, currentPd.getPrincipals()); return defineClass(classname, classData, 0, classData.length, classesPd); }
final CodeSource codeSource = new CodeSource(codeSourceUrl, (CodeSigner[]) null); final Class<?> definedClass; try {
/** * Define a class given its bytes * * @param container the container from which the class data has been read * may be a directory or a jar/zip file. * * @param classData the bytecode data for the class * @param classname the name of the class * * @return the Class instance created from the given data * * @throws IOException if the class data cannot be read. */ protected Class<?> defineClassFromData(final File container, final byte[] classData, final String classname) throws IOException { definePackage(container, classname); final ProtectionDomain currentPd = Project.class.getProtectionDomain(); final String classResource = getClassFilename(classname); final CodeSource src = new CodeSource(FILE_UTILS.getFileURL(container), getCertificates(container, classResource)); final ProtectionDomain classesPd = new ProtectionDomain(src, currentPd.getPermissions(), this, currentPd.getPrincipals()); return defineClass(classname, classData, 0, classData.length, classesPd); }
File jarFile = new File(path.substring(5, index)); try { codeSource = new CodeSource(jarFile.toURI().toURL(), (CodeSigner[]) null); } catch (MalformedURLException e) { logger.error(e.getMessage(), e);
/** * Constructor */ public DocumentJarClassLoader(URL jarURL, URL documentURL){ super(new URL[]{jarURL}); if (documentURL != null) { documentCodeSource = new CodeSource (documentURL, (Certificate[])null); } }
public JavaPolicySecurity() { // To trigger error on jdk-1.1 with lazy load new CodeSource(null, (java.security.cert.Certificate[])null); }
public RobotClassLoader(URL robotClassPath, String robotFullClassName) { super(new URL[] { robotClassPath}, Container.systemLoader); fullClassName = robotFullClassName; parent = getParent(); try { codeSource = new CodeSource(new URL(UNTRUSTED_URL), (Certificate[]) null); } catch (MalformedURLException ignored) {} }
private Class<?> defineClass(String name, byte[] b) throws IOException { CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[]) null); if (!name.equals(TRAMPOLINE)) { throw new IOException("MethodUtil: bad name " + name); } return defineClass(name, b, 0, b.length, cs); }
private CodeSource createCodeSource(final JarEntry entry) { final CodeSigner[] entryCodeSigners = entry.getCodeSigners(); final CodeSigners codeSigners = entryCodeSigners == null || entryCodeSigners.length == 0 ? EMPTY_CODE_SIGNERS : new CodeSigners(entryCodeSigners); CodeSource codeSource = codeSources.get(codeSigners); if (codeSource == null) { codeSources.put(codeSigners, codeSource = new CodeSource(rootUrl, entryCodeSigners)); } return codeSource; }
@Test public void canCreateFacadeAroundSingleCertificate() throws Exception { // Act: final URL url = new URL("http://nem.com/foo/n.jar"); final Certificate c1 = Mockito.mock(X509Certificate.class); final CodeSourceFacade facade = new CodeSourceFacade(new CodeSource(url, new Certificate[] { c1 })); // Assert: Assert.assertThat(facade.getLocation(), IsEqual.equalTo(url)); Assert.assertThat(facade.getFirstCertificate(), IsSame.sameInstance(c1)); }