From 14c53246036eba2667ab6bfef7e9eb0e3a62a8b3 Mon Sep 17 00:00:00 2001 From: Vadim Ippolitov Date: Tue, 24 Nov 2026 07:12:18 +0000 Subject: [PATCH 1/3] Skip output size accounting when the output limit is disabled --- .../jinjava/tree/output/OutputList.java | 16 +- .../jinjava/tree/output/OutputListTest.java | 168 ++++++++++++++++++ 2 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/hubspot/jinjava/tree/output/OutputListTest.java diff --git a/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java b/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java index 1d3b84710..681321623 100644 --- a/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java +++ b/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java @@ -23,21 +23,25 @@ public OutputList(long maxOutputSize) { } public void addNode(OutputNode node) { - if (maxOutputSize > 0 && currentSize + node.getSize() > maxOutputSize) { - throw new OutputTooBigException(maxOutputSize, currentSize + node.getSize()); + if (maxOutputSize > 0) { + if (currentSize + node.getSize() > maxOutputSize) { + throw new OutputTooBigException(maxOutputSize, currentSize + node.getSize()); + } + currentSize += node.getSize(); } - currentSize += node.getSize(); nodes.add(node); if (node instanceof BlockPlaceholderOutputNode) { BlockPlaceholderOutputNode blockNode = (BlockPlaceholderOutputNode) node; - if (maxOutputSize > 0 && currentSize + blockNode.getSize() > maxOutputSize) { - throw new OutputTooBigException(maxOutputSize, currentSize + blockNode.getSize()); + if (maxOutputSize > 0) { + if (currentSize + blockNode.getSize() > maxOutputSize) { + throw new OutputTooBigException(maxOutputSize, currentSize + blockNode.getSize()); + } + currentSize += blockNode.getSize(); } - currentSize += blockNode.getSize(); blocks.add(blockNode); } } diff --git a/src/test/java/com/hubspot/jinjava/tree/output/OutputListTest.java b/src/test/java/com/hubspot/jinjava/tree/output/OutputListTest.java new file mode 100644 index 000000000..a7eccb5fa --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/tree/output/OutputListTest.java @@ -0,0 +1,168 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.hubspot.jinjava.tree.output; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hubspot.jinjava.BaseJinjavaTest; +import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.interpret.OutputTooBigException; +import com.hubspot.jinjava.interpret.RenderResult; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import org.junit.Test; + +public class OutputListTest { + + @Test + public void itDoesNotQueryNodeSizeWhenLimitIsDisabled() { + for (long limit : new long[] { 0, -1 }) { + OutputList output = new OutputList(limit); + OutputNode node = new RenderedOutputNode("

Привет 😀

") { + @Override + public long getSize() { + throw new AssertionError("Size queried with output limit disabled"); + } + }; + + output.addNode(node); + + assertThat(output.getNodes()).containsExactly(node); + assertThat(output.getValue()).isEqualTo("

Привет 😀

"); + } + } + + @Test + public void itRegistersBlocksWithoutQueryingSizeWhenLimitIsDisabled() { + for (long limit : new long[] { 0, -1 }) { + OutputList output = new OutputList(limit); + BlockPlaceholderOutputNode block = new BlockPlaceholderOutputNode("content") { + @Override + public long getSize() { + throw new AssertionError("Block size queried with output limit disabled"); + } + }; + + output.addNode(block); + + assertThat(output.getNodes()).containsExactly(block); + assertThat(output.getBlocks()).containsExactly(block); + block.resolve("

Привет 😀

"); + assertThat(output.getValue()).isEqualTo("

Привет 😀

"); + } + } + + @Test + public void itAcceptsOutputExactlyAtUtf8ByteLimit() { + for (String text : Arrays.asList("hello", "Привет", "😀")) { + OutputList output = new OutputList(text.getBytes(StandardCharsets.UTF_8).length); + + output.addNode(new RenderedOutputNode(text)); + + assertThat(output.getValue()).isEqualTo(text); + } + } + + @Test + public void itRejectsOutputOneByteOverUtf8ByteLimit() { + for (String text : Arrays.asList("hello", "Привет", "😀")) { + long size = text.getBytes(StandardCharsets.UTF_8).length; + OutputList output = new OutputList(size - 1); + + assertThatThrownBy(() -> output.addNode(new RenderedOutputNode(text))) + .isInstanceOf(OutputTooBigException.class) + .hasMessage("%d byte output rendered, over limit of %d bytes", size, size - 1); + assertThat(output.getNodes()).isEmpty(); + } + } + + @Test + public void itAccountsForUtf8BytesAcrossNodes() { + for (String text : Arrays.asList("hello", "Привет", "😀")) { + long size = text.getBytes(StandardCharsets.UTF_8).length + 1; + OutputList output = new OutputList(size); + output.addNode(new RenderedOutputNode("a")); + output.addNode(new RenderedOutputNode(text)); + + assertThat(output.getValue()).isEqualTo("a" + text); + assertThatThrownBy(() -> output.addNode(new RenderedOutputNode("b"))) + .isInstanceOf(OutputTooBigException.class) + .hasMessage("%d byte output rendered, over limit of %d bytes", size + 1, size); + assertThat(output.getValue()).isEqualTo("a" + text); + } + } + + @Test + public void itPreservesResolvedBlockSizeChecksWithPositiveLimit() { + BlockPlaceholderOutputNode block = new BlockPlaceholderOutputNode("content"); + block.resolve("ab"); + OutputList output = new OutputList(3); + + assertThatThrownBy(() -> output.addNode(block)) + .isInstanceOf(OutputTooBigException.class) + .hasMessage("4 byte output rendered, over limit of 3 bytes"); + assertThat(output.getNodes()).containsExactly(block); + assertThat(output.getBlocks()).isEmpty(); + } + + @Test + public void itRendersHtmlWithVariablesLoopsAndBlocksWhenLimitIsDisabled() { + String template = + "

{{ title }}

" + + "{% block content %}{% endblock %}"; + Map context = Map.of( + "title", + "Привет 😀", + "items", + Arrays.asList("one", "два", "😀") + ); + + for (long limit : new long[] { 0, -1 }) { + Jinjava jinjava = new Jinjava( + BaseJinjavaTest.newConfigBuilder().withMaxOutputSize(limit).build() + ); + RenderResult result = jinjava.renderForResult(template, context); + + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getOutput()) + .isEqualTo( + "

Привет 😀

" + ); + } + } + + @Test + public void itResolvesInheritedBlocksWhenLimitIsDisabled() { + for (long limit : new long[] { 0, -1 }) { + Jinjava jinjava = new Jinjava( + BaseJinjavaTest.newConfigBuilder().withMaxOutputSize(limit).build() + ); + jinjava.setResourceLocator((name, encoding, interpreter) -> + "
{% block content %}parent{% endblock %}
" + ); + RenderResult result = jinjava.renderForResult( + "{% extends 'base.html' %}{% block content %}

{{ message }}

{% endblock %}", + Collections.singletonMap("message", "Привет 😀") + ); + + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getOutput()).isEqualTo("

Привет 😀

"); + } + } +} From 42c57e7c9a2c32e88efae52d38d0e66c4c1d3384 Mon Sep 17 00:00:00 2001 From: Vadim Ippolitov Date: Tue, 24 Nov 2026 07:27:06 +0000 Subject: [PATCH 2/3] Trigger CI for fork review From 00c223a90dca40898c41de0df043c3f55efda7b5 Mon Sep 17 00:00:00 2001 From: Vadim Ippolitov Date: Tue, 24 Nov 2026 07:28:49 +0000 Subject: [PATCH 3/3] Apply repository formatting to block size error --- .../java/com/hubspot/jinjava/tree/output/OutputList.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java b/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java index 681321623..12c7485c7 100644 --- a/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java +++ b/src/main/java/com/hubspot/jinjava/tree/output/OutputList.java @@ -37,7 +37,10 @@ public void addNode(OutputNode node) { if (maxOutputSize > 0) { if (currentSize + blockNode.getSize() > maxOutputSize) { - throw new OutputTooBigException(maxOutputSize, currentSize + blockNode.getSize()); + throw new OutputTooBigException( + maxOutputSize, + currentSize + blockNode.getSize() + ); } currentSize += blockNode.getSize(); }