Skip to content

Commit

Permalink
add hard limit for max slices
Browse files Browse the repository at this point in the history
  • Loading branch information
slozenko committed Mar 19, 2024
1 parent a164bac commit 38f81dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.jinjava.lib.filter;

import static com.hubspot.jinjava.util.Logging.ENGINE_LOG;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
Expand Down Expand Up @@ -53,6 +55,8 @@
)
public class SliceFilter implements Filter {

public static final int MAX_SLICES = 1000;

@Override
public String getName() {
return "slice";
Expand All @@ -79,10 +83,15 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
0,
args[0]
);
} else if (slices > MAX_SLICES) {
ENGINE_LOG.warn(
"The limit input value is too large, it's been reduced to " + MAX_SLICES
);
slices = MAX_SLICES;
}

List<List<Object>> result = new ArrayList<>();
List<Object> currentList = null; // lazy evaluation
List<Object> currentList = null;

int i = 0;
while (loop.hasNext()) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/SliceFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jinjava.lib.filter;

import static com.hubspot.jinjava.lib.filter.SliceFilter.MAX_SLICES;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
Expand All @@ -8,6 +9,9 @@
import com.hubspot.jinjava.BaseJinjavaTest;
import com.hubspot.jinjava.interpret.RenderResult;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;
Expand Down Expand Up @@ -35,6 +39,20 @@ public void itSlicesLists() throws Exception {
assertThat(dom.select(".columwrapper .column-3 li")).hasSize(1);
}

@Test
public void itSlicesToTheMaxLimit() throws Exception {
String result = jinjava.render(
Resources.toString(
Resources.getResource("filter/slice-filter-big.jinja"),
StandardCharsets.UTF_8
),
ImmutableMap.of("items", Lists.newArrayList("a", "b", "c", "d", "e"))
);

assertThat(result).isNotEmpty();
assertThat(result.split("\n")).hasSize(MAX_SLICES + 2); // 1 for each slice, 1 for the newline
}

@Test
public void itSlicesListWithReplacement() throws Exception {
String result = jinjava.render(
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/filter/slice-filter-big.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{%- for column in items|slice(999999999, 'hello') %}
{{ loop.index }}
{%- for item in column %}
{{ item }}
{%- endfor %}
{%- endfor %}

0 comments on commit 38f81dc

Please sign in to comment.