Search in sources :

Example 11 with PropertyAccessException

use of org.mvel2.PropertyAccessException in project mvel by mikebrock.

the class PropertyAccessor method getCollectionPropertyAO.

private Object getCollectionPropertyAO(Object ctx, String prop) throws Exception {
    if (prop.length() != 0) {
        ctx = getBeanProperty(ctx, prop);
    }
    if (ctx == null)
        return null;
    int _start = ++cursor;
    whiteSpaceSkip();
    if (cursor == end || scanTo(']'))
        throw new PropertyAccessException("unterminated '['", property, cursor);
    prop = new String(property, _start, cursor++ - _start);
    if (ctx instanceof Map) {
        if (hasPropertyHandler(Map.class))
            return getPropertyHandler(Map.class).getProperty(prop, ctx, variableFactory);
        else
            return ((Map) ctx).get(eval(prop, ctx, variableFactory));
    } else if (ctx instanceof List) {
        if (hasPropertyHandler(List.class))
            return getPropertyHandler(List.class).getProperty(prop, ctx, variableFactory);
        else
            return ((List) ctx).get((Integer) eval(prop, ctx, variableFactory));
    } else if (ctx instanceof Collection) {
        if (hasPropertyHandler(Collection.class))
            return getPropertyHandler(Collection.class).getProperty(prop, ctx, variableFactory);
        else {
            int count = (Integer) eval(prop, ctx, variableFactory);
            if (count > ((Collection) ctx).size())
                throw new PropertyAccessException("index [" + count + "] out of bounds on collections", property, cursor);
            Iterator iter = ((Collection) ctx).iterator();
            for (int i = 0; i < count; i++) iter.next();
            return iter.next();
        }
    } else if (ctx.getClass().isArray()) {
        if (hasPropertyHandler(Array.class))
            return getPropertyHandler(Array.class).getProperty(prop, ctx, variableFactory);
        return Array.get(ctx, (Integer) eval(prop, ctx, variableFactory));
    } else if (ctx instanceof CharSequence) {
        if (hasPropertyHandler(CharSequence.class))
            return getPropertyHandler(CharSequence.class).getProperty(prop, ctx, variableFactory);
        else
            return ((CharSequence) ctx).charAt((Integer) eval(prop, ctx, variableFactory));
    } else {
        try {
            return getClassReference(getCurrentThreadParserContext(), (Class) ctx, new TypeDescriptor(property, start, end - start, 0));
        } catch (Exception e) {
            throw new PropertyAccessException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), property, st);
        }
    }
}
Also used : TypeDescriptor(org.mvel2.ast.TypeDescriptor)

Example 12 with PropertyAccessException

use of org.mvel2.PropertyAccessException in project drools by kiegroup.

the class SecurityPolicyTest method testUntrustedMVELEnabled.

@Test
public void testUntrustedMVELEnabled() throws Exception {
    String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule R1 dialect \"mvel\" enabled( MaliciousExitHelper.isEnabled() ) \n" + "when\n" + "then\n" + "end\n";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (PropertyAccessException e) {
        // weak way of testing but couldn't find a better way
        if (e.toString().contains("The security policy should have prevented")) {
            Assert.fail("The security policy for the rule should have prevented this from executing...");
        } else {
        // test succeeded
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) PropertyAccessException(org.mvel2.PropertyAccessException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 13 with PropertyAccessException

use of org.mvel2.PropertyAccessException in project drools by kiegroup.

the class SecurityPolicyTest method testUntrustedMVELSalience.

@Test
public void testUntrustedMVELSalience() throws Exception {
    String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule R1 dialect \"mvel\" salience( MaliciousExitHelper.exit() ) \n" + "when\n" + "then\n" + "end\n";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (PropertyAccessException e) {
        // weak way of testing but couldn't find a better way
        if (e.toString().contains("The security policy should have prevented")) {
            Assert.fail("The security policy for the rule should have prevented this from executing...");
        } else {
        // test succeeded
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) PropertyAccessException(org.mvel2.PropertyAccessException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 KieServices (org.kie.api.KieServices)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5 ReleaseId (org.kie.api.builder.ReleaseId)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieSession (org.kie.api.runtime.KieSession)5 PropertyAccessException (org.mvel2.PropertyAccessException)5 IOException (java.io.IOException)3 Map (java.util.Map)3 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)3 List (java.util.List)2 Label (org.mvel2.asm.Label)2 Function (org.mvel2.ast.Function)2 TypeDescriptor (org.mvel2.ast.TypeDescriptor)2 ArrayList (java.util.ArrayList)1 WeakHashMap (java.util.WeakHashMap)1 Proto (org.mvel2.ast.Proto)1 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)1 PropertyVerifier (org.mvel2.compiler.PropertyVerifier)1 PropertyHandler (org.mvel2.integration.PropertyHandler)1