Repository: PebbleTemplates/pebble
Pull request #436 (2019-04-16 08:40:47)

issue #435 make allowGetClass check case insensitive
Closes issue #435 


Patch:
diff --git a/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MemberCacheUtils.java b/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MemberCacheUtils.java
index b8d21b17..ca39b609 100644
--- a/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MemberCacheUtils.java
+++ b/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MemberCacheUtils.java
@@ -92,7 +92,7 @@ class MemberCacheUtils {
    */
   private Method findMethod(Class<?> clazz, String name, Class<?>[] requiredTypes, String filename,
       int lineNumber, EvaluationOptions evaluationOptions) {
-    if (!evaluationOptions.isAllowGetClass() && name.equals("getClass")) {
+    if (!evaluationOptions.isAllowGetClass() && name.equalsIgnoreCase("getClass")) {
       throw new ClassAccessException(lineNumber, filename);
     }
 


Test patch:
diff --git a/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java b/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java
index 31851497..20bd84dd 100644
--- a/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java
+++ b/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java
@@ -155,13 +155,16 @@ public class GetAttributeTest {
    * Make sure we are properly accounting for getting the class object from an Object in all
    * situations:
    *
-   * | AllowGetClass | Strict Variables | Access Type | Result  | | ------------- | ----------------
-   * | ----------- | ------- | | true          | false            | property    | allowed | | true |
-   * false            | method      | allowed | | true          | true             | property    |
-   * allowed | | true          | true             | method      | allowed | | false | false
-   *   | property    | throw   | | false         | false            | method | throw   | | false
-   *     | true             | property    | throw   | | false         | true             | method
-   * | throw   |
+   * | AllowGetClass | Strict Variables | Access Type | Result  |
+   * | ------------- | ---------------- | ----------- | ------- |
+   * | true          | false            | property    | allowed |
+   * | true          | false            | method      | allowed |
+   * | true          | true             | property    | allowed |
+   * | true          | true             | method      | allowed |
+   * | false         | false            | property    | throw   |
+   * | false         | false            | method      | throw   |
+   * | false         | true             | property    | throw   |
+   * | false         | true             | method      | throw   |
    */
 
   @Test
@@ -296,6 +299,72 @@ public class GetAttributeTest {
     template.evaluate(writer, context);
   }
 
+  @Test
+  public void testAccessingClass_AllowGetClassOnIsCaseInsensitive_Property()
+      throws PebbleException, IOException {
+    PebbleEngine pebble = new PebbleEngine.Builder()
+      .loader(new StringLoader())
+      .allowGetClass(true)
+      .build();
+
+    PebbleTemplate template = pebble.getTemplate("hello [{{ object.ClAsS }}]");
+    Map<String, Object> context = new HashMap<>();
+    context.put("object", new SimpleObject());
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, context);
+    assertEquals("hello [" + SimpleObject.class.toString() + "]", writer.toString());
+  }
+
+  @Test(expected = ClassAccessException.class)
+  public void testAccessingClass_AllowGetClassOffIsCaseInsensitive_Property()
+      throws PebbleException, IOException {
+    PebbleEngine pebble = new PebbleEngine.Builder()
+      .loader(new StringLoader())
+      .allowGetClass(false)
+      .build();
+
+    PebbleTemplate template = pebble.getTemplate("hello [{{ object.ClAsS }}]");
+    Map<String, Object> context = new HashMap<>();
+    context.put("object", new SimpleObject());
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, context);
+  }
+
+  @Test
+  public void testAccessingClass_AllowGetClassOnIsCaseInsensitive_Method()
+      throws PebbleException, IOException {
+    PebbleEngine pebble = new PebbleEngine.Builder()
+      .loader(new StringLoader())
+      .allowGetClass(true)
+      .build();
+
+    PebbleTemplate template = pebble.getTemplate("hello [{{ object.GeTcLAsS() }}]");
+    Map<String, Object> context = new HashMap<>();
+    context.put("object", new SimpleObject());
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, context);
+    assertEquals("hello [" + SimpleObject.class.toString() + "]", writer.toString());
+  }
+
+  @Test(expected = ClassAccessException.class)
+  public void testAccessingClass_AllowGetClassOffIsCaseInsensitive_Method()
+      throws PebbleException, IOException {
+    PebbleEngine pebble = new PebbleEngine.Builder()
+      .loader(new StringLoader())
+      .allowGetClass(false)
+      .build();
+
+    PebbleTemplate template = pebble.getTemplate("hello [{{ object.GeTcLAsS() }}]");
+    Map<String, Object> context = new HashMap<>();
+    context.put("object", new SimpleObject());
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, context);
+  }
+
   /**
    * The GetAttribute expression involves caching, we test with different objects to make sure that
    * the caching doesnt have any negative side effects.