[ 
https://issues.apache.org/jira/browse/JEXL-375?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Henri Biestro closed JEXL-375.
------------------------------

> Cannot access enums by their name when using sandbox
> ----------------------------------------------------
>
>                 Key: JEXL-375
>                 URL: https://issues.apache.org/jira/browse/JEXL-375
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 3.2.1
>            Reporter: Jan Klička
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.3
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> Hello,
>  
> when using sandbox, accessing enum value via the dot notation fails with an 
> exception `JexlException$Property: JexlTest.addressTypeTest:186 undefined 
> property 'DOMICILE'`.
>  
> The only workaround I have found was to use the `valueOf` method on the enum.
>  
> I tried tracking down the cause. I believe the issue lies in 
> `SandboxUberspect.getPropertyGet` on line
> `final String actual = sandbox.read(obj.getClass(), property)`
> where obj is the class of the enum, and thus the permission gets read for 
> `Class<Object>` (result of `obj.getClass()`) instead of the enum.
>  
> Contrast this to the code in `Uberspect.getPropertyGet` , where it tries to 
> also resolve against `obj`, not only its superclass.
> ```
> executor = FieldGetExecutor.discover(is, claz, property);
> // static class fields (enums included)
> if (obj instanceof Class<?>)
> {    executor = FieldGetExecutor.discover(is, (Class<?>) obj, property); }
> ```
>  
> Here is unit test demonstrating this issue.
>  
> {code:java}
> @Test
> public void addressTypeTest() {
>     JexlSandbox jexlSandbox = new JexlSandbox(false);
>     jexlSandbox.allow(Type.class.getName());
>     JexlEngine engine = new JexlBuilder()
>             .sandbox(jexlSandbox)
>             .create();
>     var context = new HashMap<String, Object>();
>     context.put("Type", Type.class);
>     Type typeValueOf = (Type) engine.createScript(
>             "return Type.valueOf('DOMICILE');\n"
>     ).execute(new MapContext(context));
>     assertEquals(Type.DOMICILE, typeValueOf);
>     Type typeDirect = (Type) engine.createScript(
>             "return Type.DOMICILE;\n"
>     ).execute(new MapContext(context));
>     assertEquals(Type.DOMICILE, typeDirect);
> }
> public enum Type {
>     DELIVERY_ADDRESS,
>     DOMICILE
> }
>  {code}
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to