From fbfbbf9f494cea6fbeb6dbce96dd2135eefc2be5 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 26 Jan 2022 15:37:58 -0500 Subject: [PATCH 1/3] Limit rendering cycle detection to expression nodes --- .../hubspot/jinjava/interpret/JinjavaInterpreter.java | 3 ++- .../com/hubspot/jinjava/tree/ExpressionNodeTest.java | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 0c87441a1..2c41d57e5 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -40,6 +40,7 @@ import com.hubspot.jinjava.objects.serialization.PyishSerializable; import com.hubspot.jinjava.random.ConstantZeroRandomNumberGenerator; import com.hubspot.jinjava.random.DeferredRandomNumberGenerator; +import com.hubspot.jinjava.tree.ExpressionNode; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.tree.TreeParser; @@ -259,7 +260,7 @@ public String render(Node root, boolean processExtendRoots) { position = node.getStartPosition(); String renderStr = node.getMaster().getImage(); try { - if (context.doesRenderStackContain(renderStr)) { + if (node instanceof ExpressionNode && context.doesRenderStackContain(renderStr)) { // This is a circular rendering. Stop rendering it here. addError( new TemplateError( diff --git a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java index a955ffb60..18441f1cb 100644 --- a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java @@ -72,6 +72,15 @@ public void itAvoidsInfiniteRecursionWhenVarsContainBraceBlocks() throws Excepti assertThat(node.render(interpreter).toString()).isEqualTo("hello {{ place }}"); } + @Test + public void itAllowsRecursionForTagExpressions() throws Exception { + context.put("myvar", "{% if true %}{{ place }}{% endif %}"); + context.put("place", "{% if true %}Hello{% endif %}"); + + ExpressionNode node = fixture("simplevar"); + assertThat(node.render(interpreter).toString()).isEqualTo("Hello"); + } + @Test public void itDoesNotRescursivelyEvaluateExpressionsOfSelf() throws Exception { context.put("myvar", "hello {{myvar}}"); From 34fbe0f6213fb583b266b46f4bfe50fc1b613b8b Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 26 Jan 2022 15:44:18 -0500 Subject: [PATCH 2/3] Add additional test case --- .../com/hubspot/jinjava/tree/ExpressionNodeTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java index 18441f1cb..e45867433 100644 --- a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java @@ -81,6 +81,16 @@ public void itAllowsRecursionForTagExpressions() throws Exception { assertThat(node.render(interpreter).toString()).isEqualTo("Hello"); } + @Test + public void itAvoidsInfiniteRecursionWhenVarsAreInIfBlocks() throws Exception { + context.put("myvar", "{% if true %}{{ place }}{% endif %}"); + context.put("place", "{% if true %}{{ myvar }}{% endif %}"); + + ExpressionNode node = fixture("simplevar"); + assertThat(node.render(interpreter).toString()) + .isEqualTo("{% if true %}{{ myvar }}{% endif %}"); + } + @Test public void itDoesNotRescursivelyEvaluateExpressionsOfSelf() throws Exception { context.put("myvar", "hello {{myvar}}"); From 4e8fef2b22ace375585a103d1afba26ba6490e27 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 26 Jan 2022 15:49:10 -0500 Subject: [PATCH 3/3] Change name of test --- src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java index e45867433..ca613ef99 100644 --- a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java @@ -73,7 +73,7 @@ public void itAvoidsInfiniteRecursionWhenVarsContainBraceBlocks() throws Excepti } @Test - public void itAllowsRecursionForTagExpressions() throws Exception { + public void itAllowsNestedTagExpressions() throws Exception { context.put("myvar", "{% if true %}{{ place }}{% endif %}"); context.put("place", "{% if true %}Hello{% endif %}");