Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit infinite evaluation from recursive extends tags #719

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -308,8 +309,22 @@ public String render(Node root, boolean processExtendRoots) {

// render all extend parents, keeping the last as the root output
if (processExtendRoots) {
Set<String> extendPaths = new HashSet<>();
Optional<String> extendPath = context.getExtendPathStack().peek();
while (!extendParentRoots.isEmpty()) {
if (extendPaths.contains(extendPath.orElse(""))) {
addError(
TemplateError.fromException(
new ExtendsTagCycleException(
extendPath.orElse(""),
context.getExtendPathStack().getTopLineNumber(),
context.getExtendPathStack().getTopStartPosition()
)
)
);
break;
}
extendPaths.add(extendPath.orElse(""));
context
.getCurrentPathStack()
.push(
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/ExtendsTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ public void itSetsErrorLineNumbersCorrectlyInBlocksFromExtendedTemplateInInclude
.isEqualTo("/errors/error.html");
}

@Test
public void itLimitsExtendsWithMultipleLevels() throws IOException {
// Previously this would run infinitely
RenderResult result = jinjava.renderForResult(
locator.fixture("rec1.jinja"),
new HashMap<>()
);
assertThat(result.getOutput().trim()).isEqualTo("foo");
assertThat(result.getErrors()).hasSize(1);
assertThat(result.getErrors().get(0).getMessage())
.contains("ExtendsTagCycleException");
}

private static class ExtendsTagTestResourceLocator implements ResourceLocator {
private RelativePathResolver relativePathResolver = new RelativePathResolver();

Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/tags/extendstag/rec1.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends 'rec2.jinja' %}
{% block body %}
foo
{% endblock %}
4 changes: 4 additions & 0 deletions src/test/resources/tags/extendstag/rec2.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends 'rec3.jinja' %}
{% block body %}
foobar
{% endblock %}
4 changes: 4 additions & 0 deletions src/test/resources/tags/extendstag/rec3.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends 'rec3.jinja' %}
{% block body %}
baz
{% endblock %}