Skip to content
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 @@ -301,6 +301,46 @@ private static <E extends Throwable> void sneakyThrow0(final Throwable x) throws
*/
private boolean infiniteLoops;

/**
* Maximum depth of nested template/partial invocations while loop detection is enabled. Protects
* against recursion that evades name-based detection (like aliased locations). Default is: 256.
*/
private int maxPartialDepth = 256;

/**
* Maximum depth of nested partial invocations. Only enforced when {@link #infiniteLoops()} is
* false. Default is: 256.
*
* @return Maximum partial depth.
*/
public int maxPartialDepth() {
return maxPartialDepth;
}

/**
* Set the maximum depth of nested partial invocations. Only enforced when {@link
* #infiniteLoops()} is false.
*
* @param maxPartialDepth Maximum partial depth (must be positive).
*/
public void setMaxPartialDepth(final int maxPartialDepth) {
if (maxPartialDepth <= 0) {
throw new IllegalArgumentException("maxPartialDepth must be positive: " + maxPartialDepth);
}
this.maxPartialDepth = maxPartialDepth;
}

/**
* Set the maximum depth of nested partial invocations.
*
* @param maxPartialDepth Maximum partial depth (must be positive).
* @return The handlebars object.
*/
public Handlebars maxPartialDepth(final int maxPartialDepth) {
setMaxPartialDepth(maxPartialDepth);
return this;
}

/**
* If true, templates will be deleted once applied. Useful, in some advanced template inheritance
* use cases. Default is: false. At any time you can override the default setup with:
Expand Down Expand Up @@ -403,15 +443,14 @@ public String precompile(final String path) {
* @return JavaScript.
*/
public String precompileInline(final String template) {
return Throwing.get(
() -> {
ScriptEngine engine = engine();
Object handlebars = engine.getContext().getAttribute("Handlebars");
Bindings bindings = engine.createBindings();
bindings.put("Handlebars", handlebars);
bindings.put("template", template);
return (String) engine.eval("Handlebars.precompile(template);", bindings);
});
return Throwing.get(() -> {
ScriptEngine engine = engine();
Object handlebars = engine.getContext().getAttribute("Handlebars");
Bindings bindings = engine.createBindings();
bindings.put("Handlebars", handlebars);
bindings.put("template", template);
return (String) engine.eval("Handlebars.precompile(template);", bindings);
});
}

/**
Expand Down Expand Up @@ -1450,13 +1489,12 @@ private ScriptEngine engine() {

this.engine = new ScriptEngineManager().getEngineByName("nashorn");

Throwing.run(
() -> {
// polyfill globalThis as it is used since handlebars 4.7.x and is not supported by
// nashorn
engine.eval("var globalThis = this;");
engine.eval(Files.read(this.handlebarsJsFile, charset));
});
Throwing.run(() -> {
// polyfill globalThis as it is used since handlebars 4.7.x and is not supported by
// nashorn
engine.eval("var globalThis = this;");
engine.eval(Files.read(this.handlebarsJsFile, charset));
});
}
return this.engine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ protected void merge(final Context context, final Writer writer) throws IOExcept
LinkedList<TemplateSource> invocationStack = context.data(Context.INVOCATION_STACK);

try {
if (invocationStack.size() >= handlebars.maxPartialDepth()) {
String reason =
String.format(
"maximum partial depth exceeded (%s), partial '%s'",
handlebars.maxPartialDepth(), path);
String message = String.format("%s:%s:%s: %s", filename, line, column, reason);
throw new HandlebarsException(
new HandlebarsError(filename, line, column, reason, text(), message));
}

TemplateSource source = loader.sourceAt(path);

if (exists(invocationStack, source.filename())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,60 @@
package com.github.jknack.handlebars;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.github.jknack.handlebars.io.StringTemplateSource;

public class InfiniteLoopTest {

@Test
public void self() throws IOException {
public void self() {
assertThrows(HandlebarsException.class, () -> apply("r"));
}

@Test
public void level1() throws IOException {
public void level1() {
assertThrows(HandlebarsException.class, () -> apply("r1"));
}

@Test
public void level2() throws IOException {
public void level2() {
assertThrows(HandlebarsException.class, () -> apply("r2"));
}

@Test
public void aliasedRecursionIsBounded() {
Handlebars hb =
new Handlebars(
new com.github.jknack.handlebars.io.AbstractTemplateLoader() {
@Override
public com.github.jknack.handlebars.io.TemplateSource sourceAt(final String uri) {
return new StringTemplateSource(
"/" + uri, "{{#with d}}{{> (lookup '.' 'p')}}{{/with}}");
}
});
hb.setMaxPartialDepth(20);
Map<String, Object> root = new LinkedHashMap<>();
Map<String, Object> cur = root;
for (int i = 0; i < 1000; i++) {
Map<String, Object> n = new LinkedHashMap<>();
cur.put("p", "x" + i + "/../a");
cur.put("d", n);
cur = n;
}
HandlebarsException ex =
assertThrows(
HandlebarsException.class,
() -> hb.compileInline("{{> (lookup '.' 'p')}}").apply(root));
assertTrue(ex.getMessage().contains("maximum partial depth"), ex.getMessage());
}

private void apply(final String path) throws IOException {
try {
new Handlebars().compile(path).apply(new Object());
Expand Down
Loading