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
7 changes: 7 additions & 0 deletions src/main/java/com/hubspot/jinjava/Jinjava.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.hubspot.jinjava.interpret.InvalidInputException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.RenderingDisabledException;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
Expand Down Expand Up @@ -256,6 +257,12 @@ public RenderResult renderForResult(
interpreter.getContext(),
interpreter.getErrorsCopy()
);
} catch (RenderingDisabledException e) {
return new RenderResult(
TemplateError.fromRenderingDisabledException(e),
interpreter.getContext(),
interpreter.getErrorsCopy()
);
} catch (InterpretException e) {
if (e instanceof TemplateSyntaxException) {
return new RenderResult(
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import javax.el.ELResolver;
import org.immutables.value.Value;

Expand Down Expand Up @@ -159,6 +160,11 @@ public int getMaxNumDeferredTokens() {
return 1000;
}

@Value.Default
public Supplier<Boolean> getRenderingEnabledSupplier() {
return () -> true;
}

@Value.Default
public InterpreterFactory getInterpreterFactory() {
return new JinjavaInterpreterFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ public String renderFlat(String template, long renderLimit) {
}
}

private void checkRenderingEnabled() {
if (!Boolean.TRUE.equals(config.getRenderingEnabledSupplier().get())) {
throw new RenderingDisabledException();
}
}

private TemporaryValueClosable<ErrorHandlingStrategy> ignoreParseErrorsIfActivated() {
return config
.getFeatures()
Expand Down Expand Up @@ -314,6 +320,7 @@ public String render(String template) {
* @return rendered result
*/
public String render(String template, long renderLimit) {
checkRenderingEnabled();
return render(parse(template), true, renderLimit);
}

Expand Down Expand Up @@ -352,6 +359,7 @@ public String render(Node root, boolean processExtendRoots) {
* @return rendered result
*/
private String render(Node root, boolean processExtendRoots, long renderLimit) {
checkRenderingEnabled();
boolean pushed = false;
//noinspection ErrorProne
if (JinjavaInterpreter.getCurrent() != this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hubspot.jinjava.interpret;

public class RenderingDisabledException extends InterpretException {

public RenderingDisabledException() {
super("Rendering is disabled");
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/hubspot/jinjava/interpret/TemplateError.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ public static TemplateError fromException(Exception ex) {
);
}

public static TemplateError fromRenderingDisabledException(
RenderingDisabledException ex
) {
return new TemplateError(
ErrorType.FATAL,
ErrorReason.DISABLED,
ErrorItem.TEMPLATE,
ex.getMessage(),
null,
-1,
-1,
ex,
BasicTemplateErrorCategory.UNKNOWN,
ImmutableMap.of()
);
}

public static TemplateError fromOutputTooBigException(Exception ex) {
return new TemplateError(
ErrorType.FATAL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.hubspot.jinjava;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.RenderingDisabledException;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before;
import org.junit.Test;

public class RenderingEnabledSupplierTest {

private AtomicBoolean renderingEnabled;
private Jinjava jinjava;

@Before
public void setup() {
renderingEnabled = new AtomicBoolean(true);
jinjava =
new Jinjava(
JinjavaConfig
.newBuilder()
.withRenderingEnabledSupplier(renderingEnabled::get)
.build()
);
}

@Test
public void itRendersByDefault() {
assertThat(new Jinjava().render("{{ foo }}", ImmutableMap.of("foo", "bar")))
.isEqualTo("bar");
}

@Test
public void itRendersWhenEnabled() {
assertThat(jinjava.render("{{ foo }}", ImmutableMap.of("foo", "bar")))
.isEqualTo("bar");
}

@Test
public void itReturnsDisabledErrorWhenDisabled() {
renderingEnabled.set(false);

RenderResult result = jinjava.renderForResult(
"{{ foo }}",
ImmutableMap.of("foo", "bar")
);

assertThat(result.getOutput()).isEmpty();
assertThat(result.getErrors())
.singleElement()
.satisfies(error -> {
assertThat(error.getSeverity()).isEqualTo(ErrorType.FATAL);
assertThat(error.getReason()).isEqualTo(ErrorReason.DISABLED);
assertThat(error.getException()).isInstanceOf(RenderingDisabledException.class);
});
}

@Test
public void itThrowsFromRenderWhenDisabled() {
renderingEnabled.set(false);

assertThatThrownBy(() -> jinjava.render("{{ foo }}", ImmutableMap.of("foo", "bar")))
.isInstanceOf(FatalTemplateErrorsException.class);
}

@Test
public void itChecksSupplierOnEveryRender() {
assertThat(jinjava.render("{{ foo }}", ImmutableMap.of("foo", "bar")))
.isEqualTo("bar");

renderingEnabled.set(false);
assertThat(jinjava.renderForResult("{{ foo }}", ImmutableMap.of()).getErrors())
.isNotEmpty();

renderingEnabled.set(true);
assertThat(jinjava.render("{{ foo }}", ImmutableMap.of("foo", "baz")))
.isEqualTo("baz");
}

@Test
public void itPreventsInterpreterRenderingWhenDisabled() {
JinjavaInterpreter interpreter = jinjava.newInterpreter();
renderingEnabled.set(false);

assertThatThrownBy(() -> interpreter.render("{{ foo }}"))
.isInstanceOf(RenderingDisabledException.class);
assertThatThrownBy(() -> interpreter.render(interpreter.parse("{{ foo }}")))
.isInstanceOf(RenderingDisabledException.class);
assertThatThrownBy(() -> interpreter.renderFlat("{{ foo }}"))
.isInstanceOf(RenderingDisabledException.class);
}
}
Loading